From 2a95f22a7c8dfd246e9693d58f5f022ac5be19a1 Mon Sep 17 00:00:00 2001 From: mothcompute Date: Sun, 6 Aug 2023 21:35:05 -0700 Subject: [PATCH] initial fs code, readme and gitignore changes, etc --- .gitignore | 1 + README.md | 2 ++ doc/README.md | 7 +++++ src/disk.s | 5 ++-- src/int21/2.s | 2 ++ tool/build | 9 ++++++ tool/inc/fs.h | 58 +++++++++++++++++++++++++++++++++++++++ tool/mfs_hdr.s | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ tool/mkmothfs.c | 10 +++++++ 9 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 doc/README.md create mode 100755 tool/build create mode 100644 tool/inc/fs.h create mode 100644 tool/mfs_hdr.s create mode 100644 tool/mkmothfs.c diff --git a/.gitignore b/.gitignore index 36622de..310448a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ obj/ foxdos kernel.map +*.swp diff --git a/README.md b/README.md index 296ca88..825dc10 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ # foxdos + +WIP MS-DOS reimplementation for 386+ machines diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 0000000..7e4ffe5 --- /dev/null +++ b/doc/README.md @@ -0,0 +1,7 @@ +# foxdos docs + +most of the documentation exists in the form of code comments at the moment + +## mothfs + +`tool/inc/fs.h` contains the majority of documentation for the fs. eventually one of us will probably write a more readable version in md or html diff --git a/src/disk.s b/src/disk.s index b4f204f..b65fc62 100644 --- a/src/disk.s +++ b/src/disk.s @@ -32,10 +32,11 @@ read_sector: push bx push es .retry: - mov es, SECTOR_SEG + mov bx, SECTOR_SEG + mov es, bx mov bx, SECTOR_ADDR call lba2chs - mov ax 0x0201 + mov ax, 0x0201 int 0x13 jc .retry pop es diff --git a/src/int21/2.s b/src/int21/2.s index 4304687..f7357b4 100644 --- a/src/int21/2.s +++ b/src/int21/2.s @@ -12,6 +12,8 @@ setint: xor ah, ah shl ax, 2 mov di, ax + mov ax, word [callerds] + mov ds, ax xor ax, ax push es mov es, ax diff --git a/tool/build b/tool/build new file mode 100755 index 0000000..ef269f1 --- /dev/null +++ b/tool/build @@ -0,0 +1,9 @@ +for i in xxd; do which $i || exit; done + +CC='cc -O3 -Iinc' + +for i in bin2h; do [ ! -f "$i" ] && $CC "$i.c" -o "$i"; done + +[ ! -f mkmothfs ] && nasm mfs_hdr.s && \ + xxd -i mfs_hdr > inc/mfs_hdr.h && \ + $CC mkmothfs.c diff --git a/tool/inc/fs.h b/tool/inc/fs.h new file mode 100644 index 0000000..1732188 --- /dev/null +++ b/tool/inc/fs.h @@ -0,0 +1,58 @@ +typedef struct { + uint32_t jmp; // used to jump past the header + uint8_t magic[7]; // 'MOTHFS\0' + uint8_t version; // currently fs version 0 + uint32_t size; // partition size in sectors - sectors always 512b + // this means the max size is 'only' 2tb but i would + // be surprised if anyone managed to use 2tb in dos + + uint32_t offset; // offset of partition from start of disk + uint8_t volname[15]; // name of partition + uint8_t reserved; // number of sectors reserved for boot code. these + // are stored immediately following this header + + uint32_t abm; // number of sectors reserved for the allocation + // bitmap. these immediately follow the boot code + // sectors. the root directory is stored in the first + // non-reserved sector + + uint8_t bootcode[470]; + uint16_t signature; // 0xAA55 as usual to make it directly bootable +} mothfs_header; // 512 bytes + +typedef struct { + uint32_t sector; // sector offset relative to the partition, not the disk + uint32_t length; // number of consecutive sectors to address +} ptrlen; // 8 bytes + +typedef struct { + uint8_t type; // 0 - file + // 1 - directory + + uint8_t perms[3]; // unused in foxdos. good for alignment. top bit of + // each nibble must be zero + + uint32_t ext; // undefined in version 0 - contains uid, gid, data + // for other file types, long filename, etc + + uint8_t name[8]; // filename + uint8_t ext[3]; // extension + uint8_t lfn; // size of long filename - must be zero in version 0 + // this is on par with most filesystems + + uint16_t freebytes; // number of bytes unused in last sector of file data + uint16_t pad; + + uint32_t data[122]; + + // for files - `data` is 61 `ptrlen`s. if not all 60 are used, it must be null + // terminated by setting ptrlen.sector to zero. if there is no terminator, + // data[60].sector points to a sector containing 64 more `ptrlen`s. if there is + // no terminator in that sector, the last `ptrlen` points to another sector of + // identical layout. this continues until a sector with a terminator is found + + // for directories - `data` is 122 sector offsets. if not all 121 are used, it + // must be null terminated. if there is no terminator, data[121] points to a + // sector containing 128 more sector offsets. the rules for this are similar + // enough to how files work that i do not want to write it out again +} mothfs_file; // 512 bytes diff --git a/tool/mfs_hdr.s b/tool/mfs_hdr.s new file mode 100644 index 0000000..7ed9c21 --- /dev/null +++ b/tool/mfs_hdr.s @@ -0,0 +1,73 @@ +; TODO implement boot via chs for old bioses (even ones +; as new as 1996 lack support for lba via bios call) +[bits 16] +[org 0x7C00] + +jmp entry +times 4 - ($-$$) db 0 + +db "MOTHFS",0 +db 0 +sz: dd 32768 ; 32768 * 512 = 16mb disk image +off: dd 1 ; immediately follows mbr + db "mkmothfs",0,0,0,0,0,0,0 +rsv: db 127 ; 64kb of boot code +abm: dd 4096 ; 16mb/(512*8) + +entry: + push cs + pop ds + push dx + + ; enable a20 a few different ways + in al, 0xEE + + ; bios call + mov ax, 0x2401 ; l is real + int 0x15 + + ; fast a20 gate + in al, 0x92 + or al, 2 + out 0x92, al + + mov eax, [off] + mov dword [dapw], eax + + ; check for lba support + mov ah, 0x41 + mov bx, 0x55AA + int 0x13 ; dl is drive number - mbr should pass this value + jc .nde + shr cx, 2 ; wikipedia says bit 1 is disk packet support + jnc .nde + + ; load kernel flat binary into hma using lba + pop dx + mov ah, 0x42 + mov si, dap + int 0x13 + + ; jump to FFFF:0010, the start of HMA. this limits the kernel + ; size to 65520 bytes but we are actually loading 496 bytes + ; less than that and even that should be more than enough for + ; foxdos and any more would probably blow up some garbage bios + mov ax, 0xFFFF + push ax + mov ax, 16 + push ax + retf + +.nde: mov eax, [sz] + add eax, [off] + cmp eax, 0xFB0400 ; max chs sectors + jg $ ; too big to use chs + jmp $ ; TODO load via chs + +dap: dw 0x10 ; size of disk packet + dw 127 ; foxdos will always load 127 segments + dw 16, 0xFFFF ; offset:segment to start of HMA - foxdos loads high +dapw: dd 0, 0 + +times 510 - ($-$$) db 0 +dw 0xAA55 diff --git a/tool/mkmothfs.c b/tool/mkmothfs.c new file mode 100644 index 0000000..f705a2f --- /dev/null +++ b/tool/mkmothfs.c @@ -0,0 +1,10 @@ +#include +#include +#include + +#include + +int main(int argc, char** argv) { + if(argc < 100) return !!printf("need arguments: ?\n"); + +}