summaryrefslogtreecommitdiff
path: root/sys/src/boot/pc/mbr.s
blob: fb299584e0cce8dc031635be3434ce02aca4cf09 (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
/*
 * Hard disc boot block. Loaded at 0x7C00, relocates to 0x0600:
 *	8a mbr.s; 8l -o mbr -l -H3 -T0x0600 mbr.8
 */
#include "x16.h"
#include "mem.h"

/*#define FLOPPY	1		/* test on a floppy */
#define TRACE(C)	PUSHA;\
			CLR(rBX);\
			MOVB $C, AL;\
			LBI(0x0E, rAH);\
			BIOSCALL(0x10);\
			POPA

/*
 * We keep data on the stack, indexed by BP.
 */
#define Xdap		0x00		/* disc address packet */
#define Xtable		0x10		/* partition table entry */
#define Xdrive		0x12		/* starting disc */
#define Xtotal		0x14		/* sum of allocated data above */

/*
 * Start: loaded at 0000:7C00, relocate to 0000:0600.
 * Boot drive is in rDL.
 */
TEXT _start(SB), $0
	CLI
	CLR(rAX)
	MTSR(rAX, rSS)			/* 0000 -> rSS */
	LWI((0x7C00-Xtotal), rSP)	/* 7Bxx -> rSP */
	MW(rSP, rBP)			/* set the indexed-data pointer */

	MTSR(rAX, rDS)			/* 0000 -> rDS, source segment */
	LWI(0x7C00, rSI)		/* 7C00 -> rSI, source offset */
	MTSR(rAX, rES)			/* 0000 -> rES, destination segment */
	LWI(0x600, rDI)			/* 0600 -> rDI, destination offset */
	LWI(0x100, rCX)			/* 0100 -> rCX, loop count (words) */

	CLD
	REP; MOVSL			/* MOV DS:[(E)SI] -> ES:[(E)DI] */

	FARJUMP16(0x0000, _start0600(SB))

TEXT _start0600(SB), $0
#ifdef FLOPPY
	LBI(0x80, rDL)
#else
	CLRB(rAL)			/* some systems pass 0 */
	CMPBR(rAL, rDL)
	JNE _save
	LBI(0x80, rDL)
#endif /* FLOPPY */
_save:
	SXB(rDL, Xdrive, xBP)		/* save disc */

	LWI(confidence(SB), rSI)	/* for that warm, fuzzy feeling */
	CALL16(BIOSputs(SB))

	LWI(_start+0x01BE(SB), rSI)	/* address of partition table */
	LWI(0x04, rCX)			/* 4 entries in table */
	LBI(0x80, rAH)			/* active entry value */
	CLRB(rAL)			/* inactive entry value */

_activeloop0:
	LXB(0x00, xSI, rBL)		/* get active entry from table */
	CMPBR(rBL, rAH)			/* is this an active entry? */
	JEQ _active

	CMPBR(rBL, rAL)			/* if not active it should be 0 */
	JNE _invalidMBR

	ADDI(0x10, rSI)			/* next table entry */
	DEC(rCX)
	JNE _activeloop0

	LWI(noentry(SB), rSI)
	CALL16(buggery(SB))

_active:
	MW(rSI, rDI)			/* save table address */

_activeloop1:
	ADDI(0x10, rSI)			/* next table entry */
	DEC(rCX)
	JEQ _readsector

	LXB(0x00, xSI, rBL)		/* get active entry from table */
	CMPBR(rBL, rAH)			/* is this an active entry? */
	JNE _activeloop1		/* should only be one active */

_invalidMBR:
	LWI(invalidMBR(SB), rSI)
	CALL16(buggery(SB))

_readsector:
	LBI(0x41, rAH)			/* check extensions present */
	LWI(0x55AA, rBX)
	LXB(Xdrive, xBP, rDL)		/* drive */
	BIOSCALL(0x13)			/* CF set on failure */
	JCS _readsector2
	CMPI(0xAA55, rBX)
	JNE _readsector2
	ANDI(0x0001, rCX)
	JEQ _readsector2

_readsector42:
	SBPBI(0x10, Xdap+0)		/* packet size */
	SBPBI(0x00, Xdap+1)		/* reserved */
	SBPBI(0x01, Xdap+2)		/* number of blocks to transfer */
	SBPBI(0x00, Xdap+3)		/* reserved */
	SBPWI(0x7C00, Xdap+4)		/* transfer buffer :offset */
	SBPWI(0x0000, Xdap+6)		/* transfer buffer seg: */
	LXW(0x08, xDI, rAX)		/* LBA (64-bits) */
	SBPW(rAX, Xdap+8)
	LXW(0x0A, xDI, rAX)
	SBPW(rAX, Xdap+10)
	SBPWI(0x0000, Xdap+12)
	SBPWI(0x0000, Xdap+14)

	MW(rBP, rSI)			/* disk address packet */
	LBI(0x42, rAH)			/* extended read */
	BIOSCALL(0x13)			/* CF set on failure */
	JCC _readsectorok

	LWI(ioerror(SB), rSI)
	CALL16(buggery(SB))

/*
 * Read a sector from a disc using the traditional BIOS call.
 * For BIOSCALL(0x13/AH=0x02):
 *   rAH	0x02
 *   rAL	number of sectors to read (1)
 *   rCH	low 8 bits of cylinder
 *   rCL	high 2 bits of cylinder (7-6), sector (5-0)
 *   rDH	head
 *   rDL	drive
 *   rES:rBX	buffer address
 */
_readsector2:
	LXB(0x01, xDI, rDH)		/* head */
	LXW(0x02, xDI, rCX)		/* save active cylinder/sector */

	LWI(0x0201, rAX)		/* read one sector */
	LXB(Xdrive, xBP, rDL)		/* drive */
	LWI(0x7C00, rBX)		/* buffer address (rES already OK) */
	BIOSCALL(0x13)			/* CF set on failure */
	JCC _readsectorok

	LWI(ioerror(SB), rSI)
	CALL16(buggery(SB))

_readsectorok:
	LWI(0x7C00, rBX)		/* buffer address (rES already OK) */
	LXW(0x1FE, xBX, rAX)
	CMPI(0xAA55, rAX)
	JNE _bbnotok

	/*
	 * Jump to the loaded PBS.
	 * rDL and rSI should still contain the drive
	 * and partition table pointer respectively.
	 */
	MW(rDI, rSI)
	FARJUMP16(0x0000, 0x7C00)

_bbnotok:
	LWI(invalidPBS(SB), rSI)

TEXT buggery(SB), $0
	CALL16(BIOSputs(SB))
	LWI(reboot(SB), rSI)
	CALL16(BIOSputs(SB))

_wait:
	CLR(rAX)			/* wait for any key */
	BIOSCALL(0x16)

_reset:
	CLR(rBX)			/* set ES segment for BIOS area */
	MTSR(rBX, rES)

	LWI(0x0472, rBX)		/* warm-start code address */
	LWI(0x1234, rAX)		/* warm-start code */
	POKEW				/* MOVW	AX, ES:[BX] */

	FARJUMP16(0xFFFF, 0x0000)	/* reset */

/*
 * Output a string to the display.
 * String argument is in rSI.
 */
TEXT BIOSputs(SB), $0
	PUSHA
	CLR(rBX)
_BIOSputs:
	LODSB
	ORB(rAL, rAL)
	JEQ _BIOSputsret

	LBI(0x0E, rAH)
	BIOSCALL(0x10)
	JMP _BIOSputs

_BIOSputsret:
	POPA
	RET

/* "No active entry in MBR" */
TEXT noentry(SB), $0
	BYTE $'N'; BYTE $'o'; BYTE $' '; BYTE $'a';
	BYTE $'c'; BYTE $'t'; BYTE $'i'; BYTE $'v';
	BYTE $'e'; BYTE $' '; BYTE $'e'; BYTE $'n';
	BYTE $'t'; BYTE $'r'; BYTE $'y'; BYTE $' ';
	BYTE $'i'; BYTE $'n'; BYTE $' '; BYTE $'M';
	BYTE $'B'; BYTE $'R';
	BYTE $'\z';

/* "Invalid MBR" */
TEXT invalidMBR(SB), $0
	BYTE $'I'; BYTE $'n'; BYTE $'v'; BYTE $'a';
	BYTE $'l'; BYTE $'i'; BYTE $'d'; BYTE $' ';
	BYTE $'M'; BYTE $'B'; BYTE $'R';
	BYTE $'\z';

/* "I/O error" */
TEXT ioerror(SB), $0
	BYTE $'I'; BYTE $'/'; BYTE $'O'; BYTE $' ';
	BYTE $'e'; BYTE $'r'; BYTE $'r'; BYTE $'o';
	BYTE $'r';
	BYTE $'\z';

/* "Invalid PBS" */
TEXT invalidPBS(SB), $0
	BYTE $'I'; BYTE $'n'; BYTE $'v'; BYTE $'a';
	BYTE $'l'; BYTE $'i'; BYTE $'d'; BYTE $' ';
	BYTE $'P'; BYTE $'B'; BYTE $'S';
	BYTE $'\z';

/* "\r\nPress almost any key to reboot..." */
TEXT reboot(SB), $0
	BYTE $'\r';BYTE $'\n';
	BYTE $'P'; BYTE $'r'; BYTE $'e'; BYTE $'s';
	BYTE $'s'; BYTE $' '; BYTE $'a'; BYTE $'l'; 
	BYTE $'m'; BYTE $'o'; BYTE $'s'; BYTE $'t';
	BYTE $' '; BYTE $'a'; BYTE $'n'; BYTE $'y';
	BYTE $' '; BYTE $'k'; BYTE $'e'; BYTE $'y';
	BYTE $' '; BYTE $'t'; BYTE $'o'; BYTE $' ';
	BYTE $'r'; BYTE $'e'; BYTE $'b'; BYTE $'o';
	BYTE $'o'; BYTE $'t'; BYTE $'.'; BYTE $'.';
	BYTE $'.';
	BYTE $'\z';

/* "MBR..." */
TEXT confidence(SB), $0
	BYTE $'M'; BYTE $'B'; BYTE $'R'; BYTE $'.';
	BYTE $'.'; BYTE $'.';
	BYTE $'\z';