summaryrefslogtreecommitdiffstats
path: root/start.asm
blob: 24fa13fb187f2955e3e7b27b9e3cbd6e8a4ee63a (plain)
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
/* -*- asm -*- ----------------------------------------------------------- *
 *   
 *   Copyright 2013 H. Peter Anvin - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *   
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *   
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

#include "v9fs.h"

	.code16

/* ------------------------------------------------------------------------- *
 *  Move all the code/data to the right place and clear the bss sections
 * ------------------------------------------------------------------------- */

	.section ".textearly","ax"
	/* _start must be position-independent */
GPROC(_start)
	push %es		/* Save PSP segment */
	movw %cs,%ax
	movw %ax,%ds
	movw %ax,%es
  	std
	xorl %eax,%eax
	movw $__heap_start-4,%di
	movw $__bss_start-4,%cx
	subw %di,%cx
	negw %cx
	shrw $2,%cx
	rep stosl

	movw $__move_dwords,%cx
	movw $_edata_premove-4,%si
	rep movsl

	/* Move to the real .text segment before clearing .rbss */
	jmp start
END(_start)

	.section ".text","ax"
start:
	cld
	movw $__rbss_start,%di
	movw $__rbuf_end+3,%cx
	subw %di,%cx
	shrw $2,%cx
	rep stosl

	gibword pspseg		/* Needed for the final Save Program call */
	gibuf psp 256 16

	popw %fs		/* PSP segment */
	movw %fs,pspseg
	xorw %si,%si
	movw $psp,%di
	movw $(256/4),%cx
	fs rep movsl
	
/* ------------------------------------------------------------------------- *
 *  Check DOS specifics
 * ------------------------------------------------------------------------- */
	gibword dos_version

check_dos_ok:	
	/* Check DOS version */
	movw $0x3000,%ax
	int $0x21
	xchgb %al,%ah
	movw %ax,dos_version
	cmpw $0x0320,%ax
	jae ok_version

	printerr "Invalid DOS version!\r\n"
	cmpb $0x02,%ah
	jb 1f
	movw $0x4c0a,%ax
	int $0x21
1:
	int $0x20

ok_version:
	/* Check that we can install a redirector */
	movw $0x1100,%ax	/* Redirector interface check */
	int $0x2f
	cmpb $1,%al
	jne parse_cmdline	/* All good */

	printerr "DOS redirector interface not available\r\n"
	movw $0x4c09,%ax
	int $0x21

/* ------------------------------------------------------------------------- *
 *  Parse the command line and get the drive name and mount tag
 * ------------------------------------------------------------------------- */

#define MOUNT_TAG_MAX 32	/* By virtio protocol */

	grbbyte drive_number	/* Our own drive number (0-25) */
	grbbyte drive_letter	/* Our own drive letter (A-Z) */

	/* Mount tag in UTF-8 + length - we can spill over with a few bytes */
	gibuf mount_tag, MOUNT_TAG_MAX+4+2

	/* Mount tag in codepage, up to 11 bytes - volume label */
	.pushsection ".rdata","aw"
	.globl volume_label
volume_label:
	.ascii "           "	/* 11 space characters */
	.type volume_label,@object
	.size volume_label,.-volume_label
	.popsection

parse_cmdline:
	call utf_init_plain
	
	movw $psp+0x81,%si	/* First character of command line */
	movzbw -1(%si),%cx	/* Count of characters in command line */
	addw %si,%cx		/* First character beyond end of command */

1:
	cmpw %si,%cx
	je usage_error
	lodsb
	cmpb $0x20,%al		/* Skip spaces */
	jbe 1b

	andb $~0x20,%al		/* Must be A..Z or a..z -> upper case */
	movb %al,drive_letter
	subb $0x41,%al
	cmp $26,%al
	jae usage_error
	movb %al,drive_number

	cmpw %si,%cx
	je usage_error
	lodsb
	cmpb $':',%al		/* Must be a colon */
	jne usage_error

	cmpw %si,%cx
	je usage_error
	lodsb
	cmpb $0x20,%al
	ja usage_error		

2:
	cmpw %si,%cx
	je usage_error
	movw %si,%dx
	lodsb
	cmpb $0x20,%al
	jbe 2b			/* Skip spaces */

	movw $mount_tag+2,%di	/* 2 bytes reserved for length */
	movw $volume_label,%bx

3:
	cmpw $volume_label+11,%bx
	jae 5f			/* The volume label is simply truncated */
	movb %al,(%bx)		/* Save to volume label */
	incw %bx
