Просмотр исходного кода

0.228u-boot: load loader to memory

rain 5 лет назад
Родитель
Сommit
4fc77ff551
7 измененных файлов с 201 добавлено и 6 удалено
  1. 3 0
      LINES
  2. 3 0
      Makefile
  3. 4 0
      README.md
  4. 3 3
      boot/Makefile
  5. 100 3
      boot/boot.asm
  6. 65 0
      boot/loader.asm
  7. 23 0
      docs/How can we use BIOS.txt

+ 3 - 0
LINES

@@ -0,0 +1,3 @@
+2021-5-21 0.066: 66 lines, 1 files, rain
+2021-5-29 0.228: 228 lines, 2 files, rain
+

+ 3 - 0
Makefile

@@ -15,6 +15,9 @@ lines:
 
 
 
 
 clear: 
 clear: 
+	make -C boot/ clear
 	rm $(OBJS)
 	rm $(OBJS)
+	rm xnix.img
+
 
 
 
 

+ 4 - 0
README.md

@@ -14,3 +14,7 @@ source software, you can get its source code from
     see 'How XNIX and other operating systems startup' if you are 
     see 'How XNIX and other operating systems startup' if you are 
     interested in it. 
     interested in it. 
 
 
+
+0.228u-boot:  
+    update `boot.asm`: it reads the second sector of boot-driver (loader) and jumps to it. 
+    `loader.asm` puts a message 'Starting Loader' on screen, and die. 

+ 3 - 3
boot/Makefile

@@ -1,8 +1,8 @@
-all: bootsect
+all: bootsect 
 
 
 
 
-bootsect: boot.bin
-	cat boot.bin > bootsect
+bootsect: boot.bin loader.bin
+	cat boot.bin loader.bin > bootsect
 
 
 
 
 %.bin: %.asm
 %.bin: %.asm

+ 100 - 3
boot/boot.asm

@@ -19,6 +19,11 @@
 
 
 org 0x7c00
 org 0x7c00
 
 
+
+; yeah, it's really 0x7c00 because I don't want to use other memory. 
+
+BOOTDRIVER equ 0x7c00
+
 _start:
 _start:
 	xor ax, ax
 	xor ax, ax
 
 
@@ -28,14 +33,27 @@ _start:
 	mov ss, ax
 	mov ss, ax
 	mov sp, 0x7c00
 	mov sp, 0x7c00
 
 
+	; save startup driver
+	mov [BOOTDRIVER], dl
+
 	; display message 'Loading System'
 	; display message 'Loading System'
 
 
 	mov si, boot_msg
 	mov si, boot_msg
 	call print
 	call print
 
 
-die:
+	mov ax, 1
+	mov cl, 1
+
+	mov bx, 0x7e00
+
+	call read_sector
+
+	jmp 0x0000:0x7e00
+
+
+.die:
 	hlt
 	hlt
-	jmp die
+	jmp .die
 
 
 
 
 
 
@@ -57,7 +75,86 @@ print:
 
 
 
 
 
 
-boot_msg: db "Loading System", 0x0d, 0x0a, 0x00
+read_sector:
+	; LBA address in AX, and sectors count in CL, buffer in ES:BX
+	
+	; LBA to CHS: 
+	; TEMP     = LBA / SECTORS_PER_TRACK
+	; SECTOR   = (LBA % SECTORS_PER_TRACK) + 1
+	; CYLINDER = TEMP >> 1
+	; HEAD     = TEMP & 1
+
+	; DIV: unsigned divide
+	; 12345 / 123: 
+	; mov ax, 12345		: dividend
+	; mov bl, 123		: divisor
+	; AL: quotient
+	; AH: remainder
+
+	; 18: sectors per track for 1.44M floppy (and for my u-disk)
+
+	; save bx first
+	push bx
+	
+	mov bl, 18
+	div bl
+
+	; BIOS standard interrupt: 0x13 AH = 0x02: read disk in CHS mode
+	; AL: sectors count
+	; CH: cylinder number low
+	; CL: 0-5b: sector number, 6-7b: cylinder number (disk only)
+	; DH: head
+	; DL: driver
+	; ES:BX: buffer
+
+	; SECTOR = AH + 1
+	; but CL used to save sectors count, so we save it first. 
+	push cx
+
+	; we inc ah because it's shorter than inc cl
+
+	inc ah
+	mov cl, ah
+
+	; CH: CYLINDER = (LBA / SECTORS_PER_TRACK) >> 1
+
+
+	mov dh, al
+	and dh, 1
+
+	shr al, 1
+	mov ch, al
+
+	; for 'push cx'
+	pop ax
+
+	; for 'push bx'
+	pop bx
+	
+	mov dl, [BOOTDRIVER]
+	
+	; okay, now we read it
+
+	mov ah, 2
+	int 0x13
+
+	jc read_failed
+
+	ret
+
+
+read_failed:
+	mov si, readdisk_fail_msg
+	call print
+
+.die:	hlt
+	jmp .die
+
+	; it never go here
+
+
+boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
+readdisk_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
 
 
 
 
 
 

