From 2134e864c823b456d6de0a128bb6c55d1a04aba7 Mon Sep 17 00:00:00 2001 From: James Maki Date: Fri, 23 Apr 2010 14:27:29 -0500 Subject: initial commit --- src/Makefile.am | 8 + src/Makefile.in | 414 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/mtd_erase_all.c | 181 +++++++++++++++++++++++ src/mtd_erase_all.h | 6 + src/u_boot.c | 363 +++++++++++++++++++++++++++++++++++++++++++++ src/u_boot.h | 19 +++ 6 files changed, 991 insertions(+) create mode 100644 src/Makefile.am create mode 100644 src/Makefile.in create mode 100644 src/mtd_erase_all.c create mode 100644 src/mtd_erase_all.h create mode 100644 src/u_boot.c create mode 100644 src/u_boot.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..5016896 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,8 @@ +AUTOMAKE_OPTIONS = gnu +AM_CFLAGS = -Wall +bin_PROGRAMS = u-boot +u_boot_SOURCES = u_boot.c mtd_erase_all.c +noinst_HEADERS = u_boot.h mtd_erase_all.h + +EXTRA_DIST = + diff --git a/src/Makefile.in b/src/Makefile.in new file mode 100644 index 0000000..b5688b0 --- /dev/null +++ b/src/Makefile.in @@ -0,0 +1,414 @@ +# Makefile.in generated by automake 1.10.1 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + + +VPATH = @srcdir@ +pkgdatadir = $(datadir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +bin_PROGRAMS = u-boot$(EXEEXT) +subdir = src +DIST_COMMON = $(noinst_HEADERS) $(srcdir)/Makefile.am \ + $(srcdir)/Makefile.in +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/configure.in +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +am__installdirs = "$(DESTDIR)$(bindir)" +binPROGRAMS_INSTALL = $(INSTALL_PROGRAM) +PROGRAMS = $(bin_PROGRAMS) +am_u_boot_OBJECTS = u_boot.$(OBJEXT) mtd_erase_all.$(OBJEXT) +u_boot_OBJECTS = $(am_u_boot_OBJECTS) +u_boot_LDADD = $(LDADD) +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__depfiles_maybe = depfiles +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +CCLD = $(CC) +LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ +SOURCES = $(u_boot_SOURCES) +DIST_SOURCES = $(u_boot_SOURCES) +HEADERS = $(noinst_HEADERS) +ETAGS = etags +CTAGS = ctags +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +GREP = @GREP@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LDFLAGS = @LDFLAGS@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LTLIBOBJS = @LTLIBOBJS@ +MAKEINFO = @MAKEINFO@ +MKDIR_P = @MKDIR_P@ +OBJEXT = @OBJEXT@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +STRIP = @STRIP@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_CC = @ac_ct_CC@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build_alias = @build_alias@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host_alias = @host_alias@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +AUTOMAKE_OPTIONS = gnu +AM_CFLAGS = -Wall +u_boot_SOURCES = u_boot.c mtd_erase_all.c +noinst_HEADERS = u_boot.h mtd_erase_all.h +EXTRA_DIST = +all: all-am + +.SUFFIXES: +.SUFFIXES: .c .o .obj +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh \ + && exit 0; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu src/Makefile'; \ + cd $(top_srcdir) && \ + $(AUTOMAKE) --gnu src/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +install-binPROGRAMS: $(bin_PROGRAMS) + @$(NORMAL_INSTALL) + test -z "$(bindir)" || $(MKDIR_P) "$(DESTDIR)$(bindir)" + @list='$(bin_PROGRAMS)'; for p in $$list; do \ + p1=`echo $$p|sed 's/$(EXEEXT)$$//'`; \ + if test -f $$p \ + ; then \ + f=`echo "$$p1" | sed 's,^.*/,,;$(transform);s/$$/$(EXEEXT)/'`; \ + echo " $(INSTALL_PROGRAM_ENV) $(binPROGRAMS_INSTALL) '$$p' '$(DESTDIR)$(bindir)/$$f'"; \ + $(INSTALL_PROGRAM_ENV) $(binPROGRAMS_INSTALL) "$$p" "$(DESTDIR)$(bindir)/$$f" || exit 1; \ + else :; fi; \ + done + +uninstall-binPROGRAMS: + @$(NORMAL_UNINSTALL) + @list='$(bin_PROGRAMS)'; for p in $$list; do \ + f=`echo "$$p" | sed 's,^.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/'`; \ + echo " rm -f '$(DESTDIR)$(bindir)/$$f'"; \ + rm -f "$(DESTDIR)$(bindir)/$$f"; \ + done + +clean-binPROGRAMS: + -test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS) +u-boot$(EXEEXT): $(u_boot_OBJECTS) $(u_boot_DEPENDENCIES) + @rm -f u-boot$(EXEEXT) + $(LINK) $(u_boot_OBJECTS) $(u_boot_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mtd_erase_all.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/u_boot.Po@am__quote@ + +.c.o: +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(COMPILE) -c $< + +.c.obj: +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(COMPILE) -c `$(CYGPATH_W) '$<'` + +ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonemtpy = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + mkid -fID $$unique +tags: TAGS + +TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + tags=; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + if test -z "$(ETAGS_ARGS)$$tags$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$tags $$unique; \ + fi +ctags: CTAGS +CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + tags=; \ + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + test -z "$(CTAGS_ARGS)$$tags$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$tags $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && cd $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) $$here + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \ + fi; \ + cp -pR $$d/$$file $(distdir)$$dir || exit 1; \ + else \ + test -f $(distdir)/$$file \ + || cp -p $$d/$$file $(distdir)/$$file \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(PROGRAMS) $(HEADERS) +installdirs: + for dir in "$(DESTDIR)$(bindir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-binPROGRAMS clean-generic mostlyclean-am + +distclean: distclean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-exec-am: install-binPROGRAMS + +install-html: install-html-am + +install-info: install-info-am + +install-man: + +install-pdf: install-pdf-am + +install-ps: install-ps-am + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: uninstall-binPROGRAMS + +.MAKE: install-am install-strip + +.PHONY: CTAGS GTAGS all all-am check check-am clean clean-binPROGRAMS \ + clean-generic ctags distclean distclean-compile \ + distclean-generic distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-binPROGRAMS \ + install-data install-data-am install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-info install-info-am install-man install-pdf \ + install-pdf-am install-ps install-ps-am install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic pdf pdf-am ps ps-am tags uninstall \ + uninstall-am uninstall-binPROGRAMS + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/src/mtd_erase_all.c b/src/mtd_erase_all.c new file mode 100644 index 0000000..342fbe7 --- /dev/null +++ b/src/mtd_erase_all.c @@ -0,0 +1,181 @@ +/* + * cherry picked from mtd-utils + * + * Copyright (C) 2000 Arcom Control System Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "u_boot.h" + +int target_endian = __BYTE_ORDER; + +int mtd_erase_all(const char *mtd_device, int jffs2) +{ + int fd; + struct mtd_info_user meminfo; + struct erase_info_user erase; + int clmpos = 0; + int clmlen = 8; + int isNAND; + int bbtest = 1; + struct jffs2_unknown_node cleanmarker; + + if ((fd = open(mtd_device, O_RDWR)) < 0) { + error("%s: %s", mtd_device, strerror(errno)); + close(fd); + return false; + } + + if (ioctl(fd, MEMGETINFO, &meminfo) != 0) { + error("%s: unable to get MTD device info", mtd_device); + close(fd); + return false; + } + + erase.length = meminfo.erasesize; + isNAND = meminfo.type == MTD_NANDFLASH ? 1 : 0; + + if (jffs2) { + cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); + cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER); + if (!isNAND) + cleanmarker.totlen = cpu_to_je32(sizeof(struct jffs2_unknown_node)); + else { + struct nand_oobinfo oobinfo; + + if (ioctl(fd, MEMGETOOBSEL, &oobinfo) != 0) { + error("%s: unable to get NAND oobinfo", mtd_device); + close(fd); + return false; + } + + /* Check for autoplacement */ + if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) { + /* Get the position of the free bytes */ + if (!oobinfo.oobfree[0][1]) { + error("Eeep. Autoplacement selected and no empty space in oob"); + close(fd); + return false; + } + 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; + } + } + cleanmarker.totlen = cpu_to_je32(8); + } + cleanmarker.hdr_crc= cpu_to_je32(crc32(0, (unsigned char *) &cleanmarker, sizeof(struct jffs2_unknown_node) - 4)); + } + + for (erase.start = 0; erase.start < meminfo.size; erase.start += meminfo.erasesize) { + if (bbtest) { + loff_t offset = erase.start; + int ret = ioctl(fd, MEMGETBADBLOCK, &offset); + if (ret > 0) { + dbg("Skipping bad block at 0x%08x", erase.start); + continue; + } else if (ret < 0) { + if (errno == EOPNOTSUPP) { + bbtest = 0; + if (isNAND) { + error("%s: Bad block check not available", mtd_device); + close(fd); + return false; + } + } else { + error("%s: MTD get bad block failed: %s", mtd_device, strerror(errno)); + close(fd); + return false; + } + } + } + + if (ioctl(fd, MEMERASE, &erase) != 0) { + error("%s: MTD Erase failure: %s", mtd_device, strerror(errno)); + continue; + } + + /* format for JFFS2 ? */ + if (!jffs2) + continue; + + /* 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) { + error("%s: MTD writeoob failure: %s", mtd_device, strerror(errno)); + continue; + } + } else { + if (lseek(fd, erase.start, SEEK_SET) < 0) { + error("%s: MTD lseek failure: %s", mtd_device, strerror(errno)); + continue; + } + if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) { + error("%s: MTD write failure: %s", mtd_device, strerror(errno)); + continue; + } + } + + dbg("Cleanmarker written at %x.", erase.start); + } + + close(fd); + + return true; +} + + + + diff --git a/src/mtd_erase_all.h b/src/mtd_erase_all.h new file mode 100644 index 0000000..a5edae1 --- /dev/null +++ b/src/mtd_erase_all.h @@ -0,0 +1,6 @@ +#ifndef __MTD_ERASE_ALL_H +#define __MTD_ERASE_ALL_H + +int mtd_erase_all(const char *mtd_device, int jffs2); + +#endif /* ~__MTD_ERASE_ALL_H */ diff --git a/src/u_boot.c b/src/u_boot.c new file mode 100644 index 0000000..e240475 --- /dev/null +++ b/src/u_boot.c @@ -0,0 +1,363 @@ +/* + * U-Boot utility program for Linux + * + * Copyright (C) 2010 by Multi-Tech Systems + * + * Author: James Maki + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "u_boot.h" + +static int tokcmp(const char *cmd, const char *pattern) { + int len = strlen(cmd); + if (len > strlen(pattern)) { + return -1; + } + + return memcmp(pattern, cmd, len); +} + +#define ENV_HEADER_SIZE (sizeof(uint32_t) + sizeof(uint8_t)) +#define ENV_DATA_SIZE (MTD_SIZE - ENV_HEADER_SIZE) + +struct environment { + uint32_t crc; + uint8_t flags; + char data[ENV_DATA_SIZE]; +}; + +static int write_uboot_env(const char *device, struct environment *env) +{ + int err; + int fd; + + err = mtd_erase_all(device, 0); + if (!err) { + error("mtd_erase_all %s failed", device); + return -1; + } + + fd = open(device, O_WRONLY); + if (fd < 0) { + error("open %s failed: %d", device, errno); + return -1; + } + + err = write(fd, env, sizeof(*env)); + if (err != sizeof(*env)) { + error("write to %s failed: %d", device, errno); + close(fd); + return -1; + } + + close(fd); + + return err; +} + +static int read_uboot_env(const char *device, struct environment *env) +{ + int fd; + int err; + + memset(env, 0, sizeof(*env)); + + fd = open(device, O_RDONLY); + if (fd < 0) { + error("open %s failed: %d", device, errno); + return -1; + } + + err = read(fd, env, sizeof(*env)); + if (err != sizeof(*env)) { + error("read from %s failed: %d", device, errno); + close(fd); + return -1; + } + + close(fd); + + return err; +} + +static char *next_var(const char *var) +{ + return (char *) (var + strlen(var) + 1); +} + +static int cmd_printenv(struct environment *env, int argc, char **argv) +{ + char *var; + char *name = NULL; + size_t name_len = 0; + + if (argc) { + name = *argv; + name_len = strlen(name); + } + + var = env->data; + while (*var) { + if (!name) { + printf("%s\n", var); + } else if (!strncmp(var, name, name_len) && var[name_len] == '=') { + printf("%s\n", var); + return true; + } + + var = next_var(var); + } + + if (name) { + fprintf(stderr, "environment variable not found\n"); + return false; + } + + return true; +} + +static int cmd_setenv(struct environment *env, int argc, char **argv) +{ + int tmp; + char *var; + char *cp; + char *end = env->data + sizeof(env->data); + char *name; + size_t name_len; + size_t value_len; + + if (!argc) { + fprintf(stderr, "name expected\n"); + return false; + } + name = *argv; + argc--; argv++; + + name_len = strlen(name); + + var = env->data; + while (*var) { + if (!strncmp(var, name, name_len) && var[name_len] == '=') { + dbg("found variable (%s) at %lld", var, (long long int) var); + + cp = next_var(var); + while (*cp) { + while (*cp) { + *var++ = *cp++; + } + *var++ = *cp++; + } + + break; + } + + var = next_var(var); + } + + value_len = 0; + for (tmp = 0; tmp < argc; tmp++) { + value_len += strlen(argv[tmp]); + if (tmp + 1 < argc) { + value_len += 1; + } + } + + if (value_len) { + if (var + strlen(name) + strlen("=") + value_len + 1 >= end) { + error("end of environment reached"); + return false; + } + *var = 0; + strcat(var, name); + strcat(var, "="); + for (tmp = 0; tmp < argc; tmp++) { + strcat(var, argv[tmp]); + if (tmp + 1 < argc) { + strcat(var, " "); + } + } + var += strlen(var) + 1; + } + + *var = 0; + if (var == env->data) { + *(var + 1) = 0; + } + + env->crc = crc32(0, (uint8_t *) env->data, sizeof(env->data)); + dbg("crc: 0x%08X", env->crc); + env->flags = 0; + + tmp = write_uboot_env(MTD_ENV1, env); + if (tmp < 0) { + dbg("write_uboot_env %s failed with %d", MTD_ENV1, tmp); + } + tmp = write_uboot_env(MTD_ENV2, env); + if (tmp < 0) { + dbg("write_uboot_env %s failed with %d", MTD_ENV1, tmp); + } + + return true; +} + +static void print_version(const char *name) { + printf("%s (" PACKAGE ") " VERSION " (" __DATE__ " " __TIME__ ")\n", name); + printf("Copyright (C) 2010 by Multi-Tech Systems\n"); + printf( +"This program is free software; you may redistribute it under the terms of\n" +"the GNU General Public License version 2 or (at your option) any later version.\n" +"This program has absolutely no warranty.\n"); +} + +static void usage(FILE *out) { + fprintf(out, "Usage: u-boot { printenv [ name ] | setenv name [ value ] }\n"); + fprintf(out, "\n"); +} + +int main(int argc, char *argv[]) { + int err; + char *cmd; + char *option; + struct environment *env = NULL; + + struct environment *env1; + struct environment *env2; + uint32_t crc1_ok; + uint32_t crc2_ok; + + if (argc <= 1) { + usage(stderr); + exit(1); + } + argc--; argv++; + + while (argc) { + option = *argv; + if (*option != '-') { + break; + } else if (!strcmp(option, "--")) { + break; + } else if (!strcmp(option, "--version")) { + print_version("u-boot"); + exit(0); + } else if (!strcmp(option, "--help")) { + usage(stdout); + exit(0); + } else { + usage(stderr); + exit(1); + } + + argc--; argv++; + } + + if (!argc) { + usage(stderr); + exit(1); + } + + env1 = malloc(sizeof(struct environment)); + if (!env1) { + error("out of memory"); + return 1; + } + + env2 = malloc(sizeof(struct environment)); + if (!env2) { + error("out of memory"); + return 1; + } + + err = read_uboot_env(MTD_ENV1, env1); + if (err < 0) { + error("read_uboot_env failed"); + return 1; + } + dbg("env1 crc: 0x%08X", env1->crc); + dbg("env1 flags: %d", env1->flags); + if (crc32(0, (uint8_t *) env1->data, sizeof(env1->data)) == env1->crc) { + crc1_ok = 1; + } else { + error("crc does not match on primary env"); + crc1_ok = 0; + } + + err = read_uboot_env(MTD_ENV2, env2); + if (err < 0) { + error("read_uboot_env failed"); + return 1; + } + dbg("env2 crc: 0x%08X", env2->crc); + dbg("env2 flags: %d", env2->flags); + if (crc32(0, (uint8_t *) env2->data, sizeof(env2->data)) == env2->crc) { + crc2_ok = 1; + } else { + error("crc does not match on redundant env"); + crc2_ok = 0; + } + + if (!crc1_ok && !crc2_ok) { + error("both environments are bad: loading DEFAULT_ENV"); + env = env1; + env->flags = 0; + memcpy(env->data, DEFAULT_ENV, sizeof(DEFAULT_ENV)); + } else if (crc1_ok && !crc2_ok) { + env = env1; + } else if (!crc1_ok && crc2_ok) { + env = env2; + } else { + if (env1->flags == 255 && env2->flags == 0) { + env = env2; + } else if (env2->flags == 255 && env1->flags == 0) { + env = env1; + } else if (env1->flags > env2->flags) { + env = env1; + } else if (env2->flags > env1->flags) { + env = env2; + } else { + env = env1; + } + } + + cmd = *argv; + argc--; argv++; + if (!tokcmp(cmd, "printenv")) { + err = cmd_printenv(env, argc, argv); + } else if (!tokcmp(cmd, "setenv")) { + err = cmd_setenv(env, argc, argv); + } else { + usage(stderr); + exit(1); + } + + return !err; +} + diff --git a/src/u_boot.h b/src/u_boot.h new file mode 100644 index 0000000..5106601 --- /dev/null +++ b/src/u_boot.h @@ -0,0 +1,19 @@ +#ifndef __U_BOOT_H +#define __U_BOOT_H + +#include "config.h" + +#if DEBUG +#define dbg(format, arg...) fprintf(stderr, "DEBUG: %s:%s:%d: " format "\n" , __FILE__ , __func__ , __LINE__ , ## arg) +#else +#define dbg(format, arg...) do {} while (0) +#endif + +#define error(format, arg...) fprintf(stderr, "ERROR: %s:%s:%d: " format "\n" , __FILE__ , __func__ , __LINE__ , ## arg) + +enum { + false, + true, +}; + +#endif /* ~__U_BOOT_H */ -- cgit v1.2.3