5:
	call cptoutf
	cmpw $mount_tag+2+MOUNT_TAG_MAX,%di
	jae tag_too_long
	cmpw %si,%cx
	je 4f
	lodsb
	cmpb $0x20,%al
	ja 3b

4:
	subw $mount_tag+2,%di
	movw %di,mount_tag	/* 2 first bytes for length */

	jmp get_dos_bits
	
usage_error:
	printmsg "Usage: v9fs drive: mount_tag\r\n"
	movw $0x4c7e,%ax
	int $0x21

tag_too_long:
	printerr "Mount tag too long\r\n"
	movw $0x4c08,%ax
	int $0x21
	
/* ------------------------------------------------------------------------- *
 *  Query DOS internal data structures and make sure the selected
 *  drive is available
 * ------------------------------------------------------------------------- */
	grblong dos_sda		/* Pointer to Swappable Data Area */
	grblong dos_sdafn	/* Pointer to first filename in SDA */
	grblong	dos_sdafcb	/* Pointer to the FCB entries in SDA */
	grblong dos_cdsptr	/* Pointer to the CDS pointer in SDA */
	liblong dos_lol		/* Pointer to List of Lists */
	liblong dos_cds		/* Pointer to Current Directory Structures */
	liblong our_cds		/* Pointer to our own CDS */
	libword bytes_per_cds	/* Bytes per CDS entry */

get_dos_bits:
	call utf_init_case
	
	pushw %es
	pushw %ds

	movw $0x5d06,%ax	/* Get SDA pointer */
	cmpb $4,dos_version+1	/* DOS 4.x? */
	pushfw
	jne 1f
	movb $0x0e,%al		/* DOS 4.x only: function 0x5d0e not 0x5d06 */
1:
	int $0x21
	movw %si,%cs:dos_sda
	movw %ds,%cs:dos_sda+2
	addw $0x92,%si		/* DOS 3.x: searchdata @ 0x92 */
	movw $(0x218-0x92),%ax	/* Further offset for the FCB entries */
	movw $(0x26c-0x218),%cx	/* Further offset for the CDS pointer */
	popfw			/* DOS 4+? */
	jb 2f
	addw $(0x9e-0x92),%si
	movw $(0x22b-0x9e),%ax
	movw $(0x282-0x22b),%cx
2:
	movw %si,%cs:dos_sdafn
	movw %ds,%cs:dos_sdafn+2

	addw %ax,%si
	movw %si,%cs:dos_sdafcb
	movw %ds,%cs:dos_sdafcb+2

	addw %cx,%si
	movw %si,%cs:dos_cdsptr
	movw %ds,%cs:dos_cdsptr+2
	
	popw %ds

	movw $0x5200,%ax
	int $0x21		/* Get list of lists pointer */
	
	movw %bx,dos_lol
	movw %es,dos_lol+2

	movb %es:0x21(%bx),%al	/* Number of available drives */
	lesw %es:0x16(%bx),%bx	/* Current Directory Structures */
	movw %bx,dos_cds
	movw %es,dos_cds+2

	movw $81,%dx		/* DOS 3: 81 byte CDS; DOS 4+: 88 byte CDS */
	cmpb $3,dos_version+1
	jna 3f
	movw $88,%dx
3:
	movw %dx,bytes_per_cds
	movzbw drive_number,%cx
	imulw %cx,%dx
	addw %dx,%bx
	movw %bx,our_cds
	movw %es,our_cds+2

	movw %es:CDS_FLAGS(%bx),%dx	/* Flags for the selected CDS */
	
	popw %es

	cmpb %al,%cl
	jae invalid_drive

	testb $0xc0,%bh		/* Check to see if drive is in use */
	jz ok_drive

	printerr "Drive letter already in use"
	jmp 4f
	
invalid_drive:
	printerr "Specified drive is above LASTDRIVE"
4:
	movw $0x4c07,%ax
	int $0x21

ok_drive:

/* ------------------------------------------------------------------------- *
 *  Find virtio device
 * ------------------------------------------------------------------------- */

#define V9FS_VID 0x1af4		/* Vendor ID to look for */
#define V9FS_DID 0x1009		/* Device ID to look for */
	
	libword device_index
find_device:
	/* PCI BIOS installation check */
	movw $0xb101,%ax
	xorl %edi,%edi
	xorl %edx,%edx
	stc
	int $0x1A
	jc no_pci
	cmpl $0x20494350,%edx
	jne no_pci
	andb %ah,%ah
	jne no_pci

	/* Search for an appropriate virtio device */
