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
|
/*
* Ported to boot 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
*
* Mach Operating System
* Copyright (c) 1992, 1991 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
* HISTORY
* $Log: start.s,v $
* Revision 2.2 92/04/04 11:36:29 rpd
* Fix Intel Copyright as per B. Davies authorization.
* [92/04/03 rvb]
* Need to zero dh on hd path; at least for an adaptec card.
* [92/01/14 rvb]
*
* From 2.5 boot:
* Flush digit printing.
* Fuse floppy and hd boot by using Int 21 to tell
* boot type (slightly dubious since Int 21 is DOS
* not BIOS)
* [92/03/30 mg32]
*
* Revision 2.2 91/04/02 14:42:04 mbj
* Fix the BIG boot bug. We had missed a necessary data
* before a xor that was clearing a register used later
* as an index register.
* [91/03/01 rvb]
* Remember floppy type for swapgeneric
* Add Intel copyright
* [90/02/09 rvb]
*
*/
/*
Copyright 1988, 1989, 1990, 1991, 1992
by Intel Corporation, Santa Clara, California.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appears in all
copies and that both the copyright notice and this permission notice
appear in supporting documentation, and that the name of Intel
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.
INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "asm.h"
.file "start.s"
BOOTSEG = 0x9000 # boot will be loaded at 640k-64k
BOOTSTACK = 0xe000 # boot stack
SIGNATURE = 0xaa55
LOADSZ = 14 # size of unix boot
PARTSTART = 0x1be # starting address of partition table
NUMPART = 4 # number of partitions in partition table
PARTSZ = 16 # each partition table entry is 16 bytes
BSDPART = 0xA5 # value of boot_ind, means bootable partition
BOOTABLE = 0x80 # value of boot_ind, means bootable partition
.text
ENTRY(boot1)
# boot1 is loaded at 0x0:0x7c00
# ljmp to the next instruction to set up %cs
data32
ljmp $0x7c0, $start
start:
# set up %ds
mov %cs, %ax
mov %ax, %ds
# set up %ss and %esp
data32
mov $BOOTSEG, %eax
mov %ax, %ss
data32
mov $BOOTSTACK, %esp
/*** set up %es, (where we will load boot2 to) ***/
mov %ax, %es
#ifdef DEBUG
data32
mov $one, %esi
data32
call message
#endif
# get the boot drive id
movb $0x33, %ah
movb $0x05, %al
int $0x21
cmpb $0x80, %dl
data32
jge hd
fd:
# reset the disk system
#ifdef DEBUG
data32
mov $two, %esi
data32
call message
#endif
movb $0x0, %ah
int $0x13
data32
mov $0x0001, %ecx # cyl 0, sector 1
data32
#ifdef DEBUG
data32
mov $three, %esi
data32
call message
#endif
jmp load
hd: /**** load sector 0 into the BOOTSEG ****/
#ifdef DEBUG
data32
mov $four, %esi
data32
call message
#endif
data32
mov $0x0201, %eax
xor %ebx, %ebx # %bx = 0
data32
mov $0x0001, %ecx
#ifdef DEBUG
data32
mov $five, %esi
data32
call message
#endif
data32
andl $0xff, %edx
/*mov $0x0080, %edx*/
int $0x13
data32
jb read_error
/***# find the bootable partition *****/
data32
mov $PARTSTART, %ebx
data32
mov $NUMPART, %ecx
again:
addr16
movb %es:4(%ebx), %al
cmpb $BSDPART, %al
data32
je found
data32
add $PARTSZ, %ebx
data32
loop again
data32
mov $enoboot, %esi
data32
jmp err_stop
/*
# BIOS call "INT 0x13 Function 0x2" to read sectors from disk into memory
# Call with %ah = 0x2
# %al = number of sectors
# %ch = cylinder
# %cl = sector
# %dh = head
# %dl = drive (0x80 for hard disk, 0x0 for floppy disk)
# %es:%bx = segment:offset of buffer
# Return:
# %al = 0x0 on success; err code on failure
*/
found:
addr16
movb %es:1(%ebx), %dh /* head */
addr16
xor %ecx, %ecx
addr16
movw %es:2(%ebx), %ecx /*sect,cyl (+ 2 bytes junk in top word )*/
load:
movb $0x2, %ah /* function 2 */
movb $LOADSZ, %al /* number of blocks */
xor %ebx, %ebx /* %bx = 0, put it at 0 in the BOOTSEG */
int $0x13
data32
jb read_error
# ljmp to the second stage boot loader (boot2).
# After ljmp, %cs is BOOTSEG and boot1 (512 bytes) will be used
# as an internal buffer "intbuf".
#ifdef DEBUG
data32
mov $six, %esi
data32
call message
#endif
data32
ljmp $BOOTSEG, $EXT(boot2)
#
# read_error
#
read_error:
data32
mov $eread, %esi
err_stop:
data32
call message
data32
jmp stop
#
# message: write the error message in %ds:%esi to console
#
message:
# Use BIOS "int 10H Function 0Eh" to write character in teletype mode
# %ah = 0xe %al = character
# %bh = page %bl = foreground color (graphics modes)
data32
push %eax
data32
push %ebx
data32
mov $0x0001, %ebx
cld
nextb:
lodsb # load a byte into %al
cmpb $0x0, %al
data32
je done
movb $0xe, %ah
int $0x10 # display a byte
data32
jmp nextb
done:
data32
pop %ebx
data32
pop %eax
data32
ret
stop: hlt
data32
jmp stop # halt doesnt actually halt forever
/* error messages */
#ifdef DEBUG
one: String "1\r\n\0"
two: String "2\r\n\0"
three: String "3\r\n\0"
four: String "4\r\n\0"
five: String "5\r\n\0"
six: String "6\r\n\0"
seven: String "7\r\n\0"
#endif DEBUG
eread: String "Read error\r\n\0"
enoboot: String "No bootable partition\r\n\0"
endofcode:
/* throw in a partition in case we are block0 as well */
/* flag,head,sec,cyl,typ,ehead,esect,ecyl,start,len */
. = EXT(boot1) + PARTSTART
.byte 0x0,0,0,0,0,0,0,0
.long 0,0
.byte 0x0,0,0,0,0,0,0,0
.long 0,0
.byte 0x0,0,0,0,0,0,0,0
.long 0,0
.byte BOOTABLE,0,1,0,BSDPART,255,255,255
.long 0,50000
/* the last 2 bytes in the sector 0 contain the signature */
. = EXT(boot1) + 0x1fe
.value SIGNATURE
ENTRY(disklabel)
. = EXT(boot1) + 0x400
|