3

私はこれを書きました:

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <mtd/mtd-user.h>
#include <errno.h>

int main( void )
{
        int fd;
        char buf[4]="abc";

        fd = open("/dev/mtd0", O_RDWR);
        lseek(fd, 1, SEEK_SET);
        write(fd, &buf, 4);
        close(fd);
        perror("perror output:");

        return 0;
}

ファイル /dev/mtd0 は、nandsim カーネル モジュールを使用して作成され、実行されます。

mtdinfo /dev/mtd0

意味のある出力が得られました。プログラムを実行すると、次のように出力されます。

perror output:: Invalid argument

プログラムにエラーがある場合は?

4

5 に答える 5

2

はい、問題があります。の使い方perror()が間違っています。

perror を呼び出す前に、まずシステム コールが問題を示しているかどうかを確認する必要があります。manページは、この件に関して非常に明確です:

Note that errno is undefined after a successful library call: this call
may  well  change  this  variable, even though it succeeds, for example
because it internally used some other  library  function  that  failed.
Thus,  if  a failing call is not immediately followed by a call to per‐
ror(), the value of errno should be saved.

各システムのリターン コードをチェックし、失敗した場合にのみ perror を呼び出す必要があります。このようなもの:

fd = open("/dev/mtd0", O_RDWR);
if (fd < 0) {
    perror("open: ");
    return 1;
}
if (lseek(fd, 1, SEEK_SET) < 0) {
    perror("lseek: ");
    return 1;
}
if (write(fd, &buf, 4) < 0) {
    perror("write: ");
    return 1;
}
close(fd);
于 2012-04-28T09:12:33.873 に答える
2

あなたはこのようなものを持っている必要があります

if(-1 == write(fd, &buf, 4)){
  perror("perror output:");
}
close(fd);

perror は最後のエラーを表示するためです。

http://www.cplusplus.com/reference/clibrary/cstdio/perror/

perror の詳細http://www.java-samples.com/showtutorial.php?tutorialid=597

于 2012-04-28T09:10:28.747 に答える
1

4 バイトだけでなく、ページ全体を書き込む必要がある場合があります。

dmesgこれは、シェルでコマンドを入力して確認できます。次に、次のカーネル メッセージが表示されます。

nand_do_write_ops: ページ整列されていないデータを書き込もうとしています

次に、mtd に書き込むコードを次のように置き換えます。

char buf[2048]="abcdefghij";                      //Ajust size according to 
                                                  //mtd_info.writesize
mtd_info_t mtd_info;                              // the MTD structure

if (ioctl(fd, MEMGETINFO, &mtd_info) != 0) {...   // get the device info

memset(buf+10, 0xff, mtd_info.writesize - 10);    //Complete buf with 0xff's

if (write(fd, &buf, mtd_info.writesize) < 0) {... // write page

書き込む前に、不良ブロック ( ioctl(fd, MEMGETBADBLOCK, ...) と消去ブロック ( )を確認することも検討してください。ioctl(fd, MEMERASE, ...

お役に立てれば。

于 2015-08-20T16:34:31.347 に答える
1

多分これは役立ちますか?

http://forums.freescale.com/t5/Other-Microcontrollers/Can-t-write-new-uboot-to-mtd0-in-linux-on-MPC8313E-RDB/td-p/34727

それはすべてアクセス権に対処する必要があります。

そして、Jakub と Mat が言うように、各 API 呼び出しのエラー コードを確認してください。

于 2012-04-28T09:11:35.540 に答える