MS-DOS File System questions - partitions, mounting, files

Robert S. Grimes rsg at alum.mit.edu
Fri Oct 10 13:36:49 UTC 2008


I'm trying to use the MSDOS file system on an SD card, and I think I've 
got the hardware and driver issues sorted out.  Now, I'm pretty much 
stuck with a lack of understanding on my part on how to use it!  I'm 
almost certain my problem is my weak understanding of Unix filesystems.

I start with an SD card formatted in Windows XP as a FAT disk, and place 
the following contents on it:

    README.TXT
    dirone/
        README1.txt
    dirtwo/
        README2.txt

Here is my code, with comments used for questions:

    // This stuff (apparently) works - shown for completeness
    sc = rtems_ide_part_table_initialize( SDCARD_DEVICE_FILE);
    CHECK_SC( sc, "Initialize IDE partition table");
    rv = mkdir( SDCARD_MOUNT_POINT, S_IRWXU);
    CHECK_RVSC( rv, "Create mount point");
    rv = rtems_fsmount(fsTable, sizeof(fsTable) / sizeof(fsTable[0]), NULL);
    CHECK_RVSC( rv, "Mount file systems");

    // Here I use a function from Sebastian for printing directories -
    seems correct
    printf("\nPrint /dev directory\n");
    printf("Returned %d\n", printDir( "/dev", 0));
    printf("\nPrint /mnt directory\n");
    printf("Returned %d\n\n", printDir( "/mnt", 0));

    // According to Chris' Wiki entry on MSDOS, I need to do this, but
    it fails with a "Not supported"
    // error.  Questions:
    //
    //   1. I'm not sure what to do with the mtEntry parameter - is it
    used for other functions?
    //   2. I'm not sure about "/mnt/dirone". As I said above, there is
    a directory on my SD card
    //      called dirone, but I'm not sure that I'm doing things
    correctly.  I guess my real question
    //      is: How do I mount the SD card such that its entire contents
    are available to my app?
    rtems_filesystem_mount_table_entry_t* mtEntry;
    if (mount(&mtEntry, &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
    "/dev/sdcarda1", "/mnt/dirone") < 0) {
      printf("mount: mount failed: %s\n", strerror (errno));
    }

    // Interestingly, this works just fine, even with the previous mount...
    int readme = open("/mnt/README.TXT", O_RDONLY);
    char linebuf[32];
    linebuf[0] = '*';
    linebuf[1] = 0;
    rv = read(readme, linebuf, 27);
    if (rv > 0) {
      linebuf[rv] = 0;
      printf("Readme contains: [%s]\n", linebuf);
    } else {
      printf("Failed to read file - return code: %d\n", rv);
    }
    close(readme);

    // This doesn't work fully, though the mkdir sometimes works enough
    that Windows will see
    // the new directory; when it does, it is sometimes okay, sometimes
    corrupted.  Questions:
    //   1. Is this the correct way to do it?
    //   2. Is the mount() failure the cause of this one?
    printf("\nMake a directory and move to it\n");
    printf("Returned: %d\n", mkdir( "/mnt/testdir3", S_IRWXU));
    printf("printDir returned %d\n", printDir( "/mnt/testdir3", 0));
    printf("Change directory returned %d\n", chdir( "/mnt/testdir3"));

    // This doesn't work, but maybe that is because the mount() fails? 
    If so, would this be correct
    // if mount() had succeeded?
    int writeme = open("newfile.txt", O_WRONLY);
    printf("\nCreating newfile.txt, returned %d\n", writeme);
    if (writeme > 0) {
      sprintf(linebuf, "Hello, world!\n");
      write(writeme, linebuf, strlen(linebuf)+1);
      close(writeme);
    }

    // Same question as previous open()
    writeme = open("/mnt/newfile.txt", O_WRONLY);
    printf("\nCreating /mnt/newfile.txt, returned %d\n", writeme);
    if (writeme > 0) {
      sprintf(linebuf, "Hello, world!\n");
      write(writeme, linebuf, strlen(linebuf)+1);
      close(writeme);
    }


Here is the output from the above code:

    Print /dev directory
    <console>
    <spi0.sdcarda>
    <sdcarda>
    <sdcarda1>
    Returned 0

    Print /mnt directory
    <README.TXT>
    <DIRONE>
            <README1.TXT>
    <DIRTWO>
            <README2.TXT>
    <TESTDIR3>
    Returned 0

    mount: mount failed: Not supported
    Readme contains: [This is a simple text file.]

    Make a directory and move to it
    Returned: -1
    printDir returned 0
    Change directory returned 0

    Creating newfile.txt, returned -1

    Creating /mnt/newfile.txt, returned -1

Thanks!
-Bob






More information about the users mailing list