forked from fysnet/i440fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harddrive.asm
2588 lines (2290 loc) · 87.2 KB
/
harddrive.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
comment |*******************************************************************
* Copyright (c) 1984-2025 Forever Young Software Benjamin David Lunt *
* *
* i440FX BIOS ROM v1.0 *
* FILE: harddrive.asm *
* *
* This code is freeware, not public domain. Please use respectfully. *
* *
* You may: *
* - use this code for learning purposes only. *
* - use this code in your own Operating System development. *
* - distribute any code that you produce pertaining to this code *
* as long as it is for learning purposes only, not for profit, *
* and you give credit where credit is due. *
* *
* You may NOT: *
* - distribute this code for any purpose other than listed above. *
* - distribute this code for profit. *
* *
* You MUST: *
* - include this whole comment block at the top of this file. *
* - include contact information to where the original source is located. *
* https:;github.com/fysnet/i440fx *
* *
* DESCRIPTION: *
* harddrive include file *
* *
* BUILT WITH: NewBasic Assembler *
* http://www.fysnet/newbasic.htm *
* NBASM ver 00.27.14 *
* Command line: nbasm i440fx /z<enter> *
* *
* Last Updated: 3 Jan 2025 *
* *
****************************************************************************
* Notes: *
* - define INT13_FLAT_ADDR 1 to allow the flat address space in serv 42h *
* *
***************************************************************************|
.if DO_DEBUG
hd_tran_none_str db 'none',0
hd_tran_lba_str db 'lba',0
hd_tran_large_str db 'large',0
hd_tran_rechs_str db 'r-echs',0
hd_lchs_str db ' LCHS=%d/%d/%d',13,10,0
ata_x_x_str db 'ata%d-%d: PCHS=%u/%d/%d translation=',0
.endif
INT13_FLAT_ADDR equ 0
ata_print_slave db ' slave',0
ata_print_master db 'master',0
ata_print_str0 db 'ata%d %s: ',0
ata_print_str1 db ' ATA-%d Hard-Disk (%4u %cBytes)',13,10,0
ata_print_str2 db ' ATAPI-%d CD-Rom/DVD-Rom',13,10,0
ata_print_str3 db ' ATAPI-%d Device',13,10,0
ata_print_str4 db ' Unknown Device',13,10,0
hd_panic_string db 'PANIC: file: ', %FILE, ' -- line: %i',13,10,0
; can be up to IPL_ENTRY_MAX_DESC_LEN-1 chars
ata_controller_str db '(ATA Device)',0
atapi_controller_str db '(ATAPI Device)',0
HDD_FDPT struct
phy_max_cyls word
phy_max_heads byte
signature byte
log_spt byte
start_wp_cyl word
resv1 byte
control byte
log_max_cyls word
log_max_heads byte
landing_zone word
phy_spt byte
crc byte
HDD_FDPT ends
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; display a panic string and freeze
; on entry:
; stack has line number
; on return
; does not return...
; destroys all general
hdd_panic proc near ; add nothing here
push cs
pop ds
mov si,offset hd_panic_string
call bios_printf
add sp,2
call freeze
.noret
hdd_panic endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; initialize the hard drive(s)
; on entry:
; es = 0x0000
; on return
; nothing
; destroys all general
init_harddrive proc near uses ds
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;
mov al,0x0A ; 0000 1010 = reserved, disable IRQ 14
mov dx,0x03F6
out dx,al
mov es:[0x0474],al ; hard disk status of last operation
mov es:[0x0477],al ; hard disk port offset (XT only ???)
mov es:[0x048C],al ; hard disk status register
mov es:[0x048D],al ; hard disk error register
mov es:[0x048E],al ; hard disk task complete flag
mov al,0x01
mov es:[0x0475],al ; hard disk number attached
mov al,0xC0
mov es:[0x0476],al ; hard disk control byte
mov ax,0x13
mov bx,offset int13_handler
mov cx,BIOS_BASE
call set_int_vector
mov ax,0x76
mov bx,offset int76_handler
mov cx,BIOS_BASE
call set_int_vector
mov ax,0x41
mov bx,EBDA_DATA->fdpt0
mov cx,EBDA_SEG
call set_int_vector
mov ax,0x46
mov bx,EBDA_DATA->fdpt1
mov cx,EBDA_SEG
call set_int_vector
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; move disk geometry data from CMOS to EBDA disk parameter table(s)
mov ah,0x12
call cmos_get_byte
push ax
and al,0xF0
cmp al,0xF0
jne short @f
mov ah,0x19
mov ch,0x1B ; starting cmos register for type 47 drive 0
mov di,EBDA_DATA->fdpt0
call init_harddrive_params
@@: pop ax
and al,0x0F
cmp al,0x0F
jne short @f
mov ah,0x1A
mov ch,0x24 ; starting cmos register for type 47 drive 1
mov di,EBDA_DATA->fdpt1
call init_harddrive_params
@@: ret
init_harddrive endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; initialize the hard drive parameters
; on entry:
; ds = EBDA
; ah = cmos register to get type
; ch = starting cmos register for harddrive info (0x1B for 1st, 0x24 for second)
; di-> fixed disk parameter table in EBDA
; on return
; nothing
; destroys all general
; CMOS purpose param table offset
; 1B/24 cylinders low 00
; 1C/25 cylinders high 01
; 1D/26 heads 02
; 1E/27 write pre-comp low 05
; 1F/28 write pre-comp high 06
; 20/29 retries/bad map/heads>8 08
; 21/2A landing zone low 0C
; 22/2B landing zone high 0D
; 23/2C sectors/track 0E
init_harddrive_params proc near uses ds
call cmos_get_byte
cmp al,47 ; decimal 47 - user definable
jne init_harddrive_params_done
mov ax,EBDA_SEG
mov ds,ax
; Filling EBDA table for hard disk 0.
mov ah,ch
add ah,4 ; 0x1F / 0x28 ; write pre-comp high byte
call cmos_get_byte
mov dh,al
mov ah,ch
add ah,3 ; 0x1E / 0x27 ; write pre-comp low byte
call cmos_get_byte
mov ah,dh
mov [di+HDD_FDPT->start_wp_cyl],ax ; write precomp word
mov ah,ch
add ah,5 ; 0x20 / 0x29 ; control byte
call cmos_get_byte
mov [di+HDD_FDPT->control],al ; drive control byte
mov ah,ch
add ah,7 ; 0x22 / 0x2B ; landing zone high byte
call cmos_get_byte
mov dh,al
mov ah,ch
add ah,6 ; 0x21 / 0x2A ; landing zone low byte
call cmos_get_byte
mov ah,dh
mov [di+HDD_FDPT->landing_zone],ax ; landing zone word
mov ah,ch
add ah,1 ; 0x1C / 0x25 ; cylinders high byte
call cmos_get_byte
mov bh,al
mov ah,ch
;add ah,0 ; 0x1B / 0x24 ; cylinders low byte
call cmos_get_byte
mov bl,al ; bx = cylinders
mov ah,ch
add ah,2 ; 0x1D / 0x26 ; heads
call cmos_get_byte
mov cl,al ; cl = heads
mov ah,ch
add ah,8 ; 0x23 / 0x2C ; spt
call cmos_get_byte
mov dl,al ; dl = sectors
cmp bx,1024
ja short @f ; if cylinders > 1024, use translated style CHS
; no logical CHS mapping used, just physical CHS
; use Standard Fixed Disk Parameter Table (FDPT)
mov [di+HDD_FDPT->phy_spt],dl ; number of physical sectors
jmp short hd0_post_store_logical
; complies with Phoenix style Translated Fixed Disk Parameter Table (FDPT)
@@: mov [di+HDD_FDPT->log_max_cyls],bx ; number of physical cylinders
mov [di+HDD_FDPT->log_max_heads],cl ; number of physical heads
mov [di+HDD_FDPT->log_spt],dl ; number of physical sectors
mov [di+HDD_FDPT->phy_spt],dl ; number of logical sectors (same)
mov al,0xA0
mov [di+HDD_FDPT->signature],al ; A0h signature, indicates translated table
cmp bx,2048
ja short @f
; 1024 < c <= 2048 cylinders
shr bx,1
shl cl,1
jmp short hd0_post_store_logical
@@: cmp bx,4096
ja short @f
; 2048 < c <= 4096 cylinders
shr bx,2
shl cl,2
jmp short hd0_post_store_logical
@@: cmp bx,8192
ja short @f
; 4096 < c <= 8192 cylinders
shr bx,3
shl cl,3
jmp short hd0_post_store_logical
@@: ; 8192 < c <= 16384 cylinders
shr bx,4
shl cl,4
hd0_post_store_logical:
mov [di+HDD_FDPT->phy_max_cyls],bx ; number of physical cylinders
mov [di+HDD_FDPT->phy_max_heads],cl ; number of physical heads
; checksum
mov cx,15 ; repeat count
xor al,al ; sum
@@: add al,[di]
inc di
loop @b
not al ; now take 2s complement
inc al
mov [di],al
init_harddrive_params_done:
ret
init_harddrive_params endp
ATA_CHANNEL_SIZE equ 6 ; size of ata_x
ATA_DEVICE_SIZE equ 28 ; size of ata_x_x
; Global defines -- ATA register and register bits.
; command block & control block regs
ATA_CB_DATA equ 0 ; data reg in/out pio_base_addr1+0
ATA_CB_ERR equ 1 ; error in pio_base_addr1+1
ATA_CB_FR equ 1 ; feature reg out pio_base_addr1+1
ATA_CB_SC equ 2 ; sector count in/out pio_base_addr1+2
ATA_CB_SN equ 3 ; sector number in/out pio_base_addr1+3
ATA_CB_CL equ 4 ; cylinder low in/out pio_base_addr1+4
ATA_CB_CH equ 5 ; cylinder high in/out pio_base_addr1+5
ATA_CB_DH equ 6 ; device head in/out pio_base_addr1+6
ATA_CB_STAT equ 7 ; primary status in pio_base_addr1+7
ATA_CB_CMD equ 7 ; command out pio_base_addr1+7
ATA_CB_ASTAT equ 6 ; alternate status in pio_base_addr2+6
ATA_CB_DC equ 6 ; device control out pio_base_addr2+6
ATA_CB_DA equ 7 ; device address in pio_base_addr2+7
ATA_CB_ER_ICRC equ 0x80 ; ATA Ultra DMA bad CRC
ATA_CB_ER_BBK equ 0x80 ; ATA bad block
ATA_CB_ER_UNC equ 0x40 ; ATA uncorrected error
ATA_CB_ER_MC equ 0x20 ; ATA media change
ATA_CB_ER_IDNF equ 0x10 ; ATA id not found
ATA_CB_ER_MCR equ 0x08 ; ATA media change request
ATA_CB_ER_ABRT equ 0x04 ; ATA command aborted
ATA_CB_ER_NTK0 equ 0x02 ; ATA track 0 not found
ATA_CB_ER_NDAM equ 0x01 ; ATA address mark not found
ATA_CB_ER_P_SNSKEY equ 0xf0 ; ATAPI sense key (mask)
ATA_CB_ER_P_MCR equ 0x08 ; ATAPI Media Change Request
ATA_CB_ER_P_ABRT equ 0x04 ; ATAPI command abort
ATA_CB_ER_P_EOM equ 0x02 ; ATAPI End of Media
ATA_CB_ER_P_ILI equ 0x01 ; ATAPI Illegal Length Indication
; ATAPI Interrupt Reason bits in the Sector Count reg (CB_SC)
ATA_CB_SC_P_TAG equ 0xf8 ; ATAPI tag (mask)
ATA_CB_SC_P_REL equ 0x04 ; ATAPI release
ATA_CB_SC_P_IO equ 0x02 ; ATAPI I/O
ATA_CB_SC_P_CD equ 0x01 ; ATAPI C/D
; bits 7-4 of the device/head (CB_DH) reg
ATA_CB_DH_DEV0 equ 0xa0 ; select device 0
ATA_CB_DH_DEV1 equ 0xb0 ; select device 1
ATA_CB_DH_LBA equ 0x40 ; use LBA
; status reg (CB_STAT and CB_ASTAT) bits
ATA_CB_STAT_BSY equ 0x80 ; busy
ATA_CB_STAT_RDY equ 0x40 ; ready
ATA_CB_STAT_DF equ 0x20 ; device fault
ATA_CB_STAT_WFT equ 0x20 ; write fault (old name)
ATA_CB_STAT_SKC equ 0x10 ; seek complete
ATA_CB_STAT_SERV equ 0x10 ; service
ATA_CB_STAT_DRQ equ 0x08 ; data request
ATA_CB_STAT_CORR equ 0x04 ; corrected
ATA_CB_STAT_IDX equ 0x02 ; index
ATA_CB_STAT_ERR equ 0x01 ; error (ATA)
ATA_CB_STAT_CHK equ 0x01 ; check (ATAPI)
; device control reg (CB_DC) bits
ATA_CB_DC_HD15 equ 0x08 ; bit should always be set to one
ATA_CB_DC_SRST equ 0x04 ; soft reset
ATA_CB_DC_NIEN equ 0x02 ; disable interrupts
; Most mandatory and optional ATA commands (from ATA-3),
ATA_CMD_CFA_ERASE_SECTORS equ 0xC0
ATA_CMD_CFA_REQUEST_EXT_ERR_CODE equ 0x03
ATA_CMD_CFA_TRANSLATE_SECTOR equ 0x87
ATA_CMD_CFA_WRITE_MULTIPLE_WO_ERASE equ 0xCD
ATA_CMD_CFA_WRITE_SECTORS_WO_ERASE equ 0x38
ATA_CMD_CHECK_POWER_MODE1 equ 0xE5
ATA_CMD_CHECK_POWER_MODE2 equ 0x98
ATA_CMD_DEVICE_RESET equ 0x08
ATA_CMD_EXECUTE_DEVICE_DIAGNOSTIC equ 0x90
ATA_CMD_FLUSH_CACHE equ 0xE7
ATA_CMD_FORMAT_TRACK equ 0x50
ATA_CMD_IDENTIFY_DEVICE equ 0xEC
ATA_CMD_IDENTIFY_DEVICE_PACKET equ 0xA1
ATA_CMD_IDENTIFY_PACKET_DEVICE equ 0xA1
ATA_CMD_IDLE1 equ 0xE3
ATA_CMD_IDLE2 equ 0x97
ATA_CMD_IDLE_IMMEDIATE1 equ 0xE1
ATA_CMD_IDLE_IMMEDIATE2 equ 0x95
ATA_CMD_INITIALIZE_DRIVE_PARAMETERS equ 0x91
ATA_CMD_INITIALIZE_DEVICE_PARAMETERS equ 0x91
ATA_CMD_NOP equ 0x00
ATA_CMD_PACKET equ 0xA0
ATA_CMD_READ_BUFFER equ 0xE4
ATA_CMD_READ_DMA equ 0xC8
ATA_CMD_READ_DMA_QUEUED equ 0xC7
ATA_CMD_READ_MULTIPLE equ 0xC4
ATA_CMD_READ_SECTORS equ 0x20
ATA_CMD_READ_VERIFY_SECTORS equ 0x40
ATA_CMD_RECALIBRATE equ 0x10
ATA_CMD_REQUEST_SENSE equ 0x03
ATA_CMD_SEEK equ 0x70
ATA_CMD_SET_FEATURES equ 0xEF
ATA_CMD_SET_MULTIPLE_MODE equ 0xC6
ATA_CMD_SLEEP1 equ 0xE6
ATA_CMD_SLEEP2 equ 0x99
ATA_CMD_STANDBY1 equ 0xE2
ATA_CMD_STANDBY2 equ 0x96
ATA_CMD_STANDBY_IMMEDIATE1 equ 0xE0
ATA_CMD_STANDBY_IMMEDIATE2 equ 0x94
ATA_CMD_WRITE_BUFFER equ 0xE8
ATA_CMD_WRITE_DMA equ 0xCA
ATA_CMD_WRITE_DMA_QUEUED equ 0xCC
ATA_CMD_WRITE_MULTIPLE equ 0xC5
ATA_CMD_WRITE_SECTORS equ 0x30
ATA_CMD_WRITE_VERIFY equ 0x3C
ATA_IFACE_NONE equ 0x00
ATA_IFACE_ISA equ 0x01
ATA_IFACE_PCI equ 0x02
ATA_TYPE_NONE equ 0x00
ATA_TYPE_UNKNOWN equ 0x01
ATA_TYPE_ATA equ 0x02
ATA_TYPE_ATAPI equ 0x03
ATA_DEVICE_NONE equ 0x00
ATA_DEVICE_HD equ 0xFF
ATA_DEVICE_CDROM equ 0x05
ATA_MODE_NONE equ 0x00
ATA_MODE_PIO16 equ 0x00
ATA_MODE_PIO32 equ 0x01
ATA_MODE_ISADMA equ 0x02
ATA_MODE_PCIDMA equ 0x03
ATA_MODE_USEIRQ equ 0x10
ATA_TRANSLATION_NONE equ 0
ATA_TRANSLATION_LBA equ 1
ATA_TRANSLATION_LARGE equ 2
ATA_TRANSLATION_RECHS equ 3
ATA_DATA_NO equ 0x00
ATA_DATA_IN equ 0x01
ATA_DATA_OUT equ 0x02
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; initialize the hard drive's EBDA initial settings
; on entry:
; nothing
; on return
; nothing
; destroys all general
ata_init proc near uses ds
call bios_get_ebda
mov ds,ax
mov cx,BX_MAX_ATA_INTERFACES
xor bx,bx
@@: mov byte [bx+EBDA_DATA->ata_0_iface],ATA_IFACE_NONE
mov word [bx+EBDA_DATA->ata_0_iobase1],0x0000
mov word [bx+EBDA_DATA->ata_0_iobase2],0x0000
mov byte [bx+EBDA_DATA->ata_0_irq],0
add bx,ATA_CHANNEL_SIZE
loop @b
mov cx,BX_MAX_ATA_DEVICES
xor bx,bx
@@: mov byte [bx+EBDA_DATA->ata_0_0_type],ATA_TYPE_NONE
mov byte [bx+EBDA_DATA->ata_0_0_device],ATA_DEVICE_NONE
mov byte [bx+EBDA_DATA->ata_0_0_removable],0
mov byte [bx+EBDA_DATA->ata_0_0_lock],0
mov byte [bx+EBDA_DATA->ata_0_0_mode],ATA_MODE_NONE
mov word [bx+EBDA_DATA->ata_0_0_blksize],0
mov byte [bx+EBDA_DATA->ata_0_0_translation],ATA_TRANSLATION_NONE
mov word [bx+EBDA_DATA->ata_0_0_lchs_heads],0
mov word [bx+EBDA_DATA->ata_0_0_lchs_cyl],0
mov word [bx+EBDA_DATA->ata_0_0_lchs_spt],0
mov word [bx+EBDA_DATA->ata_0_0_pchs_heads],0
mov word [bx+EBDA_DATA->ata_0_0_pchs_cyl],0
mov word [bx+EBDA_DATA->ata_0_0_pchs_spt],0
mov dword [bx+EBDA_DATA->ata_0_0_sectors_low],0
mov dword [bx+EBDA_DATA->ata_0_0_sectors_high],0
add bx,ATA_DEVICE_SIZE
loop @b
mov cx,BX_MAX_ATA_DEVICES
xor bx,bx
@@: mov byte [bx+EBDA_DATA->ata_0_0_hdidmap],BX_MAX_ATA_DEVICES
mov byte [bx+EBDA_DATA->ata_0_0_cdidmap],BX_MAX_ATA_DEVICES
add bx,1
loop @b
mov byte [bx+EBDA_DATA->ata_hdcount],0
mov byte [bx+EBDA_DATA->ata_cdcount],0
ret
ata_init endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; initialize the hard drive(s)
; on entry:
; nothing
; on return
; nothing
; destroys all general
ata_detect proc near uses ds
call bios_get_ebda
mov ds,ax
mov byte [EBDA_DATA->ata_0_iface],ATA_IFACE_ISA
mov word [EBDA_DATA->ata_0_iobase1],PORT_ATA1_CMD_BASE
mov word [EBDA_DATA->ata_0_iobase2],0x3F0
mov byte [EBDA_DATA->ata_0_irq],14
mov byte [EBDA_DATA->ata_1_iface],ATA_IFACE_ISA
mov word [EBDA_DATA->ata_1_iobase1],PORT_ATA2_CMD_BASE
mov word [EBDA_DATA->ata_1_iobase2],0x370
mov byte [EBDA_DATA->ata_1_irq],15
mov byte [EBDA_DATA->ata_2_iface],ATA_IFACE_ISA
mov word [EBDA_DATA->ata_2_iobase1],0x1E8
mov word [EBDA_DATA->ata_2_iobase2],0x3E0
mov byte [EBDA_DATA->ata_2_irq],12
mov byte [EBDA_DATA->ata_3_iface],ATA_IFACE_ISA
mov word [EBDA_DATA->ata_3_iobase1],0x168
mov word [EBDA_DATA->ata_3_iobase2],0x360
mov byte [EBDA_DATA->ata_3_irq],11
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; allocate room on the stack for some locals
push bp
mov bp,sp
sub sp,0x256
hd_buffer equ [bp-0x200] ; hd_buffer[512]
hd_hdcount equ [bp-0x202] ; word
hd_cdcount equ [bp-0x204] ; word
hd_device equ [bp-0x206] ; word
hd_type equ [bp-0x208] ; word
hd_iobase1 equ [bp-0x20A] ; word
hd_iobase2 equ [bp-0x20C] ; word
hd_blksize equ [bp-0x20E] ; word
hd_channel equ [bp-0x210] ; word
hd_slave equ [bp-0x212] ; word
hd_sectors_low equ [bp-0x216] ; dword
hd_sectors_high equ [bp-0x21A] ; dword
hd_cylinders equ [bp-0x21C] ; word
hd_heads equ [bp-0x21E] ; word
hd_spt equ [bp-0x220] ; word
hd_translation equ [bp-0x222] ; word
hd_removable equ [bp-0x224] ; word
hd_mode equ [bp-0x226] ; word
hd_sizeinmb equ [bp-0x22A] ; dword
hd_version equ [bp-0x22C] ; word
hd_model equ [bp-0x256] ; hd_model[42]
mov word hd_hdcount,0
mov word hd_cdcount,0
mov word hd_device,0
ata_device_detection:
; calculate channel and slave values
mov ax,hd_device
mov hd_slave,ax
shr ax,1
mov hd_channel,ax
and word hd_slave,1
shl word hd_slave,4
; get the two base io addresses
imul bx,ax,ATA_CHANNEL_SIZE
mov ax,[bx+EBDA_DATA->ata_0_iobase1]
mov hd_iobase1,ax
mov ax,[bx+EBDA_DATA->ata_0_iobase2]
mov hd_iobase2,ax
; disable interrupts
mov dx,hd_iobase2
add dx,ATA_CB_DC
mov al,(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN)
out dx,al
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; look for device
; select which drive
mov dx,hd_iobase1
add dx,ATA_CB_DH
mov ax,ATA_CB_DH_DEV0
add ax,hd_slave
out dx,al
mov dx,hd_iobase1
add dx,ATA_CB_SC
mov al,0x55
out dx,al ; out ATA_CB_SC,55
inc dx
mov al,0xAA
out dx,al ; out ATA_CB_SN,AA
dec dx
out dx,al ; out ATA_CB_SC,AA
mov al,0x55
inc dx
out dx,al ; out ATA_CB_SN,55
dec dx
out dx,al ; out ATA_CB_SC,55
mov al,0xAA
inc dx
out dx,al ; out ATA_CB_SN,AA
dec dx
in al,dx ; in ATA_CB_SC (55)
mov ah,al
inc dx ; in ATA_CB_SN (AA)
in al,dx
; if ax == 0x55AA, we found a drive
cmp ax,0x55AA
jne short ata_no_drive_found
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; we found a device
mov ax,hd_device
imul bx,ax,ATA_DEVICE_SIZE
mov byte [bx+EBDA_DATA->ata_0_0_type],ATA_TYPE_UNKNOWN
push es
push ds
pop es
call ata_reset
pop es
; check for ata or atapi
mov dx,hd_iobase1
add dx,ATA_CB_DH
mov ax,ATA_CB_DH_DEV0
add ax,hd_slave
out dx,al
mov dx,hd_iobase1
add dx,ATA_CB_SC
in al,dx ; in ATA_CB_SC
mov ah,al
inc dx
in al,dx ; in ATA_CB_SN
; if ax = 0x0101, it is ata
cmp ax,0x0101
jne short ata_no_drive_found
inc dx
in al,dx ; in ATA_CB_CL
mov ah,al
inc dx
in al,dx ; in ATA_CB_CH
mov cx,ax ; save it in cx
add dx,2
in al,dx ; in ATA_CB_STAT
; if cx = 0x14EB, we have atapi
cmp cx,0x14EB
jne short @f
mov byte [bx+EBDA_DATA->ata_0_0_type],ATA_TYPE_ATAPI
jmp short ata_no_drive_found
@@: ; if cx = 0x0000 and al != 0, we have ata
cmp cx,0x0000
jne short @f
cmp al,0x00
je short @f
mov byte [bx+EBDA_DATA->ata_0_0_type],ATA_TYPE_ATA
jmp short ata_no_drive_found
@@: ; there is no drive attached
mov byte [bx+EBDA_DATA->ata_0_0_type],ATA_TYPE_NONE
ata_no_drive_found:
; calculate pointer to the current device struct
mov ax,hd_device
imul bx,ax,ATA_DEVICE_SIZE
; get the type found
xor ah,ah
mov al,[bx+EBDA_DATA->ata_0_0_type]
mov hd_type,ax
; did we find an ATA device
cmp word hd_type,ATA_TYPE_ATA
jne try_atapi_identify
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; send the IDENTIFY command
; temporary values for the transfer
mov byte [bx+EBDA_DATA->ata_0_0_device],ATA_DEVICE_HD
mov byte [bx+EBDA_DATA->ata_0_0_mode],ATA_MODE_PIO16
; on entry to ata_cmd_data_io, bx -> EBDA_device[]
lea ax,hd_buffer
push ax ; offset of buffer
push ss ; segment of buffer
pushd 0 ; lba_high
pushd 0 ; lba_low
push 0 ; sector
push 0 ; head
push 0 ; cylinder
push 1 ; count
push ATA_CMD_IDENTIFY_DEVICE
push word hd_device ; device index
push 0 ; io_flag (0 = read, 1 = write)
call ata_cmd_data_io
add sp,26
or ax,ax
jz short @f
;
; panic: we didn't detect the ATA via INQUIRY
push %LINE
jmp hdd_panic
;
; ss:di points to the buffer
@@: lea di,hd_buffer
mov al,ss:[di+0]
and al,0x80
shr al,7
mov hd_removable,al
mov byte hd_mode,ATA_MODE_PIO32
cmp byte ss:[di+96],0
jnz short @f
mov byte hd_mode,ATA_MODE_PIO16
@@: mov ax,ss:[di+10]
mov hd_blksize,ax
mov ax,ss:[di+(1 * sizeof(word))]
mov hd_cylinders,ax
mov ax,ss:[di+(3 * sizeof(word))]
mov hd_heads,ax
mov ax,ss:[di+(6 * sizeof(word))]
mov hd_spt,ax
; assume words 60 and 61
mov eax,ss:[di+(60 * sizeof(word))]
mov hd_sectors_low,eax
mov dword hd_sectors_high,0
mov ax,ss:[di+(83 * sizeof(word))]
test ax,(1 << 10)
jz short @f
mov eax,ss:[di+(100 * sizeof(word))]
mov hd_sectors_low,eax
mov eax,ss:[di+(102 * sizeof(word))]
mov hd_sectors_high,eax
@@: mov byte [bx+EBDA_DATA->ata_0_0_device],ATA_DEVICE_HD
mov al,hd_removable
mov [bx+EBDA_DATA->ata_0_0_removable],al
mov al,hd_mode
mov [bx+EBDA_DATA->ata_0_0_mode],al
mov ax,hd_blksize
mov [bx+EBDA_DATA->ata_0_0_blksize],ax
mov ax,hd_heads
mov [bx+EBDA_DATA->ata_0_0_pchs_heads],ax
mov ax,hd_cylinders
mov [bx+EBDA_DATA->ata_0_0_pchs_cyl],ax
mov ax,hd_spt
mov [bx+EBDA_DATA->ata_0_0_pchs_spt],ax
mov eax,hd_sectors_low
mov [bx+EBDA_DATA->ata_0_0_sectors_low],eax
mov eax,hd_sectors_high
mov [bx+EBDA_DATA->ata_0_0_sectors_high],eax
.if DO_DEBUG
; print the found device
push ds
push cs
pop ds
push word hd_spt
push word hd_heads
push word hd_cylinders
push word hd_slave
push word hd_channel
mov si,offset ata_x_x_str
call bios_printf
add sp,10
pop ds
.endif
; get the translation from the CMOS
mov ax,hd_channel
shr ax,1
add ax,0x39
mov ah,al
call cmos_get_byte
xor ah,ah
; for (shift=hd_device%4; shift>0; shift--) translation >>= 2;
mov cx,hd_device
and cx,3
@@: jcxz short @f ; the loop below will catch cx=0, but
shr ax,2 ; we jmp back to here so to use @@'s
loop @b
@@: and ax,3
mov hd_translation,ax
mov [bx+EBDA_DATA->ata_0_0_translation],al
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; initalize the parameters due to the translation used
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; translation = none
cmp ax,ATA_TRANSLATION_NONE
jne short hd_not_translation_none
.if DO_DEBUG
push ds
push cs
pop ds
mov si,offset hd_tran_none_str
call display_string
pop ds
.endif
jmp hd_translation_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; translation = lba
hd_not_translation_none:
cmp ax,ATA_TRANSLATION_LBA
jne short hd_not_translation_lba
.if DO_DEBUG
push ds
push cs
pop ds
mov si,offset hd_tran_lba_str
call display_string
pop ds
.endif
mov word hd_spt,63
xor edx,edx
mov eax,hd_sectors_low
mov ecx,63
div ecx
mov hd_sectors_low,eax
push eax ; save sectors_low
shr eax,10 ; div by 1024
; if hd_heads > 64, hd_heads = 128
cmp ax,64
jna short @f
mov ax,128
jmp short hd_lba_cylinders
@@: ; if hd_heads > 32 hd_heads = 64
cmp ax,32
jna short @f
mov ax,64
jmp short hd_lba_cylinders
@@: ; if hd_heads > 16 hd_heads = 32
cmp ax,16
jna short @f
mov ax,32
jmp short hd_lba_cylinders
; else, hd_heads = 16
@@: mov ax,16
hd_lba_cylinders:
mov hd_heads,ax
mov cx,ax ; cx = heads
pop eax ; restore sectors_low
xor edx,edx
movzx ecx,cx
div ecx
mov hd_cylinders,ax
jmp short hd_translation_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; translation = large
hd_not_translation_lba:
cmp ax,ATA_TRANSLATION_LARGE
jne short hd_not_translation_large
.if DO_DEBUG
push ds
push cs
pop ds
mov si,offset hd_tran_large_str
call display_string
pop ds
.endif
hd_translation_is_large:
mov ax,hd_cylinders
mov cx,hd_heads
@@: cmp ax,1024
jbe short @f
shr ax,1 ; cyl >>= 1
shl cx,1 ; heads <<= 1
cmp cx,128 ; stop if heads == 128+
jae short @f
jmp short @b
@@: jmp short hd_translation_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; translation = r-echs
hd_not_translation_large:
cmp ax,ATA_TRANSLATION_RECHS
jne short hd_translation_done
.if DO_DEBUG
push ds
push cs
pop ds
mov si,offset hd_tran_rechs_str
call display_string
pop ds
.endif
; make sure we don't overflow
mov ax,hd_heads
cmp ax,16
jne short hd_translation_is_large
mov ax,hd_cylinders
cmp ax,61439
jbe short @f
mov word hd_cylinders,61439
@@: mov word hd_heads,15
xor edx,edx
movzx eax,word hd_cylinders
mov ecx,15 ;
shl eax,4 ; mul 16
div ecx ; div 15
mov hd_cylinders,ax
; then do the 'large' translation stuff too
jmp short hd_translation_is_large
hd_translation_done:
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; make sure the lchs.cylinders isn't above 1024
cmp word hd_cylinders,1024
jbe short @f
mov word hd_cylinders,1024
@@:
.if DO_DEBUG
push ds
push cs
pop ds
mov si,offset hd_lchs_str
push word hd_spt
push word hd_heads
push word hd_cylinders
call bios_printf
add sp,6
pop ds
.endif
mov ax,hd_heads
mov [bx+EBDA_DATA->ata_0_0_lchs_heads],ax
mov ax,hd_cylinders
mov [bx+EBDA_DATA->ata_0_0_lchs_cyl],ax
mov ax,hd_spt
mov [bx+EBDA_DATA->ata_0_0_lchs_spt],ax
mov ax,hd_device
mov si,hd_hdcount
mov [si+EBDA_DATA->ata_0_0_hdidmap],al
inc si
mov hd_hdcount,si
jmp short not_atapi_identify
try_atapi_identify:
; did we find an ATA device
cmp word hd_type,ATA_TYPE_ATAPI
jne short not_atapi_identify
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; send the ATAPI IDENTIFY command
; temporary values for the transfer
mov byte [bx+EBDA_DATA->ata_0_0_device],ATA_DEVICE_CDROM
mov byte [bx+EBDA_DATA->ata_0_0_mode],ATA_MODE_PIO16
; on entry to ata_cmd_data_io, bx -> EBDA_device[]
lea ax,hd_buffer
push ax ; offset of buffer
push ss ; segment of buffer
pushd 0 ; lba_high
pushd 0 ; lba_low
push 0 ; sector
push 0 ; head
push 0 ; cylinder
push 1 ; count
push ATA_CMD_IDENTIFY_DEVICE_PACKET
push word hd_device ; device index
push 0 ; io_flag (0 = read, 1 = write)
call ata_cmd_data_io
add sp,26
or ax,ax
jz short @f
;
; panic: we didn't detect the ATA via INQUIRY
push %LINE
jmp hdd_panic
;
; ss:di points to the buffer
@@: lea di,hd_buffer
; type