Skip to contentSkip to conversation

mtd-utils: move libmtd source files to lib/ subdirectory

PATCH Superseded Kevin Cernekee 3 participants 8 messages Jul 5, 2010 – Jul 6, 2010

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 mtd-utils: move libmtd source files to lib/ subdirectory

Source files for libmtd, crc32, and fec are scattered throughout the tree. Move them to a central location so they can be built into a common "libmtd.a" library used by all mtd-utils programs.

This patch only renames/deletes files and does not change the content.

---
 crc32.c                       |   95 ----
 crc32.h                       |   19 -
 fec.c                         |  904 --------------------------------
 include/common.h              |   87 +++
 include/crc32.h               |   22 +
 include/libmtd.h              |  246 +++++++++
 lib/crc32.c                   |   95 ++++
 lib/fec.c                     |  904 ++++++++++++++++++++++++++++++++
 lib/libmtd.c                  | 1157 +++++++++++++++++++++++++++++++++++++++++
 lib/libmtd_int.h              |   88 ++++
 lib/libmtd_legacy.c           |  359 +++++++++++++
 mkfs.ubifs/crc32.c            |   95 ----
 mkfs.ubifs/crc32.h            |   22 -
 ubi-utils/include/libmtd.h    |  246 ---------
 ubi-utils/src/common.h        |   87 ---
 ubi-utils/src/crc32.c         |   95 ----
 ubi-utils/src/crc32.h         |   17 -
 ubi-utils/src/libmtd.c        | 1157 -----------------------------------------
 ubi-utils/src/libmtd_int.h    |   88 ----
 ubi-utils/src/libmtd_legacy.c |  359 -------------
 20 files changed, 2958 insertions(+), 3184 deletions(-)
 delete mode 100644 crc32.c
 delete mode 100644 crc32.h
 delete mode 100644 fec.c
 create mode 100644 include/common.h
 create mode 100644 include/crc32.h
 create mode 100644 include/libmtd.h
 create mode 100644 lib/crc32.c
 create mode 100644 lib/fec.c
 create mode 100644 lib/libmtd.c
 create mode 100644 lib/libmtd_int.h
 create mode 100644 lib/libmtd_legacy.c
 delete mode 100644 mkfs.ubifs/crc32.c
 delete mode 100644 mkfs.ubifs/crc32.h
 delete mode 100644 ubi-utils/include/libmtd.h
 delete mode 100644 ubi-utils/src/common.h
 delete mode 100644 ubi-utils/src/crc32.c
 delete mode 100644 ubi-utils/src/crc32.h
 delete mode 100644 ubi-utils/src/libmtd.c
 delete mode 100644 ubi-utils/src/libmtd_int.h
 delete mode 100644 ubi-utils/src/libmtd_legacy.c
crc32.c deleted +0 -95
deleted file mode 100644
index 6b1e50c..0000000
@@ -1,95 +0,0 @@
-/*
- *  COPYRIGHT (C) 1986 Gary S. Brown.  You may use this program, or
- *  code or tables extracted from it, as desired without restriction.
- *
- *  First, the polynomial itself and its table of feedback terms.  The
- *  polynomial is
- *  X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
- *
- *  Note that we take it "backwards" and put the highest-order term in
- *  the lowest-order bit.  The X^32 term is "implied"; the LSB is the
- *  X^31 term, etc.  The X^0 term (usually shown as "+1") results in
- *  the MSB being 1
- *
- *  Note that the usual hardware shift register implementation, which
- *  is what we're using (we're merely optimizing it by doing eight-bit
- *  chunks at a time) shifts bits into the lowest-order term.  In our
- *  implementation, that means shifting towards the right.  Why do we
- *  do it this way?  Because the calculated CRC must be transmitted in
- *  order from highest-order term to lowest-order term.  UARTs transmit
- *  characters in order from LSB to MSB.  By storing the CRC this way
- *  we hand it to the UART in the order low-byte to high-byte; the UART
- *  sends each low-bit to hight-bit; and the result is transmission bit
- *  by bit from highest- to lowest-order term without requiring any bit
- *  shuffling on our part.  Reception works similarly
- *
- *  The feedback terms table consists of 256, 32-bit entries.  Notes
- *
- *      The table can be generated at runtime if desired; code to do so
- *      is shown later.  It might not be obvious, but the feedback
- *      terms simply represent the results of eight shift/xor opera
- *      tions for all combinations of data and CRC register values
- *
- *      The values must be right-shifted by eight bits by the "updcrc
- *      logic; the shift must be unsigned (bring in zeroes).  On some
- *      hardware you could probably optimize the shift in assembler by
- *      using byte-swap instructions
- *      polynomial $edb88320
- */
-
-#include <stdint.h>
-
-const uint32_t crc32_table[256] = {
-	0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
-	0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
-	0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
-	0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
-	0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
-	0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
-	0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
-	0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
-	0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
-	0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
-	0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
-	0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
-	0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
-	0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
-	0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
-	0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
-	0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
-	0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
-	0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
-	0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
-	0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
-	0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
-	0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
-	0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
-	0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
-	0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
-	0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
-	0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
-	0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
-	0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
-	0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
-	0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
-	0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
-	0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
-	0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
-	0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
-	0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
-	0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
-	0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
-	0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
-	0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
-	0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
-	0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
-	0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
-	0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
-	0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
-	0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
-	0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
-	0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
-	0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
-	0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
-	0x2d02ef8dL
-};
crc32.h deleted +0 -19
deleted file mode 100644
index ee3145b..0000000
@@ -1,19 +0,0 @@
-#ifndef CRC32_H
-#define CRC32_H
-
-#include <stdint.h>
-
-extern const uint32_t crc32_table[256];
-
-/* Return a 32-bit CRC of the contents of the buffer. */
-
-	static inline uint32_t
-crc32(uint32_t val, const void *ss, int len)
-{
-	const unsigned char *s = ss;
-	while (--len >= 0)
-		val = crc32_table[(val ^ *s++) & 0xff] ^ (val >> 8);
-	return val;
-}
-
-#endif
fec.c deleted +0 -904

