aboutsummaryrefslogtreecommitdiffstats
path: root/gpxe/src/arch/i386/prefix/bootpart.S
blob: 968da1a3817e2044f321144b4610e16aa759dc6b (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
FILE_LICENCE ( GPL2_OR_LATER )

#define BOOT_SEG	0x07c0
#define EXEC_SEG	0x0100
#define STACK_SEG 	0x0200
#define STACK_SIZE	0x2000
	
	.text
	.arch i386
	.section ".prefix", "awx", @progbits
	.code16

/*
 * Find active partition
 *
 * Parameters:
 *   %dl	: BIOS drive number
 *   %bp	: Active partition handler routine
 */
find_active_partition:
	/* Set up stack at STACK_SEG:STACK_SIZE */
	movw	$STACK_SEG, %ax
	movw	%ax, %ss
	movw	$STACK_SIZE, %sp

	/* Relocate self to EXEC_SEG */
	pushw	$BOOT_SEG
	popw	%ds
	pushw	$EXEC_SEG
	popw	%es
	xorw	%si, %si
	xorw	%di, %di
	movw	$0x200, %cx
	rep movsb
	ljmp	$EXEC_SEG, $1f
1:	pushw	%ds
	popw	%es
	pushw	%cs
	popw	%ds

	/* Check for LBA extensions */
	movb	$0x41, %ah
	movw	$0x55aa, %bx
	stc
	int	$0x13
	jc	1f
	cmpw	$0xaa55, %bx
	jne	1f
	movw	$read_lba, read_sectors
1:	
	/* Read and process root partition table */
	xorb	%dh, %dh
	movw	$0x0001, %cx
	xorl	%esi, %esi
	xorl	%edi, %edi
	call	process_table

	/* Print failure message */
	movw	$10f, %si
	jmp	boot_error
10:	.asciz	"Could not locate active partition\r\n"

/*
 * Print failure message and boot next device
 *
 * Parameters:
 *   %si	: Failure string
 */
boot_error:
	cld
	movw	$0x0007, %bx
	movb	$0x0e, %ah
1:	lodsb
	testb	%al, %al
	je	99f
	int	$0x10
	jmp	1b
99:	/* Boot next device */
	int	$0x18

/*
 * Process partition table
 *
 * Parameters:
 *   %dl	: BIOS drive number
 *   %dh	: Head
 *   %cl	: Sector (bits 0-5), high two bits of cylinder (bits 6-7)
 *   %ch	: Low eight bits of cylinder
 *   %esi:%edi	: LBA address
 *   %bp	: Active partition handler routine
 *
 * Returns:
 *   CF set on error
 */
process_table:
	pushal
	call	read_boot_sector
	jc	99f
	movw	$446, %bx
1:	call	process_partition
	addw	$16, %bx
	cmpw	$510, %bx
	jne	1b
99:	popal
	ret

/*
 * Process partition
 *
 * Parameters:
 *   %dl	: BIOS drive number
 *   %dh	: Head
 *   %cl	: Sector (bits 0-5), high two bits of cylinder (bits 6-7)
 *   %ch	: Low eight bits of cylinder
 *   %esi:%edi	: LBA address
 *   %bx	: Offset within partition table
 *   %bp	: Active partition handler routine
 */
process_partition:
	pushal
	/* Load C/H/S values from partition entry */
	movb	%es:1(%bx), %dh
	movw	%es:2(%bx), %cx
	/* Update LBA address from partition entry */
	addl	%es:8(%bx), %edi
	adcl	$0, %esi
	/* Check active flag */
	testb	$0x80, %es:(%bx)
	jz	1f
	call	read_boot_sector
	jc	99f
	jmp	*%bp
1:	/* Check for extended partition */
	movb	%es:4(%bx), %al
	cmpb	$0x05, %al
	je	2f
	cmpb	$0x0f, %al
	je	2f
	cmpb	$0x85, %al
	jne	99f
2:	call	process_table
99:	popal
	/* Reload original partition table */
	call	read_boot_sector
	ret

/*
 * Read single sector to %es:0000 and verify 0x55aa signature
 *
 * Parameters:
 *   %dl	: BIOS drive number
 *   %dh	: Head
 *   %cl	: Sector (bits 0-5), high two bits of cylinder (bits 6-7)
 *   %ch	: Low eight bits of cylinder
 *   %esi:%edi	: LBA address
 *
 * Returns:
 *   CF set on error
 */
read_boot_sector:
	pushw	%ax
	movw	$1, %ax
	call	*read_sectors
	jc	99f
	cmpw	$0xaa55, %es:(510)
	je	99f
	stc	
99:	popw	%ax
	ret
	
/*
 * Read sectors to %es:0000
 *
 * Parameters:
 *   %dl	: BIOS drive number
 *   %dh	: Head
 *   %cl	: Sector (bits 0-5), high two bits of cylinder (bits 6-7)
 *   %ch	: Low eight bits of cylinder
 *   %esi:%edi	: LBA address
 *   %ax	: Number of sectors (max 127)
 *
 * Returns:
 *   CF set on error
 */
read_sectors:	.word	read_chs

read_chs:
	/* Read sectors using C/H/S address */
	pushal
	xorw	%bx, %bx
	movb	$0x02, %ah
	stc
	int	$0x13
	sti
	popal
	ret

read_lba:
	/* Read sectors using LBA address */
	pushal
	movw	%ax, (lba_desc + 2)
	pushw	%es
	popw	(lba_desc + 6)
	movl	%edi, (lba_desc + 8)
	movl	%esi, (lba_desc + 12)
	movw	$lba_desc, %si
	movb	$0x42, %ah
	int	$0x13
	popal
	ret

lba_desc:
	.byte	0x10
	.byte	0
	.word	1
	.word	0x0000
	.word	0x0000
	.long	0, 0