1:
	movw device_index,%si
	incw device_index
	movw $0xb102,%ax	/* Find PCI device */
	movw $V9FS_DID,%cx
	movw $V9FS_VID,%dx
	stc
	int $0x1A
	jc none_found
	andb %ah,%ah
	jne none_found

	/* Found a device candidate, bus:dev:fn is in %bx */

	/* XXX: This assumes that the I/O BAR is the first BAR */
	movw $0xb10a,%ax	/* Read configuration dword */
	movw $0x10,%di		/* Register number */
	stc
	int $0x1A
	jc 1b
	xorl $1,%ecx
	testl $0xffff0003,%ecx
	jnz 1b
	movw %cx,%dx
	movw %cx,%bx

	/* Check to see if this device carries our mount tag */
	/* XXX: if MSI is somehow enabled we need to +4 to the offset */
	
	addw $20,%dx
	inw (%dx),%ax
	cmpw mount_tag,%ax
	jne 1b
	addw $2,%dx
	movw %ax,%cx
	movw $mount_tag+2,%si
2:
	inb (%dx),%al
	incw %dx
	cmpb (%si),%al
	jne 1b
	incw %si
	loopw 2b

	/* Found it! */
	jmp init_device

no_pci:
	printerr "No PCI BIOS detected!\r\n"
	jmp 99f

none_found:
	printerr "No virtio-9p-pci device found!\r\n"
99:
	movw $0x4c07,%ax
	int $0x21
	
/* ------------------------------------------------------------------------- *
 *  Initialize the device and compute buffer addresses
 * ------------------------------------------------------------------------- */

#define STACK_MIN	1024
	
	grbword vring_kicker	/* I/O base for the kicker register */
	grbword int2f_sp	/* Local stack pointer */
	grbword vring_size	/* Size of the ring */
	grbword vring_mask	/* Mask of the ring */
	grbword vring_avail	/* virtio ring available */
	grbword vring_used	/* virtio ring used */
	grbword msgbuf		/* Message buffer address */
	grbword msgbuf7		/* Message buffer address + 7 */
	grbword msgbuf_size	/* Message buffer size */
	grblong msgbuf_paddr	/* Physical address of msgbuf */
	gibword rbuf_end	/* End of the memory we need from the rbuf */
	giblong vring_linaddr	/* Linear address of vring */
	giblong rbuf_linaddr	/* Linear address of rbuf section */
	libword save_paras	/* Number of paragraphs we need to TSR */
	
	grbuf vring_dds 16 4	/* DMA descriptor structure for the vring */
	
vring_bytes	= vring_dds	/* Size of the virtio ring + msgbuf */
	.globl vring_desc
vring_desc	= vring_dds+4	/* virtio ring descriptors */
vring_seg	= vring_dds+8	/* == our segment */
vring_paddr	= vring_dds+12	/* Physical address */
	
	/* Careful: %bx -> device I/O base across this entire code block */
	
init_device:
	/* %bx -> device I/O base */
	xorw %ax,%ax

	leaw 16(%bx),%cx
	movw %cx,vring_kicker

	leaw 18(%bx),%dx
	outb %al,(%dx)		/* Reset device */

	leaw 14(%bx),%dx	/* Queue select */
	outw %ax,(%dx)
	
	leaw 12(%bx),%dx
	inw (%dx),%ax		/* Get ring size */
	movw %ax,vring_size
	decw %ax
	movw %ax,vring_mask
	/* XXX: check for vring_size of 32, 64 or 128 */