904 lines not on this page · raw message

include/common.h new file +87 -0

87 lines not on this page · raw message

include/crc32.h new file +22 -0
new file mode 100644
index 0000000..86fc841
@@ -0,0 +1,22 @@
+/*
+ * This code was taken from the linux kernel. The license is GPL Version 2.
+ */
+
+#ifndef __CRC32_H__
+#define __CRC32_H__
+
+#include <stdint.h>
+
+extern const uint32_t crc32_table[256];
+
+/* Return a 32-bit CRC of the contents of the buffer. */
+static inline uint32_t ubifs_crc32(uint32_t val, const void *ss, int len)
+{
+	const unsigned char *s = ss;
+
+	while (--len >= 0)
+		val = crc32_table[(val ^ *s++) & 0xff] ^ (val >> 8);
+	return val;
+}
+
+#endif /* __CRC32_H__ */
include/libmtd.h new file +246 -0

246 lines not on this page · raw message

lib/crc32.c new file +95 -0

95 lines not on this page · raw message

lib/fec.c new file +904 -0

904 lines not on this page · raw message

lib/libmtd.c new file +1157 -0

628 lines not on this page · raw message

lib/libmtd_int.h new file +88 -0

88 lines not on this page · raw message

lib/libmtd_legacy.c new file +359 -0

359 lines not on this page · raw message

mkfs.ubifs/crc32.c deleted +0 -95

95 lines not on this page · raw message

mkfs.ubifs/crc32.h deleted +0 -22
deleted file mode 100644
index 86fc841..0000000

Lines of this file are not shown. View the raw message.

ubi-utils/include/libmtd.h deleted +0 -246

246 lines not on this page · raw message

ubi-utils/src/common.h deleted +0 -87

87 lines not on this page · raw message

ubi-utils/src/crc32.c deleted +0 -95

95 lines not on this page · raw message

ubi-utils/src/crc32.h deleted +0 -17
deleted file mode 100644
index c001fcf..0000000

Lines of this file are not shown. View the raw message.

ubi-utils/src/libmtd.c deleted +0 -1157

1,157 lines not on this page · raw message

ubi-utils/src/libmtd_int.h deleted +0 -88

88 lines not on this page · raw message

ubi-utils/src/libmtd_legacy.c deleted +0 -359

359 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.6.5

Part of the diff is not shown here. View the raw message.

#1 lore raw

PATCH 2/5 mtd-utils: update Makefiles, source files to use common libmtd.a

Modify the build system and source code so that libmtd.a can be built from a "common" location (lib/). Statically link all utilities at the top level with libmtd.a . Minor changes to mkfs.ubifs to allow using the common crc32 implementation.

---
 Makefile                  |   21 ++++++---------------
 common.mk                 |    2 +-
 fectest.c                 |    2 +-
 flash_eraseall.c          |    2 +-
 include/crc32.h           |    2 +-
 jffs2dump.c               |    2 +-
 lib/Makefile              |   22 ++++++++++++++++++++++
 mkfs.jffs2.c              |    4 ++--
 mkfs.ubifs/Makefile       |    3 ++-
 mkfs.ubifs/compr.c        |    4 +++-
 mkfs.ubifs/mkfs.ubifs.c   |    5 +++--
 mkfs.ubifs/mkfs.ubifs.h   |    1 -
 recv_image.c              |    2 +-
 serve_image.c             |    2 +-
 sumtool.c                 |    2 +-
 ubi-utils/Makefile        |   15 +++++++--------
 ubi-utils/src/libscan.c   |    2 +-
 ubi-utils/src/libubigen.c |    2 +-
 ubi-utils/src/ubicrc32.c  |    2 +-
 ubi-utils/src/ubiformat.c |    2 +-
 20 files changed, 57 insertions(+), 42 deletions(-)
 create mode 100644 lib/Makefile
Makefile +6 -15
index 577634f..b558705 100644
@@ -7,7 +7,7 @@ ifeq ($(WITHOUT_XATTR), 1)
   CPPFLAGS += -DWITHOUT_XATTR
 endif
 
-SUBDIRS = ubi-utils mkfs.ubifs
+SUBDIRS = lib ubi-utils mkfs.ubifs
 
 TARGETS = ftl_format flash_erase flash_eraseall nanddump doc_loadbios \
 	ftl_check mkfs.jffs2 flash_lock flash_unlock flash_info \
@@ -20,6 +20,9 @@ TARGETS = ftl_format flash_erase flash_eraseall nanddump doc_loadbios \
 
 SYMLINKS =
 
+LDLIBS = -L$(BUILDDIR)/lib -lmtd
+LDDEPS = $(BUILDDIR)/lib/libmtd.a
+
 include common.mk
 
 clean::
@@ -36,28 +39,16 @@ $(SYMLINKS):
 	ln -sf ../fs/jffs2/$@ $@
 
 $(BUILDDIR)/mkfs.jffs2: $(addprefix $(BUILDDIR)/,\
-	crc32.o compr_rtime.o mkfs.jffs2.o compr_zlib.o compr_lzo.o \
+	compr_rtime.o mkfs.jffs2.o compr_zlib.o compr_lzo.o \
 	compr.o rbtree.o)
 LDFLAGS_mkfs.jffs2 = $(ZLIBLDFLAGS) $(LZOLDFLAGS)
 LDLIBS_mkfs.jffs2  = -lz -llzo2
 
