How to erase mtdblock in C program? - Embedded

This is a discussion on How to erase mtdblock in C program? - Embedded ; Hi, I have two mtdblocks in my flash for two file systems, FS1(current root) and FS2(for backup). If the current root fs is FS1(mtdblock2), and I want to write a new filesystem image (download from network) to FS2(mtdblock3) for next ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: How to erase mtdblock in C program?

  1. How to erase mtdblock in C program?

    Hi,

    I have two mtdblocks in my flash for two file systems,
    FS1(current root) and FS2(for backup).
    If the current root fs is FS1(mtdblock2), and
    I want to write a new filesystem image
    (download from network) to FS2(mtdblock3) for next boot up,
    which means the FS2 would become root fs after reboot.
    How can I erase the mtdblock3(FS2) before writing to it in
    a C program?

    Currently I use write() to directly write to FS2.
    But my question is, If the filesystem image is
    smaller than the target mtdblock size, what should
    I do for the rest unused space? For example, FS2 has
    4MB, and I write only 2MB filesystem image to
    FS2, what should I do for the rest of the 4-2=2MB?
    I think I should erase them, but how ? Or can
    I write() the rest bytes to all a specific byte(0xff,
    for example)?
    Or should I erase FS2 before do write(), but How to erase??

    Could someone give advice?
    Thanks.
    Sarick


  2. Re: How to erase mtdblock in C program?

    Sarick a écrit :
    > Hi,

    Hi
    >
    > I have two mtdblocks in my flash for two file systems,
    > FS1(current root) and FS2(for backup).
    > If the current root fs is FS1(mtdblock2), and
    > I want to write a new filesystem image
    > (download from network) to FS2(mtdblock3) for next boot up,
    > which means the FS2 would become root fs after reboot.
    > How can I erase the mtdblock3(FS2) before writing to it in
    > a C program?
    >

    Which file system do you use?
    Personnaly I use a JFFS2 file system for my flash file system,
    and I am not worried about erasing flash before writing,
    it' s manage by the jffs2, so when I want to write a root
    jffs2 file system image to mtdblock in c I use the binary dd
    or cat like that:
    system("cat /tmp/myJFFS2RootImage.img > /dev/mtdblock3");

    > Currently I use write() to directly write to FS2.
    > But my question is, If the filesystem image is
    > smaller than the target mtdblock size, what should
    > I do for the rest unused space? For example, FS2 has
    > 4MB, and I write only 2MB filesystem image to
    > FS2, what should I do for the rest of the 4-2=2MB?
    > I think I should erase them, but how ? Or can
    > I write() the rest bytes to all a specific byte(0xff,
    > for example)?
    > Or should I erase FS2 before do write(), but How to erase??
    >

    In my case once there is a JFFS2 file system in the mtdblock,
    even if the root file system grow, the erase and write is
    manage by jffs2, so you you write and read as simple as an ext3
    or fat32 file system on a hard disk with your OS.
    > Could someone give advice?
    > Thanks.
    > Sarick
    >


    I' m interesting about how proceed others?

    Pes

  3. Re: How to erase mtdblock in C program?

    Sarick wrote:
    > Hi,
    >
    > I have two mtdblocks in my flash for two file systems,
    > FS1(current root) and FS2(for backup).
    > If the current root fs is FS1(mtdblock2), and
    > I want to write a new filesystem image
    > (download from network) to FS2(mtdblock3) for next boot up,
    > which means the FS2 would become root fs after reboot.
    > How can I erase the mtdblock3(FS2) before writing to it in
    > a C program?


    There are erase and eraseall commands included in the MTD distribution,
    grab the latest version from the linux-mtd homepage. Erasure of MTD
    devices is achieved using ioctl() calls.

    FWIW, flashing an actual filesystem onto your mtdblock devices defeats
    the purpose of using a wear levelling filesystem. Instead you should
    delete the old files and replace them with the new ones.

    > But my question is, If the filesystem image is
    > smaller than the target mtdblock size, what should
    > I do for the rest unused space? For example, FS2 has
    > 4MB, and I write only 2MB filesystem image to
    > FS2, what should I do for the rest of the 4-2=2MB?


    Instead of generating a filesystem, try simply copying the actual files
    onto your board.

  4. Re: How to erase mtdblock in C program?

    Hi,

    I tried using ioctl(), but fail to get mtd info through the MEMGETINFO
    i/o command.
    The strerror result is: "Inappropriate ioctl for device".
    I checked both the file descriptor and MEMGETINFO value,
    the fd is valid, while the MEMGETINFO has a strange value
    (-2145366783).
    Does anyone know this problem?

    The reason that I cannot use 'cat' command to copy to the mtdblock
    is that, I am using network downloading to get the new fs image.
    The filesystem image is downloaded via tftp, which receives
    as separated packets. And I have to write each received block into
    FS2 immediately since the buffer size isn't big enough for whole image.
    The only one way I knew is to write() to the mtdblock directly.
    But is there any other appropriate ways for my purpose?

    Further that, I tried using write() to write to mtdblock(FS2), and
    write() the rest bytes to '0xff' for unused purpose. But I'm getting
    error messages like this:

    MTD do_write_oneword(): Wacky! Unable to decode failure status
    Possible buggy device - try enabling CONFIG_MTD_CFI_AMDSTD_RETRY
    in your kernel config and setting driver retry_cmd_max
    MTD do_write_oneword(): 0x005419ac(0x00001985): 0x0000ffff 0x0000ffff
    0x0000ffff 0x0000ffff
    Write of 74 bytes at 0x004219ac failed. returned -5, retlen 0

    Anyone know why?

    Thanks
    Sarick


  5. Re: How to erase mtdblock in C program?

    Hi Pes,

    I'm using JFFS2 as my file system.
    But still having the ioctl problem as I described in my previous mail.
    Any help would be very appreciative.

    Sarick


  6. Re: How to erase mtdblock in C program?

    Sarick a écrit :
    > Hi Pes,
    >
    > I'm using JFFS2 as my file system.
    > But still having the ioctl problem as I described in my previous mail.
    > Any help would be very appreciative.
    >
    > Sarick
    >


    Hi,

    According to Geronimo, I' ve ommit to say that I do a
    "eraseall /dev/mtdX" to erase /dev/mtdblockX before writing a file system.
    I' m sorry I don' t know how to use the ioctl interface but maybe, if
    you don' t find a better solution and if you haven' t enough memory, you
    could separate your root file system in several little files and
    generate something like that in c:
    dd if=temp.bin of=/dev/mtdblock0 bs=512 count=1
    dd if=temp.bin of=/dev/mtdblock0 bs=512 count=1 seek=1
    dd if=temp.bin of=/dev/mtdblock0 bs=512 count=1 seek=2
    ....


  7. Re: How to erase mtdblock in C program?

    Sarick wrote:
    > Hi,
    >
    > I tried using ioctl(), but fail to get mtd info through the MEMGETINFO
    > i/o command.
    > The strerror result is: "Inappropriate ioctl for device".
    > I checked both the file descriptor and MEMGETINFO value,
    > the fd is valid, while the MEMGETINFO has a strange value
    > (-2145366783).
    > Does anyone know this problem?


    Sounds like the incorrect data type is being used.

    If you have the source to erase.c and eraseall.c, you should be able to
    compile those on your system and confirm that they work correctly (they
    work just fine for me and I guess everyone else who uses MTD). So you
    are doing something wrong in your code.

    > The reason that I cannot use 'cat' command to copy to the mtdblock
    > is that, I am using network downloading to get the new fs image.
    > The filesystem image is downloaded via tftp, which receives
    > as separated packets. And I have to write each received block into
    > FS2 immediately since the buffer size isn't big enough for whole image.
    > The only one way I knew is to write() to the mtdblock directly.
    > But is there any other appropriate ways for my purpose?


    I would suggest that the data you are storing be kept in a tar file,
    which you unpack onto your flash mount once you have downloaded it. By
    programming the image directly you are bypassing the wear-levelling
    features of JFFS2. There is almost no point in using JFFS2 if you do not
    need wear-levelling.

    > Further that, I tried using write() to write to mtdblock(FS2), and
    > write() the rest bytes to '0xff' for unused purpose. But I'm getting
    > error messages like this:


    I think you do not understand how flash devices work; I suggest you
    Google for some documentation or read the data sheet of your flash
    device. Writing 0xFF to a flash address does not erase it. The flash
    chip requires that you execute a separate erase command on the sector
    which is to be cleared. To do that, you need to use the MTD ioctl.

    Once data is written in a flash sector it cannot be rewritten, the
    sector must be erased first.




  8. Re: How to erase mtdblock in C program?

    I am a little late in the coming but I am wondering if you, Sarick, ever figured out your problem. I am having a similar problem now and can't seem to find any definitive answers. I am receiving my rootfs image in 2k blocks at a time. I need to write it to another partition (mtd6). I am using the system write function. It keeps telling me that I do not have enough room on the device. My image is ~10MB my partition is 16MB. When I try to use the mtd-utils nandwrite I get the error message Unknown flash (not normal NAND).

    Any input would be greatly appreciated.

    Thank you

+ Reply to Thread