9
0
Fork 0

scripts: bareboxcrc32 as host and target userspacetool

This patch adds the crc32 command to be build
as host and optionally as target tool.

Signed-off-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Michael Grzeschik 2013-12-04 00:06:38 +01:00 committed by Sascha Hauer
parent a83f635fb2
commit 1e7f2bd25c
9 changed files with 162 additions and 78 deletions

View File

@ -21,66 +21,8 @@
#include <command.h> #include <command.h>
#include <fs.h> #include <fs.h>
#include <getopt.h> #include <getopt.h>
#include <fcntl.h>
#include <errno.h>
#include <xfuncs.h>
#include <malloc.h> #include <malloc.h>
#include <linux/ctype.h> #include <environment.h>
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;
}
static int crc_from_file(const char* file, ulong *crc) 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) if (file_crc(filename, start, size, &crc, &total) < 0)
return 1; 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 #ifdef CONFIG_CMD_CRC_CMP
if (vfilename) { if (vfilename) {
size = total; size = total;

View File

@ -618,6 +618,13 @@ config BAREBOXENV_TARGET
'bareboxenv' is a tool to access the barebox environment from a running Linux 'bareboxenv' is a tool to access the barebox environment from a running Linux
system. Say yes here to build it for the target. 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 config POLLER
bool "generic polling infrastructure" bool "generic polling infrastructure"

View File

@ -10,6 +10,12 @@
#ifdef __BAREBOX__ /* Shut down "ANSI does not permit..." warnings */ #ifdef __BAREBOX__ /* Shut down "ANSI does not permit..." warnings */
#include <common.h> #include <common.h>
#include <xfuncs.h>
#include <fs.h>
#include <fcntl.h>
#include <malloc.h>
#include <linux/ctype.h>
#include <errno.h>
#endif #endif
#ifdef CONFIG_DYNAMIC_CRC_TABLE #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; 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

View File

@ -115,6 +115,8 @@ long simple_strtol(const char *cp,char **endp,unsigned int base);
/* lib_generic/crc32.c */ /* lib_generic/crc32.c */
uint32_t crc32(uint32_t, const void*, unsigned int); uint32_t crc32(uint32_t, const void*, unsigned int);
uint32_t crc32_no_comp(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 */ /* common/console.c */
int ctrlc (void); int ctrlc (void);

2
scripts/.gitignore vendored
View File

@ -10,3 +10,5 @@ mkublheader
omap_signGP omap_signGP
zynq_mkimage zynq_mkimage
socfpga_mkimage socfpga_mkimage
bareboxcrc32
bareboxcrc32-target

View File

@ -8,6 +8,7 @@ hostprogs-y += bin2c
hostprogs-y += mkimage hostprogs-y += mkimage
hostprogs-y += fix_size hostprogs-y += fix_size
hostprogs-y += bareboxenv hostprogs-y += bareboxenv
hostprogs-y += bareboxcrc32
hostprogs-y += kernel-install hostprogs-y += kernel-install
hostprogs-$(CONFIG_KALLSYMS) += kallsyms hostprogs-$(CONFIG_KALLSYMS) += kallsyms
hostprogs-$(CONFIG_ARCH_MVEBU) += kwbimage kwboot hostprogs-$(CONFIG_ARCH_MVEBU) += kwbimage kwboot
@ -25,6 +26,7 @@ subdir-$(CONFIG_X86) += setupmbr
subdir-$(CONFIG_DTC) += dtc subdir-$(CONFIG_DTC) += dtc
targetprogs-$(CONFIG_BAREBOXENV_TARGET) += bareboxenv-target targetprogs-$(CONFIG_BAREBOXENV_TARGET) += bareboxenv-target
targetprogs-$(CONFIG_BAREBOXCRC32_TARGET) += bareboxcrc32-target
targetprogs-$(CONFIG_KERNEL_INSTALL_TARGET) += bareboxenv-target targetprogs-$(CONFIG_KERNEL_INSTALL_TARGET) += bareboxenv-target
# Let clean descend into subdirs # Let clean descend into subdirs

60
scripts/bareboxcrc32.c Normal file
View File

@ -0,0 +1,60 @@
/*
* bareboxcrc32.c - generate crc32 checksum in little endian
*
* Copyright (c) 2013 Michael Grzeschik <mgr@pengutronix.de>
*
* 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 <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
#include <limits.h>
#include <errno.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <libgen.h>
#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);
}

View File

@ -35,25 +35,6 @@
#define debug(...) #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. /* Find out if the last character of a string matches the one given.
* Don't underrun the buffer if the string length is 0. * Don't underrun the buffer if the string length is 0.
*/ */

View File

@ -107,4 +107,29 @@ typedef uint32_t __u32;
# define be64_to_cpu(x) (x) # define be64_to_cpu(x) (x)
#endif #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 #endif