All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marco <marco.stornelli@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v4] Add imls utility command
Date: Fri, 10 Apr 2009 18:26:00 +0200	[thread overview]
Message-ID: <49DF7318.10100@gmail.com> (raw)

Applied comments of Wolgang Denk. Created a new tools sub-folder.

Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
---

diff --git a/tools/imls/Makefile b/tools/imls/Makefile
new file mode 100644
index 0000000..d654658
--- /dev/null
+++ b/tools/imls/Makefile
@@ -0,0 +1,106 @@
+#
+# (C) Copyright 2009 Marco Stornelli <marco.stornelli@gmail.com>
+#
+# 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 $(TOPDIR)/config.mk
+
+HOST_CFLAGS = -Wall -pedantic
+
+# Generated executable files
+BIN_FILES-y += imls
+
+# Source files which exist outside the tools directory
+EXT_OBJ_FILES-y += lib_generic/crc32.o
+EXT_OBJ_FILES-y += lib_generic/md5.o
+EXT_OBJ_FILES-y += lib_generic/sha1.o
+EXT_OBJ_FILES-y += common/image.o
+
+# Source files located in the tools directory
+OBJ_FILES-y += imls.o
+
+# Flattened device tree objects
+LIBFDT_OBJ_FILES-y += fdt.o
+LIBFDT_OBJ_FILES-y += fdt_ro.o
+LIBFDT_OBJ_FILES-y += fdt_rw.o
+LIBFDT_OBJ_FILES-y += fdt_strerror.o
+LIBFDT_OBJ_FILES-y += fdt_wip.o
+
+# now $(obj) is defined
+SRCS	+= $(addprefix $(SRCTREE)/,$(EXT_OBJ_FILES-y:.o=.c))
+SRCS	+= $(addprefix $(SRCTREE)/tools/,$(OBJ_FILES-y:.o=.c))
+SRCS	+= $(addprefix $(SRCTREE)/libfdt/,$(LIBFDT_OBJ_FILES-y:.o=.c))
+BINS	:= $(addprefix $(obj),$(sort $(BIN_FILES-y)))
+LIBFDT_OBJS	:= $(addprefix $(obj),$(LIBFDT_OBJ_FILES-y))
+
+#
+# Use native tools and options
+# Define __KERNEL_STRICT_NAMES to prevent typedef overlaps
+#
+CPPFLAGS   = -idirafter $(SRCTREE)/include \
+		-idirafter $(OBJTREE)/include2 \
+		-idirafter $(OBJTREE)/include \
+	        -I $(SRCTREE)/libfdt \
+		-I $(SRCTREE)/tools \
+		-DUSE_HOSTCC -D__KERNEL_STRICT_NAMES
+CFLAGS     = $(HOST_CFLAGS) $(CPPFLAGS) -O
+
+# No -pedantic switch to avoid libfdt compilation warnings
+FIT_CFLAGS = -Wall $(CPPFLAGS) -O
+
+CC	   = $(CROSS_COMPILER)gcc
+STRIP	   = $(CROSS_COMPILER)strip
+
+ifeq ($(MTD_VERSION),old)
+CPPFLAGS += -DMTD_OLD
+endif
+
+all:	$(BINS)
+
+$(obj)imls:	$(obj)imls.o $(obj)crc32.o $(obj)image.o $(obj)md5.o \
+		$(obj)sha1.o $(LIBFDT_OBJS)
+	$(CC) $(CFLAGS) -o $@ $^
+	$(STRIP) $@
+
+# Some files complain if compiled with -pedantic, use FIT_CFLAGS
+$(obj)image.o: $(SRCTREE)/common/image.c
+	$(CC) -g $(FIT_CFLAGS) -c -o $@ $<
+
+$(obj)imls.o: imls.c
+	$(CC) -g $(FIT_CFLAGS) -c -o $@ $<
+
+# Some of the tool objects need to be accessed from outside the tools directory
+$(obj)%.o: $(SRCTREE)/common/%.c
+	$(CC) -g $(FIT_CFLAGS) -c -o $@ $<
+
+$(obj)%.o: $(SRCTREE)/lib_generic/%.c
+	$(CC) -g $(CFLAGS) -c -o $@ $<
+
+$(obj)%.o: $(SRCTREE)/libfdt/%.c
+	$(CC) -g $(FIT_CFLAGS) -c -o $@ $<
+
+clean:
+	rm -rf *.o imls
+
+#########################################################################
+
+# defines $(obj).depend target
+include $(SRCTREE)/rules.mk
+
+sinclude $(obj).depend
+
+#########################################################################
diff --git a/tools/imls/imls.c b/tools/imls/imls.c
new file mode 100644
index 0000000..47c892c
--- /dev/null
+++ b/tools/imls/imls.c
@@ -0,0 +1,240 @@
+/*
+ * (C) Copyright 2009 Marco Stornelli
+ *
+ * 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 <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/user.h>
+
+#ifdef MTD_OLD
+# include <stdint.h>
+# include <linux/mtd/mtd.h>
+#else
+# define  __user	/* nothing */
+# include <mtd/mtd-user.h>
+#endif
+
+#include <sha1.h>
+#include <fdt.h>
+#include <libfdt.h>
+#include <fdt_support.h>
+#include <image.h>
+
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+
+extern unsigned long crc32(unsigned long crc, const char *buf, unsigned int len);
+static void usage(void);
+static int image_verify_header(char *ptr, int fd);
+
+char	*cmdname;
+char	*devicefile;
+
+unsigned int sectorcount = 0;
+int sflag = 0;
+unsigned int sectoroffset = 0;
+unsigned int sectorsize = 0;
+int cflag = 0;
+
+int main (int argc, char **argv)
+{
+	int fd = -1, err = 0, readbyte = 0, j;
+	uint32_t mtdsize;
+	struct mtd_info_user mtdinfo;
+	char buf[sizeof(image_header_t)];
+	int found = 0;
+
+	cmdname = *argv;
+
+	while (--argc > 0 && **++argv == '-') {
+		while (*++*argv) {
+			switch (**argv) {
+			case 's':
+				if (--argc <= 0) 
+					usage ();
+				sectorcount = (unsigned int)atoi(*++argv);
+				sflag = 1;
+				goto NXTARG;
+			case 'o':
+				if (--argc <= 0)
+					usage ();
+				sectoroffset = (unsigned int)atoi(*++argv);
+				goto NXTARG;
+
+			case 'c':
+				if (--argc <= 0)
+					usage ();
+				sectorsize = (unsigned int)atoi(*++argv);
+				cflag = 1;
+				goto NXTARG;
+			default:
+				usage ();
+			}
+		}
+NXTARG:		;
+	}
+
+	if (argc != 1 || cflag == 0 || sflag == 0)
+		usage();
+
+	devicefile = *argv;
+
+	fd = open(devicefile, O_RDONLY);
+	if (fd < 0) {
+		fprintf (stderr, "%s: Can't open %s: %s\n",
+		cmdname, devicefile, strerror(errno));
+		exit(EXIT_FAILURE);
+	}
+
+	err = ioctl(fd, MEMGETINFO, &mtdinfo);
+	if (err < 0) {
+		fprintf(stderr, "%s: Cannot get MTD information: %s\n",cmdname,
+		strerror(errno));
+		exit(EXIT_FAILURE);
+	}
+
+	if (mtdinfo.type != MTD_NORFLASH && mtdinfo.type != MTD_NANDFLASH) {
+		fprintf(stderr, "Unsupported flash type %u\n", mtdinfo.type);
+		exit(EXIT_FAILURE);
+	}
+
+	mtdsize = mtdinfo.size;
+
+	if (sectorsize * sectorcount != mtdsize) {
+		fprintf(stderr, "%s: Partition size (%d) incompatible with "
+			"sector size and count\n", cmdname, mtdsize);
+		exit(EXIT_FAILURE);
+	}
+
+	if (sectorsize * sectoroffset >= mtdsize) {
+		fprintf(stderr, "%s: Partition size (%d) incompatible with "
+			"sector offset given\n", cmdname, mtdsize);
+		exit(EXIT_FAILURE);
+	}
+
+	if (sectoroffset > sectorcount - 1) {
+		fprintf(stderr, "%s: Sector offset cannot be grater than "
+			"sector count minus one\n", cmdname);
+		exit(EXIT_FAILURE);
+	}
+
+	printf("Searching....\n");
+
+	for (j = sectoroffset; j < sectorcount; ++j) {
+
+		if (lseek(fd, j*sectorsize, SEEK_SET) != j*sectorsize) {
+			fprintf(stderr, "%s: lseek failure: %s\n",
+			cmdname, strerror(errno));
+			exit(EXIT_FAILURE);
+		}
+
+		readbyte = read(fd, buf, sizeof(image_header_t));
+		if (readbyte != sizeof(image_header_t)) {
+			fprintf(stderr, "%s: Can't read from device: %s\n",
+			cmdname, strerror(errno));
+			exit(EXIT_FAILURE);
+		}
+
+		if (fdt_check_header(buf)) {
+			/* old-style image */
+			if (image_verify_header(buf, fd)) {
+				found = 1;
+				image_print_contents((image_header_t *)buf);
+			}
+		} else {
+			/* FIT image */
+			fit_print_contents(buf);
+		}
+
+	}	
+
+	close(fd);
+
+	if(!found)
+		printf("No images found\n");
+
+	exit(EXIT_SUCCESS);
+}
+
+void usage()
+{
+	fprintf (stderr, "Usage:\n"
+			 "       %s [-o offset] -c size -s count device\n"
+			 "          -o ==> number of sectors to use as offset\n"
+			 "          -s ==> number of sectors\n"
+			 "          -c ==> size of sectors (byte)\n",
+		cmdname);
+
+	exit(EXIT_FAILURE);
+}
+
+static int image_verify_header(char *ptr, int fd)
+{
+	int len, nread;
+	char *data;
+	uint32_t checksum;
+	image_header_t *hdr = (image_header_t *)ptr;
+	char buf[PAGE_SIZE];
+
+	if (image_get_magic(hdr) != IH_MAGIC)
+		return 0;
+
+	data = (char *)hdr;
+	len  = image_get_header_size();
+
+	checksum = image_get_hcrc(hdr);
+	hdr->ih_hcrc = htonl(0);	/* clear for re-calculation */
+
+	if (crc32(0, data, len) != checksum) {
+		fprintf(stderr,
+		      "%s: Maybe image found but it has bad header checksum!\n",
+		      cmdname);
+		return 0;
+	}
+
+	len = image_get_size(hdr);
+	checksum = 0;
+
+	while (len > 0) {
+		nread = read(fd, buf, MIN(len,PAGE_SIZE));
+		if (nread != MIN(len,PAGE_SIZE)) {
+			fprintf(stderr,
+				"%s: Error while reading: %s\n",
+				cmdname, strerror(errno));
+			exit(EXIT_FAILURE);
+		}
+		checksum = crc32(checksum, buf, nread);
+		len -= nread;
+	}
+
+	if (checksum != image_get_dcrc(hdr)) {
+		fprintf (stderr,
+			"%s: Maybe image found but it has corrupted data!\n",
+			cmdname);
+		return 0;
+	}
+	
+	return 1;
+}

                 reply	other threads:[~2009-04-10 16:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=49DF7318.10100@gmail.com \
    --to=marco.stornelli@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.