flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Format a floppy image file with FAT12

Author
Thread Post new topic Reply to topic
ksanjeet



Joined: 27 May 2012
Posts: 3
ksanjeet 29 Jun 2013, 05:55
I have started an OS project. I have created a simple real mode bootloader. I am still to learn coding PM and kernel. I want to use a floppy image file to store my bootloader and kernel. I have created a qemu-img file. I want to format the image file with FAT12 for which I want to write a FASM code. I have read documents on floppy disk geometry but I am looking for some example code to learn from it.
Post 29 Jun 2013, 05:55
View user's profile Send private message Reply with quote
TightCoderEx



Joined: 14 Feb 2013
Posts: 58
Location: Alberta
TightCoderEx 29 Jun 2013, 06:16
OS Dev will probably give everything you need.
Post 29 Jun 2013, 06:16
View user's profile Send private message Visit poster's website Reply with quote
ksanjeet



Joined: 27 May 2012
Posts: 3
ksanjeet 29 Jun 2013, 09:13
Thanks TightCoderEx.
Post 29 Jun 2013, 09:13
View user's profile Send private message Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 243
Mike Gonta 31 Jul 2013, 22:02
ksanjeet wrote:
I have started an OS project. I have created a simple real mode bootloader. I am still to learn coding PM and kernel. I want to use a floppy image file to store my bootloader and kernel. I have created a qemu-img file. I want to format the image file with FAT12 for which I want to write a FASM code. I have read documents on floppy disk geometry but I am looking for some example code to learn from it.
This simple code will create a formatted FAT12 image.
Use dd to transfer the boot sector.
Use ImDisk to mount the image.

Code:
org 7C00h
  jmp start
  nop
  db '        '
  dw 512
  db 1                          ; sectors per cluster
  dw 1                          ; reserved sector count
  db 2                          ; number of FATs
  dw 16*14                      ; root directory entries
  dw 18*2*80                    ; sector count
  db 0F8h                       ; media byte
  dw 9                          ; sectors per fat
  dw 18                         ; sectors per track
  dw 2                          ; number of heads
  dd 0                          ; hidden sectors
  dd 0                          ; sectors large
  db 0                          ; drive number
  db 0                          ; reserved
  db 29h                        ; signature
  dd 78563412                   ; Volume ID
  db '           '              ; volume label
  db 'FAT12   '                 ; file system type

start:

  times 510-($-$$)                db 0
                                  dw 0AA55h

fat1:                           ; empty FAT12 file system
  db 0F0h, 0FFh, 0FFh
  times 512*9-($-fat1)            db 0
fat2:
  db 0F0h, 0FFh, 0FFh
  times 512*9-($-fat2)            db 0
root:
  times 512*14                    db 0

;  times 512*18*2*80-($-$$)        db 0    

_________________
Mike Gonta
look and see - many look but few see

https://mikegonta.com
Post 31 Jul 2013, 22:02
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 31 Jul 2013, 22:58
Mike Gonta,

Perhaps it's better to start it like this:
Code:
org 7C00h
  jmp start
  times $$+3-$ nop; just in case of start>7C81h
  db '        '    
Post 31 Jul 2013, 22:58
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 01 Aug 2013, 00:31
ksanjeet said that he will try to learn my mkfloppy sources.

I'm working from time to time on new style mkfatxx. For example:
Code:
file "bootstub.bin"

orgd
  ...
  orgd "DIR"
    ...
  find
  ...
find    
Would you want to test it?

I plan to implement autocalculation of FAT/data area size when special parameter AVAILABLESECTORS is not equal to zero. For example: AVAILABLESECTORS=2*80*18.
Post 01 Aug 2013, 00:31
View user's profile Send private message Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 01 Aug 2013, 09:14
I'm just curious why do everybody bother with FAT12 and floppies... You'll hardly find a PC with a floppy drive today. Very strange and a complete waste of time in my understanding.
Post 01 Aug 2013, 09:14
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 01 Aug 2013, 12:20
I have support for all FAT variants (except exFAT). FAT12/16 can be useful for small partitions, which are often used for OSDev. I think almost nobody uses floppies now but somebody still uses floppy images for emulators. I still provide floppy driver in my OS distribution to make possible use of that images with my OS.
Post 01 Aug 2013, 12:20
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 02 Aug 2013, 15:10
egos
Am I wrong thinking you have a different nickname on wasm forum?
ksanjeet
If you are interested in convenient macros for creating floppy images, you may want to try the attached macros originally posted here. The simplest floppy image would then look like this:
Code:
include 'floppy.inc'
floppy_image <@@: jmp @B>
endfi    


But you can put any boot sector and create any file system structure you want:
Code:
macro bootCode
{
    use16   
    jmp 0:loader.configure
    loader.configure:
        xor ax,ax
        mov ds,ax
        mov es,ax
        cli
        mov ss,ax
        mov sp,7c00h
        sti
        
        mov si,loader
        mov di,loader.start
        mov cx,loader.end-loader.start
        cld
        rep movsb
    jmp loader.start

    loader:
    org 600h
    loader.start:
        push 0b800h
        pop es
        mov si,msg
        xor di,di
        mov cx,(loader.end-msg)/2
        rep movsw
        
        @@: hlt
        jmp @B
        msg db 'H',7,'e',7,'l',7,'l',7,'o',7,' ',7,'W',7,'o',7,'r',7,'l',7,'d',7,'!',7
    loader.end:
}

