-
Notifications
You must be signed in to change notification settings - Fork 18
/
bootblock.lisp
440 lines (379 loc) · 13.1 KB
/
bootblock.lisp
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
;;;;------------------------------------------------------------------
;;;;
;;;; Copyright (C) 2001,2000, 2002-2005,
;;;; Department of Computer Science, University of Tromso, Norway
;;;;
;;;; Filename: bootblock.lisp
;;;; Description: A simple, single-stage, floppy bootloader.
;;;; Author: Frode Vatvedt Fjeld <frodef@acm.org>
;;;; Created at: Mon Oct 9 20:47:19 2000
;;;; Distribution: See the accompanying file COPYING.
;;;;
;;;; $Id: bootblock.lisp,v 1.16 2008/03/03 22:40:55 ffjeld Exp $
;;;;
;;;;------------------------------------------------------------------
(in-package movitz)
(defvar *bootblock-build-file* #p"bootblock-id.txt")
(defvar *bootblock-build*
;; make this variable persistent.
(or (ignore-errors
(with-open-file (s *bootblock-build-file* :direction :input)
(with-standard-io-syntax (read s))))
(warn "Unable to read ~S from ~A, resetting to zero."
'*bootblock-build*
*bootblock-build-file*)
0))
(defvar *floppy-size* (* 512 18 2 80))
(defun make-segment-descriptor-byte (&rest descriptor-args)
(list (complex (binary-types::bitfield-compute-numeric-value
(find-binary-type 'segment-descriptor)
(apply #'make-segment-descriptor descriptor-args))
8)))
(defun mkasm16-bios-print ()
"Print something to the terminal. [es:si] points to the text"
`((:movzxb (:si) :cx)
(:incw :si)
(:movb #xe :ah)
(:movw 7 :bx)
print-loop
(:lodsb)
(:int #x10)
(:loop 'print-loop)
(:ret)))
(defun mkasm16-format-hex ()
"Format a 16-bit word (in DX) into hex string (in DI)"
`((:std)
(:movw 4 :cx)
(:addw :cx :di)
(:decw :di)
format-loop
(:movb :dl :bl)
(:andw #x0f bx)
(:movb ('hex-table bx) :al)
(:stosb)
(:shrw :dx 4)
(:decw :cx)
(:jnz 'format-loop)
(:cld)
(:ret)
hex-table (% format nil "0123456789abcdef")))
(defconstant +SECTOR-SIZE+ 512)
(defconstant +HEAD+ 0)
(defconstant +TRACK+ 1)
(defconstant +NOSEC+ 2)
(defconstant +DADDR+ 4)
(defconstant +DADDRSEG+ 6)
(defconstant +STARTSEC+ 8)
(defconstant +linear-sector+ -4)
(defconstant +destination+ -8)
(defconstant +sectors-per-track+ -12)
(defconstant +stack-frame-size+ 16)
(defconstant +read-buffer+ #x10000)
(defun mkasm16-bios-bootloader (image-size load-address &optional (skip-sectors 0))
(let* ((first-sector (1+ skip-sectors))
(last-sector (+ first-sector (ceiling image-size +sector-size+)))
(read-buffer-segment (floor +read-buffer+ #x10)))
`((:jmp (:pc+ 0)) ; some BIOSes might check for this.
;;
;; We are running at address #x7c00.
;;
(:xorw :ax :ax)
(:movw :ax :ds)
(:movw :ax :es)
(:movw #x9000 :ax)
(:movw :ax :ss)
(:movw #xfffc :bp)
(:leaw (:bp ,(- +stack-frame-size+)) :sp)
(:movw 'welcome :si) ; Print welcome message)
(:callw 'print)
;;
;; Enable the A20 gate
;;
(:callw 'empty-8042)
(:movb #xd1 :al)
(:outb :al #x64)
(:callw 'empty-8042)
(:movb #xdf :al)
(:outb :al #x60)
(:callw 'empty-8042)
;; Poll the floppy's sectors per track
(:movw 5 (:bp ,+sectors-per-track+))
check-geometry
(:incb (:bp ,+sectors-per-track+))
(:jz 'read-error)
(:movw (:bp ,+sectors-per-track+) :cx )
(:movw #x0201 :ax)
(:xorw :dx :dx)
(:movw ,read-buffer-segment :bx)
(:movw :bx :es)
(:xorw :bx :bx)
(:int #x13) ; Call BIOS routine
(:testb :ah :ah)
(:jz 'check-geometry)
(:decb (:bp ,+sectors-per-track+))
;;
;; Read sectors into memory
;;
(:movw ,first-sector (:bp ,+linear-sector+))
(:movl ,load-address (:bp ,+destination+))
read-loop
(:cmpw ,last-sector (:bp ,+linear-sector+))
(:jg 'read-done)
(:movw 'track-start-msg :si) ; Print '(' to screen for each track
(:callw 'print)
(:movw (:bp ,+linear-sector+) :ax)
(:movb (:bp ,+sectors-per-track+) :cl)
(:divb :cl :ax) ; al=quotient, ah=remainder of :ax/:cl
(:movb :ah :cl) ; sector - 1
(:movb :al :dh)
(:andb 1 :dh) ; head
(:movb :al :ch)
(:shrb 1 :ch) ; track
(:xorb :dl :dl) ; drive = 0
(:movw (:bp ,+sectors-per-track+) :ax)
(:subb :cl :al) ; number of sectors (rest of track)
(:incb :cl)
(:addw :ax (:bp ,+linear-sector+)) ; update read pointer
(:movw (:bp ,+linear-sector+) :bx) ; subtract some if it's the last track.
(:subw ,last-sector :bx)
(:jc 'subtract-zero-sectors)
(:subw :bx :ax)
(:jz 'read-done)
subtract-zero-sectors
(:movb 2 :ah)
(:movw ,read-buffer-segment :bx)
(:movw :bx :es)
(:xorw :bx :bx)
(:int #x13) ; Call BIOS routine
(:jc 'read-error)
(:movzxb :al :ecx)
;;
;; Install GS as 4GB segment
;; http://www.faqs.org/faqs/assembly-language/x86/general/part2/
;;
(:cli)
(:lgdt ('gdt-addr)) ; load gdt
(:movcr :cr0 :eax)
(:orb 1 :al)
(:movcr :eax :cr0)
(:jmp (:pc+ 0))
(:movw 16 :bx)
(:movw :bx :gs)
(:andb #xfe :al)
(:movcr :eax :cr0)
(:jmp (:pc+ 0))
(:sti)
;; Completed install GS as 4GB segment.
;; Copy data to destination
(:shll ,(+ 9 -2) :ecx) ; 512/4 = sector-size/word-size
(:movl ,+read-buffer+ :ebx)
(:movl (:bp ,+destination+) :esi)
(:leal (:esi (:ecx 4)) :edx)
(:movl :edx (:bp ,+destination+))
copy-loop
(:decl :ecx)
((:gs-override) :movl (:ebx (:ecx 4)) :edx)
((:gs-override) :movl :edx (:esi (:ecx 4)))
(:jnz 'copy-loop)
(:movw 'track-end-msg :si) ; Print ')' to screen after each track
(:callw 'print)
(:jmp 'read-loop)
read-done
motor-loop ; Wait for floppy motor
(:btw 8 (#x43e))
(:jc 'motor-loop)
(:movw 'entering :si) ; Print welcome message
(:callw 'print)
;; Read the cursor position into DH (row) and DL (column).
(:movb 3 :ah)
(:movb 0 :bh)
(:int #x10)
(:cli) ; Disable interrupts
(:lgdt ('gdt-addr)) ; load gdt
(:xorw :ax :ax)
(:movw :ax :es) ; reset es
;;
;; Turn off the cursor
;;
;;; (movb #x01 :ah)
;;; (movw #x0100 :cx)
;;; (int #x10)
;;
;; Load machine status word. This will enable
;; protected mode. The subsequent instruction MUST
;; reload the code segment register with a selector for
;; the protected mode code segment descriptor (see
;; GDT specification).
;;
(:movw 1 :ax)
(:lmsw :ax) ; load word 0 of cr0
;;
;; Do a longjump to new-world. This will cause the CS to
;; be loaded with the correct descriptor, and the processor
;; will now run in 32 bit mode.
;;
(:jmp 8 ('new-world))
;;
;; Display error message and hang
;;
read-error
(:movw 'error :si) ; Print error message
(:callw 'print)
halt-cpu
(:halt)
(:jmp 'halt-cpu) ; Infinite loop
;;
;; Empty the 8042 Keyboard controller
;;
empty-8042
(:callw 'delay)
(:inb #x64 :al) ; 8042 status port
(:testb 1 :al) ; if ( no information available )
(:jz 'no-output) ; goto no_output
(:callw 'delay)
(:inb #x60 :al) ; read it
(:jmp 'empty-8042)
no-output
(:testb 2 :al) ; if ( input buffer is full )
(:jnz 'empty-8042) ; goto empty_8042
(:ret)
delay
(:xorw :cx :cx)
delay-loop
(:loop 'delay-loop)
(:ret)
print ,@(mkasm16-bios-print)
;; Data
welcome (:% :format 8 "Loading Movitz ~D..~%"
,(incf *bootblock-build*))
entering (:% :format 8 "~%Enter..")
error (:% :format 8 "Failed!)")
track-start-msg (:% :format 8 "(")
track-end-msg (:% :format 8 ")")
sector-msg (:% :format 8 "-")
(:% :align 16)
gdt
(:% :bytes 16 0)
gdt-addr
(:% :bytes 16 ,(1- (* 3 8)))
(:% :bytes 32 'gdt) ; both the null and pointer to gdt
;; (% fun (make-segment-descriptor-byte)) ; dummy null descriptor
(:% :fun (make-segment-descriptor-byte :base 0 :limit #xfffff ; 1: code segment
:type 10 :dpl 0
:flags (s p d/b g)))
(:% :fun (make-segment-descriptor-byte :base 0 :limit #xfffff ; 2: data segment
:type 2 :dpl 0
:flags (s p d/b g)))
;; (% align 4)
new-world
;; ..must be concatenated onto here.
)))
(defconstant +screen-base+ #xb8000)
(defparameter +message+ "Ok.")
(defparameter +halt-message+ "Halt!")
(defun make-vga-string (string)
(loop for char across string
collect (char-code char)
collect #x07))
(defun mkasm-loader (image-size load-address call-address)
"Make the 32-bit loader."
(assert (<= load-address call-address (+ load-address image-size)) ()
"Call-address #x~X is not in range #x~X to #x~X."
call-address load-address (+ load-address image-size))
`((:movw ,(* 2 8) :ax) ; Load DS, ES and SS with the correct data segment descriptors
(:movw :ax :ds)
(:movw :ax :es)
(:movw :ax :fs)
(:movw :ax :gs)
(:movw :ax :ss)
(:movl #x20000 :esp)
;;; (pushl -1) ; stack-end-marker
;; If we are not on a 386, perform WBINVD to flush caches.
;; (:testl :edi :edi) ; clear ZF
(:pushfl) ; push original EFLAGS
(:popl :eax) ; get original EFLAGS
(:movl :eax :ecx) ; save original EFLAGS
(:xorl #x40000 :eax) ; flip AC bit in EFLAGS
(:pushl :eax) ; save new EFLAGS value on stack
(:popfl) ; replace current EFLAGS value
(:pushfl) ; get new EFLAGS
(:popl :eax) ; store new EFLAGS in EAX
(:xorl :ecx :eax) ; can't toggle AC bit, processor=80386, ZF=1
(:jz 'skip-wbinvd) ; jump if 80386 processor
(:wbinvd)
skip-wbinvd
(:movzxb :dl :eax) ; cursor column
(:movzxb :dh :ebx) ; cursor row
(:imull 160 :ebx :ebx)
(:movl 'i-am-32 :esi)
os-loop
(:leal ((:eax 2) :ebx ,+screen-base+) :edi)
(:xorl :ecx :ecx)
(:movb ,(length +message+) :cl)
((:repz) :movsw) ; print i-am-32
(:movl ,call-address :eax)
(:jmp :eax) ; call OS
;;; (:movl ,(length +halt-message+) :ecx)
;;; (:movl 'halt-msg :esi)
;;; (:movl ,(+ +screen-base+ (* 2 80 11) (* 2 35)) :edi)
;;; ((:repz) movsw)
;;;
;;; (:movw #x7400 (:edi))
;;; eternal
;;; (:incb (:edi))
;;; (:halt)
;;; (:jmp 'eternal) ; OS returned?
;; (% align 2)
i-am-32 (:% :bytes 8 ,@(make-vga-string +message+))
;;; halt-msg (% fun ((lambda ()
;;; (loop for char across ,+halt-message+
;;; collect (complex (logior #x4700 (char-code char)) 2)))))
))
(defun make-bootblock (image-size load-address call-address
&key (skip-sectors 0) (include-records))
(when *floppy-size*
(let ((floppy-room (- *floppy-size* 512))) ; Size of floppy minus the bootloader.
(if (> image-size floppy-room)
(warn "The image is ~D bytes too big to fit on a ~,2F MB floppy."
(- image-size floppy-room)
(/ *floppy-size* (* 1024 1000)))
(format t "~&;; Bootloader has room for ~,1F KB more."
(/ (- floppy-room image-size) 1024)))))
(multiple-value-bind (bios-loader bb-symtab)
(let ((asm-x86:*position-independent-p* nil)
(asm-x86:*cpu-mode* :16-bit))
(asm:assemble-proglist (mkasm16-bios-bootloader image-size load-address skip-sectors)
:start-pc #x7c00))
(multiple-value-bind (protected-loader protected-symtab)
(let ((asm-x86:*position-independent-p* nil)
(asm-x86:*cpu-mode* :32-bit))
(asm:assemble-proglist (mkasm-loader image-size load-address call-address)
:start-pc (cdr (or (assoc 'new-world bb-symtab)
(error "No new-world defined in bios-loader.")))))
(let* ((loader-length (+ (length bios-loader)
(length protected-loader)))
(bootblock (progn
(assert (<= loader-length 510) ()
"Bootblock size of ~D octets is too big, max is 510!" loader-length)
(make-array 512 :element-type '(unsigned-byte 8)
:fill-pointer loader-length))))
(setf (subseq bootblock 0) bios-loader
(subseq bootblock (length bios-loader)) protected-loader)
(loop until (zerop (mod (fill-pointer bootblock) 4))
do (vector-push 0 bootblock))
(dolist (record include-records)
(let ((*endian* :little-endian))
(with-binary-output-to-vector (stream bootblock)
(write-binary-record record stream))))
(setf (fill-pointer bootblock) 512
(subseq bootblock 510) #(#x55 #xaa)) ; bootblock signature
(format t "~&;; Bootblock size is ~D octets.~%" loader-length)
(format t "~&;; Bootblock build ID: ~D.~%" *bootblock-build*)
(with-open-file (s #p"bootblock-id.txt"
:direction :output
:if-exists :supersede)
(with-standard-io-syntax
(write *bootblock-build* :stream s)))
(values bootblock (append bb-symtab protected-symtab))))))