compute_buffers:
	xorl %eax,%eax
	cltd			/* %edx <- 0 */
	movw %ds,%ax
	shll $4,%eax
	movw $_rbuf,%dx
	addl %edx,%eax
	movl %eax,rbuf_linaddr
	movl %eax,%ecx

	/* The vring needs to start on a 4K aligned address */
	negw %ax
	andw $0xfff,%ax
	/* %ax now contains the offset from the start of rbuf to the vring */
	cwtl			/* %eax <- %ax */
	addl %eax,%ecx
	addw %ax,%dx
	movl %ecx,vring_paddr	/* Physical address unless proven otherwise */
	movw %dx,vring_desc
	pushw %dx
	movw vring_size,%ax
	shlw $4,%ax		/* 16 bytes/descriptor entry */
	addw %ax,%dx
	movw %dx,vring_avail
	shrw $3,%ax		/* 2 bytes/avail entry */
	addw $4,%dx		/* 4 bytes = avail header */
	addw %ax,%dx
	movw %dx,msgbuf		/* Message buffer between avail and used */
	pushw %dx
	addw $7,%dx
	movw %dx,msgbuf7	/* Message buffer + 7 -> after common header */
	popw %dx
	popw %ax		/* %ax <- vring_desc */
	addw $4096,%ax
	movw %ax,vring_used
	pushw %ax
	subw %dx,%ax
	movw %ax,msgbuf_size

	/*
	 * Finally, compute the address of the int2f stack.  Depending
	 * on the alignment of rbuf, there will be space either before
	 * or after the vring.  If the space before is sufficient
	 * based on STACK_MIN defined above, use it; otherwise allocate
	 * STACK_MIN bytes beyond the end of the used ring.  The highest
	 * such address 
	 */
	popw %dx		/* %dx <- vring_used */
	movw vring_size,%ax
	shlw $3,%ax		/* 8 bytes/used entry */
	addw $4,%ax		/* 4 bytes = used header */
	addw %ax,%dx		/* %dx = end of vring_used */
	movw %dx,%ax		/* %dx = end of vring_used */
	movw vring_desc,%cx
	subw %cx,%ax
	movw %ax,vring_bytes	/* vring_dds.region_size */
	movw %cx,int2f_sp
	cmpw $(_rbuf+STACK_MIN),%cx
	jae 1f			/* Enough space, we are done */

	/* Insufficient space before, must go after */
	addw $STACK_MIN,%dx
	movw %dx,int2f_sp
	
1:
	movw %dx,rbuf_end	/* This is how much memory we need to TSR */
	addw $15,%dx
	shrw $4,%dx
	movw %ds,%ax
	addw %ax,%dx
	subw pspseg,%dx
	movw %dx,save_paras	/* Number of paras we will ask DOS for */
	printerr "We will need "
	movzwl %dx,%eax
	shll $4,%eax
	pushl %eax
	call printnum
	printmsg " bytes of memory\r\n"

/* ------------------------------------------------------------------------- *
 *  Set up the next fields in all the descriptor entries
 * ------------------------------------------------------------------------- */

	movw vring_size,%cx
	movw vring_desc,%si	/* Next field in the first descriptor */
	xorw %ax,%ax
1:
	incw %ax
	andw vring_mask,%ax
	movw %ax,14(%si)
	addw $16,%si
	loopw 1b
	
/* ------------------------------------------------------------------------- *
 *  Detect VDS if present, and map the vring, then tell the host
 * ------------------------------------------------------------------------- */

	grbbyte have_vds
	
configure_vring:
	movw %ds,vring_seg	/* Only field not yet set up */

	printerr "vring at "
	pushw %ds
	call printhex4
	printmsg ":"
	pushw vring_desc
	call printhex4

	smsw %ax		/* If we are not in protected mode = no VDS */
	shrw $1,%ax
	jnc vring_mapped
	
	pushw %es
	movw $0x354b,%ax	/* Read int $0x4b vector */
	int $0x21
	movw %es,%ax
	orw %bx,%ax
	popw %es
	jz vring_mapped		/* Interrupt vector is 0:0, do not call */

	movw $0x8102,%ax	/* VDS presence detect */
	xorw %dx,%dx
	stc
	int $0x4b
	jc vring_mapped		/* VDS presence detect failed */
	cmpb $0x81,%ah
	je vring_mapped		/* Something cleared CF but still failed */

	movb $-1,have_vds	/* Note: usable as a bitmask */
	
	printmsg " VDS"
	
	movw $0x8103,%ax	/* Lock for DMA */
	movw $0x0004,%dx	/* Do not use bounce buffer, remap is OK */
	movw $vring_dds,%di
	stc
	int $0x4b
	jnc vring_mapped
	
vds_error:
	call crlf
	printerr "Failed to map virtio ring for DMA - error "
	pushw %ax
	call printhex4
	call crlf
	movw $0x4c06,%ax
	int $0x21
	
vring_mapped:
	movl vring_paddr,%eax
	printmsg " physical "
	pushl %eax
	call printhex8
	call crlf

	movzwl msgbuf,%edx
	subw vring_desc,%dx
	addl %edx,%eax
	movl %eax,msgbuf_paddr	/* Save the physical address of msgbuf */

	movw vring_kicker,%dx
	shrl $12,%eax
	addw $(8-16),%dx
	outl %eax,(%dx)		/* Configure ring base address */

	movb $0x07,%al
	addw $(18-8),%dx
	outb %al,(%dx)		/* Device enabled */

/* ------------------------------------------------------------------------- *
 *  Send the version negotiation message
 * ------------------------------------------------------------------------- */
send_version:
	movw msgbuf7,%di

	/* The absolutely longest packet = full msgbuf + 64K-1 bytes data */
	movzwl msgbuf_size,%eax
	addl $0xffff,%eax
	stosl

	.pushsection ".rodata","a"
