files
format/F.ASM
2025-09-14 08:08:18 +02:00

295 lines
4.3 KiB
NASM

use32
org 0x0
db 'MENUET01'
dd 1
dd start
dd i_end
dd mem_end
dd stack_top
dd param
dd 0
param rb 4096
path rb 256
drive db 0
msg_confirm db 'Format floppy in FAT32? (Y/N) ',0
msg_success db 10,'Formatted successfully. Press any key.',10,0
msg_error db 10,'Error: '
msg_invalid_path db 'Invalid path (use /fd0/1 or /fd1/1)',10,0
msg_not_floppy db 'Not a 1.44 MB floppy disk',10,0
msg_write_error db 'Write failed',10,0
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
sector_buffer rb 512 ; zero-filled
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
mov eax, 3
int 0x40
mov [boot_sector + 0x27], eax
; Get file size to confirm floppy
mov eax, 58
mov ebx, 4
mov ecx, 0
mov edx, 0
mov edi, sector_buffer ; reuse buffer for info
mov ebp, path
int 0x40
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
mov eax, 58
mov ebx, 2
mov ecx, 512
mov edx, 0
mov esi, 0
mov edi, boot_sector
mov ebp, path
int 0x40
cmp eax, 0
jnz write_error
; Write FSInfo at 512
mov edx, 512
mov edi, fs_info
int 0x40
cmp eax, 0
jnz write_error
; Write backup boot at 3072
mov edx, 3072
mov edi, boot_sector
int 0x40
cmp eax, 0
jnz write_error
; Write backup FSInfo at 3584
mov edx, 3584
mov edi, fs_info
int 0x40
cmp eax, 0
jnz write_error
; Write first FAT at 16384
mov edx, 16384
mov edi, fat_first
int 0x40
cmp eax, 0
jnz write_error
; Write first FAT2 at 28160
mov edx, 28160
int 0x40
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
mov edi, sector_buffer
int 0x40
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
mov edi, sector_buffer
int 0x40
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
mov edi, sector_buffer
int 0x40
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:
mov eax, -1
int 0x40
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
mov eax, 63
mov ebx, 1
int 0x40
jmp print_string
.end:
ret
get_key:
mov eax, 2
int 0x40
shr eax, 8
ret
i_end:
rb 1024 ; reserve for data if needed
stack_top:
rb 4096
mem_end: