From patchwork Fri Jun 18 10:32:52 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Stanley.Miao" X-Patchwork-Id: 63987 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [18.85.46.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 4CB79B6F10 for ; Tue, 7 Sep 2010 21:06:26 +1000 (EST) Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.72 #1 (Red Hat Linux)) id 1Osvz0-0007ie-HE; Tue, 07 Sep 2010 11:04:58 +0000 Received: from mail.windriver.com ([147.11.1.11]) by bombadil.infradead.org with esmtps (Exim 4.72 #1 (Red Hat Linux)) id 1OPYgg-0005bh-WB for linux-mtd@lists.infradead.org; Fri, 18 Jun 2010 10:20:40 +0000 Received: from ALA-MAIL03.corp.ad.wrs.com (ala-mail03 [147.11.57.144]) by mail.windriver.com (8.14.3/8.14.3) with ESMTP id o5IAKc6S003226; Fri, 18 Jun 2010 03:20:38 -0700 (PDT) Received: from ala-mail06.corp.ad.wrs.com ([147.11.57.147]) by ALA-MAIL03.corp.ad.wrs.com with Microsoft SMTPSVC(6.0.3790.1830); Fri, 18 Jun 2010 03:20:37 -0700 Received: from localhost.localdomain ([128.224.163.169]) by ala-mail06.corp.ad.wrs.com with Microsoft SMTPSVC(6.0.3790.1830); Fri, 18 Jun 2010 03:20:37 -0700 From: "Stanley.Miao" To: linux-mtd@lists.infradead.org Subject: [v3 2/3] Discard the legacy interface MEMGETOOBSEL in flash_eraseall Date: Fri, 18 Jun 2010 18:32:52 +0800 Message-Id: <1276857173-10848-3-git-send-email-stanley.miao@windriver.com> X-Mailer: git-send-email 1.5.6.3 In-Reply-To: <1276857173-10848-2-git-send-email-stanley.miao@windriver.com> References: <1276857173-10848-1-git-send-email-stanley.miao@windriver.com> <1276857173-10848-2-git-send-email-stanley.miao@windriver.com> X-OriginalArrivalTime: 18 Jun 2010 10:20:37.0960 (UTC) FILETIME=[E561C480:01CB0ECF] X-Bad-Reply: References and In-Reply-To but no 'Re:' in Subject. X-CRM114-Version: 20090807-BlameThorstenAndJenny ( TRE 0.7.6 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20100618_062039_440728_8B586F7B X-CRM114-Status: GOOD ( 24.52 ) X-Spam-Score: -0.7 (/) X-Spam-Report: SpamAssassin version 3.3.1 on bombadil.infradead.org summary: Content analysis details: (-0.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [147.11.1.11 listed in list.dnswl.org] -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay domain X-Mailman-Approved-At: Tue, 07 Sep 2010 07:04:57 -0400 Cc: Artem.Bityutskiy@nokia.com, joakim.tjernlund@transmode.se X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-mtd-bounces@lists.infradead.org Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The "struct nand_oobinfo" is able to record only 32 ECC code positions,which is not enough for many big NAND chips. Therefore, this structure is replaced by "struct nand_ecclayout" in linux kernel from the version 2.6.17. Consequently, the ioctl command changed from MEMGETOOBSEL to ECCGETLAYOUT. Now update flash_eraseall to use the new ioctl command ECCGETLAYOUT. In order to keep compatible with the old linux kernel, a linux version detection function is added. Signed-off-by: Stanley.Miao --- flash_eraseall.c | 79 ++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 50 insertions(+), 29 deletions(-) diff --git a/flash_eraseall.c b/flash_eraseall.c index a22fc49..b8068c0 100644 --- a/flash_eraseall.c +++ b/flash_eraseall.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "crc32.h" #include @@ -43,6 +44,8 @@ #define PROGRAM "flash_eraseall" #define VERSION "$Revision: 1.22 $" +#define LINUX_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c)) + static const char *exe_name; static const char *mtd_device; static int quiet; /* true -- don't output progress */ @@ -55,6 +58,28 @@ static void display_version (void); static struct jffs2_unknown_node cleanmarker; int target_endian = __BYTE_ORDER; +static int get_linux_version(void) +{ + int a, b, c, ret, err = 0; + struct utsname buf; + + ret = uname(&buf); + if (ret == 0) { + ret = sscanf(buf.release, "%d.%d.%d", &a, &b, &c); + if (ret != 3) + err = 1; + } else + err = 1; + + if (err) { + a = b = c = 0xff; + fprintf(stderr, "Warning: Can't get linux kernel version." + "Set the default version to the latest version.\n"); + } + + return LINUX_VERSION(a, b, c); +} + int main (int argc, char *argv[]) { mtd_info_t meminfo; @@ -84,41 +109,37 @@ int main (int argc, char *argv[]) if (!isNAND) cleanmarker.totlen = cpu_to_je32 (sizeof (struct jffs2_unknown_node)); else { - struct nand_oobinfo oobinfo; - - if (ioctl(fd, MEMGETOOBSEL, &oobinfo) != 0) { - fprintf(stderr, "%s: %s: unable to get NAND oobinfo\n", exe_name, mtd_device); - return 1; - } + struct nand_ecclayout ecclayout; - /* Check for autoplacement */ - if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) { - /* Get the position of the free bytes */ - if (!oobinfo.oobfree[0][1]) { - fprintf (stderr, " Eeep. Autoplacement selected and no empty space in oob\n"); + memset(&ecclayout, 0, sizeof(ecclayout)); + if (get_linux_version() > LINUX_VERSION(2, 6, 17)) { + if (ioctl(fd, ECCGETLAYOUT, &ecclayout) != 0) { + fprintf(stderr, "%s: %s: unable to get NAND oob layout\n", \ + exe_name, mtd_device); return 1; } - clmpos = oobinfo.oobfree[0][0]; - clmlen = oobinfo.oobfree[0][1]; - if (clmlen > 8) - clmlen = 8; } else { - /* Legacy mode */ - switch (meminfo.oobsize) { - case 8: - clmpos = 6; - clmlen = 2; - break; - case 16: - clmpos = 8; - clmlen = 8; - break; - case 64: - clmpos = 16; - clmlen = 8; - break; + struct nand_oobinfo oi; + + if (ioctl(fd, MEMGETOOBSEL, &oi) != 0) { + fprintf(stderr, "%s: %s: unable to get NAND oobinfo\n", \ + exe_name, mtd_device); + return 1; } + memcpy(&ecclayout.eccpos, &oi.eccpos, sizeof(oi.eccpos)); + memcpy(&ecclayout.oobfree, &oi.oobfree, sizeof(oi.oobfree)); + ecclayout.eccbytes = oi.eccbytes; + } + + /* Get the position of the free bytes */ + if (!ecclayout.oobfree[0].length) { + fprintf(stderr, " Eeep. Autoplacement selected and no empty space in oob\n"); + return 1; } + clmpos = ecclayout.oobfree[0].offset; + clmlen = ecclayout.oobfree[0].length; + if (clmlen > 8) + clmlen = 8; cleanmarker.totlen = cpu_to_je32(8); } cleanmarker.hdr_crc = cpu_to_je32 (crc32 (0, &cleanmarker, sizeof (struct jffs2_unknown_node) - 4));