-
Notifications
You must be signed in to change notification settings - Fork 1
/
usb.asm
2034 lines (1776 loc) · 72.6 KB
/
usb.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-2024 Forever Young Software Benjamin David Lunt *
* *
* i440FX BIOS ROM v1.0 *
* FILE: usb.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: *
* usb include file *
* *
* BUILT WITH: NewBasic Assembler *
* http://www.fysnet/newbasic.htm *
* NBASM ver 00.27.14 *
* Command line: nbasm i440fx /z<enter> *
* *
* Last Updated: 25 Oct 2024 *
* *
****************************************************************************
* Notes: *
* *
***************************************************************************|
.if DO_INIT_BIOS32
; Reset wait times (in ms). USB 2.0 specs, page 153, section 7.1.7.5, paragraph 3
USB_TDRSTR equ 50 ; reset on a root hub
USB_TDRST equ 10 ; minimum delay for a reset
USB_TRHRSI equ 3 ; No more than this between resets for root hubs
USB_TRSTRCY equ 10 ; reset recovery
BBB_CBW struct
sig dword
tag dword
length dword
flags byte
lun byte
cb_len byte
cmnd dup 16
BBB_CBW ends
BBB_CSW struct
sig dword
tag dword
residue dword
status byte
BBB_CSW ends
PID_SETUP equ 0x2D
PID_IN equ 0x69
PID_OUT equ 0xE1
.enum CONTROL_EP, ISO_EP, BULK_EP, INTERRUPT_EP
; arbatrary numbers so we can determine which one we are using
USB_PROTO_BBB equ 0xBB
USB_PROTO_CBI equ 0xCB
;USB_PROTO_UASP equ 0xAA
USB_REQUEST struct
request_type byte
request byte
value word
index word
length word
USB_REQUEST ends
request_device_str db 0x80, 0x06, 0x00, 0x01, 0x00, 0x00, ?, ?
request_config_str db 0x80, 0x06, 0x00, 0x02, 0x00, 0x00, ?, ?
request_set_config db 0x00, 0x09, ?, ?, 0x00, 0x00, ?, ?
request_cbi_cmd_str db 0x21, 0, 0, 0, 0, 0, ?, ?
; can be up to IPL_ENTRY_MAX_DESC_LEN-1 chars
CONTROLLER_STR_LEN equ 14
usb_controller_str db '(UHCI Device)',0
db '(OHCI Device)',0
db '(EHCI Device)',0
db '(xHCI Device)',0
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; USB Disk Emulation: initialize
; on entry:
; es = 0x0000
; on return
; nothing
; destroys nothing
usb_disk_init proc near uses ax bx cx es
call bios_get_ebda
mov es,ax
mov byte es:[EBDA_DATA->usb_disk_active],0x00
mov byte es:[EBDA_DATA->usb_next_device_id],0x00
; get the ehci legacy flag from the escd
mov bx,ESCD_DATA->ehci_legacy
mov cx,sizeof(byte)
call bios_read_escd
mov es:[EBDA_DATA->usb_ehci_legacy],al
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; detect any xHCI devices
call init_xhci_boot
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; detect any EHCI devices (must be before the uhci and ohci)
call init_ehci_boot
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; detect any UHCI devices
call init_uhci_boot
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; detect any OHCI devices
call init_ohci_boot
ret
usb_disk_init endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; USB Disk Emulation: is emulation active
; on entry:
; es = segment of EBDA
; on return
; ax = zero = not active, else is active
; destroys nothing
usb_disk_emu_active proc near
movzx ax,byte es:[EBDA_DATA->usb_disk_active]
ret
usb_disk_emu_active endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; USB Disk Emulation: return the drive number that is being emulated
; on entry:
; es = segment of EBDA
; on return
; ax = drive number being emulated
; destroys nothing
usb_disk_emu_drive proc near
movzx ax,byte es:[EBDA_DATA->usb_disk_emulated_drive]
ret
usb_disk_emu_drive endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; Get next device address id
; on entry:
; es = segment of EBDA
; on return
; ax = next id to use
; destroys nothing
usb_get_address_id proc near
inc byte es:[EBDA_DATA->usb_next_device_id]
movzx ax,byte es:[EBDA_DATA->usb_next_device_id]
cmp al,0x0F
jbe short @f
; error, we have too many devices attached
xchg cx,cx
@@: ret
usb_get_address_id endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; given a configuration, find specified descriptor
; on entry:
; fs:edi -> configuration buffer
; al = type of descriptor to find
; ah = index (0 = 1st, 1 = 2nd, etc)
; cx = length of configuration buffer
; on return
; eax = offset from edi to found descriptor
; = -1 if not found
; destroys nothing
usb_config_find_desc proc near uses ebx ecx edx
movzx ebx,word cx ; save the length of the config buffer
movzx cx,byte ah ; cx = the index
mov dl,al ; type to search for
xor eax,eax
config_find_0:
cmp fs:[edi + eax + 1],dl
je short @f
config_find_1:
push edx
movzx edx,byte fs:[edi + eax + 0]
add eax,edx
pop edx
cmp eax,ebx
jb short config_find_0
mov eax,-1
ret
@@: or cx,cx
jz short @f
dec cx
jmp short config_find_1
; make sure the whole descriptor is available
@@: movzx edx,byte fs:[edi + eax + 0]
add edx,eax
cmp edx,ebx
jbe short @f
mov eax,-1
; 09 02 20 00 01 01 00 C0-00
; 09 04 00 00 02 08 06 50 00
; 07 05 81 02 40 00-00
; 07 05 02 02 40 00 00
@@: ret
usb_config_find_desc endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; get the endpoint information from the config descriptor
; on entry:
; fs:ebx -> USB_DEVICE
; fs:edi -> configuration buffer
; cx = length of configuration buffer
; on return
; ax = count of found endpoints (should be 2)
; destroys nothing
usb_configure_device proc near uses ebx ecx edx esi edi
push bp
mov bp,sp
sub sp,6
usb_config_eps equ [bp-2]
usb_config_len equ [bp-4]
usb_config_idx equ [bp-5]
mov word usb_config_eps,0
mov usb_config_len,cx
; now find the interface descriptor and check the class (08), subclass (06), and protocol (50)
mov al,0x04 ; type of descriptor to find (Interface)
mov ah,0 ; index (0 = 1st, 1 = 2nd, etc)
call usb_config_find_desc
cmp eax,-1
jle usb_configure_device_error
; eax = offset from edi
; all devices must have a class code of 8
cmp byte fs:[edi + eax + 5],0x08 ; 8 = MSD
jne usb_configure_device_done
; if a device has a sub_class code of 6 and a protocol of 0x50, do BBB
cmp word fs:[edi + eax + 6],0x5006 ; 6 = SCSI transparent command set
je short usb_config_is_bbb ; 0x50 = Bulk Only Transport
; if a device has a sub_class code of 4 and a protocol of 0x50, do BBB
cmp word fs:[edi + eax + 6],0x5004 ; 4 = UFI Command Specs
je short usb_config_is_bbb ; 0x50 = Bulk Only Transport
; if a device has a sub_class code of 4 and a protocol of 0x00, do CB(i)
cmp word fs:[edi + eax + 6],0x0004 ; 4 = UFI Command Specs
je usb_config_is_cbi ; 0x00 = Control/Bulk
; if a device has a sub_class code of 4 and a protocol of 0x01, do CB(i)
cmp word fs:[edi + eax + 6],0x0104 ; 4 = UFI Command Specs
je usb_config_is_cbi ; 0x01 = Control/Bulk/Interrupt
; ; if a device has a sub_class code of 6 and a protocol of 0x62, do UASP
; ; however, most if not all UASP devices will default to BBB, and since
; ; we are simply booting it, BBB will be just fine...
; cmp word fs:[edi + eax + 6],0x6206 ; 6 = SCSI transparent command set
; je short usb_config_is_uasp ; 0x62 = UASP
jmp usb_configure_device_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; found a BBB device, so gather some information about it
; we need two EndPoints
usb_config_is_bbb:
mov byte fs:[ebx+USB_DEVICE->protocol],USB_PROTO_BBB
mov byte usb_config_idx,0
usb_config_ep_loop:
mov ah,usb_config_idx ; index (0 = 1st, 1 = 2nd, etc)
mov cx,usb_config_len ; length of config desc
mov al,0x05 ; type of descriptor to find (endpoint)
call usb_config_find_desc
cmp eax,-1
jle usb_configure_device_error
; found an endpoint, is a bulk ep?
cmp byte fs:[edi + eax + 3],0x02 ; 2 = bulk
jne short usb_config_ep_loop_0
mov dl,fs:[edi + eax + 2]
lea esi,[ebx+USB_DEVICE->endpoint_in]
test dl,0x80
jnz short @f
add esi,sizeof(USB_DEVICE_EP)
@@: and dl,0x7F
mov fs:[esi+USB_DEVICE_EP->ep_val],dl
mov dx,fs:[edi + eax + 4]
mov fs:[esi+USB_DEVICE_EP->ep_mps],dx
mov dl,fs:[edi + eax + 5]
mov fs:[esi+USB_DEVICE_EP->ep_interval],dl
; todo: we must check to make sure a sector size (2048 max)
; divided by this mps isn't more than the TDs we have allocated)
; (for 512 byte sectors, we must have at least an 8-byte mps)
; (for 2048 byte sectors, we must have at least a 32-byte mps)
; (for 4096 byte sectors, we must have at least a 64-byte mps)
; (we currently only allow 2048 max sector size)
mov byte fs:[esi+USB_DEVICE_EP->ep_toggle],0
; if this device is a super-speed device, the next descriptor
; will be an endpoint-companion descriptor. If so we need to
; get the max burst value
mov byte fs:[esi+USB_DEVICE_EP->ep_max_burst],0
cmp word fs:[edi + eax + 7],0x3006 ; 0x06 is the size, 0x30 is the type
jne short @f
mov dl,fs:[edi + eax + 7 + 2]
mov fs:[esi+USB_DEVICE_EP->ep_max_burst],dl
@@: inc word usb_config_eps
cmp word usb_config_eps,2
je short usb_configure_device_done
usb_config_ep_loop_0:
inc byte usb_config_idx
jmp short usb_config_ep_loop
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; found a CB(i) device, so gather some information about it
; we need at least two EndPoints (possibly 3)
usb_config_is_cbi:
mov byte fs:[ebx+USB_DEVICE->protocol],USB_PROTO_CBI
; we only need the two bulk eps, so do the same as BBB
mov byte usb_config_idx,0
jmp usb_config_ep_loop
; ; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; ; found a UASP device, so gather some information about it
; ; we need at least four EndPoints
;usb_config_is_uasp:
; mov byte fs:[ebx+USB_DEVICE->protocol],USB_PROTO_UASP
; ;;;;;;;;;;;;;;
; jmp short usb_configure_device_error
usb_configure_device_done:
mov ax,usb_config_eps
mov sp,bp
pop bp
ret
usb_configure_device_error:
mov ax,-1
mov sp,bp
pop bp
ret
usb_configure_device endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read the drive's capacity
; on entry:
; dx = zero based port number (0 -> (USB_DEVICE_MAX-1)) (is device number - 1)
; es:esi-> = this USB_CONTROLLER structure
; fs:ebx -> USB_DEVICE
; on return
; eax = bytes read
; = negative value if error
; destroys none
usb_drive_capacity proc near
cmp byte fs:[ebx+USB_DEVICE->protocol],USB_PROTO_BBB ; BBB
jne short @f
call usb_drive_capacity_bbb
ret
@@: cmp byte fs:[ebx+USB_DEVICE->protocol],USB_PROTO_CBI ; CB(i)
jne short @f
call usb_drive_capacity_cbi
ret
;@@: cmp byte fs:[ebx+USB_DEVICE->protocol],USB_PROTO_UASP ; UASP
; jne short @f
; call usb_drive_capacity_uasp
; ret
@@: ;xchg cx,cx ; ben ;;;;;;;;;;;;;;;;;;;
ret
usb_drive_capacity endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read the drive's capacity (using BBB)
; on entry:
; dx = zero based port number (0 -> (USB_DEVICE_MAX-1)) (is device number - 1)
; es:esi-> = this USB_CONTROLLER structure
; fs:ebx -> USB_DEVICE
; on return
; eax = bytes read
; = negative value if error
; destroys none
usb_drive_capacity_bbb proc near uses ebx ecx edx esi edi
push bp
mov bp,sp
sub sp,16
usb_caps_bbb_cbw equ [bp-4]
usb_caps_bbb_csw equ [bp-8]
usb_caps_bbb_buf equ [bp-12]
usb_caps_bbb_tag equ [bp-16]
; save the addresses to our buffers
lea ecx,[ebx+USB_DEVICE->cbw]
mov usb_caps_bbb_cbw,ecx
lea ecx,[ebx+USB_DEVICE->csw]
mov usb_caps_bbb_csw,ecx
lea ecx,[ebx+USB_DEVICE->rxtx_buffer]
mov usb_caps_bbb_buf,ecx
lea ecx,[ebx+USB_DEVICE->next_tag]
inc dword fs:[ecx]
mov ecx,fs:[ecx]
mov usb_caps_bbb_tag,ecx
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; build a command block wrapper
mov edi,usb_caps_bbb_cbw
mov ax,sizeof(BBB_CBW)
call memset32
mov dword fs:[edi+BBB_CBW->sig],0x43425355
mov fs:[edi+BBB_CBW->tag],ecx
mov dword fs:[edi+BBB_CBW->length],8
mov byte fs:[edi+BBB_CBW->flags],0x80
mov byte fs:[edi+BBB_CBW->lun],0x00
mov byte fs:[edi+BBB_CBW->cb_len],10
mov byte fs:[edi+BBB_CBW->cmnd+0],0x25 ; read capacity(10)
;mov byte fs:[edi+BBB_CBW->cmnd+1],0
;mov byte fs:[edi+BBB_CBW->cmnd+2],0
;mov byte fs:[edi+BBB_CBW->cmnd+3],0
;mov byte fs:[edi+BBB_CBW->cmnd+4],0
;mov byte fs:[edi+BBB_CBW->cmnd+5],0
;mov byte fs:[edi+BBB_CBW->cmnd+6],0
;mov byte fs:[edi+BBB_CBW->cmnd+7],0
;mov byte fs:[edi+BBB_CBW->cmnd+8],0
;mov byte fs:[edi+BBB_CBW->cmnd+9],0
;mov byte fs:[edi+BBB_CBW->cmnd+10],0
;mov byte fs:[edi+BBB_CBW->cmnd+11],0
; send the CBW packet
mov al,PID_OUT ; direction (PID_IN or PID_OUT)
mov cx,sizeof(BBB_CBW) ; packet size
call es:[esi+USB_CONTROLLER->callback_bulk]
cmp eax,-1
jle short usb_caps_bbb_done
; send the IN packets packet
mov edi,usb_caps_bbb_buf
mov al,PID_IN ; direction (PID_IN or PID_OUT)
mov cx,8 ; packet size
call es:[esi+USB_CONTROLLER->callback_bulk]
cmp eax,-1
jle short usb_caps_bbb_done
mov usb_caps_bbb_buf,eax ; save the count (8)
; send the CSW packets packet
mov edi,usb_caps_bbb_csw
mov al,PID_IN ; direction (PID_IN or PID_OUT)
mov cx,sizeof(BBB_CSW) ; packet size
call es:[esi+USB_CONTROLLER->callback_bulk]
cmp eax,-1
jle short usb_caps_bbb_done
; make sure the tag is the same
mov ecx,fs:[edi+BBB_CSW->tag]
cmp ecx,usb_caps_bbb_tag
jne short usb_caps_bbb_done
mov eax,usb_caps_bbb_buf ; restore the count from above
usb_caps_bbb_done:
mov sp,bp
pop bp
ret
usb_drive_capacity_bbb endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read the drive's capacity (Using CBI)
; on entry:
; dx = zero based port number (0 -> (USB_DEVICE_MAX-1)) (is device number - 1)
; es:esi-> = this USB_CONTROLLER structure
; fs:ebx -> USB_DEVICE
; on return
; eax = bytes read
; = negative value if error
; destroys none
usb_drive_capacity_cbi proc near uses ebx ecx edx esi edi
push bp
mov bp,sp
sub sp,4
usb_caps_cbi_buf equ [bp-4]
; save the addresses to our buffers
lea ecx,[ebx+USB_DEVICE->rxtx_buffer]
mov usb_caps_cbi_buf,ecx
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; build a command block
mov edi,usb_caps_cbi_buf
mov ax,12
call memset32
mov byte fs:[edi+0],0x25 ; read capacity(10)
;mov byte fs:[edi+1],0
;mov byte fs:[edi+2],0
;mov byte fs:[edi+3],0
;mov byte fs:[edi+4],0
;mov byte fs:[edi+5],0
;mov byte fs:[edi+6],0
;mov byte fs:[edi+7],0
;mov byte fs:[edi+8],0
;mov byte fs:[edi+9],0
;mov byte fs:[edi+10],0
;mov byte fs:[edi+11],0
; send the CBW packet
mov edi,offset request_cbi_cmd_str
mov cx,12
xor dx,dx
mov al,PID_OUT
call es:[esi+USB_CONTROLLER->callback_control]
cmp eax,-1
jle short usb_caps_cbi_done
; send the IN packets packet
mov edi,usb_caps_cbi_buf
mov al,PID_IN ; direction (PID_IN or PID_OUT)
mov cx,8 ; packet size
call es:[esi+USB_CONTROLLER->callback_bulk]
usb_caps_cbi_done:
mov sp,bp
pop bp
ret
usb_drive_capacity_cbi endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read/write a sector from the drive
; on entry:
; eax = lba to read/write
; cl = PID_IN or PID_OUT
; es:esi-> = this USB_CONTROLLER structure
; fs:ebx -> USB_DEVICE
; edi-> = physical address of buffer to read/write
; on return
; eax = bytes read (512 for a 'floppy' or 'hard drive', 2048 for a cdrom)
; = negative value if error
; destroys none
usb_rxtx_sector proc near
cmp byte fs:[ebx+USB_DEVICE->protocol],USB_PROTO_BBB ; BBB
jne short @f
call usb_rxtx_sector_bbb
ret
@@: cmp byte fs:[ebx+USB_DEVICE->protocol],USB_PROTO_CBI ; CB(i)
jne short @f
call usb_rxtx_sector_cbi
ret
;@@: cmp byte fs:[ebx+USB_DEVICE->protocol],USB_PROTO_UASP ; UASP
; jne short @f
; call usb_drive_capacity_uasp
; ret
@@: xchg cx,cx ; ben ;;;;;;;;;;;;;;;;;;;
ret
usb_rxtx_sector endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read/write a sector from/to the drive (Using BBB)
; on entry:
; eax = lba to read/write
; cl = PID_IN or PID_OUT
; es:esi-> = this USB_CONTROLLER structure
; fs:ebx -> USB_DEVICE
; edi-> = physical address of buffer to read/write
; on return
; eax = bytes read (512 for a 'floppy' or 'hard drive', 2048 for a cdrom)
; = negative value if error
; destroys none
usb_rxtx_sector_bbb proc near uses ebx ecx edx esi edi
push bp
mov bp,sp
sub sp,22
usb_rxtx_bbb_lba equ [bp-4] ; dword
usb_rxtx_bbb_cbw equ [bp-8] ; dword
usb_rxtx_bbb_csw equ [bp-12] ; dword
usb_rxtx_bbb_buf equ [bp-16] ; dword
usb_rxtx_bbb_tag equ [bp-20] ; dword
usb_rxtx_bbb_dir equ [bp-21] ; byte
; save the addresses to our buffers
mov usb_rxtx_bbb_dir,cl
mov usb_rxtx_bbb_lba,eax
mov usb_rxtx_bbb_buf,edi
lea ecx,[ebx+USB_DEVICE->cbw]
mov usb_rxtx_bbb_cbw,ecx
lea ecx,[ebx+USB_DEVICE->csw]
mov usb_rxtx_bbb_csw,ecx
lea ecx,[ebx+USB_DEVICE->next_tag]
inc dword fs:[ecx]
mov ecx,fs:[ecx]
mov usb_rxtx_bbb_tag,ecx
; determine the direction state
mov dx,0x2880 ; dh = read(10) command, dl = 0x80 = flags
cmp byte usb_rxtx_bbb_dir,PID_IN
je short @f
mov dx,0x2A00 ; dh = write(10) command, dl = 0x00 = flags
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; build a command block wrapper
@@: mov edi,usb_rxtx_bbb_cbw
mov ax,sizeof(BBB_CBW)
call memset32
mov dword fs:[edi+BBB_CBW->sig],0x43425355
mov fs:[edi+BBB_CBW->tag],ecx
movzx eax,word fs:[ebx+USB_DEVICE->block_size]
mov fs:[edi+BBB_CBW->length],eax
mov fs:[edi+BBB_CBW->flags],dl
mov byte fs:[edi+BBB_CBW->lun],0x00
mov byte fs:[edi+BBB_CBW->cb_len],10
mov fs:[edi+BBB_CBW->cmnd+0],dh
;mov byte fs:[edi+BBB_CBW->cmnd+1],0
mov eax,usb_rxtx_bbb_lba
bswap eax
mov fs:[edi+BBB_CBW->cmnd+2],eax
;mov byte fs:[edi+BBB_CBW->cmnd+6],0
mov byte fs:[edi+BBB_CBW->cmnd+7],0 ; count high-byte
mov byte fs:[edi+BBB_CBW->cmnd+8],1 ; count low-byte
;mov byte fs:[edi+BBB_CBW->cmnd+9],0
;mov byte fs:[edi+BBB_CBW->cmnd+10],0
;mov byte fs:[edi+BBB_CBW->cmnd+11],0
; send the CBW packet
mov al,PID_OUT ; direction (PID_IN or PID_OUT)
mov cx,sizeof(BBB_CBW) ; packet size
call es:[esi+USB_CONTROLLER->callback_bulk]
cmp eax,-1
jle short usb_rxtx_sector_bbb_done
; send the IN/OUT packets
mov edi,usb_rxtx_bbb_buf
mov al,usb_rxtx_bbb_dir ; direction (PID_IN or PID_OUT)
mov cx,fs:[ebx+USB_DEVICE->block_size] ; packet size
call es:[esi+USB_CONTROLLER->callback_bulk]
cmp eax,-1
jle short usb_rxtx_sector_bbb_done
mov usb_rxtx_bbb_buf,eax ; save the count (512 or 2048)
; send the CSW packets packet
mov edi,usb_rxtx_bbb_csw
mov al,PID_IN ; direction (PID_IN or PID_OUT)
mov cx,sizeof(BBB_CSW) ; packet size
call es:[esi+USB_CONTROLLER->callback_bulk]
cmp eax,-1
jle short usb_rxtx_sector_bbb_done
; make sure the tag is the same
mov ecx,fs:[edi+BBB_CSW->tag]
cmp ecx,usb_rxtx_bbb_tag
jne short usb_rxtx_sector_bbb_done
mov eax,usb_rxtx_bbb_buf ; restore the count from above
usb_rxtx_sector_bbb_done:
mov sp,bp
pop bp
ret
usb_rxtx_sector_bbb endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; read a sector from the drive (Using CBI)
; on entry:
; eax = lba to read/write
; cl = PID_IN or PID_OUT
; es:esi-> = this USB_CONTROLLER structure
; fs:ebx -> USB_DEVICE
; fs:edi-> = buffer to read/write
; on return
; eax = bytes read (512 for a 'floppy' or 'hard drive', 2048 for a cdrom)
; = negative value if error
; destroys none
usb_rxtx_sector_cbi proc near uses ebx ecx edx esi edi
push bp
mov bp,sp
sub sp,10
usb_rxtx_cbi_lba equ [bp-4] ; dword
usb_rxtx_cbi_buf equ [bp-8] ; dword
usb_rxtx_cbi_dir equ [bp-9] ; byte
; save the addresses to our buffers
mov usb_rxtx_cbi_dir,cl
mov usb_rxtx_cbi_lba,eax
mov usb_rxtx_cbi_buf,edi
lea edi,[ebx+USB_DEVICE->rxtx_buffer]
; determine the direction state
mov dh,0x28 ; dh = read(10) command
cmp byte usb_rxtx_cbi_dir,PID_IN
je short @f
mov dh,0x2A ; dh = write(10) command
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; build a command block
; (must be at [eax+USB_DEVICE->rxtx_buffer])
@@: mov ax,12
call memset32
mov fs:[edi+0],dh
;mov byte fs:[edi+1],0
mov eax,usb_rxtx_cbi_lba
bswap eax
mov fs:[edi+2],eax
;mov byte fs:[edi+6],0
mov byte fs:[edi+7],0 ; count high-byte
mov byte fs:[edi+8],1 ; count low-byte
;mov byte fs:[edi+9],0
;mov byte fs:[edi+10],0
;mov byte fs:[edi+11],0
; send the CBW packet
mov edi,offset request_cbi_cmd_str
mov cx,12
xor dx,dx
mov al,PID_OUT
call es:[esi+USB_CONTROLLER->callback_control]
cmp eax,-1
jle short usb_rxtx_cbi_done
; send the IN/OUT packets packet
mov edi,usb_rxtx_cbi_buf
mov al,usb_rxtx_cbi_dir ; direction (PID_IN or PID_OUT)
mov cx,fs:[ebx+USB_DEVICE->block_size] ; packet size
call es:[esi+USB_CONTROLLER->callback_bulk]
usb_rxtx_cbi_done:
mov sp,bp
pop bp
ret
usb_rxtx_sector_cbi endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; mount the device
; on entry:
; dx = zero based port number (0 -> (USB_DEVICE_MAX-1)) (is device number - 1)
; fs:ebx -> USB_DEVICE
; es:esi-> = this USB_CONTROLLER structure
; on return
; al = 1 if successful
; destroys none
usb_mount_device proc near uses ebx ecx edx esi edi ds
push bp
mov bp,sp
sub sp,4
mt_tx_buffer equ [bp-4]
lea eax,[ebx+USB_DEVICE->rxtx_buffer]
mov mt_tx_buffer,eax
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; try the inquiry command
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; try to get the drive capacity
; QEMU requires you do this twice, for the first time
; is for QEMU's sake to get the capacity.......weird
;.ifdef BX_QEMU
call usb_drive_capacity
;.endif
call usb_drive_capacity
cmp eax,8 ; we are expecting 8 bytes
jl usb_mount_error
; 8-byte return has last LBA, size of sector
mov edi,mt_tx_buffer
mov eax,fs:[edi+0]
bswap eax
inc eax
mov fs:[ebx+USB_DEVICE->sectors+0],eax
mov dword fs:[ebx+USB_DEVICE->sectors+4],0
mov eax,fs:[edi+4]
bswap eax
mov fs:[ebx+USB_DEVICE->block_size],ax
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; try to read a sector from the device
xor eax,eax
mov cl,PID_IN
mov edi,mt_tx_buffer
call usb_rxtx_sector
cmp eax,-1
jle usb_mount_error
; does count of bytes read = fs:[ebx+USB_DEVICE->block_size]
movzx ecx,word fs:[ebx+USB_DEVICE->block_size]
cmp eax,ecx
jne usb_mount_error
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; determine if the loaded sector is a MBR, or has a BPB, etc,
; and try to indicate it a HD, Floppy, or CDROM
; 1) check the count of sectors, if it is 2880, we are a floppy
; 2) check the sector size, if it is 2048, we are a cdrom
; 3) see if the first sector is a BPB, if not HD
; if it is a BPB, check some of the items to determine HD or Floppy
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; is it a CD-ROM
cmp word fs:[ebx+USB_DEVICE->block_size],2048
jne short usb_mount_test_floppy
call usb_mount_hdd_cdrom
jmp short usb_mount_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; is it a floppy
; check to see if the sectors = 2880
; * we assume it is a floppy if it has 2880 sectors *
usb_mount_test_floppy:
cmp word fs:[ebx+USB_DEVICE->sectors+4],0
ja short @f
cmp word fs:[ebx+USB_DEVICE->sectors+0],2880
jne short @f
; we need to update the CHS values from the LBA value
mov byte fs:[ebx+USB_DEVICE->org_media],USB_MSD_MEDIA_FLOPPY
mov byte fs:[ebx+USB_DEVICE->media],USB_MSD_MEDIA_FLOPPY
mov byte fs:[ebx+USB_DEVICE->boot_dl],0
mov dword fs:[ebx+USB_DEVICE->base_lba],0
; convert from LBAs to CHS
mov cl,18 ; sectors per track
mov al,2 ; heads
call convert_lba_cylinders
call usb_add_boot_vector
call usb_mount_display
jmp short usb_mount_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; is it a hard drive?
; we have read the first sector. (at mt_tx_buffer)
; determine if it is a MBR, BPB, etc.
; if the BPB states a floppy emulation, we set the LBAs to 2880
; if the BPB states a hard drive, or is MBR, continue on
; A lot of OSes, including freedos, *assume* that if we boot the floppy, dl will be 0
; (freedos' bootsector doesn't even save dl)
; therefore, we set dl to zero for floppys, 0x80 for harddrives, and 0xE0 for cdroms
@@: ; we need to update the CHS values from the LBA value
mov byte fs:[ebx+USB_DEVICE->org_media],USB_MSD_MEDIA_HARDDRIVE
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; check to see if it is a floppy disk image with a BPB
mov edi,mt_tx_buffer
call usb_mount_hdd_floppy
or al,al
jnz short usb_mount_done
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; else emulate a hard drive
mov byte fs:[ebx+USB_DEVICE->media],USB_MSD_MEDIA_HARDDRIVE
mov byte fs:[ebx+USB_DEVICE->boot_dl],0x80
mov dword fs:[ebx+USB_DEVICE->base_lba],0
; convert from LBAs to CHS
mov cl,63
mov al,16
call convert_lba_cylinders ; ax = 41 (msdos), 656 (win95), 3641 (winxp)
call usb_add_boot_vector
call usb_mount_display
; we need to increment the count of hard drives in the BDA
push ds
xor cx,cx
mov ds,cx
inc byte [0x0475]
pop ds
usb_mount_done:
; successful mount
mov al,1
mov sp,bp
pop bp
ret
usb_mount_error:
xor al,al
mov sp,bp
pop bp
ret
usb_mount_device endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
; display the type of device we found
; on entry:
; fs:ebx -> USB_DEVICE
; on return
; nothing
; destroys none
usb_mount_display proc near uses alld ds
; is it an actual floppy disk
cmp byte fs:[ebx+USB_DEVICE->org_media],USB_MSD_MEDIA_FLOPPY
jne short usb_mount_display_0
push word fs:[ebx+USB_DEVICE->bios_addr]
push word fs:[ebx+USB_DEVICE->sectors]
mov bx,4
mov si,offset usb_mount_floppy_str
jmp usb_mount_disp_do
usb_mount_display_0:
; else see if the actual media is a hard drive
cmp byte fs:[ebx+USB_DEVICE->org_media],USB_MSD_MEDIA_HARDDRIVE
jne short usb_mount_display_1
; now see if we are emulating a floppy
cmp byte fs:[ebx+USB_DEVICE->media],USB_MSD_MEDIA_FLOPPY
jne short @f
; is an emulated floppy type
push word fs:[ebx+USB_DEVICE->bios_addr]
push word fs:[ebx+USB_DEVICE->sectors]
push dword fs:[ebx+USB_DEVICE->base_lba]
mov bx,8
mov si,offset usb_mount_hdd_flpy_str
jmp short usb_mount_disp_do
@@: ; is an actual hard drive type
push word fs:[ebx+USB_DEVICE->bios_addr]
push dword fs:[ebx+USB_DEVICE->sectors+0]
mov bx,6
mov si,offset usb_mount_harddisk_str
jmp short usb_mount_disp_do
usb_mount_display_1:
cmp byte fs:[ebx+USB_DEVICE->org_media],USB_MSD_MEDIA_CDROM
jne short usb_mount_display_2
; now see if we are emulating a floppy
cmp byte fs:[ebx+USB_DEVICE->media],USB_MSD_MEDIA_FLOPPY
jne short @f
; is an emulated floppy type
push word fs:[ebx+USB_DEVICE->bios_addr]
push word fs:[ebx+USB_DEVICE->sectors]
push dword fs:[ebx+USB_DEVICE->base_lba]
mov bx,8
mov si,offset usb_mount_cd_flpy_str
jmp short usb_mount_disp_do
@@: ; now see if we are emulating a hard drive
cmp byte fs:[ebx+USB_DEVICE->media],USB_MSD_MEDIA_HARDDRIVE
jne short @f
; is an emulated hard drive type
push word fs:[ebx+USB_DEVICE->bios_addr]
push dword fs:[ebx+USB_DEVICE->sectors+0]
push dword fs:[ebx+USB_DEVICE->base_lba]
mov bx,10
mov si,offset usb_mount_cd_hdd_str
jmp short usb_mount_disp_do
@@: ; else is an actual cdrom
push word fs:[ebx+USB_DEVICE->bios_addr]
push dword fs:[ebx+USB_DEVICE->sectors+0]
mov bx,6
mov si,offset usb_mount_cdrom_str
usb_mount_disp_do:
mov ax,BIOS_BASE2
mov ds,ax
call bios_printf
add sp,bx
usb_mount_display_2:
ret
usb_mount_display endp
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-