-
Notifications
You must be signed in to change notification settings - Fork 1
/
mbrtst.asm
358 lines (276 loc) · 7.58 KB
/
mbrtst.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; MBR tester by Alexey Frunze (c) 2023 ;;
;; 2-clause BSD license. ;;
;; ;;
;; ;;
;; How to Compile: ;;
;; ~~~~~~~~~~~~~~~ ;;
;; nasm mbrtst.asm -f bin -o mbrtst.bin ;;
;; ;;
;; ;;
;; Features: ;;
;; ~~~~~~~~~ ;;
;; - can replace the code of standard FAT12/16 and FAT32 VBRs and be ;;
;; booted by a generic MBR (not just MBiRa) to show what information ;;
;; is passed from the MBR to the VBR ;;
;; ;;
;; - prints BIOS geometry/params (CHS dimensions) for the boot drive ;;
;; and the product of Cylinders, Heads and Sectors, which gives the disk ;;
;; size in sectors (capped by some 16 million or 8GB) ;;
;; ;;
;; - prints start values of important registers: dx, si, bp, sp, seg regs ;;
;; ;;
;; - prints partition entry's File System ID/type, start CHS & LBA, size, ;;
;; active flag / boot drive (again) ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
BITS 16
CPU 8086
ORG 7C00h
; Fill FAT12/16 and FAT32 BPB areas with NOPs
; (3Eh and 5Ah bytes respectively), so it won't matter at
; which point this code will start execution, 3Eh or 5Ah.
times (5Ah-($-$$)) nop
; prepare to be popped and printed one by one
push sp
push ss
push es
push ds
push cs
push bp
push si
push dx
; save ds:si away to es:di
push ds
pop es
mov di, si
jmp 0:main
main:
cld
; cs=ds=0
push cs
pop ds
; Get disk geometry/drive params according to BIOS
mov ah, 8
int 13h
jc .0
call printl
db "Geo", 0
mov bp, 1
call printCHS
call printl
db "=", 0
call printdec32
call printl
db " sect", 13, 10, 10, 0
.0:
; Partition entry: active/drive;
; Start reg values
mov al, [es:di]
mov ah, 0
push ax
call printl
db "drv dx si bp cs ds es ss sp", 13, 10, 0
mov cx, 9
.1:
pop ax
call printhex
mov al, ' '
call printc
loop .1
; Partition entry: File System ID/Type
call printl
db 13, 10, 10, "FS=", 0
mov al, [es:di+4]
mov ah, 0
call printdec
; Partition entry: Start CHS
mov dh, [es:di+1]
mov cx, [es:di+2]
xor bp, bp
call printCHS
; Partition entry: Start LBA
call printl
db " LBA=", 0
mov ax, [es:di+8]
mov dx, [es:di+10]
call printdec32
; Partition entry: Size, MB
call printl
db " Sz,MB=", 0
mov ax, [es:di+12]
mov dx, [es:di+14]
mov cx, 11
.2:
shr dx, 1
rcr ax, 1
loop .2
call printdec32
call printl
db 13, 10, 0
Halt:
hlt
jmp short Halt
; prints CHS packed in DH:CX as in int 13h functions 2 and 8,
; adds bp (can be 0 or 1) to head and cylinder,
; returns product in dx:ax.
printCHS:
push bx
call printl
db " CHS=", 0
; cylinder
push cx
mov al, ch
mov ah, cl
mov cl, 6
shr ah, cl
add ax, bp
pop cx
call printdec
mov bx, ax
mov al, ','
call printc
; head
mov al, dh
mov ah, 0
add ax, bp
call printdec
mov dx, ax
mov al, ','
call printc
; sector
mov ax, cx
and ax, 63
call printdec
xchg bx, dx
mul dx
mul bx
pop bx
ret
; Print char from AL
printc:
push ax
push bx
push bp
mov ah, 0Eh
mov bx, 7
int 10h
pop bp
pop bx
pop ax
ret
prints_inner:
push ax
cld
.1:
lodsb
test al, al
jz .2
call printc
jmp short .1
.2:
pop ax
ret
%if 0
; Print ASCIIZ string at SI
prints:
push si
call prints_inner
pop si
ret
%endif
; Print ASCIIZ string immediately following the call to this subroutine
printl:
pop si
call prints_inner
push si
ret
; Print AX in hexadecimal
printhex:
push ax
push cx
push dx
push si
mov dx, ax
mov si, 4
mov cx, si
.1:
rol dx, cl
mov al, dl
and al, 0Fh
add al, '0'
cmp al, '9'
jbe .2
add al, 'A' - '0' - 10
.2:
call printc
dec si
jnz .1
pop si
pop dx
pop cx
pop ax
ret
%if 0
; Print DX:AX in hexadecimal
printhex32:
xchg ax, dx
call printhex
xchg ax, dx
jmp printhex
%endif
printdec32_inner:
push ax
push cx
push dx
push si
push di
push bp
mov si, cx
mov di, 10
.1:
xchg ax, bp
xchg ax, dx
xor dx, dx
div di
xchg ax, bp
div di
xchg dx, bp
push bp
loop .1
mov cx, si
.2:
pop ax
add al, '0'
call printc
loop .2
pop bp
pop di
pop si
pop dx
pop cx
pop ax
ret
; Print AX in decimal
printdec:
push cx
push dx
xor dx, dx
mov cx, 5
call printdec32_inner
pop dx
pop cx
ret
; Print DX:AX in decimal
printdec32:
push cx
mov cx, 10
call printdec32_inner
pop cx
ret
times (1FEh-($-$$)) db 40h
;;;;;;;;;;;;;;;;;;;;
;; Boot sector ID ;;
;;;;;;;;;;;;;;;;;;;;
dw 0AA55h ; BIOS checks for this ID