Browse Source

First code commit

Stephen Downward 4 months ago
parent
commit
113a44d6f2
3 changed files with 232 additions and 0 deletions
  1. 15 0
      Makefile
  2. 187 0
      boot/bootsect.asm
  3. 30 0
      boot/secondstage.asm

+ 15 - 0
Makefile

@ -0,0 +1,15 @@
1
steveos2.bin: boot/bootsect.bin boot/secondstage.bin
2
	cat boot/bootsect.bin boot/secondstage.bin > steveos2.bin
3
4
%.bin: %.asm
5
	nasm $< -o $@
6
7
run: steveos2.bin
8
	qemu-system-i386 -fda steveos2.bin
9
10
debug: steveos2.bin
11
	qemu-system-i386 -fda steveos2.bin -S -s &
12
	gdb -ex \
13
	-ex 'file steveos2.bin' \
14
    -ex 'set arch i386:intel' \
15
    -ex 'target remote localhost:1234' \

+ 187 - 0
boot/bootsect.asm

@ -0,0 +1,187 @@
1
	org 0x7c00
2
	bits 16
3
	cpu 486
4
5
	STAGE2 equ 0x7e00
6
7
	mov esp, 0x7c00				;top of stack
8
	mov ebp, 0					;bottom of stack
9
10
	mov ax, 0
11
	mov ds, ax
12
	mov ss, ax
13
	mov es, ax
14
	mov fs, ax
15
	mov gs, ax
16
17
	;; Init screen
18
	mov ah, 0x0
19
	mov al, 0x03
20
	int 0x10
21
22
	;; Write string
23
	mov bp, STR_DISK_START
24
	mov cx, 41
25
	call WRITE_STRING
26
27
	;; Read in OS
28
	mov ch, 0					;cylinder
29
	;; sector 1 is bootloader
30
	;; so we start at sector 2
31
	mov cl, 2					;sector
32
	mov dh, 0					;head
33
	push word 0x7e0				;0x7e00 / 0x10
34
	pop es
35
READ_NEXT:
36
	mov ah, 0x02				;Read
37
	mov al, 1					;Read 1 sector
38
	;; bios puts boot drive in dl
39
	;mov dl, 0x0					;drive #0 (A:)
40
	mov bx, 0					;segment:offset, offset = 0
41
	int 0x13
42
	jc READ_ERROR
43
44
	cmp al, 1
45
	jne READ_ERROR
46
	
47
	;; reset read attempt counter
48
	mov ah, 0
49
	mov [READ_ATTEMPTS], ah
50
51
	mov ax, es
52
	call WRITE_HEX
53
	mov ax, [es: bx]
54
	call WRITE_HEX
55
	mov ax, dx
56
	call WRITE_HEX
57
	
58
	;; inc es
59
	mov ax, es
60
	add ax, 512 / 16
61
	mov es, ax
62
	
63
	;; inc sector count
64
	inc cl
65
	cmp cl, 19					;18 sectors
66
	je INC_HEAD
67
	
68
	jmp READ_NEXT
69
70
INC_HEAD:
71
	mov cl, 1					;reset sector count
72
	inc dh
73
	cmp dh, 2
74
	je INC_CYL
75
	jmp READ_NEXT
76
	
77
INC_CYL:
78
	mov dh, 0					;reset head count
79
	inc ch						;inc cyl
80
	cmp ch, 5					;read 19 cylinders
81
	;; 19 * 18 * 2 * 512 = 350kb
82
	je READ_DONE
83
	jmp READ_NEXT
84
	
85
READ_ERROR:
86
	mov ah, [READ_ATTEMPTS]
87
	inc ah
88
	cmp ah, 5
89
	jge READ_GIVEUP
90
	mov [READ_ATTEMPTS], ah
91
	jmp READ_NEXT
92
93
READ_GIVEUP:
94
	;; Print err message
95
	mov bp, STR_DISK_FAIL
96
	mov cx, 41
97
	call WRITE_STRING
98
	
99
	jmp $
100
101
READ_DONE:
102
	;; Write string
103
	mov bp, STR_DISK_DONE
104
	mov cx, 41
105
	call WRITE_STRING
106
	push word 0
107
	pop es
108
	
109
	jmp STAGE2
110
	
111
	;; Put string in bp
112
	;; put length in cx
113
WRITE_STRING:
114
	pusha
115
	mov ah, 0x13
116
	mov al, 0x01
117
	mov bh, 0
118
	mov bl, 7
119
	push word 0
120
	pop es
121
	mov dl, 0
122
	mov dh, 0
123
	int 0x10
124
	popa
125
	ret
126
127
	;; input in eax
128
WRITE_HEX:
129
	push eax					;need to be explicit about 32 bit registers
130
	pusha
131
	push ax
132
	mov ah, 0x0e
133
	mov al, '0'
134
	mov bx, 0
135
	int 0x10