-$(BUILDDIR)/flash_eraseall: $(BUILDDIR)/crc32.o $(BUILDDIR)/flash_eraseall.o
-
 $(BUILDDIR)/jffs2reader: $(BUILDDIR)/jffs2reader.o
 LDFLAGS_jffs2reader = $(ZLIBLDFLAGS) $(LZOLDFLAGS)
 LDLIBS_jffs2reader  = -lz -llzo2
 
-$(BUILDDIR)/jffs2dump: $(BUILDDIR)/jffs2dump.o $(BUILDDIR)/crc32.o
-
-$(BUILDDIR)/sumtool: $(BUILDDIR)/sumtool.o $(BUILDDIR)/crc32.o
-
-$(BUILDDIR)/serve_image: $(BUILDDIR)/serve_image.o $(BUILDDIR)/crc32.o $(BUILDDIR)/fec.o
-
-$(BUILDDIR)/recv_image: $(BUILDDIR)/recv_image.o $(BUILDDIR)/crc32.o $(BUILDDIR)/fec.o
-
-$(BUILDDIR)/fectest: $(BUILDDIR)/fectest.o $(BUILDDIR)/crc32.o $(BUILDDIR)/fec.o
-
-
+$(BUILDDIR)/lib/libmtd.a: subdirs_lib_all ;
 
 install:: ${TARGETS}
 	mkdir -p ${DESTDIR}/${SBINDIR}
common.mk +1 -1
index d704b44..be785b4 100644
@@ -47,7 +47,7 @@ clean:: $(SUBDIRS_CLEAN)
 
 install:: $(TARGETS) $(SUBDIRS_INSTALL)
 
-%: %.o
+%: %.o $(LDDEPS) $(LDDEPS_$(notdir $@))
 	$(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_$(notdir $@)) -g -o $@ $^ $(LDLIBS) $(LDLIBS_$(notdir $@))
 
 $(BUILDDIR)/%.a:
fectest.c +1 -1
index d5893b9..c1fbd52 100644
@@ -8,7 +8,7 @@
 #include <sys/stat.h>
 
 #include "mcast_image.h"
-#include "crc32.h"
+#include <crc32.h>
 
 #define ERASE_SIZE 131072
 //#define PKT_SIZE 1400
flash_eraseall.c +1 -1
index a22fc49..e6f8d50 100644
@@ -35,7 +35,7 @@
 #include <getopt.h>
 #include <sys/ioctl.h>
 #include <sys/mount.h>
-#include "crc32.h"
+#include <crc32.h>
 
 #include <mtd/mtd-user.h>
 #include <mtd/jffs2-user.h>
include/crc32.h +1 -1
index 86fc841..4b51177 100644
@@ -10,7 +10,7 @@
 extern const uint32_t crc32_table[256];
 
 /* Return a 32-bit CRC of the contents of the buffer. */
-static inline uint32_t ubifs_crc32(uint32_t val, const void *ss, int len)
+static inline uint32_t crc32(uint32_t val, const void *ss, int len)
 {
 	const unsigned char *s = ss;
 
jffs2dump.c +1 -1
index 2802682..cd8b6fa 100644
@@ -31,7 +31,7 @@
 #include <endian.h>
 #include <byteswap.h>
 #include <getopt.h>
-#include "crc32.h"
+#include <crc32.h>
 #include "summary.h"
 
 #define PROGRAM "jffs2dump"
lib/Makefile new file +22 -0
new file mode 100644
index 0000000..91507f3
@@ -0,0 +1,22 @@
+#
+# Makefile for libmtd
+#
+
+SUBDIRS =
+
+# CFLAGS += -Werror
+CPPFLAGS += -I../include
+LIBS = libmtd
+TARGETS = libmtd.a
+
+include ../common.mk
+
+$(BUILDDIR)/libmtd.a: $(addprefix $(BUILDDIR)/,\
+	libmtd.o libmtd_legacy.o crc32.o fec.o)
+
+clean::
+	rm -f $(addsuffix .a, $(LIBS))
+
+install::
+
+uninstall:
mkfs.jffs2.c +2 -2
index 64cafb6..2c78fd5 100644
@@ -69,10 +69,10 @@
 #include <sys/acl.h>
 #endif
 #include <byteswap.h>
-#define crc32 __complete_crap
+#define crc32 __zlib_crc32
 #include <zlib.h>
 #undef crc32
-#include "crc32.h"
+#include <crc32.h>
 #include "rbtree.h"
 
 /* Do not use the weird XPG version of basename */
mkfs.ubifs/Makefile +2 -1
index 61d0e20..ba21a8c 100644
@@ -7,12 +7,13 @@ ALL_SOURCES=*.[ch] hashtable/*.[ch]
 TARGETS = mkfs.ubifs
 
 LDLIBS_mkfs.ubifs = -lz -llzo2 -lm -luuid -L$(BUILDDIR)/../ubi-utils/ -lubi
+LDLIBS_mkfs.ubifs += -L$(BUILDDIR)/../lib -lmtd
 LDLIBS_mkfs.ubifs += $(ZLIBLDFLAGS) $(LZOLDFLAGS)
 
 include ../common.mk
 
 $(BUILDDIR)/mkfs.ubifs: $(addprefix $(BUILDDIR)/,\
-	crc16.o crc32.o lpt.o compr.o devtable.o \
+	crc16.o lpt.o compr.o devtable.o \
 	hashtable/hashtable.o hashtable/hashtable_itr.o)
 
 clean::
mkfs.ubifs/compr.c +3 -1
index e378c5d..7f084c5 100644
@@ -24,10 +24,12 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
-#include <zlib.h>
 #include <lzo/lzo1x.h>
 #include <linux/types.h>
 
+#define crc32 __zlib_crc32
+#include <zlib.h>
+
 #include "compr.h"
 #include "ubifs-media.h"
 #include "mkfs.ubifs.h"
mkfs.ubifs/mkfs.ubifs.c +3 -2
index e4b4e3c..95b20b0 100644
@@ -21,6 +21,7 @@
  */
 
 #include "mkfs.ubifs.h"
+#include <crc32.h>
 
 #define PROGRAM_VERSION "1.3"
 
@@ -752,7 +753,7 @@ static void prepare_node(void *node, int len)
 	ch->group_type = UBIFS_NO_NODE_GROUP;
 	ch->sqnum = cpu_to_le64(++c->max_sqnum);
 	ch->padding[0] = ch->padding[1] = 0;
-	crc = ubifs_crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
+	crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
 	ch->crc = cpu_to_le32(crc);
 }
 
