natevw / fatfs

Standalone FAT16/FAT32 filesystem implementation in JavaScript
47 stars 13 forks source link

Example of translating positions in a driver #20

Closed jviotti closed 9 years ago

jviotti commented 9 years ago

I'm looking forward to read from a FAT partition which happens to be the fifth partition of an *.img file, however I'm a little bit confused in how should I build the driver to interact with that specific partition of the file.

I'm generating the driver like this:

SECTOR_SIZE = 512

exports.getDriver = (fd, offset, size) ->
    return {
        sectorSize: SECTOR_SIZE
        numSectors: size / SECTOR_SIZE
        readSectors: (sector, dest, callback) ->            
            position = offset + sector * SECTOR_SIZE
            fs.read fd, dest, 0, dest.length, position, (error, bytesRead, buffer) ->
                return callback(error, buffer)
    }

I'm then opening the *.img file with fs.open(), passing the file descriptor, and the offset (the beginning of the partition in the image file) and size of the partition, which happens to be (this matches fdisk):

size = 4194304
offset = 398458880

However I get the following error when calling fatfs.createFileSystem():

/Users/jviotti/Projects/resin/ng/device-init/resin-image-fs/node_modules/fatfs/vol.js:7
    if (bootSector[510] !== 0x55 || bootSector[511] !== 0xAA) throw Error("Inv
                                                                    ^
Error: Invalid volume signature!
  at Error (native)
  at Object.exports.init (/Users/jviotti/Projects/resin/ng/device-init/resin-image-fs/node_modules/fatfs/vol.js:7:69)
  at init (/Users/jviotti/Projects/resin/ng/device-init/resin-image-fs/node_modules/fatfs/index.js:58:35)
  at /Users/jviotti/Projects/resin/ng/device-init/resin-image-fs/node_modules/fatfs/index.js:48:17
  at /Users/jviotti/Projects/resin/ng/device-init/resin-image-fs/lib/driver.coffee:32:12
  at FSReqWrap.wrapper [as oncomplete] (fs.js:529:17)

I'm sure this is an error with my code rather than with fatfs, but I wonder if you could showcase a driver translating the positions as you suggest on the README.

natevw commented 9 years ago

Hmm, I don't see anything wrong offhand. The examples I have currently are:

You might try tweaking my card_debug.js script, which is currently configured to use a raw sdcard interface but might help you dump relevant partition info to double-check offsets (and content at each offset).

jviotti commented 9 years ago

@natevw Thanks for the examples. Turned out to be an issue with the image file I was trying to read from. My snippet worked with another image!

Thanks for the support, and sorry for the trouble!