+ 65 - 0
boot/loader.asm

@@ -0,0 +1,65 @@
+; Copyright (C) 2021 Rain
+
+; This file is part of XNIX. 
+; XNIX is free software: you can redistribute it and/or modify 
+; it under the terms of the GNU General Public License as published by 
+; the Free Software Foundation, either version 3 of the License, or 
+; (at your option) and later version. 
+
+; XNIX is distributed in the hope that it will be useful, 
+; but WITHOUT ANY WARRANTY; without even the implied warranty of 
+; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+; GNU General Public License for more details. 
+
+; You should have received a copy of the GNU General Public License 
+; along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+
+
+
+
+org 0x7e00
+
+
+_start:
+	xor ax, ax
+
+	mov ds, ax
+	mov es, ax
+
+	mov ss, ax
+	mov sp, 0x7c00
+
+	; display message 'Starting Loader...'
+
+	mov si, message
+	call print
+
+.die:
+	hlt
+	jmp .die
+
+
+
+print:
+	cld
+	lodsb
+
+	test al,al
+	jz .ret
+
+	mov ah, 0x0e
+	mov bx, 0x0f
+	int 0x10
+
+	jmp print
+
+.ret:
+	ret
+
+
+message: db "Starting Loader", 0x0d, 0x0a, 0x00
+
+
+times 510 - ($ - $$) db 0x00
+db 0x55, 0xaa
+

+ 23 - 0
docs/How can we use BIOS.txt

@@ -0,0 +1,23 @@
+If you have been read boot.asm, you must know it reads loader from disk and jump to it. 
+But, how can we read disk without a standard IDE driver? 
+
+You can know how computers startup after you read 'How XNIX and other operating systems startup': CPU will execute BIOS in ROM. And BIOS will initialize all devices it can find. Then BIOS jump to our MBR (boot.asm in XNIX). 
+
+But BIOS did another work before it jumps to MBR: initial Onterrupt Vectors Table (IVT, Interrupt Descriptor Table in protect-mode). Word 'interrupts' in OS world often means 'traps'. 
+When your program makes a mistake (for example, divide by zero), CPU will raise a trap (or interrupt). Then OS should do something (kill your program). The OS uses a function to 'do something', it should regist in IDT. 
+
+And almost devices will raise an interrupt when it finished its work. The OSes will accept it (like clock, mouse, keyboard etc. )
+
+So IDT uses to save trap handler's address (in fact, it saves other things in protect-mode or long-mode). CPU will read from IDT when a trap was raised. And call trap handler if it's avaliable. 
+
+We know we should use a driver to read / write anything (but without registers and memory). BIOS did it for us. So it's really Basic Input & Output System. Now many BIOSes (EFI BIOS) support network stack! We can use network in BIOS. 
+
+But you should know a good OS can't use BIOS all the time because it's not cool (but DOS used all the time :-)). You should implement by yourself. 
+
+Okay, it's time to write codes. Goodbye! 
+
+ASK ME AT BLOG.CSDN.NET (https://blog.csdn.net/utf80/)! 
+
+
+driver: a program uses to read/write your devices (like your disk)
+