@@ -822,7 +823,7 @@ static int do_pad(void *buf, int len)
 		pad_len -= UBIFS_PAD_NODE_SZ;
 		pad_node->pad_len = cpu_to_le32(pad_len);
 
-		crc = ubifs_crc32(UBIFS_CRC32_INIT, buf + 8,
+		crc = crc32(UBIFS_CRC32_INIT, buf + 8,
 				  UBIFS_PAD_NODE_SZ - 8);
 		ch->crc = cpu_to_le32(crc);
 
mkfs.ubifs/mkfs.ubifs.h +0 -1
index 16b34c7..c00dce0 100644
@@ -49,7 +49,6 @@
 #include <sys/file.h>
 
 #include "libubi.h"
-#include "crc32.h"
 #include "defs.h"
 #include "crc16.h"
 #include "ubifs-media.h"
recv_image.c +1 -1
index d65aa2a..2be511a 100644
@@ -16,7 +16,7 @@
 #include <netinet/in.h>
 #include <sys/ioctl.h>
 #include <sys/time.h>
-#include "crc32.h"
+#include <crc32.h>
 #include "mtd/mtd-user.h"
 #include "mcast_image.h"
 
serve_image.c +1 -1
index adb4869..5aafa35 100644
@@ -16,7 +16,7 @@
 #include <sys/mman.h>
 #include <netinet/in.h>
 #include <sys/time.h>
-#include "crc32.h"
+#include <crc32.h>
 #include "mcast_image.h"
 
 int tx_rate = 80000;
sumtool.c +1 -1
index 6bb7168..966e110 100644
@@ -44,7 +44,7 @@
 #include <endian.h>
 #include <byteswap.h>
 #include <getopt.h>
-#include "crc32.h"
+#include <crc32.h>
 #include "summary.h"
 
 #define PAD(x) (((x)+3)&~3)
ubi-utils/Makefile +7 -8
index e736a09..327d2d7 100644
@@ -9,11 +9,12 @@ SUBDIRS = old-utils
 # CFLAGS += -Werror
 CPPFLAGS += -Iinclude -Isrc -I$(KERNELHDR)
 
-LIBS = libubi libmtd libubigen libiniparser libscan
+LIBS = libubi libubigen libiniparser libscan
 TARGETS = ubiupdatevol ubimkvol ubirmvol ubicrc32 ubinfo ubiattach \
           ubidetach ubinize ubiformat ubirename mtdinfo ubirsvol
 
 VPATH = src
+LDLIBS = -L$(BUILDDIR)/../lib -lmtd
 
 include ../common.mk
 
@@ -23,32 +24,30 @@ $(TARGETS): $(addprefix $(BUILDDIR)/,\
 #	$(CC) $(CFLAGS) $(filter %.o, $^) -L. -lubi -o $@
 
 $(BUILDDIR)/ubicrc32: $(addprefix $(BUILDDIR)/,\
-	ubicrc32.o crc32.o)
+	ubicrc32.o)
 #	$(CC) $(CFLAGS) -o $@ $^
 
 $(BUILDDIR)/ubinize: $(addprefix $(BUILDDIR)/,\
-	ubinize.o common.o crc32.o libiniparser.a libubigen.a)
+	ubinize.o common.o libiniparser.a libubigen.a)
 #	$(CC) $(CFLAGS) $(filter %.o, $^) -L. -liniparser -lubigen -o $@
 
 $(BUILDDIR)/mtdinfo: $(addprefix $(BUILDDIR)/,\
-	libmtd.a libubigen.a crc32.o common.o)
+	libubigen.a common.o)
 #	$(CC) $(CFLAGS) $(filter %.o, $^) -L. -lmtd -lubigen -o $@
 
 $(BUILDDIR)/ubiformat: $(addprefix $(BUILDDIR)/,\
-	ubiformat.o common.o crc32.o libmtd.a libscan.a libubi.a libubigen.a)
+	ubiformat.o common.o libscan.a libubi.a libubigen.a)
 #	$(CC) $(CFLAGS) $(filter %.o, $^) -L. -lmtd -lscan -lubi -lubigen -o $@
 
 $(BUILDDIR)/libubi.a: $(BUILDDIR)/libubi.o
 
