Przeglądaj źródła

7.110u-boot: find 'ksetup' in the root-directory

rain 5 lat temu
rodzic
commit
9adbd218aa
4 zmienionych plików z 150 dodań i 108 usunięć
  1. 1 0
      LINES
  2. 4 0
      README.md
  3. 5 107
      boot/boot.asm
  4. 140 1
      boot/loader.asm

+ 1 - 0
LINES

@@ -24,4 +24,5 @@
 2021-7-26 5.039: 5039 lines, 41 files, rain
 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
 

+ 4 - 0
README.md

@@ -102,3 +102,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &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). 
 
+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. 
+

+ 5 - 107
boot/boot.asm

@@ -48,7 +48,7 @@ _start:
 	mov si, boot_msg
 	call print
 
-	call read_it
+	call read_sb
 
 	; get e820 memory
 
@@ -104,9 +104,9 @@ print:
 	ret
 
 
-; NOTE: QEMU's HD only supports LBA mode. So this MBR can't load it successfully. :-)
+; load the first block (4096 bytes)
 
-read_it:
+read_sb:
 	; 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
@@ -118,109 +118,8 @@ read_it:
 
 	call read_lba
 	
-	jnc using_lba
-
-	; so we use CHS to read now. 
-
-	mov ax, 0x07e0
-	mov es, ax
-
-	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
-
-
-	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
 
-	popaw
-
-	ret
-
-
 using_lba:
 	; put message on screen
 	mov si, lamsg
@@ -249,10 +148,9 @@ using_lba:
 
 	call read_lba
 	
-	; 1024 sectors = 512 KB, 
-	; I think this is enough anytime for our kernel. 
+	; 8 sectors = 4KB
 
-	cmp ax, 1024
+	cmp ax, 8
 	jb .read_loop
 
 	ret

+ 140 - 1
boot/loader.asm

@@ -45,6 +45,8 @@ _start:
 	mov di, msg
 	call print
 
+	call load_ksetup
+
 .die:	jmp .die
 
 .set_vga:
@@ -127,6 +129,138 @@ print:
 	ret
 
 
+MAGIC		equ 	0
+NINODES		equ	4
+NBLOCKS 	equ 	8
+
+NB_BBM 		equ 	12
+NB_IBM 		equ 	16
+
+BBM_BN 		equ 	20
+IBM_BN 		equ 	24
+
+NB_BIM 		equ 	28
+NB_INODES 	equ 	32
+
+BIM_BN 		equ 	36
+INODES_BN 	equ 	40
+
+ROOT_INO 	equ 	44
+INODE_SIZE 	equ 	32
+
+FIRSTDATABLOCK	equ 	76
+NEXT_INODE	equ 	80
+
+ZERO 		equ 84
+
+DENTRY_NAME 	equ 4
+
+
+load_ksetup:
+	; find 'ksetup' in the root-directory, and then read it to 0x8000
+	; get the inode of 'ksetup', then we can find in bim. 
+
+	; the first block of root-inode must be first-data-block
+	mov eax, [0x7e00 + FIRSTDATABLOCK]
+
+	; the fs buffer
+	mov bx, 0x5000
+
+	call read_block
+
+	; and start find in this directory
+	mov si, 0x5000 + DENTRY_NAME
+	mov di, fname
+	mov cx, 6
+
+.cmp_loop:
+	repe
+	cmpsb
+
+	jz found
+
+	; not found, continue
+	add si, 128
+	cmp si, 0x6000
+	
+	je not_found
+	jmp .cmp_loop
+
+found:
+	mov di, found_msg
+	call print
+
+.die:	hlt
+	jmp .die
+
+not_found:
+	mov di, not_found_msg
+	call print
+
+.die:	hlt
+	jmp .die
+
+read_block:
+	push ax
+	push cx
+
+	; 8 sectors = 4 KB = 1 block 
+	mov cx, 8
+	shl eax, 3
+
+.loop:
+	call read_lba
+	jc read_failed
+
+	inc eax
+	dec cx
+
+	add bx, 512
+
+	cmp cx, 0
+	jne .loop
+
+	pop cx
+	pop ax
+
+	ret
+
+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
+
+read_failed:
+	mov di, read_failed_msg
+	call print
+
+.die:	hlt
+	jmp die
+
+
 bits 32
 
 init_prot:
@@ -240,7 +374,12 @@ init_prot:
 	
 	jmp code64:0x8000
 
-msg: db "welcome to XNIX", 0x0d, 0x0a, 0x00
+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
 
 alignb 16