diff --git a/x86_64/bootsector.asm b/x86_64/bootsector.asm index 8ae4db3d00d547b4a68fc80db20a78d92543209d..506e569c70342773c4c20bfc29f53d3817f195d5 100644 --- a/x86_64/bootsector.asm +++ b/x86_64/bootsector.asm @@ -150,6 +150,7 @@ DAPACK: .seg: dw 0 ; in memory page zero .addr: dq 0 ; put the lba to read in this spot -times 510-($-$$) db 0 +times 446-($-$$) db 0 +partitions: times 4 * 16 db 0 db 0x55 db 0xaa diff --git a/x86_64/disk.asm b/x86_64/disk.asm index 1ef4cc36c957febb3a356be14a8a0d4005d78051..a47454ac62625866d0792f42d85d0d36a15578e1 100644 --- a/x86_64/disk.asm +++ b/x86_64/disk.asm @@ -22,12 +22,10 @@ startup_end: %else align BLOCK_SIZE, db 0 %ifdef FILESYSTEM - filesystem: - %defstr FILESYSTEM_STR %[FILESYSTEM] - incbin FILESYSTEM_STR - .end: - align BLOCK_SIZE, db 0 - %else - filesystem: + filesystem: + %defstr FILESYSTEM_STR %[FILESYSTEM] + incbin FILESYSTEM_STR + align BLOCK_SIZE, db 0 + .end: %endif %endif diff --git a/x86_64/partitions.asm b/x86_64/partitions.asm new file mode 100644 index 0000000000000000000000000000000000000000..4fbede1edf06ae466d5ae0d7a29a54b02683e402 --- /dev/null +++ b/x86_64/partitions.asm @@ -0,0 +1,38 @@ +struc mbr_partition_rec +.sys: resb 1 +.chs_start: resb 3 +.ty: resb 1 +.chs_end: resb 3 +.lba_start: resd 1 +.sector_count: resd 1 +endstruc + +; Find a partition to load RedoxFS from. +; The partition has to be one of the primary MBR partitions. +; OUT +; eax - start_lba +; edx - sector count +; CLOBBER +; ebx +find_redoxfs_partition: + xor ebx, ebx +.loop: + mov al, byte [partitions + mbr_partition_rec + mbr_partition_rec.ty] + cmp al, 0x83 + je .found + add ebx, 1 + cmp ebx, 4 + jb .loop + jmp .notfound +.found: + mov eax, [partitions + mbr_partition_rec + mbr_partition_rec.lba_start] + mov edx, [partitions + mbr_partition_rec + mbr_partition_rec.sector_count] + ret +.notfound: + mov si, .no_partition_found_msg + call print +.halt: + cli + hlt + jmp .halt +.no_partition_found_msg: db "No MBR partition with type 0x83 found", 0xA, 0xD, 0 diff --git a/x86_64/redoxfs.asm b/x86_64/redoxfs.asm index 8b04b6656cd734f7e2539c9fdc03ce679615df0f..8dec21e4040108feae148a2d0e4fe2efedfa6eb8 100644 --- a/x86_64/redoxfs.asm +++ b/x86_64/redoxfs.asm @@ -37,7 +37,12 @@ struc Header .padding: resb (BLOCK_SIZE - 56) endstruc +; IN +; eax - the first sector of the filesystem +; edx - the amount of sectors in the filesystem redoxfs: + mov [.first_sector], eax + mov [.sector_count], ebx call redoxfs.open test eax, eax jz .good_header @@ -53,7 +58,7 @@ redoxfs: ; node in eax, buffer in bx .node: shl eax, (BLOCK_SHIFT - 9) - add eax, (filesystem - boot)/512 + add eax, [redoxfs.first_sector] mov cx, (BLOCK_SIZE/512) mov dx, 0 call load @@ -71,6 +76,9 @@ redoxfs: .file: times BLOCK_SIZE db 0 + .first_sector: dd 0 + .sector_count: dd 0 + .env: db "REDOXFS_BLOCK=" .env.block: @@ -111,7 +119,15 @@ redoxfs.open: mov al, ' ' call print_char - mov ebx, (filesystem - boot)/BLOCK_SIZE + push eax + push edx + xor edx, edx + mov eax, [redoxfs.first_sector] + mov ebx, (BLOCK_SIZE / 512) + div ebx ; EDX:EAX = EDX:EAX / EBX + mov ebx, eax + pop edx + pop eax mov di, redoxfs.env.block_end - 1 .block: mov al, bl @@ -314,7 +330,7 @@ redoxfs.kernel: shl eax, (BLOCK_SHIFT - 9) - add eax, (filesystem - boot)/512 + add eax, [redoxfs.first_sector] add ecx, BLOCK_SIZE dec ecx shr ecx, 9 diff --git a/x86_64/startup-common.asm b/x86_64/startup-common.asm index b12978df8907bead6447f4f4ba3934e4a78791b8..147001dd0822c54f3409f5dea3b1cf2f441f23e0 100644 --- a/x86_64/startup-common.asm +++ b/x86_64/startup-common.asm @@ -25,6 +25,14 @@ startup: shr ecx, 9 call load_extent %else + + %ifdef FILESYSTEM + mov eax, (filesystem - boot) / 512 + mov ebx, (filesystem.end - filesystem) / 512 + %else + call find_redoxfs_partition + %endif + call redoxfs test eax, eax jnz error @@ -50,10 +58,10 @@ startup: jmp startup_arch -# load a disk extent into high memory -# eax - sector address -# ecx - sector count -# edi - destination +; load a disk extent into high memory +; eax - sector address +; ecx - sector count +; edi - destination load_extent: ; loading kernel to 1MiB ; move part of kernel to startup_end via bootsector#load and then copy it up @@ -132,6 +140,9 @@ load_extent: %include "initialize.asm" %ifndef KERNEL %include "redoxfs.asm" + %ifndef FILESYSTEM + %include "partitions.asm" + %endif %endif init_fpu_msg: db "Init FPU",13,10,0