-$(BUILDDIR)/libmtd.a: $(BUILDDIR)/libmtd.o $(BUILDDIR)/libmtd_legacy.o
-
 $(BUILDDIR)/libubigen.a: $(BUILDDIR)/libubigen.o
 
 $(BUILDDIR)/libiniparser.a: $(addprefix $(BUILDDIR)/,\
 	libiniparser.o dictionary.o)
 
 $(BUILDDIR)/libscan.a: $(addprefix $(BUILDDIR)/,\
-	libscan.o crc32.o)
+	libscan.o)
 
 clean::
 	rm -f $(addsuffix .a, $(LIBS))
ubi-utils/src/libscan.c +1 -1
index 5a2ea78..85f3d7f 100644
@@ -32,8 +32,8 @@
 #include <mtd/mtd-user.h>
 #include <libmtd.h>
 #include <libscan.h>
+#include <crc32.h>
 #include "common.h"
-#include "crc32.h"
 
 #define PROGRAM_NAME "libscan"
 
ubi-utils/src/libubigen.c +1 -1
index 8f060da..62792d0 100644
@@ -32,7 +32,7 @@
 #include <mtd/ubi-media.h>
 #include <mtd_swab.h>
 #include <libubigen.h>
-#include "crc32.h"
+#include <crc32.h>
 #include "common.h"
 
 #define PROGRAM_NAME "libubigen"
ubi-utils/src/ubicrc32.c +1 -1
index d39af10..2dd69e1 100644
@@ -28,8 +28,8 @@
 #include <getopt.h>
 #include <unistd.h>
 #include <mtd/ubi-media.h>
+#include <crc32.h>
 
-#include "crc32.h"
 #include "common.h"
 
 #define BUFSIZE 4096
ubi-utils/src/ubiformat.c +1 -1
index 8487fd5..f0a87c4 100644
@@ -41,7 +41,7 @@
 #include <libscan.h>
 #include <libubigen.h>
 #include <mtd_swab.h>
-#include "crc32.h"
+#include <crc32.h>
 #include "common.h"
 
 #define PROGRAM_VERSION "1.5"
Signature · 1 line
1.6.5
#2 lore raw

PATCH 3/5 mtd-utils: update to latest mtd-abi.h from kernel.org

In reply to Kevin Cernekee
---
 include/mtd/mtd-abi.h |   89 ++++++++++++++++++++++++++++--------------------
 mtd_debug.c           |    2 +-
 2 files changed, 53 insertions(+), 38 deletions(-)
include/mtd/mtd-abi.h +52 -37
index 86defe1..c6954ed 100644
@@ -1,23 +1,35 @@
 /*
- * $Id: mtd-abi.h,v 1.13 2005/11/07 11:14:56 gleixner Exp $
- *
  * Portions of MTD ABI definition which are shared by kernel and user space
  */
 
 #ifndef __MTD_ABI_H__
 #define __MTD_ABI_H__
 
+#include <linux/types.h>
+
 struct erase_info_user {
-	uint32_t start;
-	uint32_t length;
+	__u32 start;
+	__u32 length;
+};
+
+struct erase_info_user64 {
+	__u64 start;
+	__u64 length;
 };
 
 struct mtd_oob_buf {
-	uint32_t start;
-	uint32_t length;
+	__u32 start;
+	__u32 length;
 	unsigned char *ptr;
 };
 
+struct mtd_oob_buf64 {
+	__u64 start;
+	__u32 pad;
+	__u32 length;
+	__u64 usr_ptr;
+};
+
 #define MTD_ABSENT		0
 #define MTD_RAM			1
 #define MTD_ROM			2
