diff --git a/macros.asm b/macros.asm new file mode 100644 index 0000000..135a85b --- /dev/null +++ b/macros.asm @@ -0,0 +1,245 @@ +meos_app_start + +udata + param rb 4096 + path rb 256 + drive db 0 + sector_buffer rb 512 +meos_app_end + +data + szZ msg_confirm, 'Format floppy in FAT32? (Y/N) ' + szZ msg_success, 10,'Formatted successfully. Press any key.',10 + szZ msg_error, 10,'Error: ' + szZ msg_invalid_path, 'Invalid path (use /fd0/1 or /fd1/1)',10 + szZ msg_not_floppy, 'Not a 1.44 MB floppy disk',10 + szZ msg_write_error, 'Write failed',10 + + boot_sector: + db 0xeb,0x58,0x90 + db 'MSWIN4.1' + dw 512 + db 1 + dw 32 + db 2 + dw 0 + dw 0 + db 0xf0 + dw 0 + dw 2 + dd 0 + dd 2880 + dd 23 + dd 0 + dw 0 + dd 2 + dw 1 + dw 6 + times 12 db 0 + db 0 + db 0 + db 0x29 + dd 0x12345678 ; serial, will be set + db 'NO NAME ' + db 'FAT32 ' + times 420 db 0x90 ; boot code nop + db 0x55,0xaa + + fs_info: + dd 0x41615252 + times 480 db 0 + dd 0x61417272 + dd 2801 ; free clusters + dd 3 ; last allocated + times 12 db 0 + dd 0xaa550000 + + fat_first: + dd 0x0ffffff8 + dd 0x0fffffff + dd 0x0fffffff + times 512-12 db 0 +data + +code +start: + mov esi, param + call skip_spaces + mov edi, path + call copy_string + + ; Parse and validate path + mov esi, path + cmp byte [esi], '/' + jne invalid_path + inc esi + cmp word [esi], 'fd' + jne invalid_path + add esi, 2 + mov al, [esi] + sub al, '0' + cmp al, 0 + jl invalid_path + cmp al, 1 + jg invalid_path + mov [drive], al + inc esi + cmp byte [esi], '/' + jne invalid_path + inc esi + cmp byte [esi], '1' + jne invalid_path + inc esi + cmp byte [esi], 0 + jne invalid_path + + ; Set drive number in boot sector + mov al, [drive] + mov [boot_sector + 0x24], al + + ; Set volume serial using system time + mcall 3, 0, 0, 0, 0, 0, 0 + mov [boot_sector + 0x27], eax + + ; Get file size to confirm floppy + mcall 58, 4, 0, 0, sector_buffer, path + cmp eax, 0 + jnz write_error + cmp dword [sector_buffer + 20], 0 ; size high + jnz not_floppy + cmp dword [sector_buffer + 16], 1474560 ; size low + jnz not_floppy + + ; Confirm + mov esi, msg_confirm + call print_string + call get_key + or al, 0x20 ; to lowercase + cmp al, 'y' + jne exit + + ; Write boot sector at 0 + mcall 58, 2, 512, 0, 0, boot_sector, path + cmp eax, 0 + jnz write_error + + ; Write FSInfo at 512 + mcall 58, 2, 512, 512, 0, fs_info, path + cmp eax, 0 + jnz write_error + + ; Write backup boot at 3072 + mcall 58, 2, 512, 3072, 0, boot_sector, path + cmp eax, 0 + jnz write_error + + ; Write backup FSInfo at 3584 + mcall 58, 2, 512, 3584, 0, fs_info, path + cmp eax, 0 + jnz write_error + + ; Write first FAT at 16384 + mcall 58, 2, 512, 16384, 0, fat_first, path + cmp eax, 0 + jnz write_error + + ; Write first FAT2 at 28160 + mcall 58, 2, 512, 28160, 0, fat_first, path + cmp eax, 0 + jnz write_error + + ; Zero other FAT sectors (1 to 22 for each FAT) + mov ecx, 22 + mov edx, 16384 + 512 ; FAT1 start +512 +zero_fat1: + push ecx + mcall 58, 2, 512, edx, 0, sector_buffer, path + cmp eax, 0 + jnz write_error + add edx, 512 + pop ecx + loop zero_fat1 + + mov ecx, 22 + mov edx, 28160 + 512 ; FAT2 +zero_fat2: + push ecx + mcall 58, 2, 512, edx, 0, sector_buffer, path + cmp eax, 0 + jnz write_error + add edx, 512 + pop ecx + loop zero_fat2 + + ; Zero data sectors (LBA 78 to 2879, position 78*512 to 2879*512) + mov ecx, 2802 ; data sectors + mov edx, 78 * 512 ; start data +zero_data: + push ecx + mcall 58, 2, 512, edx, 0, sector_buffer, path + cmp eax, 0 + jnz write_error + add edx, 512 + pop ecx + loop zero_data + + mov esi, msg_success + call print_string + call get_key + jmp exit + +invalid_path: + mov esi, msg_error + call print_string + mov esi, msg_invalid_path + call print_string + jmp exit + +not_floppy: + mov esi, msg_error + call print_string + mov esi, msg_not_floppy + call print_string + jmp exit + +write_error: + mov esi, msg_error + call print_string + mov esi, msg_write_error + call print_string + jmp exit + +exit: + mcall -1 + +skip_spaces: + cmp byte [esi], ' ' + jne .end + inc esi + jmp skip_spaces +.end: + ret + +copy_string: + lodsb + stosb + or al, al + jnz copy_string + ret + +print_string: + lodsb + or al, al + jz .end + mov ah, 0 + mov ebx, eax + mcall 63, 1 + jmp print_string +.end: + ret + +get_key: + mcall 2 + shr eax, 8 + ret +code \ No newline at end of file