136
	mov ah, 0x0e
137
	mov al, 'x'
138
	mov bx, 0
139
	int 0x10
140
	pop ax
141
	mov dx, 0
142
WH_LOOP:
143
	rol eax, 4
144
	inc dx
145
	cmp dx, 8
146
	jg WH_DONE
147
	mov cx, ax
148
	and cl, 0xF
149
	cmp cl, 9
150
	jg WH_HEX
151
	add cl, 0x30				;dec -> ascii
152
	push ax
153
	mov ah, 0x0e
154
	mov al, cl
155
	mov bx, 0
156
	int 0x10
157
	pop ax
158
	jmp WH_LOOP
159
160
WH_HEX:
161
	add cl, 0x37
162
	push ax
163
	mov ah, 0x0e
164
	mov al, cl
165
	mov bx, 0
166
	int 0x10
167
	pop ax
168
	jmp WH_LOOP
169
170
WH_DONE:
171
	mov ah, 0x0e
172
	mov al, ' '
173
	mov bx, 0
174
	int 0x10
175
	mov al, ' '
176
	int 0x10
177
	popa
178
	pop eax
179
	ret
180
	
181
READ_ATTEMPTS:	db 0
182
STR_DISK_START:	db "Loading SPEEDOS from floppy diskette...", 0xA, 0xD, 0
183
STR_DISK_FAIL:	db "Error! Disk seems to be corrupted!     ", 0xA, 0xD, 0
184
STR_DISK_DONE:	db "Ready to load second stage!            ", 0xA, 0xD, 0
185
	
186
	times 510-($-$$) db 0
187
	dw 0xaa55

+ 30 - 0
boot/secondstage.asm

@ -0,0 +1,30 @@
1
	org 0x7e00
2
	bits 16
3
4
	;; No longer using 0x7c00-0c7e00 so we can make the stack a little bigger
5
	mov esp, 0x7e00
6
7
	mov bp, STR_MADEIT
8
	mov cx, 1000
9
	CALL WRITE_STRING
10
11
	jmp $
12
	;; Put string in bp
13
	;; put length in cx
14
WRITE_STRING:
15
	pusha
16
	mov ah, 0x13
17
	mov al, 0x01
18
	mov bh, 0
19
	mov bl, 7
20
	push word 0
21
	pop es
22
	mov dl, 0
23
	mov dh, 0
24
	int 0x10
25
	popa
26
	ret
27
28
times 8500 db "X"
29
30
STR_MADEIT: times 2 db "Stage 2 of the bootloader has loaded!  141592653589793238462643383279502884197169399375105 82097494459230781640628620899862803482534211706798 21480865132823066470938446095505822317253594081284 81117450284102701938521105559644622948954930381964 42881097566593344612847564823378678316527120190914 56485669234603486104543266482133936072602491412737 24587006606315588174881520920962829254091715364367 89259036001133053054882046652138414695194151160943 30572703657595919530921861173819326117931051185480 74462379962749567351885752724891227938183011949129 83367336244065664308602139494639522473719070217986 09437027705392171762931767523846748184676694051320 00568127145263560827785771342757789609173637178721 46844090122495343014654958537105079227968925892354 20199561121290219608640344181598136297747713099605 18707211349999998372978049951059731732816096318595 02445945534690830264252230825334468503526193118817 10100031378387528865875332083814206171776691473035 98253490428755468731159562863882353787593751957781 85778053217122680661300192787661119590921642019893 80952572010654858632788659361533818279682303019520 35301852968995773622599413891249721775283479131515 57485724245415069595082953311686172785588907509838 17546374649393192550604009277016711390098488240128 58361603563707660104710181942955596198946767837449 44825537977472684710404753464620804668425906949129 33136770289891521047521620569660240580381501935112 53382430035587640247496473263914199272604269922796 78235478163600934172164121992458631503028618297455 57067498385054945885869269956909272107975093029553 21165344987202755960236480665499119881834797753566 36980742654252786255181841757467289097777279380008 16470600161452491921732172147723501414419735685481 61361157352552133475741849468438523323907394143334 54776241686251898356948556209921922218427255025425 68876717904946016534668049886272327917860857843838 27967976681454100953883786360950680064225125205117 39298489608412848862694560424196528502221066118630 67442786220391949450471237137869609563643719172874 67764657573962413890865832645995813390478027590099 46576407895126946839835259570982582262052248940772 67194782684826014769909026401363944374553050682034 96252451749399651431429809190659250937221696461515 70985838741059788595977297549893016175392846813826 86838689427741559918559252459539594310499725246808 45987273644695848653836736222626099124608051243884 39045124413654976278079771569143599770012961608944 16948685558484063534220722258284886481584560285060 16842739452267467678895252138522549954666727823986 45659611635488623057745649803559363456817432411251 50760694794510965960940252288797108931456691368672 ", 0xA, 0xD, 0