diff --git a/commands/crc.c b/commands/crc.c index ee8dacff0..824dda4c7 100644 --- a/commands/crc.c +++ b/commands/crc.c @@ -21,66 +21,8 @@ #include #include #include -#include -#include -#include #include -#include - -static int file_crc(char* filename, ulong start, ulong size, ulong *crc, - ulong *total) -{ - int fd, now; - int ret = 0; - char *buf; - - *total = 0; - *crc = 0; - - fd = open(filename, O_RDONLY); - if (fd < 0) { - printf("open %s: %s\n", filename, errno_str()); - return fd; - } - - if (start > 0) { - off_t lseek_ret; - errno = 0; - lseek_ret = lseek(fd, start, SEEK_SET); - if (lseek_ret == (off_t)-1 && errno) { - perror("lseek"); - ret = -1; - goto out; - } - } - - buf = xmalloc(4096); - - while (size) { - now = min((ulong)4096, size); - now = read(fd, buf, now); - if (now < 0) { - ret = now; - perror("read"); - goto out_free; - } - if (!now) - break; - *crc = crc32(*crc, buf, now); - size -= now; - *total += now; - } - - printf ("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx", - filename, start, start + *total - 1, *crc); - -out_free: - free(buf); -out: - close(fd); - - return ret; -} +#include static int crc_from_file(const char* file, ulong *crc) { @@ -143,6 +85,9 @@ static int do_crc(int argc, char *argv[]) if (file_crc(filename, start, size, &crc, &total) < 0) return 1; + printf("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx", + filename, (ulong)start, (ulong)start + total - 1, crc); + #ifdef CONFIG_CMD_CRC_CMP if (vfilename) { size = total; diff --git a/common/Kconfig b/common/Kconfig index 623372b62..b511645cc 100644 --- a/common/Kconfig +++ b/common/Kconfig @@ -618,6 +618,13 @@ config BAREBOXENV_TARGET 'bareboxenv' is a tool to access the barebox environment from a running Linux system. Say yes here to build it for the target. +config BAREBOXCRC32_TARGET + bool + prompt "build bareboxcrc32 tool for target" + help + 'bareboxcrc32' is a userspacetool to generate the crc32 checksums the same way + barebox does. Say yes here to build it for the target. + config POLLER bool "generic polling infrastructure" diff --git a/crypto/crc32.c b/crypto/crc32.c index 275edb4c5..e7b1bd76b 100644 --- a/crypto/crc32.c +++ b/crypto/crc32.c @@ -10,6 +10,12 @@ #ifdef __BAREBOX__ /* Shut down "ANSI does not permit..." warnings */ #include +#include +#include +#include +#include +#include +#include #endif #ifdef CONFIG_DYNAMIC_CRC_TABLE @@ -178,3 +184,57 @@ uint32_t crc32_no_comp(uint32_t crc, const void *_buf, unsigned int len) return crc; } +int file_crc(char *filename, ulong start, ulong size, ulong *crc, + ulong *total) +{ + int fd, now; + int ret = 0; + char *buf; + + *total = 0; + *crc = 0; + + fd = open(filename, O_RDONLY); + if (fd < 0) { + printf("open %s: %s\n", filename, strerror(errno)); + return fd; + } + + if (start > 0) { + off_t lseek_ret; + errno = 0; + lseek_ret = lseek(fd, start, SEEK_SET); + if (lseek_ret == (off_t)-1 && errno) { + perror("lseek"); + ret = -1; + goto out; + } + } + + buf = xmalloc(4096); + + while (size) { + now = min((ulong)4096, size); + now = read(fd, buf, now); + if (now < 0) { + ret = now; + perror("read"); + goto out_free; + } + if (!now) + break; + *crc = crc32(*crc, buf, now); + size -= now; + *total += now; + } + +out_free: + free(buf); +out: + close(fd); + + return ret; +} +#ifdef __BAREBOX__ +EXPORT_SYMBOL(file_crc); +#endif diff --git a/include/common.h b/include/common.h index 00f1642cd..293f5041a 100644 --- a/include/common.h +++ b/include/common.h @@ -115,6 +115,8 @@ long simple_strtol(const char *cp,char **endp,unsigned int base); /* lib_generic/crc32.c */ uint32_t crc32(uint32_t, const void*, unsigned int); uint32_t crc32_no_comp(uint32_t, const void*, unsigned int); +int file_crc(char *filename, ulong start, ulong size, ulong *crc, + ulong *total); /* common/console.c */ int ctrlc (void); diff --git a/scripts/.gitignore b/scripts/.gitignore index b88f8d866..fac394d12 100644 --- a/scripts/.gitignore +++ b/scripts/.gitignore @@ -10,3 +10,5 @@ mkublheader omap_signGP zynq_mkimage socfpga_mkimage +bareboxcrc32 +bareboxcrc32-target diff --git a/scripts/Makefile b/scripts/Makefile index ce3fd5336..a83707024 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -8,6 +8,7 @@ hostprogs-y += bin2c hostprogs-y += mkimage hostprogs-y += fix_size hostprogs-y += bareboxenv +hostprogs-y += bareboxcrc32 hostprogs-y += kernel-install hostprogs-$(CONFIG_KALLSYMS) += kallsyms hostprogs-$(CONFIG_ARCH_MVEBU) += kwbimage kwboot @@ -25,6 +26,7 @@ subdir-$(CONFIG_X86) += setupmbr subdir-$(CONFIG_DTC) += dtc targetprogs-$(CONFIG_BAREBOXENV_TARGET) += bareboxenv-target +targetprogs-$(CONFIG_BAREBOXCRC32_TARGET) += bareboxcrc32-target targetprogs-$(CONFIG_KERNEL_INSTALL_TARGET) += bareboxenv-target # Let clean descend into subdirs diff --git a/scripts/bareboxcrc32.c b/scripts/bareboxcrc32.c new file mode 100644 index 000000000..e00ffafeb --- /dev/null +++ b/scripts/bareboxcrc32.c @@ -0,0 +1,60 @@ +/* + * bareboxcrc32.c - generate crc32 checksum in little endian + * + * Copyright (c) 2013 Michael Grzeschik + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * 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. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "compiler.h" + +#define debug(...) + +#include "../crypto/crc32.c" + +int main(int argc, char *argv[]) +{ + loff_t start = 0, size = ~0; + ulong crc = 0, total = 0; + char *filename = NULL; + int i; + + if (!filename && argc < 2) { + printf("usage: %s filename\n", argv[0]); + exit(1); + } + + for (i = 1; i < argc; i++) { + filename = argv[i]; + if (file_crc(filename, start, size, &crc, &total) < 0) + exit(1); + printf("%08lx\t%s\n", crc, filename); + } + + exit(0); + +} diff --git a/scripts/bareboxenv.c b/scripts/bareboxenv.c index f372685ae..da420db57 100644 --- a/scripts/bareboxenv.c +++ b/scripts/bareboxenv.c @@ -35,25 +35,6 @@ #define debug(...) -static void *xmalloc(size_t size) -{ - void *p = NULL; - - if (!(p = malloc(size))) { - printf("ERROR: out of memory\n"); - exit(1); - } - - return p; -} - -static void *xzalloc(size_t size) -{ - void *p = xmalloc(size); - memset(p, 0, size); - return p; -} - /* Find out if the last character of a string matches the one given. * Don't underrun the buffer if the string length is 0. */ diff --git a/scripts/compiler.h b/scripts/compiler.h index 53f84b6d1..0891c3bfa 100644 --- a/scripts/compiler.h +++ b/scripts/compiler.h @@ -107,4 +107,29 @@ typedef uint32_t __u32; # define be64_to_cpu(x) (x) #endif +#define min(x, y) ({ \ + typeof(x) _min1 = (x); \ + typeof(y) _min2 = (y); \ + (void) (&_min1 == &_min2); \ + _min1 < _min2 ? _min1 : _min2; }) + +inline void *xmalloc(size_t size) +{ + void *p = NULL; + + if (!(p = malloc(size))) { + printf("ERROR: out of memory\n"); + exit(1); + } + + return p; +} + +inline void *xzalloc(size_t size) +{ + void *p = xmalloc(size); + memset(p, 0, size); + return p; +} + #endif