-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcdrom.asm
1924 lines (1739 loc) · 65 KB
/
cdrom.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: cdrom.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: *
* cdrom include file *
* *
* BUILT WITH: NewBasic Assembler *
* http://www.fysnet/newbasic.htm *
* NBASM ver 00.27.16 *
* Command line: nbasm i440fx /z<enter> *
* *
* Last Updated: 30 Jan 2025 *
* *
****************************************************************************
* Notes: *
* *
***************************************************************************|
ATAPI_CMD_TEST_READY equ 0x00
ATAPI_CMD_REQUEST_SENSE equ 0x03
ATAPI_CMD_FORMAT_UNIT equ 0x04
ATAPI_CMD_INQUIRY equ 0x12
ATAPI_CMD_START_STOP equ 0x1B
ATAPI_CMD_LOCK_UNLOCK equ 0x1E
ATAPI_CMD_READ_FORMAT_CAPACITIES equ 0x23
ATAPI_CMD_READ_CAPACITY equ 0x25
ATAPI_CMD_READ_10 equ 0x28
ATAPI_CMD_WRITE_10 equ 0x2A
ATAPI_CMD_SEEK equ 0x2B
ATAPI_CMD_WRITE_AND_VERIFY_10 equ 0x2E
ATAPI_CMD_VERIFY_10 equ 0x2F
ATAPI_CMD_FLUSH_CACHE equ 0x35
ATAPI_CMD_READ_SUB_CHANNEL equ 0x42
ATAPI_CMD_READ_TOC equ 0x43
ATAPI_CMD_READ_HEADER equ 0x44
ATAPI_CMD_PLAY_AUDIO equ 0x45
ATAPI_CMD_GET_CONFIGURATION equ 0x46
ATAPI_CMD_PLAY_AUDIO_MSF equ 0x47
ATAPI_CMD_PLAY_AUDIO_TI equ 0x48
ATAPI_CMD_GET_EVENT_STATUS_NOTIFICATION equ 0x4A
ATAPI_CMD_PAUSE_RESUME equ 0x4B
ATAPI_CMD_STOP_PLAY_SCAN equ 0x4E
ATAPI_CMD_READ_DISC_INFO equ 0x51
ATAPI_CMD_READ_TRACK_RZONE_INFO equ 0x52
ATAPI_CMD_RESERVE_RZONE_TRACK equ 0x53
ATAPI_CMD_SEND_OPC equ 0x54
ATAPI_CMD_MODE_SELECT equ 0x55
ATAPI_CMD_REPAIR_RZONE_TRACK equ 0x58
ATAPI_CMD_MODE_SENSE equ 0x5A
ATAPI_CMD_CLOSE_TRACK equ 0x5B
ATAPI_CMD_BLANK equ 0xA1
ATAPI_CMD_SEND_EVENT equ 0xA2
ATAPI_CMD_SEND_KEY equ 0xA3
ATAPI_CMD_REPORT_KEY equ 0xA4
ATAPI_CMD_LOAD_UNLOAD equ 0xA6
ATAPI_CMD_SET_READ_AHEAD equ 0xA7
ATAPI_CMD_READ_12 equ 0xA8
ATAPI_CMD_WRITE_12 equ 0xAA
ATAPI_CMD_READ_SERIAL_NUM equ 0xAB
ATAPI_CMD_GET_PERFORMANCE equ 0xAC
ATAPI_CMD_READ_DVD_STRUCTURE equ 0xAD
ATAPI_CMD_SET_STREAMING equ 0xB6
ATAPI_CMD_READ_CD_MSF equ 0xB9
ATAPI_CMD_SCAN equ 0xBA
ATAPI_CMD_SET_SPEED equ 0xBB
ATAPI_CMD_PLAY_CD equ 0xBC
ATAPI_CMD_MECH_STATUS equ 0xBD
ATAPI_CMD_READ_CD equ 0xBE
cdrom_isotag db 'CD001'
cdrom_eltorito db 'EL TORITO SPECIFICATION'
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; CD-ROM disc El Torito: initialize
; on entry:
; es = 0x0000
; on return
; nothing
; destroys nothing
cdemu_init proc near uses ax es
call bios_get_ebda
mov es,ax
mov byte es:[EBDA_DATA->cdemu_active],0x00
ret
cdemu_init endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; CD-ROM disc El Torito: is emulation active
; on entry:
; es = segment of EBDA
; on return
; ax = zero = not active, else is active
; destroys nothing
cdrom_emu_active proc near
xor ah,ah
mov al,es:[EBDA_DATA->cdemu_active]
ret
cdrom_emu_active endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; CD-ROM disc El Torito: return the drive number that is being emulated
; on entry:
; es = segment of EBDA
; on return
; ax = drive number being emulated
; destroys nothing
cdrom_emu_drive proc near
xor ah,ah
mov al,es:[EBDA_DATA->cdemu_emulated_drive]
ret
cdrom_emu_drive endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; load CD-ROM boot sector
; on entry:
; es = segment of EBDA
; dl = device
; on return
; al = status = 0 = successful
; ah = drive number to return (0x81, 0xE0, etc.)
; destroys nothing
boot_cdrom_funtion proc near uses bx cx dx si di ds
push bp
mov bp,sp
sub sp,0x810
cdrom_buffer equ [bp-0x800] ; cdrom_buffer[2048]
cdrom_atapi_cmd equ [bp-0x80C] ; cdrom_atapi_cmd[12]
cdrom_device equ [bp-0x80E] ; word
cdrom_segment equ [bp-0x810] ; word
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; find the first cdrom
xor cx,cx
@@: mov ax,cx
call atapi_is_cdrom
or ax,ax
jnz short @f
inc cx
cmp cx,BX_MAX_ATA_DEVICES
jb short @b
; error, didn't find a cdrom
mov ax,0x0002
jmp cdrom_boot_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; store the device index
@@: mov ax,cx ; ax = device index
mov cdrom_device,ax
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; see if it is ready
call atapi_is_ready
or al,al
jz short @f
; error, cdrom not ready
mov ax,0x0002
jmp cdrom_boot_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read the Boot Record Volume Descriptor
@@: push ss
pop ds
lea bx,cdrom_atapi_cmd
mov byte [bx+00],ATAPI_CMD_READ_10
mov byte [bx+01],0x00
mov byte [bx+02],((0x11 & 0xFF000000) >> 24) ; LBA
mov byte [bx+03],((0x11 & 0x00FF0000) >> 16)
mov byte [bx+04],((0x11 & 0x0000FF00) >> 8)
mov byte [bx+05],((0x11 & 0x000000FF) >> 0)
mov byte [bx+06],0x00
mov byte [bx+07],((0x01 & 0xFF00) >> 8) ; count
mov byte [bx+08],((0x01 & 0x00FF) >> 0)
mov byte [bx+09],0x00
mov word [bx+10],0x0000
lea bx,cdrom_buffer
push bx
push ss
push ATA_DATA_IN
pushd 2048
push 0
lea ax,cdrom_atapi_cmd
push ax
push ss
push 12
push word cdrom_device
call atapi_cmd_packet
add sp,20
or ax,ax
jz short @f
mov ax,3
jmp cdrom_boot_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; check for valid items
@@: cmp byte [bx+0],0x00
je short @f
mov ax,0x0004
jmp cdrom_boot_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; string at [bx+1] should be 'CD001'
@@: push 5
push cs
push offset cdrom_isotag
push ss
lea ax,[bx+1]
push ax
call strncmp
add sp,10
or ax,ax
jz short @f
mov ax,5
jmp cdrom_boot_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; string at [bx+7] should be 'EL TORITO SPECIFICATION'
@@: push 23
push cs
push offset cdrom_eltorito
push ss
lea ax,[bx+7]
push ax
call strncmp
add sp,10
or ax,ax
jz short @f
mov ax,6
jmp cdrom_boot_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; Get the Boot Catalog address
@@: mov eax,[bx+0x0047] ; LBA
lea bx,cdrom_atapi_cmd
mov byte [bx+00],ATAPI_CMD_READ_10
mov byte [bx+01],0x00
rol eax,8 ; LBA
mov [bx+02],al ; (high byte)
rol eax,8 ;
mov [bx+03],al ;
rol eax,8 ;
mov [bx+04],al ;
rol eax,8 ;
mov [bx+05],al ; (low byte)
mov byte [bx+06],0x00
mov byte [bx+07],((0x01 & 0xFF00) >> 8) ; count
mov byte [bx+08],((0x01 & 0x00FF) >> 0)
mov byte [bx+09],0x00
mov word [bx+10],0x0000
lea bx,cdrom_buffer
push bx
push ss
push ATA_DATA_IN
pushd 2048
push 0
lea ax,cdrom_atapi_cmd
push ax
push ss
push 12
push word cdrom_device
call atapi_cmd_packet
add sp,20
or ax,ax
jz short @f
mov ax,7
jmp cdrom_boot_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; check the validation entry
; 0000A000 01 00 00 00 4D 69 63 72-6F 73 6F 66 74 20 43 6F ....Microsoft.Co
; 0000A010 72 70 6F 72 61 74 69 6F-6E 00 00 00 4C 49 55 AA rporation...LIU.
; offset size value description
; 0x00 1 0x01 Header ID (must be 1)
; 0x01 1 0,1,2,0xEF Platform (0 = 80x86, 1 = Power PC, 2 = Mac, 0xEF = UEFI)
; 0x02 2 0x0000 reserved
; 0x04 24 varies Manufacturer ID
; 0x1C 2 crc two-byte crc of this entry (zero sum)
; 0x1E 2 sig 0x55 0xAA
@@: cmp byte [bx+0x00],0x01 ; 1 = Header ID
je short @f
mov ax,8
jmp cdrom_boot_done
@@: cmp byte [bx+0x01],0x00 ; 0 = platform = 80x86
je short @f
; found a platform other than 0x00 (x86)
movzx ax,byte [bx+0x01]
push cs
pop ds
push ax
mov si,offset cdrom_emu_ukn_platform
call bios_printf
add sp,2
mov ax,9
jmp cdrom_boot_done
@@: cmp word [bx+0x1E],0xAA55 ; 0x55 0xAA
je short @f
mov ax,10
jmp cdrom_boot_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; we are at the first entry
; (should be the initial/default entry)
; 0000A020 88 02 00 00 00 00 01 00-15 00 00 00 00 00 00 00 ................
; 0000A030 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
; offset size value description
; 0x00 1 0x00,0x88 0 = not bootable, 0x88 = bootable
; 0x01 1 0,1,2,3 media type (0=non emulation, 1 = 1_20 floppy, 2 = 1_44 floppy, 3 = 2_88, 4 = hard drive
; 0x02 2 varies Load segment of image (0 = 0x07C0)
; 0x04 1 varies Partition Entry type (from MBR)
; 0x05 1 0x00 reserved
; 0x06 2 varies Count of (512-byte) sectors to load
; 0x08 4 lba (2048-byte) lba to start loading
; 0x0C 20 zeros reserved
@@: add bx,0x20 ; size of entry
cmp byte [bx+0x00],0x88 ; 0x88 = Bootable
je short @f
; todo: go to the next entry.
; the one after the initial will have 0x90 as the header id if there are
; more entries to follow, else 0x91 means last entry.
; a lot of CD-ROMs have a zero at the 2nd entry with nothing following.
; (i.e.: they only have the Initial entry abov)
mov ax,11
jmp cdrom_boot_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; save some information to the EBDA
@@: mov al,[bx+0x01] ; media type
mov es:[EBDA_DATA->cdemu_media],al
; if media type = no emulation, we have to set drive to 0xE0 (a Win2k 'bug' ?)
; todo: if more than 1 cdrom is used, this should be 0xE0, 0xE1, etc. ????
mov ah,0xE0 ; assume no emulation
cmp al,0x00 ; 0 = no emu
je short @f ;
mov ah,0x00 ; assume floppy
cmp al,4 ; 1,2,3 = floppy
jb short @f ;
mov ah,0x80 ; 4 = hard drive
@@: mov es:[EBDA_DATA->cdemu_emulated_drive],ah
mov ax,cdrom_device
shr ax,1
mov es:[EBDA_DATA->cdemu_controller_index],al
mov ax,cdrom_device
and al,1
mov es:[EBDA_DATA->cdemu_device_spec],al
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; boot segment value, lba, count, etc
mov dx,0x07C0
cmp word [bx+0x02],0x0000
je short @f
mov dx,[bx+0x02] ; save in dx for later
@@: mov es:[EBDA_DATA->cdemu_load_segment],dx
mov word es:[EBDA_DATA->cdemu_buffer_offset],0x0000
movzx ecx,word [bx+06] ; count of 512-byte sectors
mov es:[EBDA_DATA->cdemu_sector_count],cx
mov eax,[bx+0x08] ; 2048-byte lba
mov es:[EBDA_DATA->cdemu_ilba],eax
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read the sector(s) into memory
lea bx,cdrom_atapi_cmd
mov byte [bx+00],ATAPI_CMD_READ_10
mov byte [bx+01],0x00
rol eax,8 ; LBA
mov [bx+02],al ; (high byte)
rol eax,8 ;
mov [bx+03],al ;
rol eax,8 ;
mov [bx+04],al ;
rol eax,8 ;
mov [bx+05],al ; (low byte)
mov byte [bx+06],0x00
push cx ; save count of sectors
dec cx ; count of 2048-byte sectors =
shr cx,2 ; ((512-byte sectors - 1) / 4) + 1
inc cx ;
ror cx,8 ; count
mov [bx+07],cl ; (high byte)
ror cx,8 ;
mov [bx+08],cl ; (low byte)
pop cx ; restore count of sectors
mov byte [bx+09],0x00
mov word [bx+10],0x0000
push 0x0000
push dx
push ATA_DATA_IN
shl ecx,9 ; 512-byte sectors to bytes
push ecx
push 0
lea ax,cdrom_atapi_cmd
push ax
push ss
push 12
push word cdrom_device
call atapi_cmd_packet
add sp,20
or ax,ax
jz short @f
mov ax,12
jmp cdrom_boot_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; save the media type
@@: mov al,es:[EBDA_DATA->cdemu_media]
cmp al,0x01 ; 1.2M floppy
jne short @f
mov word es:[EBDA_DATA->cdemu_vchs_spt],15
mov word es:[EBDA_DATA->cdemu_vchs_cyl],80
mov word es:[EBDA_DATA->cdemu_vchs_heads],2
jmp short cdrom_boot_done_emu
@@: cmp al,0x02 ; 1.44M floppy
jne short @f
mov word es:[EBDA_DATA->cdemu_vchs_spt],18
mov word es:[EBDA_DATA->cdemu_vchs_cyl],80
mov word es:[EBDA_DATA->cdemu_vchs_heads],2
jmp short cdrom_boot_done_emu
@@: cmp al,0x03 ; 2.88M floppy
jne short @f
mov word es:[EBDA_DATA->cdemu_vchs_spt],36
mov word es:[EBDA_DATA->cdemu_vchs_cyl],80
mov word es:[EBDA_DATA->cdemu_vchs_heads],2
jmp short cdrom_boot_done_emu
@@: cmp al,0x04 ; hard drive
jne short cdrom_boot_done_emu
;;;;;;;;;;;;;;;
; todo: ******
; this assumes the loaded data is a MBR with a valid partition table,
; *and* the first entry is the booted entry
push ds
mov ax,es:[EBDA_DATA->cdemu_load_segment]
mov ds,ax
mov si,es:[EBDA_DATA->cdemu_buffer_offset]
xor ah,ah
mov al,[si+(446+6)]
push ax
and al,0x3F
mov es:[EBDA_DATA->cdemu_vchs_spt],ax
pop ax
shl ax,2
mov al,[si+(446+7)]
inc ax
mov es:[EBDA_DATA->cdemu_vchs_cyl],ax
xor ah,ah
mov al,[si+(446+5)]
inc ax
mov es:[EBDA_DATA->cdemu_vchs_heads],ax
pop ds
;;;;; end of todo:
cdrom_boot_done_emu:
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; Increase bios installed hardware number of devices?
cmp byte es:[EBDA_DATA->cdemu_media],0
je short cdrom_boot_done_1
; increment the count of drives installed
cmp byte es:[EBDA_DATA->cdemu_emulated_drive],0
jne short @f
; increment the count of floppy drives installed
push ds
xor ax,ax
mov ds,ax
or byte [0x0410],0x41
pop ds
jmp short cdrom_boot_done_0
; increment the count of hard drives installed
@@: inc byte es:[EBDA_DATA->ata_hdcount]
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; if there was an emulation specified, it should now be active
cdrom_boot_done_0:
mov byte es:[EBDA_DATA->cdemu_active],0x01
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; set return value to ah = drive, al = 0 = no error
cdrom_boot_done_1:
mov ah,es:[EBDA_DATA->cdemu_emulated_drive]
xor al,al ; no error
cdrom_boot_done:
mov sp,bp ; restore the stack
pop bp
ret
boot_cdrom_funtion endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; CD-ROM disc El Torito services
; on entry:
; es = segment of EBDA
; stack currently has (after we set bp):
; flags cs ip es ds
; [bp+44] [bp+42] [bp+40] [bp+38] [bp+36]
; edi esi ebp esp ebx edx ecx eax
; [bp+04] [bp+08] [bp+12] [bp+16] [bp+20] [bp+24] [bp+28] [bp+32]
; on return
; nothing
; destroys nothing
int13_eltorito_function proc near ; don't put anything here
push bp
mov bp,sp
; sub sp,4
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; ds -> BIOS Data Area
mov ax,0x0040
mov ds,ax
; service call
mov ax,REG_AX
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; terminate disk emu / get status
; ds:si-> points to packet
; dl = drive or 0x7F to terminate all
cmp ax,0x4B00
je short int13_eltorito_4B
cmp ax,0x4B01
jne short @f
int13_eltorito_4B:
; todo: for now we hard code this. This really needs to be worked on
; if DL != 0xE0, should we return carry and *not* fill the packet????
cmp byte REG_DL,0xE0
jne short int13_eltorito_fail
;.if DO_INIT_BIOS32
; todo: check to see if DL = physical cdrom or USB CDROM and do accordingly.
;.endif
push ds
mov ax,REG_DS
mov ds,ax
mov bx,REG_SI
mov al,0x13
mov [bx+0x00],al
mov al,es:[EBDA_DATA->cdemu_media]
mov [bx+0x01],al
mov al,es:[EBDA_DATA->cdemu_emulated_drive]
mov [bx+0x02],al
mov al,es:[EBDA_DATA->cdemu_controller_index]
mov [bx+0x03],al
mov eax,es:[EBDA_DATA->cdemu_ilba]
mov [bx+0x04],eax
mov ax,es:[EBDA_DATA->cdemu_device_spec]
mov [bx+0x08],ax
mov ax,es:[EBDA_DATA->cdemu_buffer_offset]
mov [bx+0x0A],ax
mov ax,es:[EBDA_DATA->cdemu_load_segment]
mov [bx+0x0C],ax
mov ax,es:[EBDA_DATA->cdemu_sector_count]
mov [bx+0x0E],ax
mov ax,es:[EBDA_DATA->cdemu_vchs_cyl]
mov [bx+0x10],al
mov al,es:[EBDA_DATA->cdemu_vchs_spt]
and al,0x3F
shl ah,6
or al,ah
mov [bx+0x11],al
mov ax,es:[EBDA_DATA->cdemu_vchs_heads]
mov [bx+0x12],al
pop ds
cmp byte REG_AL,0x00
jne short int13_eltorito_success
mov byte es:[EBDA_DATA->cdemu_active],0x00
mov byte es:[EBDA_DATA->usb_disk_emu_cdrom],0x00 ; for now, we terminate both...
jmp short int13_eltorito_success
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;
@@: ;cmp ah,0x ; next value
;jne short @f
;
;
;jmp short int13_eltorito_success
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; unknown function found
xchg cx,cx ; ben ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
jmp short int13_eltorito_fail
int13_eltorito_fail:
mov byte REG_AH,0x01 ; default to invalid function in AH or invalid parameter
int13_eltorito_fail_noah:
mov ah,REG_AH
mov [0x0074],ah
int13_eltorito_fail_nostatus:
or word REG_FLAGS,0x0001
mov sp,bp
pop bp
ret
int13_eltorito_success:
mov byte REG_AH,0x00 ; no error
int13_eltorito_success_noah:
mov byte [0x0074],0x00
and word REG_FLAGS,(~0x0001)
mov sp,bp
pop bp
ret
int13_eltorito_function endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; CD-ROM disc El Torito services: Emulation function
; on entry:
; es = segment of EBDA
; stack currently has (after we set bp):
; flags cs ip es ds
; [bp+44] [bp+42] [bp+40] [bp+38] [bp+36]
; edi esi ebp esp ebx edx ecx eax
; [bp+04] [bp+08] [bp+12] [bp+16] [bp+20] [bp+24] [bp+28] [bp+32]
; on return
; nothing
; destroys nothing
cdrom_emu_function proc near ; don't put anything here
push bp
mov bp,sp
sub sp,0x0E
cdemu_emu_device equ [bp-0x02]
cdrom_emu_atapi_cmd equ [bp-0x0E] ; cdrom_emu_atapi_cmd[12]
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; ds -> BIOS Data Area
mov ax,0x0040
mov ds,ax
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; at this point, we are emulating a floppy or hardisk
; from a CD-ROM
xor ah,ah
mov al,es:[EBDA_DATA->cdemu_controller_index]
shl al,1
add al,es:[EBDA_DATA->cdemu_device_spec]
mov cdemu_emu_device,ax
imul bx,ax,ATA_DEVICE_SIZE
; clear the BDA status byte
mov byte [0x0074],0x00
; service call
mov ah,REG_AH
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; these functions simply return success
cmp ah,0x00 ; disk controller reset
je cdrom_emu_success
cmp ah,0x09 ; initialize drive parameters
je cdrom_emu_success
cmp ah,0x0C ; seek to specified cylinder
je cdrom_emu_success
cmp ah,0x0D ; alternate disk reset // FIXME: should really reset ?
je cdrom_emu_success
cmp ah,0x10 ; check drive ready // FIXME: should check if ready ?
je cdrom_emu_success
cmp ah,0x11 ; recalibrate
je cdrom_emu_success
cmp ah,0x14 ; controller internal diagnostic
je cdrom_emu_success
cmp ah,0x16 ; detect disk change
je cdrom_emu_success
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; these functions return disk write-protected
mov byte REG_AH,0x03
cmp ah,0x03 ; write disk sectors
je cdrom_emu_fail_noah
cmp ah,0x05 ; format disk track
je cdrom_emu_fail_noah
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read disk status
cmp ah,0x01 ; read disk status
jne short @f
mov al,[0x0074]
mov REG_AH,al
mov byte [0x0074],0x00
or al,al
jnz cdrom_emu_fail_nostatus
jmp cdrom_emu_success_noah
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read/verify disk sectors
@@: cmp ah,0x02 ; read disk sectors
je short cdrom_emu_transfer
cmp ah,0x04 ; verify disk sectors
jne @f
cdrom_emu_transfer:
; if sectors = 0, no need to continue
mov al,REG_AL
or al,al
je cdrom_emu_success
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; if (sector > emulated spt) or (cylinder >= emulated cyls)
; or (head > emulated heads), then error
mov ax es:[EBDA_DATA->cdemu_vchs_spt]
mov cl,REG_CL
and cx,0x3F
cmp cx,ax
ja cdrom_emu_fail
mov ax,es:[EBDA_DATA->cdemu_vchs_cyl]
mov cl,REG_CL
shl cx,2 ; ch = 0 from above
mov cl,REG_CH
cmp cx,ax
jae cdrom_emu_fail
mov ax,es:[EBDA_DATA->cdemu_vchs_heads]
xor ch,ch
mov cl,REG_DH
cmp cx,ax
jae cdrom_emu_fail
; now that we verified good parameters,
; if verify call, simply return good
cmp byte REG_AH,0x04
je cdrom_emu_success
; calculate the virtual lba inside the emulated image
; vlba = (((cylinder * vheads) + head) * vspt) + sector - 1;
movzx eax,byte REG_CL
shl ax,2
mov al,REG_CH
movzx ebx,word es:[EBDA_DATA->cdemu_vchs_heads]
mul ebx
movzx ebx,byte REG_DH
add eax,ebx
movzx ebx,word es:[EBDA_DATA->cdemu_vchs_spt]
mul ebx
movzx ebx,byte REG_CL
and bl,0x3F
add eax,ebx
dec eax
; eax = virtual (512-byte) lba in emulated image
xor edx,edx ;
mov ebx,eax ; save in ebx
shr eax,2 ; eax = starting physical lba on (2048-byte) cd-rom
mov cx,bx ;
and cx,0x03 ; cx = count of (512-byte) sectors before wanted sector
shl cx,9 ; (convert to bytes)
mov dl,REG_AL ;
add ebx,edx ;
dec ebx ;
shr ebx,2 ; ebx = ending (2048-byte) lba on cd-rom
mov edx,ebx ;
sub edx,eax ;
inc dx ; dx = count of (2048-byte) sectors to read
; add the base of the emulated image
add eax,es:[EBDA_DATA->cdemu_ilba]
lea si,cdrom_emu_atapi_cmd
mov byte ss:[si+00],ATAPI_CMD_READ_10
mov byte ss:[si+01],0x00
rol eax,8 ; LBA
mov ss:[si+02],al ; (high byte)
rol eax,8 ;
mov ss:[si+03],al ;
rol eax,8 ;
mov ss:[si+04],al ;
rol eax,8 ;
mov ss:[si+05],al ; (low byte)
mov byte ss:[si+06],0x00
mov ss:[si+07],dh ; Count (high byte)
mov ss:[si+08],dl ; (low byte)
mov byte ss:[si+09],0x00
mov word ss:[si+10],0x0000
; 'normalize' the address
mov bx,REG_BX
shr bx,4
mov ax,REG_ES
add ax,bx
mov bx,REG_BX
and bx,0x000F
push bx ; offset
push ax ; segment
push ATA_DATA_IN ; read
movzx eax,byte REG_AL ;
shl eax,9 ; number of bytes wanted
push eax ;
push cx ; count before
push si ; offset command block
push ss ; segment command block
push 12 ; size in byte of command block
push word cdemu_emu_device
call atapi_cmd_packet
add sp,20
or ax,ax
jz cdrom_emu_success
mov word REG_AX,0x0200
jmp short cdrom_emu_fail_noah
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read disk drive parameters
@@: cmp ah,0x08
jne short @f
mov ax,es:[EBDA_DATA->cdemu_vchs_cyl]
dec ax
mov bx,es:[EBDA_DATA->cdemu_vchs_spt]
mov cx,es:[EBDA_DATA->cdemu_vchs_heads]
dec cx
mov byte REG_AL,0x00
mov byte REG_BL,0x00
mov REG_CH,al
shr ax,2
and al,0xC0
and bl,0x3F
or al,bl
mov REG_CL,al
mov REG_DH,cl
mov byte REG_DL,0x02 ; todo: floppy: 1 or 2, hddrv = hdcount
mov ax,es:[EBDA_DATA->cdemu_media]
shl ax,1
mov REG_BL,al
mov ax,offset diskette_param_table
mov REG_DI,ax
mov REG_ES,cs
jmp short cdrom_emu_success
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read disk drive type/size
@@: cmp ah,0x15
jne short @f
mov ah,0x02 ; assume a floppy with change-line support
mov al,es:[EBDA_DATA->cdemu_media]
cmp al,0x04
jbe short cdrom_emu_ah15_0
mov ah,0x03
cdrom_emu_ah15_0:
;
; todo: cx:dx ???? (most bios' don't set it anyway)
;
mov REG_AH,ah
jmp short cdrom_emu_success_noah
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;
@@: ;cmp ah,0x ; next value
;jne short @f
;
;
;jmp short cdrom_emu_success
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; print a message of this unknown call value
@@: push ds
push cs
pop ds
shr ax,8
push ax
mov si,offset cdrom_emu_unknown_call_str
call bios_printf
add sp,2
pop ds
jmp short cdrom_emu_fail
cdrom_emu_fail:
mov byte REG_AH,0x01 ; default to invalid function in AH or invalid parameter
cdrom_emu_fail_noah:
mov ah,REG_AH
mov [0x0074],ah
cdrom_emu_fail_nostatus:
or word REG_FLAGS,0x0001
jmp short cdrom_emu_function_done
cdrom_emu_success:
mov byte REG_AH,0x00 ; no error
cdrom_emu_success_noah:
mov byte [0x0074],0x00
and word REG_FLAGS,(~0x0001)
cdrom_emu_function_done:
mov sp,bp
pop bp
ret
cdrom_emu_function endp
cdrom_emu_ukn_platform db 'Found unsupported platform ID: 0x%02X',13,10,0
cdrom_emu_unknown_call_str db 'cdrom_emu: Unknown call 0x%02X',13,10,0
cdrom_rdy_size_str db '%iMB medium detected',13,10,0
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; CD-ROM disc services
; on entry:
; es = segment of EBDA
; stack currently has (after we set bp):
; flags cs ip es ds
; [bp+44] [bp+42] [bp+40] [bp+38] [bp+36]
; edi esi ebp esp ebx edx ecx eax
; [bp+04] [bp+08] [bp+12] [bp+16] [bp+20] [bp+24] [bp+28] [bp+32]
; on return
; nothing
; destroys nothing
int13_cdrom_function proc near ; don't put anything here
push bp
mov bp,sp
sub sp,0x0E
cd_sv_device equ [bp-0x02]
cd_atapi_cmd equ [bp-0x0E] ; cd_atapi_cmd[12]
; get the device
movzx bx,byte REG_DL
cmp bl,0xE0
jb cd_int13_fail
cmp bl,(0xE0 + BX_MAX_ATA_DEVICES)
jae cd_int13_fail
; else, valid DL number
sub bx,0xE0
mov al,es:[bx+EBDA_DATA->ata_0_0_cdidmap]
cmp al,BX_MAX_ATA_DEVICES
jae cd_int13_fail
xor ah,ah
mov cd_sv_device,ax
imul bx,ax,ATA_DEVICE_SIZE
mov ah,REG_AH
; cd_sv_device = device
; es = segment of EBDA
; ah = service
; bx ->EBDA_DATA->device
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; todo: the following currently return success
; (some of them we can call the int13_harddisk_function equivalent)
; (if we restore sp and pop bp, we can 'jmp' to the function)
; mov sp,bp
; pop bp
; jmp int13_harddisk_function
;
; controller reset
cmp ah,0x00
je cd_int13_success
; initialize drive parameters
cmp ah,0x09
je cd_int13_success
; seek to specified cylinder
cmp ah,0x0C
je cd_int13_success
; alternate disk reset
cmp ah,0x0D
je cd_int13_success
; check drive ready
cmp ah,0x10
je cd_int13_success
; recalibrate
cmp ah,0x11
je cd_int13_success
; internal diagnostic
cmp ah,0x14
je cd_int13_success
; detect disc change
cmp ah,0x16
je cd_int13_success
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; these return disc write protected
mov byte REG_AH,03
; write disc sectors
cmp ah,0x03
je cd_int13_fail_noah