1

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;
Yos
  • 113

1 Answers1

3

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.

grawity
  • 501,077