include 'floppy.inc'
floppy_image bootCode,root
    cd "C:\fasm\BootLoad"
    directory root,\
                        windows,,\
                        "bootload.bin",,\
                        "floppy.asm", "source.txt",\
                        "D:\empty.bin", "empty"
    cd "C:\windows\"
    directory windows,\
                        system32,"system",\
                        temp,,\
                        "system32\taskmgr.exe"
    directory system32,\
                        "notepad.exe","click_me.exe"
endfi    

This code creates the following file structure:
Code:
\-+
  |
  +- WINDOWS -+
  |           |
  |           +- SYSTEM -+
  |           |          |
  |           |          +- CLICK_ME.EXE
  |           |
  |           +-  TEMP  -+
  |           |
  |           +- TASKMGR.EXE
  |
  +- BOOTLOAD.BIN
  |
  +- EMPTY
  |
  +- SOURCE.TXT    


Here the first argument of the macro floppy_image is anything what expands into the desired boot sector code. E.g., you could use <include 'bootcode.asm'> to take the boot sector source from a separate file or <file 'bootcode.bin'> to take the boot sector binary code from a separate file. The second argument represents the root folder you will be referring to when describing the image folder tree.

In order to define a folder you just use the macro directory followed by a previously declared folder id (like "root") then a list of pairs: first item is a folder id (i.e. it's declaration) or file path (in the latter case it must be a string), second item is the name of the file/folder.
The macro cd is used to specify a base path to take files from, so that one can specify paths relative to the base path. An empty string can be passed to undo the base path usage.

P.S. If someone's interested, I've also written such macros for creating ISO9660 images with optional ElTorito and Joliet support. Originally posted here.


Description:
Download
Filename: floppy.zip
Filesize: 3.67 KB
Downloaded: 610 Time(s)


_________________
Faith is a superposition of knowledge and fallacy
Post 02 Aug 2013, 15:10
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 02 Aug 2013, 16:41
Hi, I use this nick in international forums.

I resolved the problem described here. Now I think about parameters and their sequence. Most likely I will use following sequence:
Code:
stof hostname,shortname,attributes,longname    
Post 02 Aug 2013, 16:41
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 02 Aug 2013, 18:19
egos
Quote:
Most likely I will use following sequence

What's "hostname"? If you mean the parent directory name, then according to your original idea of expanded tree syntax no parent directory is needed there, cause it's defined by the position of the file definition.

_________________
Faith is a superposition of knowledge and fallacy
Post 02 Aug 2013, 18:19
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 02 Aug 2013, 19:21
This name specifies where original file is located and what name it has. For example:
Code:
stof "content/readme.txt", \
     "README  TXT", \ ; Yes, I still use crude short names Smile
     FA_ARC, \ ; Now it's default attribute for files
     "ReadMe.txt"    
Post 02 Aug 2013, 19:21
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 02 Aug 2013, 19:32
egos
Oh, you mean its source path. Clear.

_________________
Faith is a superposition of knowledge and fallacy
Post 02 Aug 2013, 19:32
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 03 Aug 2013, 11:01
Why do you move a part of boot loader? Is it to show that org directive may be used inside a boot loader and you don't require to put it at the first place?
Post 03 Aug 2013, 11:01
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 03 Aug 2013, 13:56
egos
That's what a loader may normally wanna do. I actually don't remember why I chose to use this piece of code (it's kinda 2-3 years old), but your justification seems reasonable. Smile

Quote:
you don't require to put it at the first place

I even require to not put it at the beginning, because the code position depends on the size of the BPB and should not therefore be considered by the user.

_________________
Faith is a superposition of knowledge and fallacy
Post 03 Aug 2013, 13:56
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 03 Aug 2013, 15:38
I meant org directive, not the code. I require to put this directive at the start of the volume and to not use it anywhere else on this volume.
Post 03 Aug 2013, 15:38
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 03 Aug 2013, 15:42
egos
Quote:
I meant org directive

Yes. That's also how I understood you.

_________________
Faith is a superposition of knowledge and fallacy
Post 03 Aug 2013, 15:42
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 03 Aug 2013, 16:13
Well, but how do you find out the size of boot code?
Post 03 Aug 2013, 16:13
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 03 Aug 2013, 16:25
egos
I redefine the org directive, so that it calculates the size:
Code:
..bpb: BPB ..bootCode
..bootCode:
org 7C00h+(..bootCode-..bpb)
macro org arg*
{
    bootCode.size = bootCode.size + ($-$$)
    org arg
}
bootCode.size = 0

bootCode

bootCode.size = bootCode.size + ($-$$)
purge org    

The org directive can sometimes be a real pain in the ass because of its property to start a new addressing space. Thus I more or less often use the redefinition trick.

_________________
Faith is a superposition of knowledge and fallacy


Last edited by l_inc on 04 Aug 2013, 10:08; edited 2 times in total
Post 03 Aug 2013, 16:25
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 03 Aug 2013, 16:33
Oh, I understand, thanks.
Post 03 Aug 2013, 16:33
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.