I need to write a program that creates a floppy image of FAT12. The instructions include creating a boot sector, making sure I set aside space for two FAT tables, setting up space for root directory and finally for data. However the instructions don't mention anything about handling new files/directories.

For example, let's say I have a ready floopy image called "floppy". Then I can mount the floppy in Ubuntu terminal:

sudo mount -o loop,uid=user, gid=user floppy mntpoint/

mkdir mntpoint/test

echo "Hello World" > mntpoint/test/foo

Does mount automatically recognize the information contained in the boot sector and understands that it's FAT12? If yes how does mount know where to put foo file within the floppy image? I assume I must somehow handle this. But how can I handle this line for example, what kind of functions would I have to have:

echo "Hello World" > mntpoint/test/foo

I'm writing in C. I'm not adding the code because my question is not code specific but rather conceptual. Just in case I'm adding the code for boot sector struct:

typedef struct {
    uint8_t     bootjmp[3];  /* 0  Jump to boot code */
    uint8_t     oem_id[8];   /* 3  OEM name & version */
    uint16_t    sector_size; /* 11 Bytes per sector hopefully 512 */
    uint8_t     sectors_per_cluster;    /* 13 Cluster size in sectors */
    uint16_t    reserved_sector_count;  /* 14 Number of reserved (boot) sectors */
    uint8_t     number_of_fats;         /* 16 Number of FAT tables hopefully 2 */
    uint16_t    number_of_dirents;      /* 17 Number of directory slots */

     * If 0, look in sector_count_large
    uint16_t    sector_count;           /* 19 Total sectors on disk */
    uint8_t     media_type;             /* 21 Media descriptor=first byte of FAT */

     * Set for FAT12/16 only.
     * The number of blocks occupied by one copy of the File Allocation Table.
    uint16_t    fat_size_sectors;       /* 22 Sectors in FAT */
    uint16_t    sectors_per_track;      /* 24 Sectors/track */
    uint16_t    nheads;                 /* 26 Heads */
    uint32_t    sectors_hidden;         /* 28 number of hidden sectors */
    uint32_t    sector_count_large;     /* 32 big total sectors */  

} __attribute__ ((packed)) boot_record_t;
  • 1
    Do you just need to create the filesystem or do you need to include files? A filesystem is dumb and in most cases doesn't know anything about how to add files as that logic get implemented in the OS and filesystem driver. – Seth Jan 9 '18 at 10:15
  • @Seth I need to create the system but them I need to support adding files like in the echo example. – Yos Jan 9 '18 at 10:16
  • Would this be more suited to StackOverflow, since it's essentially programming related? A user (even a "superuser" ;-) would just mount & echo. And have you looked through the fat driver's source code yet, all the "how" answers should be in there somewhere. – Xen2050 Jan 9 '18 at 11:02
  • @Xen2050 I actually posted this question in the beginning to StackOverflow but they said it doesn't belong to SO and suggested I post it here. In any case my fundamental question has now been answered. – Yos Jan 9 '18 at 11:09

mount just uses the same filesystem driver as when mounting your real disks and USB sticks. So, yes, it recognizes FAT12.

You can explicitly tell it to use the FAT driver using -t vfat (or -t msdos). If you don't, it tries to automatically recognize what filesystem is inside (using libblkid, if I remember correctly) and still calls vfat.

Alternatively, instead of mounting the image, you can use the "mtools" package (mcopy, mdir, etc.) to update it directly.

  • yes but what about adding files to that system? does the parent OS (linux) know when to assign foo file in my example? Or does this logic need to be inside floppy image? – Yos Jan 9 '18 at 10:25
  • This logic is inside the filesystem driver used by the OS. The driver understands the directory entries and knows how to add a new one. – user1686 Jan 9 '18 at 10:26
  • So if I need to write FAT12 I need to also write this logic? – Yos Jan 9 '18 at 10:27
  • 1
    If your program wants to make the entire filesystem "by hand", then yes, it needs to do all the same things that filesystem drivers usually do. (But you can probably find a library for this.) – user1686 Jan 9 '18 at 10:29
  • 1
    You would have to look into the specifics of that driver and the standard on how this should be handled. With your above operation there are already quite a few operations involved. For your application its a simple write but the FS has to be mounted, it has to be recognized its a mounted location, the FDs have to be opened accordingly and the data has to be passed to/through the FS driver. – Seth Jan 9 '18 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.