diff --git a/Makefile b/Makefile index 94aa139..8fef9c6 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,7 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.s $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(CC) $(CFLAGS) -c -o $@ $< -MBR_TEST_OBJ = obj/mbr_test.o obj/serial.o obj/itoa.o obj/util.o obj/test_disk_target.o +MBR_TEST_OBJ = obj/mbr_test.o obj/serial.o obj/itoa.o obj/util.o obj/test_disk_target.o obj/test_disk_read.o mbr_test: $(MBR_TEST_OBJ) $(DISK) $(LD) -T src/mbr_test.ld -o $(BIN_DIR)/mbr_test.bin $(MBR_TEST_OBJ) diff --git a/src/mbr_test.s b/src/mbr_test.s index 34d4c73..721f8ae 100644 --- a/src/mbr_test.s +++ b/src/mbr_test.s @@ -36,7 +36,7 @@ entry: .bootloader: call serial_init - mov $0x01, %al # disk 1 - call test_disk_params + mov $73728, %eax + call lba_to_chs .halt: jmp .halt diff --git a/src/test_disk_read.s b/src/test_disk_read.s index cb75fc1..2a9dc73 100644 --- a/src/test_disk_read.s +++ b/src/test_disk_read.s @@ -1,8 +1,72 @@ .section .text .code16 +.globl lba_to_chs .globl test_disk_read +# eax - LBA +# dh - head, ch - cylinder low 8 bits, cl - high 2 cylinder bits, 6 low bits is sector +lba_to_chs: + # we actually gonna need stack frame for that one lol + push %bp + mov %sp, %bp + sub $10, %sp + # -4(%bp) dword LBA + # -6(%bp) word cylinder + # -8(%bp) word head + # -10(%bp) word sector + mov %eax, -4(%bp) # save LBA + + # chs->head = lba / (conf->num_heads * conf->num_sectors); + # (conf->num_heads * conf->num_sectors) + movw disk_heads, %ax + mulw disk_sectors + mov %ax, %bx + # lba / + xor %dx, %dx + mov -4(%bp), %eax + div %ebx + # ax - head + mov %ax, -8(%bp) + + # chs->cylinder = (lba / conf->num_sectors) % conf->num_heads; + # (lba / conf->num_sectors) + xor %dx, %dx + movw disk_sectors, %bx + mov -4(%bp), %eax + div %ebx + # % conf->num_heads + xor %dx, %dx + divw disk_heads + # dx, as remainder, now has cylinder + mov %dx, -6(%bp) + + # chs->sector = (lba % conf->num_sectors) + 1; + xor %dx, %dx + mov -4(%bp), %eax + divw disk_sectors + # dx now has sector num, but we need to increment it + inc %dx + # dx - sector + mov %dx, -10(%bp) + + # now lets print C H S values + mov -6(%bp), %ax + mov $str_cylinder, %si + call prints_number + + mov -8(%bp), %ax + mov $str_head, %si + call prints_number + + mov -10(%bp), %ax + mov $str_sector, %si + call prints_number + + mov %bp, %sp + pop %bp + ret + # eax - LBA address # cx - number of sectors # es:di - destination @@ -14,11 +78,14 @@ test_disk_read: test_disk_taste_sector: # test pattern partition - each sector has 32 bit integer in beginning # so we read this integer and output it, it must increment as well as LBA address - lodsd # load 32 bit into eax + mov (%esi), %eax mov $str_taste, %si call prints_number ret .section .rodata -str_taste: .asciz "Sector test pattern: " +str_cylinder: .asciz "Cylinder: " +str_head: .asciz "Head: " +str_sector: .asciz "Sector: " +str_taste: .asciz "Sector test pattern: " diff --git a/src/test_disk_target.s b/src/test_disk_target.s index 05bfef5..4a93f81 100644 --- a/src/test_disk_target.s +++ b/src/test_disk_target.s @@ -1,6 +1,11 @@ # template file for disk parameters # this will target disk 1 +.globl disk_id +.globl disk_heads +.globl disk_cylinders +.globl disk_sectors + .section .rodata disk_id: .word 1 disk_heads: .word 16