Explorar el Código

7.301u-boot: loader reads 'ksetup' from the boot-device, and jump to it

rain hace 5 años
padre
commit
00b72e3177
Se han modificado 4 ficheros con 222 adiciones y 24 borrados
  1. 1 0
      LINES
  2. 11 5
      README.md
  3. 209 18
      boot/loader.asm
  4. 1 1
      kernel.lds

+ 1 - 0
LINES

@@ -25,4 +25,5 @@
 2021-7-28 5.063: 5063 lines, 41 files, rain
 2021-8-04 7.073: 7073 lines, 50 files, rain
 2021-8-04 7.110: 7110 lines, 50 files, rain
+2021-8-08 7.301: 7301 lines, 50 files, rain
 

+ 11 - 5
README.md

@@ -98,11 +98,17 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;using more colors (very beautiful! ) :-)
 
 7.073u-boot:  
-&nbsp;&nbsp;&nbsp;&nbsp;using CFS to be start-up device, not a binary device (without any file systems). 
-&nbsp;&nbsp;&nbsp;&nbsp;now cfs can creat/lsdir/mkdir/rw (but only starts at the start of file). 
-&nbsp;&nbsp;&nbsp;&nbsp;and boot jumps to 0x7e60 (0x7e00 + sizeof(struct superblock)), not 0x7e00 (the second sector). 
+&nbsp;&nbsp;&nbsp;&nbsp;using CFS to be start-up device, not a binary device (without any file systems). <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;now cfs can creat/lsdir/mkdir/rw (but only starts at the start of file). <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;and boot jumps to 0x7e60 (0x7e00 + sizeof(struct superblock)), not 0x7e00 (the second sector). <br/>
 
 7.110u-boot:  
-&nbsp;&nbsp;&nbsp;&nbsp;loader uses assembly code to read the root-directory, and search the file 'ksetup'. 
-&nbsp;&nbsp;&nbsp;&nbsp;and we just search in the first block of the root-directory. 
+&nbsp;&nbsp;&nbsp;&nbsp;loader uses assembly code to read the root-directory, and search the file 'ksetup'. <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;and we just search in the first block of the root-directory. <br/>
+
+7.301u-boot:  
+&nbsp;&nbsp;&nbsp;&nbsp;now loader reads the 'ksetup' to 0x9000 (not 0x8000 in the past), and jump to it. <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;it works on my 16G disk (real-machine) and under, but failed on the 1TB disk (real and virtual machine). <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;maybe that is too large? <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;and, main didn't work on the real-machine. :-(
 

+ 209 - 18
boot/loader.asm

@@ -47,8 +47,6 @@ _start:
 
 	call load_ksetup
 
-.die:	jmp .die
-
 .set_vga:
 	; get mode info
 	mov ax, 0x4f01
@@ -57,7 +55,7 @@ _start:
 
 	int 0x10
 
-die:	jc die
+	jc die
 
 	mov al,[es:di]
 	mov [VMODE], al
@@ -151,9 +149,9 @@ INODE_SIZE 	equ 	32
 FIRSTDATABLOCK	equ 	76
 NEXT_INODE	equ 	80
 
-ZERO 		equ 84
+ZERO 		equ 	84
 
-DENTRY_NAME 	equ 4
+DENTRY_NAME 	equ 	4
 
 
 load_ksetup:
@@ -183,22 +181,181 @@ load_ksetup:
 	add si, 128
 	cmp si, 0x6000
 	
-	je not_found
+	jae load_failed
 	jmp .cmp_loop
 
 found:
 	mov di, found_msg
 	call print
 
