瀏覽代碼

2.154u-boot: boot can run on my computer (but not floppy) now

rain 5 年之前
父節點
當前提交
8607fd4eef
共有 4 個文件被更改,包括 163 次插入 和 40 次删除
  1. 1 0
      LINES
  2. 3 0
      README.md
  3. 158 39
      boot/boot.asm
  4. 1 1
      init/main.c

+ 1 - 0
LINES

@@ -11,4 +11,5 @@
 2021-6-15 2.156: 2156 lines, 21 files, rain
 2021-6-20 2.040: 2040 lines, 25 files, rain
 2021-7-04 2.035: 2035 lines, 25 files, rain
+2021-7-04 2.154: 2154 lines, 25 files, rain
 

+ 3 - 0
README.md

@@ -59,3 +59,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 2.035u-init:  
 &nbsp;&nbsp;&nbsp;&nbsp;`fd` also need a `inode_descriptor`, but we forgot it. update it again. 
 
+2.154u-boot:  
+&nbsp;&nbsp;&nbsp;&nbsp;now this boot routine can run on my computer successfully (16GB u-disk). but it can't run on old-PCs which uses floppy. 
+

+ 158 - 39
boot/boot.asm

@@ -73,64 +73,153 @@ print:
 ; NOTE: QEMU's HD only supports LBA mode. So this MBR can't load it successfully. :-)
 
 read_it:
-	; BIOS 0x13, AH = 0x02: read disk in CHS mode
-	; AL: sectors will read
-	; CH: cylinder
-	; CL: sector start
-	; DH: head
-	; DL: driver
-	; BX: buffer address (segment register ES)
+	; we try LBA first because some disks supports wrong CHS. (bad sector per track)
+	; if this boot disk doesn't support LBA mode, it should be a floppy. 
+	mov ax, 0x07e0
+	mov es, ax
+
+	mov ax, 1
+	mov cl, 1
+	xor bx, bx
+
+	call read_lba
+	
+	jnc using_lba
+
+	; so we use CHS to read now. 
 
-	; segment address
 	mov ax, 0x07e0
 	mov es, ax
 
-	mov ch, 0
-	mov dh, 0
+	xor ax, ax
+
+.read_it:
+	xor bx, bx
+
+	inc ax
+	mov cl, 1
+
+	push ax
+	push bx
+
+	call read_chs
+
+	pop bx
+	pop ax
+
+	mov bx, es
+	add bx, 512 / 16
+	mov es, bx
+
+	cmp ax, 1024
+
+	jb .read_it
+
+read_chs:
+	pushaw
+
+	; 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
+
 
-	; sector 1 is MBR (this code)
-	mov cl, 2
+	mov dh, al
+	and dh, 1
 
-.retry:
-	mov ah, 0x02
+	shr al, 1
+	mov ch, al
 
-	; only read one sectors
-	mov al, 1
-	mov bx, 0x0000
+	; 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
 
-.next_sector:
-	mov ax, es
-	; add 512 for memory address
-	add ax, 512 / 16
+	popaw
+
+	ret
+
+
+using_lba:
+	; put message on screen
+	mov si, lamsg
+	call print
+
+	; the first sector (LBA address starts at 0) read, 
+	; so we read start at address 1
+	
+	; and we should add address
+
+	mov ax, 0x07e0
 	mov es, ax
 
-	; sector++
-	add cl, 1
-	; if sector > 18
-	cmp cl, 18
-	jbe .retry
+	mov ax, 1
+
+.read_loop:
+	mov bx, es
+	add bx, 512 / 16
+	mov es, bx
+
+	inc ax
 
-	; next head
 	mov cl, 1
-	; head++
-	add dh, 1
-	; if head == 2 (head can only be 0 or 1)
-	cmp dh, 2
-	jb .retry
-
-	; next cylinder
-	mov dh, 0
-	add ch, 1
+
+	xor bx, bx
+
+	call read_lba
 	
-	; if cylinder == CYLINDERS_NEED
-	cmp ch, cyldsneed
-	jb .retry
+	; 1024 sectors = 512 KB, 
+	; I think this is enough anytime for our kernel. 
+
+	cmp ax, 1024
+	jb .read_loop
 
 	ret
 
@@ -143,8 +232,38 @@ read_failed:
 	jmp .die
 
 
+read_lba:
+	pushaw
+
+	push dword 0x0000
+	push dword eax
+
+	; buffer address
+	push word es
+	push word bx
+
+	; sectors count
+	push word cx
+
+	; package size
+	push word 16
+
+	mov ah, 0x42
+	mov dl, [BOOTDRIVER]
+	mov si, sp
+
+	int 0x13
+
+	add sp, 16
+	
+	popaw
+
+	ret
+
+
 boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
 read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
+lamsg: db "LBA mode is avaliable. ", 0x0d, 0x0a, 0x00
 
 
 times 510 - ($ - $$) db 0x00

+ 1 - 1
init/main.c

@@ -45,7 +45,7 @@
 int init(void) {
 	init_int();
 
-	print(0x0f, 0x00, "XNIX KERNEL IS STARTING...\nHELLO, WORLD");
+	print(0x0f, 0x00, "XNIX kernel is starting...\ninitial done. \n");
 
 	return 0;
 }