Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hello,
I need to copy a file into a Flash memory which is connected to my computer via USB. The file must start at a specific sector.
Can anyone guide me how to do this? (it can be through a C program, a line command, or any other way)
Thanks in advance,
Moshe
How is this file to be accessed? If it must be accessed using a normal filesystem call, then I think you cannot bypass that and put it on a specific sector.
I need the file to start at sector 300, and that the file will appear in a standard way in the directory, so it can be normaly read by file system.
I'll check what the 'dd' utility can offer.
Many thanks.
Nope, you can only appear to write to a specific sector - dd included.
Think about what hapens if that sector is "bad" and has been remapped by the hardware. Fruitless demand IMHO.
Assuming that the hardware manages bad sectors independently of how you write to the drive, then there is an unambiguous location at a fixed offset using dd.
Suppose you do this:
dd if=filename of=/dev/sdb1 bs=512 seek=10
This will write the contents of "filename" beginning at what **appears to be** sector 10 of device sdb1. You can unambiguously recover the data with another dd command. My earlier point was that I believe you CANNOT recover the file with a normal filesystem command.
Assuming that the hardware manages bad sectors independently of how you write to the drive, then there is an unambiguous location at a fixed offset using dd.
Suppose you do this:
dd if=filename of=/dev/sdb1 bs=512 seek=10
This will write the contents of "filename" beginning at what **appears to be** sector 10 of device sdb1. You can unambiguously recover the data with another dd command. My earlier point was that I believe you CANNOT recover the file with a normal filesystem command.
I guess that this will be good as the first step - writing the file at the sector that is assumed to be the sector-number I need. There is no real danger of overriding other staff- as this action I do after formating this device, so this file should be the only file on the disk.
I guess now I need to write a new entry in the directory, which will map the occupied sectores with the file.
Is there an API which I can use for manipulating the directory of the disk?
I am still very confused about what you are trying to do....
First, why not let the file system do its job?
Second, assuming that a file system is in place, doing a low-level write with something like dd will very likely damage that filesystem---thus what you are calling "directories" will no longer exist. To be precise, the structures used in filesystems to locate data are called "inodes". Directories are used simply to flag files which are to be viewed together, and do not relate to physical storage on the disk.
Third, when you do a low-level write, the data will go on the disk contiguously (unless you write a more complex routine to split it up.) Thus, for example, suppose you have a file with 5270 bytes, and you write is starting at the beginning of the 3rd sector. It will fill (contiguously) sectors 3 thru 12, plus 150 bytes of sector 13. This is almost certainly NOT how a file system would write it.
I am still very confused about what you are trying to do....
First, why not let the file system do its job?
Second, assuming that a file system is in place, doing a low-level write with something like dd will very likely damage that filesystem---thus what you are calling "directories" will no longer exist. To be precise, the structures used in filesystems to locate data are called "inodes". Directories are used simply to flag files which are to be viewed together, and do not relate to physical storage on the disk.
Third, when you do a low-level write, the data will go on the disk contiguously (unless you write a more complex routine to split it up.) Thus, for example, suppose you have a file with 5270 bytes, and you write is starting at the beginning of the 3rd sector. It will fill (contiguously) sectors 3 thru 12, plus 150 bytes of sector 13. This is almost certainly NOT how a file system would write it.
Let me explain my need:
I have an embaded system (a communication card) which reads a flash memory. When this system powers up, the program that is burned in the ROM on this system starts. It reads the flash memory and looks for a certain file that starts at sector 300. Later, this file starts executing instead of the ROM program. This second program already expects the flash to be formated with DOS file system, and it reads flies in the regular way, by filename.
So, I formated the flash memory, and now I wish to write this particular file on sector 300, and some other files anywhere else on the disk.
If I just use 'dd' to write the first file on sector 300, and then just copy files into this flash, there is a good chance that some file will override my first file - unless I write it in the directory, so any file system that will attempt to add a file to this storage, will see that those sectors starting from 300, are occupied.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.