@@ -29,7 +41,7 @@ struct mtd_oob_buf {
 #define MTD_WRITEABLE		0x400	/* Device is writeable */
 #define MTD_BIT_WRITEABLE	0x800	/* Single bits can be flipped */
 #define MTD_NO_ERASE		0x1000	/* No erase necessary */
-#define MTD_STUPID_LOCK		0x2000	/* Always locked after reset */
+#define MTD_POWERUP_LOCK	0x2000	/* Always locked after reset */
 
 // Some common devices / combinations of capabilities
 #define MTD_CAP_ROM		0
@@ -50,30 +62,30 @@ struct mtd_oob_buf {
 #define MTD_OTP_USER		2
 
 struct mtd_info_user {
-	uint8_t type;
-	uint32_t flags;
-	uint32_t size;	 // Total size of the MTD
-	uint32_t erasesize;
-	uint32_t writesize;
-	uint32_t oobsize;   // Amount of OOB data per block (e.g. 16)
+	__u8 type;
+	__u32 flags;
+	__u32 size;	 // Total size of the MTD
+	__u32 erasesize;
+	__u32 writesize;
+	__u32 oobsize;   // Amount of OOB data per block (e.g. 16)
 	/* The below two fields are obsolete and broken, do not use them
 	 * (TODO: remove at some point) */
-	uint32_t ecctype;
-	uint32_t eccsize;
+	__u32 ecctype;
+	__u32 eccsize;
 };
 
 struct region_info_user {
-	uint32_t offset;		/* At which this region starts,
+	__u32 offset;		/* At which this region starts,
 					 * from the beginning of the MTD */
-	uint32_t erasesize;		/* For this region */
-	uint32_t numblocks;		/* Number of blocks in this region */
-	uint32_t regionindex;
+	__u32 erasesize;		/* For this region */
+	__u32 numblocks;		/* Number of blocks in this region */
+	__u32 regionindex;
 };
 
 struct otp_info {
-	uint32_t start;
-	uint32_t length;
-	uint32_t locked;
+	__u32 start;
+	__u32 length;
+	__u32 locked;
 };
 
 #define MEMGETINFO		_IOR('M', 1, struct mtd_info_user)
@@ -86,8 +98,8 @@ struct otp_info {
 #define MEMGETREGIONINFO	_IOWR('M', 8, struct region_info_user)
 #define MEMSETOOBSEL		_IOW('M', 9, struct nand_oobinfo)
 #define MEMGETOOBSEL		_IOR('M', 10, struct nand_oobinfo)
-#define MEMGETBADBLOCK		_IOW('M', 11, loff_t)
-#define MEMSETBADBLOCK		_IOW('M', 12, loff_t)
+#define MEMGETBADBLOCK		_IOW('M', 11, __kernel_loff_t)
+#define MEMSETBADBLOCK		_IOW('M', 12, __kernel_loff_t)
 #define OTPSELECT		_IOR('M', 13, int)
 #define OTPGETREGIONCOUNT	_IOW('M', 14, int)
 #define OTPGETREGIONINFO	_IOW('M', 15, struct otp_info)
@@ -95,21 +107,24 @@ struct otp_info {
 #define ECCGETLAYOUT		_IOR('M', 17, struct nand_ecclayout)
 #define ECCGETSTATS		_IOR('M', 18, struct mtd_ecc_stats)
 #define MTDFILEMODE		_IO('M', 19)
+#define MEMERASE64		_IOW('M', 20, struct erase_info_user64)
+#define MEMWRITEOOB64		_IOWR('M', 21, struct mtd_oob_buf64)
+#define MEMREADOOB64		_IOWR('M', 22, struct mtd_oob_buf64)
 
 /*
  * Obsolete legacy interface. Keep it in order not to break userspace
  * interfaces
  */
 struct nand_oobinfo {
-	uint32_t useecc;
-	uint32_t eccbytes;
-	uint32_t oobfree[8][2];
-	uint32_t eccpos[32];
+	__u32 useecc;
+	__u32 eccbytes;
+	__u32 oobfree[8][2];
+	__u32 eccpos[32];
 };
 
 struct nand_oobfree {
-	uint32_t offset;
-	uint32_t length;
+	__u32 offset;
+	__u32 length;
 };
 
 #define MTD_MAX_OOBFREE_ENTRIES	8
@@ -118,9 +133,9 @@ struct nand_oobfree {
  * diagnosis and to allow creation of raw images
  */
 struct nand_ecclayout {
-	uint32_t eccbytes;
-	uint32_t eccpos[64];
-	uint32_t oobavail;
+	__u32 eccbytes;
+	__u32 eccpos[64];
+	__u32 oobavail;
 	struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES];
 };
 
@@ -133,10 +148,10 @@ struct nand_ecclayout {
  * @bbtblocks:	number of blocks reserved for bad block tables
  */
 struct mtd_ecc_stats {
-	uint32_t corrected;
-	uint32_t failed;
-	uint32_t badblocks;
-	uint32_t bbtblocks;
+	__u32 corrected;
+	__u32 failed;
+	__u32 badblocks;
+	__u32 bbtblocks;
 };
 
 /*
mtd_debug.c +1 -1
index 49a4567..4934699 100644
@@ -301,7 +301,7 @@ int showinfo (int fd)
 			{ "MTD_WRITEABLE", MTD_WRITEABLE },
 			{ "MTD_BIT_WRITEABLE", MTD_BIT_WRITEABLE },
 			{ "MTD_NO_ERASE", MTD_NO_ERASE },
-			{ "MTD_STUPID_LOCK", MTD_STUPID_LOCK },
+			{ "MTD_POWERUP_LOCK", MTD_POWERUP_LOCK },
 			{ NULL, -1 }
 		};
 		for (i = 0; flags[i].name != NULL; i++)
Signature · 1 line
1.6.5
#3 lore raw

PATCH 4/5 libmtd: add support for 64-bit offsets, OOB

In reply to Kevin Cernekee

Change mtd_erase() so that it attempts to use MEMERASE64 first, then falls back to the old <2.6.31 MEMERASE if MEMERASE64 is unsupported.

Add mtd_read_oob(), mtd_write_oob() functions to wrap the OOB ioctls. Similar ioctl fallback logic is used in these functions as well.

---
 include/libmtd.h |   26 +++++++++++++++++++++
 lib/libmtd.c     |   65 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 88 insertions(+), 3 deletions(-)
include/libmtd.h +26 -0
index 0aea966..2241343 100644
@@ -149,6 +149,32 @@ int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd);
 int mtd_erase(const struct mtd_dev_info *mtd, int fd, int eb);
 
 /**
+ * mtd_read_oob - read OOB bytes
+ * @fd: MTD device node file descriptor
+ * @start: page-aligned start address
+ * @length: number of OOB bytes to read
+ * @data: read buffer
+ *
+ * This function reads @length OOB bytes starting from address @start on
+ * MTD device described by @fd. Returns %0 in case of success and %-1 in
+ * case of failure.
+ */
+int mtd_read_oob(int fd, uint64_t start, uint64_t length, void *data);
+
+/**
+ * mtd_write_oob - write OOB bytes
+ * @fd: MTD device node file descriptor
+ * @start: page-aligned start address
+ * @length: number of OOB bytes to write
+ * @data: write buffer
+ *
+ * This function writes @length OOB bytes starting from address @start on
+ * MTD device described by @fd. Returns %0 in case of success and %-1 in
+ * case of failure.
+ */
+int mtd_write_oob(int fd, uint64_t start, uint64_t length, void *data);
+
+/**
  * mtd_torture - torture an eraseblock.
  * @mtd: MTD device description object
  * @fd: MTD device node file descriptor
lib/libmtd.c +62 -3

79 lines not on this page · raw message

Signature · 1 line
1.6.5
#6 lore raw

PATCH 5/5 mtd-utils: change flash_eraseall to use libmtd-wrapped ioctls

In reply to Kevin Cernekee
---
 flash_eraseall.c |   58 +++++++++++++++++++++++++++--------------------------
 1 files changed, 30 insertions(+), 28 deletions(-)
flash_eraseall.c +30 -28
index e6f8d50..b8222ad 100644
@@ -36,6 +36,7 @@
 #include <sys/ioctl.h>
 #include <sys/mount.h>
 #include <crc32.h>
+#include <libmtd.h>
 
 #include <mtd/mtd-user.h>
 #include <mtd/jffs2-user.h>
@@ -49,7 +50,7 @@ static int quiet;		/* true -- don't output progress */
 static int jffs2;		// format for jffs2 usage
 
 static void process_options (int argc, char *argv[]);
-void show_progress (mtd_info_t *meminfo, erase_info_t *erase);
+void show_progress (struct mtd_dev_info *mtd, uint64_t start);
 static void display_help (void);
 static void display_version (void);
 static struct jffs2_unknown_node cleanmarker;
@@ -57,26 +58,31 @@ int target_endian = __BYTE_ORDER;
 
 int main (int argc, char *argv[])
 {
-	mtd_info_t meminfo;
-	int fd, clmpos = 0, clmlen = 8;
-	erase_info_t erase;
+	libmtd_t mtd_desc;
+	struct mtd_dev_info mtd;
+	int fd, clmpos = 0, clmlen = 8, eb;
 	int isNAND, bbtest = 1;
+	uint64_t offset = 0;
 
 	process_options(argc, argv);
 
+	mtd_desc = libmtd_open();
+	if (mtd_desc == NULL) {
+		fprintf(stderr, "%s: can't initialize libmtd\n", exe_name);
+		return 1;
+	}
+
 	if ((fd = open(mtd_device, O_RDWR)) < 0) {
 		fprintf(stderr, "%s: %s: %s\n", exe_name, mtd_device, strerror(errno));
 		return 1;
 	}
 
-
-	if (ioctl(fd, MEMGETINFO, &meminfo) != 0) {
-		fprintf(stderr, "%s: %s: unable to get MTD device info\n", exe_name, mtd_device);
+	if (mtd_get_dev_info(mtd_desc, mtd_device, &mtd) < 0) {
+		fprintf(stderr, "%s: mtd_get_dev_info failed\n", exe_name);
 		return 1;
 	}
 
-	erase.length = meminfo.erasesize;
-	isNAND = meminfo.type == MTD_NANDFLASH ? 1 : 0;
+	isNAND = mtd.type == MTD_NANDFLASH ? 1 : 0;
 
 	if (jffs2) {
 		cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
@@ -104,7 +110,7 @@ int main (int argc, char *argv[])
 					clmlen = 8;
 			} else {
 				/* Legacy mode */
-				switch (meminfo.oobsize) {
+				switch (mtd.oob_size) {
 					case 8:
 						clmpos = 6;
 						clmlen = 2;
@@ -124,13 +130,13 @@ int main (int argc, char *argv[])
 		cleanmarker.hdr_crc =  cpu_to_je32 (crc32 (0, &cleanmarker,  sizeof (struct jffs2_unknown_node) - 4));
 	}
 
-	for (erase.start = 0; erase.start < meminfo.size; erase.start += meminfo.erasesize) {
+	for (eb = 0; eb < (mtd.size / mtd.eb_size); eb++) {
+		offset = eb * mtd.eb_size;
 		if (bbtest) {
-			loff_t offset = erase.start;
-			int ret = ioctl(fd, MEMGETBADBLOCK, &offset);
+			int ret = mtd_is_bad(&mtd, fd, eb);
 			if (ret > 0) {
 				if (!quiet)
-					printf ("\nSkipping bad block at 0x%08x\n", erase.start);
+					printf ("\nSkipping bad block at 0x%08llx\n", (unsigned long long)offset);
 				continue;
 			} else if (ret < 0) {
 				if (errno == EOPNOTSUPP) {
@@ -147,9 +153,9 @@ int main (int argc, char *argv[])
 		}
 
 		if (!quiet)
-			show_progress(&meminfo, &erase);
+			show_progress(&mtd, offset);
 
-		if (ioctl(fd, MEMERASE, &erase) != 0) {
+		if (mtd_erase(&mtd, fd, eb) != 0) {
 			fprintf(stderr, "\n%s: %s: MTD Erase failure: %s\n", exe_name, mtd_device, strerror(errno));
 			continue;
 		}
@@ -160,16 +166,12 @@ int main (int argc, char *argv[])
 
 		/* write cleanmarker */
 		if (isNAND) {
-			struct mtd_oob_buf oob;
-			oob.ptr = (unsigned char *) &cleanmarker;
-			oob.start = erase.start + clmpos;
-			oob.length = clmlen;
-			if (ioctl (fd, MEMWRITEOOB, &oob) != 0) {
+			if (mtd_write_oob(fd, offset + clmpos, clmlen, &cleanmarker) != 0) {
 				fprintf(stderr, "\n%s: %s: MTD writeoob failure: %s\n", exe_name, mtd_device, strerror(errno));
 				continue;
 			}
 		} else {
-			if (lseek (fd, erase.start, SEEK_SET) < 0) {
+			if (lseek (fd, (loff_t)offset, SEEK_SET) < 0) {
 				fprintf(stderr, "\n%s: %s: MTD lseek failure: %s\n", exe_name, mtd_device, strerror(errno));
 				continue;
 			}
@@ -179,10 +181,10 @@ int main (int argc, char *argv[])
 			}
 		}
 		if (!quiet)
-			printf (" Cleanmarker written at %x.", erase.start);
+			printf (" Cleanmarker written at %llx.", (unsigned long long)offset);
 	}
 	if (!quiet) {
-		show_progress(&meminfo, &erase);
+		show_progress(&mtd, offset);
 		printf("\n");
 	}
 
@@ -250,11 +252,11 @@ void process_options (int argc, char *argv[])
 	mtd_device = argv[optind];
 }
 
-void show_progress (mtd_info_t *meminfo, erase_info_t *erase)
+void show_progress (struct mtd_dev_info *mtd, uint64_t start)
 {
-	printf("\rErasing %d Kibyte @ %x -- %2llu %% complete.",
-		meminfo->erasesize / 1024, erase->start,
-		(unsigned long long) erase->start * 100 / meminfo->size);
+	printf("\rErasing %d Kibyte @ %llx -- %2llu %% complete.",
+		mtd->eb_size / 1024, (unsigned long long)start,
+		(unsigned long long) start * 100 / mtd->size);
 	fflush(stdout);
 }
 
Signature · 1 line
1.6.5
#7 lore raw
In reply to Kevin Cernekee
Quoted 5 lines · Kevin Cernekee

Source files for libmtd, crc32, and fec are scattered throughout the tree.  Move them to a central location so they can be built into a common "libmtd.a" library used by all mtd-utils programs.

This patch only renames/deletes files and does not change the content.

might wan to use the -M flag next time
-mike

#4 lore raw

Re: [PATCH 4/5] libmtd: add support for 64-bit offsets, OOB

In reply to Kevin Cernekee
Quoted 2 lines · Kevin Cernekee

Change mtd_erase() so that it attempts to use MEMERASE64 first, then falls back to the old <2.6.31 MEMERASE if MEMERASE64 is unsupported.

very cool

Quoted 4 lines

+               struct erase_info_user ei_old;
+
+               ei_old.start = (__u32)ei.start;
+               ei_old.length = (__u32)ei.length;

the casts are useless since the assigned types are __u32

Quoted 4 lines

+               struct mtd_oob_buf oob_old; +
+               oob_old.start = (__u32)oob.start;
+               oob_old.length = (__u32)oob.length;

same here
-mike

#5 lore raw

Re: [PATCH 4/5] libmtd: add support for 64-bit offsets, OOB

In reply to Kevin Cernekee
Quoted 81 lines · Kevin Cernekee

Change mtd_erase() so that it attempts to use MEMERASE64 first, then falls back to the old <2.6.31 MEMERASE if MEMERASE64 is unsupported.

Add mtd_read_oob(), mtd_write_oob() functions to wrap the OOB ioctls. Similar ioctl fallback logic is used in these functions as well.

---
 include/libmtd.h |   26 +++++++++++++++++++++
 lib/libmtd.c     |   65 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 88 insertions(+), 3 deletions(-)
include/libmtd.h +26 -0
index 0aea966..2241343 100644
@@ -149,6 +149,32 @@ int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd);
 int mtd_erase(const struct mtd_dev_info *mtd, int fd, int eb);
 
 /**
+ * mtd_read_oob - read OOB bytes
+ * @fd: MTD device node file descriptor
+ * @start: page-aligned start address
+ * @length: number of OOB bytes to read
+ * @data: read buffer
+ *
+ * This function reads @length OOB bytes starting from address @start on
+ * MTD device described by @fd. Returns %0 in case of success and %-1 in
+ * case of failure.
+ */
+int mtd_read_oob(int fd, uint64_t start, uint64_t length, void *data);
+
+/**
+ * mtd_write_oob - write OOB bytes
+ * @fd: MTD device node file descriptor
+ * @start: page-aligned start address
+ * @length: number of OOB bytes to write
+ * @data: write buffer
+ *
+ * This function writes @length OOB bytes starting from address @start on
+ * MTD device described by @fd. Returns %0 in case of success and %-1 in
+ * case of failure.
+ */
+int mtd_write_oob(int fd, uint64_t start, uint64_t length, void *data);
+
+/**
  * mtd_torture - torture an eraseblock.
  * @mtd: MTD device description object
  * @fd: MTD device node file descriptor
lib/libmtd.c +12 -3
index 3ff031c..368493b 100644
@@ -32,6 +32,7 @@
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <mtd/mtd-user.h>
+#include <mtd/mtd-abi.h>
 
 #include <libmtd.h>
 #include "libmtd_int.h"
@@ -791,11 +792,69 @@ int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd)
 
 int mtd_erase(const struct mtd_dev_info *mtd, int fd, int eb)
 {
-	struct erase_info_user ei;
+	int ret;
+	struct erase_info_user64 ei;
 
-	ei.start = eb * mtd->eb_size;;
+	ei.start = (__u64)eb * mtd->eb_size;
 	ei.length = mtd->eb_size;
-	return ioctl(fd, MEMERASE, &ei);
+	ret = ioctl(fd, MEMERASE64, &ei);
+
+	if (ret < 0 && errno == ENOTTY) {
+		struct erase_info_user ei_old;
+
+		ei_old.start = (__u32)ei.start;
+		ei_old.length = (__u32)ei.length;
+

lib/libmtd.c int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd) · line 817

Quoted 10 lines
lib/libmtd.c +10 -0
+		if (ei_old.start != ei.start) {
+			errno = EOVERFLOW;
+			return -1;
+		}
+
+		ret = ioctl(fd, MEMERASE, &ei_old);
+	}
+
+	return ret;
+}

Kevin, looks like a good sanitization in general, thanks. But let's avoid calling 2 ioctls for old kernels - just introduce a flag in 'struct mtd_dev_info' and check whether 64-bit offsets are supported.

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Артём Битюцкий)