summaryrefslogtreecommitdiffstats
path: root/diskspc.asm
blob: ef89908c750cdae6fb15d3860192caea38666dc2 (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
/* -*- 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
	.section ".rtext","ax"

/* ------------------------------------------------------------------------- *
 *  fxn_diskspace
 *
 *  Get free disk space in a format DOS can deal with.  Since each path
 *  may have different values, return the value for the current directory.
 * ------------------------------------------------------------------------- */

GPROC(fxn_diskspace)
	movw F_ES,%fs
	movw F_DI,%si
	addw %fs:CDS_ROOTOFFSET(%si),%si
	movl $FID_DISKSPACE,%eax
	pushl %eax
	call path_walk_asciiz

	movw msgbuf7,%di
	popl %eax
	stosl
	movb $P9_TSTATFS,%al
	call simple_message
	jc 12f

	/*
	 * In the response:
	 * dword @  4(%di) = block size
	 * qword @  8(%di) = total blocks
	 * qword @ 24(%di) = free blocks for non-superuser
	 */
#ifdef DEBUG
	debugmsg "diskspace: "
	pushl 4(%di)
	call debug_printhex8
	debugmsg " "
	pushl 12(%di)
	call debug_printhex8
	pushl 8(%di)
	call debug_printhex8
	debugmsg " "
	pushl 28(%di)
	call debug_printhex8
	pushl 24(%di)
	call debug_printhex8
	call debug_crlf
#endif
	movl 12(%di),%eax
	mull 4(%di)
	andl %edx,%edx
	jne 10f			/* > 2^64 bytes! */
	movl %eax,%ebx
	movl 8(%di),%eax
	mull 4(%di)
	addl %ebx,%edx
	jc 10f

	/*
	 * %edx:%eax now contains a byte count; we can represent it in
	 * a meaningful way iff it is < 2^38...
	 */
#ifdef DEBUG
	debugmsg "bytes: "
	pushl %edx
	call debug_printhex8
	pushl %eax
	call debug_printhex8
	call debug_crlf
#endif
	bsrl %edx,%ecx
	jnz 2f
	bsrl %eax,%ecx
	jmp 3f
2:
	addb $32,%cl
3:
	cmpb $37,%cl
	ja 10f
	subb $15,%cl
	jnb 31f
	xorb %cl,%cl
31:
	shrdl %cl,%edx,%eax

#ifdef DEBUG
	debugmsg "shift: "
	pushw %cx
	call debug_printhex2
	debugmsg "\r\nclusters: "
	pushw %ax
	call debug_printhex4
	call debug_crlf
#endif
	
4:
	movw %ax,F_BX		/* Total clusters */

	movl 28(%di),%eax
	mull 4(%di)
	andl %edx,%edx
	jne 11f
	movl %eax,%ebx
	movl 24(%di),%eax
	mull 4(%di)
	addl %ebx,%edx
	jc 11f

	shrdl %cl,%edx,%eax
	shrl %cl,%edx

	andl %edx,%edx
	jnz 11f
	cmpl $0x10000,%eax
	jbe 5f
11:
	debugmsg "Free space overflow\r\n"
	movw $0xffff,%ax

5:
	movw %ax,F_DX		/* Available clusters */

	/* Now, we need to turn %cl into a count */
#ifdef DEBUG
	debugmsg "Shift: "
	pushw %cx
	call debug_printhex2
	call debug_crlf
#endif
	movw $1,%ax
	movb $15,%ch
	pushw %ax
	cmpb %ch,%cl
	jbe 6f
	subb %ch,%cl
	shlw %cl,%ax
	movb %ch,%cl
6:
	movb %al,F_AL		/* Sectors/cluster */
	popw %ax		/* %ax <- 1 */
	shlw %cl,%ax
	movw %ax,F_CX		/* Bytes/sector */

7:
	movl $FID_DISKSPACE,%eax
	call clunk
	
	xorw %ax,%ax
	retw

10:	/* Total size overflow */
	debugmsg "Size overflow\r\n"
	movb $(37-15),%cl
	movw $0xffff,%ax
	jmp 4b

12:	/* Error, but DOS doesn't like this.  Return zero space. */
	xorw %ax,%ax
	movw $512,F_CX
	movb $64,F_AL
	movw %ax,F_BX
	movw %ax,F_DX
	jmp 7b
END(fxn_diskspace)