-.die:	hlt
-	jmp .die
+.get_inode:
+	; store its inode number in te eax
+	; 6: the length of file name
+
+	mov ecx, [si - 6 - DENTRY_NAME]
+
+	; and then read this inode (only read the first block of inodes)
+	mov eax, [0x7e00 + INODES_BN]
+	mov bx, 0x5000
+
+	call read_block
+
+	; back up the inode nr, for .search_bim
+	mov edx, ecx
+	
+	; * sizeof(struct inode)
+	shl cx, 5
+
+	mov si, 0x5000
+	add si, cx
+
+	; and get its size
+
+INO_INODE 	equ 	0
+INO_PINODE 	equ 	4
+INO_ACCESS 	equ 	8
+INO_BLOCKSNR 	equ 	9
+INO_UPSIZE 	equ 	13
+INO_CTIME 	equ 	17
+INO_WTIME 	equ 	21
+INO_NLINKS 	equ 	25
+INO_UID 	equ 	26
+INO_GID 	equ 	28
+INO_EACCESS 	equ 	30
+
+	mov eax, [si + INO_BLOCKSNR]
+
+	cmp dword [si + INO_UPSIZE], 0
+
+	; upsize is zero, so eax is blocks we need. 
+	je .read_ksetup
+
+	; okay, this is enough
+	inc eax
+
+.read_ksetup:
+	; find in the bim (only first block, too)
+	; first read the first block of bim to 0x5000
+
+	; blocks need is in the eax, save it 
+
+BIM_INODE 	equ 	0
+BIM_THBLOCK	equ 	4
+
+	mov ecx, eax
+
+	mov eax, [0x7e00 + BIM_BN]
+	mov bx, 0x5000
+
+	call read_block
+
+	; and start search
+	mov si, 0x5000
+
+	mov eax, ecx
+	xor ecx, ecx
+	; the to address
+
+	; and this is the address of kernel
+	xor bx, bx
+
+	push eax
+
+	mov ax, 0x0900
+	mov es, ax
+
+	pop eax
+
+.search_bim:
+	cmp ecx, eax
+
+	; ecx is the blocks we loaded (also bim->th_block), eax is the blocks of this file
+	je .load_done
+
+	; the inode of this file is edx
+	cmp edx, [si]
+
+	; not match, search again
+	jne .search_again
+
+	cmp ecx, [si + BIM_THBLOCK]
+	jne .search_again
+
+
+.load_file:
+	; only one block, and coutinue search
+	; the block is the index of si (base is 0x5000), [si] is the inode
+
+	; not this!!! loader must uses 32-bit registers, so we should save/restore 
+	; them in 32-bits
+	; pushaw
+
+	push eax
+	push ecx
+	push edx
+	push ebx
+
+	push esp
+	push ebp
+	push esi
+	push edi
+
+	; first get the block nr
+	xor eax, eax
+	mov ax, si
+
+	; - base
+	sub eax, 0x5000
+
+	; / 8, get the index
+	shr eax, 3
+
+	; and += first_data_block, because bim start at this. 
+	add eax, [0x7e00 + FIRSTDATABLOCK]
+
+	; now eax is the real block nr of this file
+
+	; read it! 
+	; bx is the address, we don't need change
+	call read_block
+
+	pop edi
+	pop esi
+	pop ebp
+	pop esp
+
+	pop ebx
+	pop edx
+	pop ecx
+	pop eax
+
+	add bx, 4096
+	inc ecx
+
+	add si, 8
+
+	jmp .search_bim
+
+.search_again:
+	; the size of a bim entry
+	add si, 8
+	cmp si, 0x6000
+
+	jae load_failed
+	jmp .search_bim
+
+.load_done:
+	mov di, load_done_msg
+	call print
+
+	ret
 
-not_found:
-	mov di, not_found_msg
+load_failed:
+	mov di, load_failed_msg
 	call print
 
-.die:	hlt
-	jmp .die
+die:	hlt
+	jmp die
 
 read_block:
 	push ax
@@ -210,13 +367,16 @@ read_block:
 
 .loop:
 	call read_lba
-	jc read_failed
 
 	inc eax
 	dec cx
 
 	add bx, 512
+	cmp bx, 0
+
+	je .go_on_es
 
+.cont:
 	cmp cx, 0
 	jne .loop
 
@@ -225,8 +385,29 @@ read_block:
 
 	ret
 
+.go_on_es:
+	; update the es, and continue
+	push ax
+
+	mov ax, es
+	add ax, 512 / 16
+	mov es, ax
+
+	pop ax
+
+	jmp .cont
+
+
 read_lba:
-	pushaw
+	push eax
+	push ecx
+	push edx
+	push ebx
+
+	push esp
+	push ebp
+	push esi
+	push edi
 
 	push dword 0x0000
 	push dword eax
@@ -249,7 +430,17 @@ read_lba:
 
 	add sp, 16
 	
-	popaw
+	pop edi
+	pop esi
+	pop ebp
+	pop esp
+
+	pop ebx
+	pop edx
+	pop ecx
+	pop eax
+
+	jc read_failed
 
 	ret
 
@@ -257,7 +448,6 @@ read_failed:
 	mov di, read_failed_msg
 	call print
 
-.die:	hlt
 	jmp die
 
 
@@ -372,14 +562,15 @@ init_prot:
 
 	; okay! now set segment registers and init long mode
 	
-	jmp code64:0x8000
+	jmp code64:0x9000
 
 msg: db "welcome to XNIX Loader. ", 0x0d, 0x0a, 0x00
 read_failed_msg: db "fatal: read disk failed. ", 0x0d, 0x0a, 0x00
 fname: db "ksetup", 0x00
 
 found_msg: db "found ksetup in the root. ", 0x0d, 0x0a, 0x00
-not_found_msg: db "ksetup not found. ", 0x0d, 0x0a, 0x00
+load_failed_msg: db "failed when trying to load ksetup. ", 0x0d, 0x0a, 0x00
+load_done_msg: db "load ksetup done. ", 0x0d, 0x0a, 0x00
 
 alignb 16
 

+ 1 - 1
kernel.lds

@@ -3,7 +3,7 @@ OUTPUT_ARCH(i386:x86-64)
 ENTRY(_start)
 
 SECTIONS {
-	. = 0x8000;
+	. = 0x9000;
 	.text : {
 		_text = .;
 		*(.text)