101:
	.word 103f-102f
102:
	.ascii "9P2000.L"
103:
	.popsection
	
	movw $101b,%si
	movw $(103b-101b),%cx
	rep movsb

	movb $P9_TVERSION,%al
	call simple_message
	jc mount_fail
	
/* ------------------------------------------------------------------------- *
 *  Send the attach message
 * ------------------------------------------------------------------------- */
send_attach:
	movw msgbuf7,%di

	.pushsection ".rodata","a"
101:
	.long FID_ROOT
	.long -1		/* No authentication so no afid */
	.word 103f-102f
102:
	.ascii "root"		/* User name "root" */
103:
	.popsection
	
	movw $101b,%si
	movw $(103b-101b),%cx
	rep movsb

	movw $mount_tag,%si
	movw (%si),%cx		/* Bytes in actual string */
	movsw			/* Copy string length without changing %cx */
	rep movsb		/* Copy the proper string */

	movw $-1,%ax		/* n_uname = -1 = not applicable */
	stosw			/* Smaller than two size overides... */
	stosw

	movb $P9_TATTACH,%al
	call simple_message
	jc mount_fail

/* ------------------------------------------------------------------------- *
 *  Actually install the filesystem driver
 * ------------------------------------------------------------------------- */
#ifdef DEBUG
	debugmsg "Our CDS = "
	pushw our_cds+2
	call debug_printhex4
	debugmsg ":"
	pushw our_cds
	call debug_printhex4
	call debug_crlf
#endif

	/* NOTE: we stop maintaining %es == %ds here */
	
	movw $0x352f,%ax	/* Get interrupt vector 0x2f */
	int $0x21

	movw %bx,int2f_chain
	movw %es,int2f_chain+2

	lesw our_cds,%bx
	movw %es:CDS_FLAGS(%bx),%ax
	andw $~0xf080,%ax		/* Clear undesired flags */
	orb $0xc0,%ah			/* Network drive, physical, real */
	movw %ax,%es:CDS_FLAGS(%bx)

	movl $0x005c3a00,%eax
	movb drive_letter,%al	/* Now %eax = "X:\" + NUL for the proper X */
	movl %eax,%es:(%bx)	/* Set current directory */

	movw $CDS_MAGIC,%es:CDS_USERVAL(%bx)	/* Available field, set to magic */
	movw $2,%es:CDS_ROOTOFFSET(%bx)	/* Path starts after X: */

/* ------------------------------------------------------------------------- *
 *  Finish up and terminate
 * ------------------------------------------------------------------------- */

	/* Free our environment block */
	movw psp+0x2c,%es	/* Segment address of environment block */
	movb $0x49,%ah		/* Free memory */
	int $0x21

	/* Link us into the int $0x2f chain */
	movw $int2f,%dx		/* %ds == %cs already; %ds:%dx -> vector */
	movw $0x252f,%ax	/* Set interrupt vector 0x2f */
	int $0x21

	/* Terminate and stay resident */
	movw save_paras,%dx
	movw $0x3100,%ax
	int $0x21

	/* This should never return... */
	
/* ------------------------------------------------------------------------- *
 *  We go here if we have to bail after we have already started the ring
 * ------------------------------------------------------------------------- */
mount_fail:
	printerr "Mount failed\r\n"
	
shutdown:
	/* Shut down device */
	movw vring_kicker,%dx
	xorb %al,%al
	addw $(18-16),%dx
	outb %al,(%dx)		/* Reset device */

	/* Unlock DMA ring */
	cmpb $0,have_vds
	je 1f
	
	movw $0x8104,%ax
	xorw %dx,%dx
	movw $vring_dds,%di
	int $0x4b
1:
	
	/* Exit to DOS */
	movw $0x4c01,%ax
	int $0x21

/* ------------------------------------------------------------------------- *
 *  Buffer segment definition
 * ------------------------------------------------------------------------- */

	/*
	 * This is a tight bound as long as STACK_MIN <= 4080
	 *
	 * 4080 bytes (4K - 1 paragraph) for alignment slack and stack
	 * 4096 bytes for the descriptors, avail ring, and msgbuf
	 * 4 + 128*8 bytes for the used ring
	 */
	.section ".rbuf","aw",@nobits
	.globl _rbuf
_rbuf:
	.space (4096-16) + 4096 + (4 + 128*8)
	.size _rbuf,.-_rbuf
	.type _rbuf,@object

	lidbyte dummy,123	/* Don't be empty */