From 980a69b8c20ad975553980ccb320bf25ff7c0b16 Mon Sep 17 00:00:00 2001 From: Patrick Georgi Date: Thu, 24 Jun 2010 11:16:10 +0000 Subject: Attached patch moves functions out of the huge libpayload.h into headers according to libc/posix traditions, to simplify porting applications to payloads. It also adds a couple of functions: strcasecmp, strncasecmp, strcat, strtol, strspn, strcspn, strtok_r, strtok, perror, exit, getpagesize Signed-off-by: Patrick Georgi Acked-by: Stefan Reinauer git-svn-id: svn://svn.coreboot.org/coreboot/trunk@5643 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1 --- payloads/libpayload/arch/i386/virtual.c | 5 + payloads/libpayload/arch/powerpc/virtual.c | 4 + payloads/libpayload/include/ctype.h | 55 +++++++ payloads/libpayload/include/errno.h | 35 ++++ payloads/libpayload/include/libpayload.h | 167 +------------------ payloads/libpayload/include/pci.h | 3 + payloads/libpayload/include/stdarg.h | 44 +++++ payloads/libpayload/include/stdint.h | 30 ++++ payloads/libpayload/include/stdio.h | 46 ++++++ payloads/libpayload/include/stdlib.h | 144 ++++++++++++++++ payloads/libpayload/include/string.h | 71 ++++++++ payloads/libpayload/include/sys/types.h | 30 ++++ payloads/libpayload/include/unistd.h | 30 ++++ payloads/libpayload/libc/lib.c | 7 + payloads/libpayload/libc/printf.c | 1 + payloads/libpayload/libc/string.c | 253 ++++++++++++++++++++++++++--- 16 files changed, 743 insertions(+), 182 deletions(-) create mode 100644 payloads/libpayload/include/ctype.h create mode 100644 payloads/libpayload/include/errno.h create mode 100644 payloads/libpayload/include/stdarg.h create mode 100644 payloads/libpayload/include/stdint.h create mode 100644 payloads/libpayload/include/stdio.h create mode 100644 payloads/libpayload/include/stdlib.h create mode 100644 payloads/libpayload/include/string.h create mode 100644 payloads/libpayload/include/sys/types.h create mode 100644 payloads/libpayload/include/unistd.h (limited to 'payloads') diff --git a/payloads/libpayload/arch/i386/virtual.c b/payloads/libpayload/arch/i386/virtual.c index cff26117dc..256bfc25f7 100644 --- a/payloads/libpayload/arch/i386/virtual.c +++ b/payloads/libpayload/arch/i386/virtual.c @@ -30,3 +30,8 @@ unsigned long virtual_offset = 0; + +int getpagesize(void) +{ + return 4096; +} diff --git a/payloads/libpayload/arch/powerpc/virtual.c b/payloads/libpayload/arch/powerpc/virtual.c index cff26117dc..6312ae13ca 100644 --- a/payloads/libpayload/arch/powerpc/virtual.c +++ b/payloads/libpayload/arch/powerpc/virtual.c @@ -30,3 +30,7 @@ unsigned long virtual_offset = 0; +int getpagesize(void) +{ + return 4096; +} diff --git a/payloads/libpayload/include/ctype.h b/payloads/libpayload/include/ctype.h new file mode 100644 index 0000000000..7dfec41048 --- /dev/null +++ b/payloads/libpayload/include/ctype.h @@ -0,0 +1,55 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2008 Advanced Micro Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _CTYPE_H +#define _CTYPE_H + +/** + * @defgroup ctype Character type functions + * @{ + */ +int isalnum(int c); +int isalpha(int c); +int isascii(int c); +int isblank(int c); +int iscntrl(int c); +int isdigit(int c); +int isgraph(int c); +int islower(int c); +int isprint(int c); +int ispunct(int c); +int isspace(int c); +int isupper(int c); +int isxdigit(int c); +int tolower(int c); +int toupper(int c); +/** @} */ + + +#endif diff --git a/payloads/libpayload/include/errno.h b/payloads/libpayload/include/errno.h new file mode 100644 index 0000000000..f8eae83dc4 --- /dev/null +++ b/payloads/libpayload/include/errno.h @@ -0,0 +1,35 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2010 coresystems GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _ERRNO_H +#define _ERRNO_H + +extern int errno; + +#endif diff --git a/payloads/libpayload/include/libpayload.h b/payloads/libpayload/include/libpayload.h index 51e42c7ea2..2efb88e328 100644 --- a/payloads/libpayload/include/libpayload.h +++ b/payloads/libpayload/include/libpayload.h @@ -44,12 +44,16 @@ #define _LIBPAYLOAD_H #include +#include #include +#include +#include +#include +#include #include #include #include #include -#include #include #ifdef CONFIG_LAR #include @@ -224,27 +228,6 @@ void console_add_input_driver(struct console_input_driver *in); #define havechar havekey /** @} */ -/** - * @defgroup ctype Character type functions - * @{ - */ -int isalnum(int c); -int isalpha(int c); -int isascii(int c); -int isblank(int c); -int iscntrl(int c); -int isdigit(int c); -int isgraph(int c); -int islower(int c); -int isprint(int c); -int ispunct(int c); -int isspace(int c); -int isupper(int c); -int isxdigit(int c); -int tolower(int c); -int toupper(int c); -/** @} */ - /** * @defgroup ipchecksum IP checksum functions * @{ @@ -252,87 +235,6 @@ int toupper(int c); unsigned short ipchksum(const void *ptr, unsigned long nbytes); /** @} */ -/** - * @defgroup malloc Memory allocation functions - * @{ - */ -#if defined(CONFIG_DEBUG_MALLOC) && !defined(IN_MALLOC_C) -#define free(p) \ - ({ \ - extern void print_malloc_map(void); \ - extern void free(void *); \ - printf("free(%p) called from %s:%s:%d...\n", p, __FILE__, __func__, \ - __LINE__);\ - printf("PRE free()\n"); \ - print_malloc_map(); \ - free(p); \ - printf("POST free()\n"); \ - print_malloc_map(); \ - }) -#define malloc(s) \ - ({ \ - extern void print_malloc_map(void); \ - extern void *malloc(size_t); \ - void *ptr; \ - printf("malloc(%u) called from %s:%s:%d...\n", s, __FILE__, __func__, \ - __LINE__);\ - printf("PRE malloc\n"); \ - print_malloc_map(); \ - ptr = malloc(s); \ - printf("POST malloc (ptr = %p)\n", ptr); \ - print_malloc_map(); \ - ptr; \ - }) -#define calloc(n,s) \ - ({ \ - extern void print_malloc_map(void); \ - extern void *calloc(size_t,size_t); \ - void *ptr; \ - printf("calloc(%u, %u) called from %s:%s:%d...\n", n, s, __FILE__, \ - __func__, __LINE__);\ - printf("PRE calloc\n"); \ - print_malloc_map(); \ - ptr = calloc(n,s); \ - printf("POST calloc (ptr = %p)\n", ptr); \ - print_malloc_map(); \ - ptr; \ - }) -#define realloc(p,s) \ - ({ \ - extern void print_malloc_map(void); \ - extern void *realloc(void*,size_t); \ - void *ptr; \ - printf("realloc(%p, %u) called from %s:%s:%d...\n", p, s, __FILE__, \ - __func__, __LINE__);\ - printf("PRE realloc\n"); \ - print_malloc_map(); \ - ptr = realloc(p,s); \ - printf("POST realloc (ptr = %p)\n", ptr); \ - print_malloc_map(); \ - ptr; \ - }) -#define memalign(a,s) \ - ({ \ - extern void print_malloc_map(void); \ - extern void *memalign(size_t, size_t); \ - void *ptr; \ - printf("memalign(%u, %u) called from %s:%s:%d...\n", a, s, __FILE__, \ - __func__, __LINE__);\ - printf("PRE memalign\n"); \ - print_malloc_map(); \ - ptr = memalign(a,s); \ - printf("POST realloc (ptr = %p)\n", ptr); \ - print_malloc_map(); \ - ptr; \ - }) -#else -void free(void *ptr); -void *malloc(size_t size); -void *calloc(size_t nmemb, size_t size); -void *realloc(void *ptr, size_t size); -void *memalign(size_t align, size_t size); -#endif -/** @} */ /** * @defgroup exec Execution functions @@ -355,36 +257,6 @@ u8 hex2bin(u8 h); void fatal(const char *msg) __attribute__ ((noreturn)); /** @} */ -/** - * @defgroup memory Memory manipulation functions - * @{ - */ -void *memset(void *s, int c, size_t n); -void *memcpy(void *dst, const void *src, size_t n); -void *memmove(void *dst, const void *src, size_t n); -int memcmp(const void *s1, const void *s2, size_t len); -/** @} */ - -/** - * @defgroup printf Print functions - * @{ - */ -int snprintf(char *str, size_t size, const char *fmt, ...); -int sprintf(char *str, const char *fmt, ...); -int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); -int vsprintf(char *str, const char *fmt, va_list ap); -int printf(const char *fmt, ...); -int vprintf(const char *fmt, va_list ap); -/** @} */ - -/** - * @defgroup rand Random number generator functions - * @{ - */ -int rand_r(unsigned int *seed); -int rand(void); -void srand(unsigned int seed); -/** @} */ /** * @defgroup hash Hashing functions @@ -404,27 +276,6 @@ void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context); u8 *sha1(const u8 *data, size_t len, u8 *buf); /** @} */ -/** - * @defgroup string String functions - * @{ - */ -size_t strnlen(const char *str, size_t maxlen); -size_t strlen(const char *str); -int strcmp(const char *s1, const char *s2); -int strncmp(const char *s1, const char *s2, size_t maxlen); -char *strncpy(char *d, const char *s, size_t n); -char *strcpy(char *d, const char *s); -char *strncat(char *d, const char *s, size_t n); -size_t strlcat(char *d, const char *s, size_t n); -char *strchr(const char *s, int c); -char *strrchr(const char *s, int c); -char *strdup(const char *s); -char *strstr(const char *h, const char *n); -char *strsep(char **stringp, const char *delim); -unsigned int strtoul(const char *s, char **nptr, int base); - -/** @} */ - /** * @defgroup time Time functions * @{ @@ -527,14 +378,6 @@ void udelay(unsigned int n); void mdelay(unsigned int n); void delay(unsigned int n); -#define abort() halt() /**< Alias for the halt() function */ - -/** - * Stop execution and halt the processor (this function does not return). - */ -void halt(void) __attribute__ ((noreturn)); -/** @} */ - /** * @defgroup readline Readline functions * This interface provides a simple implementation of the standard readline() diff --git a/payloads/libpayload/include/pci.h b/payloads/libpayload/include/pci.h index 93d1267ef5..2490f6cb4c 100644 --- a/payloads/libpayload/include/pci.h +++ b/payloads/libpayload/include/pci.h @@ -36,8 +36,11 @@ typedef u32 pcidev_t; #define REG_VENDOR_ID 0x00 #define REG_COMMAND 0x04 +#define REG_CLASS_DEV 0x0A #define REG_HEADER_TYPE 0x0E #define REG_PRIMARY_BUS 0x18 +#define REG_SUBSYS_VENDOR_ID 0x2C +#define REG_SUBSYS_ID 0x2E #define REG_COMMAND_BM (1 << 2) diff --git a/payloads/libpayload/include/stdarg.h b/payloads/libpayload/include/stdarg.h new file mode 100644 index 0000000000..7a6244e9ec --- /dev/null +++ b/payloads/libpayload/include/stdarg.h @@ -0,0 +1,44 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2008 Advanced Micro Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _LIBPAYLOAD_STDARG_H +#define _LIBPAYLOAD_STDARG_H + +#include_next + +/** + * @defgroup vprintf Varargs print functions + * @{ + */ +int vsnprintf(char *str, size_t size, const char *fmt, va_list ap); +int vsprintf(char *str, const char *fmt, va_list ap); +int vprintf(const char *fmt, va_list ap); +/** @} */ + +#endif diff --git a/payloads/libpayload/include/stdint.h b/payloads/libpayload/include/stdint.h new file mode 100644 index 0000000000..ae143d702c --- /dev/null +++ b/payloads/libpayload/include/stdint.h @@ -0,0 +1,30 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2010 coresystems GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include diff --git a/payloads/libpayload/include/stdio.h b/payloads/libpayload/include/stdio.h new file mode 100644 index 0000000000..2aea4459fa --- /dev/null +++ b/payloads/libpayload/include/stdio.h @@ -0,0 +1,46 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2008 Advanced Micro Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _STDIO_H +#define _STDIO_H + +#include + +/** + * @defgroup printf Print functions + * @{ + */ +int snprintf(char *str, size_t size, const char *fmt, ...); +int sprintf(char *str, const char *fmt, ...); +int printf(const char *fmt, ...); +/** @} */ + +void perror(const char *s); + +#endif diff --git a/payloads/libpayload/include/stdlib.h b/payloads/libpayload/include/stdlib.h new file mode 100644 index 0000000000..a106607b4b --- /dev/null +++ b/payloads/libpayload/include/stdlib.h @@ -0,0 +1,144 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2008 Advanced Micro Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _STDLIB_H +#define _STDLIB_H + +#include + +/** + * @defgroup malloc Memory allocation functions + * @{ + */ +#if defined(CONFIG_DEBUG_MALLOC) && !defined(IN_MALLOC_C) +#define free(p) \ + ({ \ + extern void print_malloc_map(void); \ + extern void free(void *); \ + printf("free(%p) called from %s:%s:%d...\n", p, __FILE__, __func__, \ + __LINE__);\ + printf("PRE free()\n"); \ + print_malloc_map(); \ + free(p); \ + printf("POST free()\n"); \ + print_malloc_map(); \ + }) +#define malloc(s) \ + ({ \ + extern void print_malloc_map(void); \ + extern void *malloc(size_t); \ + void *ptr; \ + printf("malloc(%u) called from %s:%s:%d...\n", s, __FILE__, __func__, \ + __LINE__);\ + printf("PRE malloc\n"); \ + print_malloc_map(); \ + ptr = malloc(s); \ + printf("POST malloc (ptr = %p)\n", ptr); \ + print_malloc_map(); \ + ptr; \ + }) +#define calloc(n,s) \ + ({ \ + extern void print_malloc_map(void); \ + extern void *calloc(size_t,size_t); \ + void *ptr; \ + printf("calloc(%u, %u) called from %s:%s:%d...\n", n, s, __FILE__, \ + __func__, __LINE__);\ + printf("PRE calloc\n"); \ + print_malloc_map(); \ + ptr = calloc(n,s); \ + printf("POST calloc (ptr = %p)\n", ptr); \ + print_malloc_map(); \ + ptr; \ + }) +#define realloc(p,s) \ + ({ \ + extern void print_malloc_map(void); \ + extern void *realloc(void*,size_t); \ + void *ptr; \ + printf("realloc(%p, %u) called from %s:%s:%d...\n", p, s, __FILE__, \ + __func__, __LINE__);\ + printf("PRE realloc\n"); \ + print_malloc_map(); \ + ptr = realloc(p,s); \ + printf("POST realloc (ptr = %p)\n", ptr); \ + print_malloc_map(); \ + ptr; \ + }) +#define memalign(a,s) \ + ({ \ + extern void print_malloc_map(void); \ + extern void *memalign(size_t, size_t); \ + void *ptr; \ + printf("memalign(%u, %u) called from %s:%s:%d...\n", a, s, __FILE__, \ + __func__, __LINE__);\ + printf("PRE memalign\n"); \ + print_malloc_map(); \ + ptr = memalign(a,s); \ + printf("POST realloc (ptr = %p)\n", ptr); \ + print_malloc_map(); \ + ptr; \ + }) +#else +void free(void *ptr); +void *malloc(size_t size); +void *calloc(size_t nmemb, size_t size); +void *realloc(void *ptr, size_t size); +void *memalign(size_t align, size_t size); +#endif +/** @} */ + +/** + * @defgroup stdlib String conversion functions + * @{ + */ +long int strtol(const char *s, char **nptr, int base); +unsigned long int strtoul(const char *s, char **nptr, int base); + +/** @} */ + +/** + * @defgroup rand Random number generator functions + * @{ + */ +int rand_r(unsigned int *seed); +int rand(void); +void srand(unsigned int seed); +/** @} */ + +/** + * Stop execution and halt the processor (this function does not return). + */ +void halt(void) __attribute__ ((noreturn)); +void exit(int status) __attribute__ ((noreturn)); +#define abort() halt() /**< Alias for the halt() function */ + +/** @} */ + +#endif diff --git a/payloads/libpayload/include/string.h b/payloads/libpayload/include/string.h new file mode 100644 index 0000000000..88054e017b --- /dev/null +++ b/payloads/libpayload/include/string.h @@ -0,0 +1,71 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2008 Advanced Micro Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _STRING_H +#define _STRING_H + +#include + +/** + * @defgroup memory Memory manipulation functions + * @{ + */ +void *memset(void *s, int c, size_t n); +void *memcpy(void *dst, const void *src, size_t n); +void *memmove(void *dst, const void *src, size_t n); +int memcmp(const void *s1, const void *s2, size_t len); +/** @} */ + +/** + * @defgroup string String functions + * @{ + */ +size_t strnlen(const char *str, size_t maxlen); +size_t strlen(const char *str); +int strcmp(const char *s1, const char *s2); +int strncmp(const char *s1, const char *s2, size_t maxlen); +int strcasecmp(const char *s1, const char *s2); +int strncasecmp(const char *s1, const char *s2, size_t maxlen); +char *strncpy(char *d, const char *s, size_t n); +char *strcpy(char *d, const char *s); +char *strncat(char *d, const char *s, size_t n); +size_t strlcat(char *d, const char *s, size_t n); +char *strcat(char *d, const char *s); +char *strchr(const char *s, int c); +char *strrchr(const char *s, int c); +char *strdup(const char *s); +char *strstr(const char *h, const char *n); +char *strsep(char **stringp, const char *delim); +size_t strspn(const char *s, const char *a); +size_t strcspn(const char *s, const char *a); +char* strtok(char *str, const char *delim); +char* strtok_r(char *str, const char *delim, char **ptr); +/** @} */ + +#endif diff --git a/payloads/libpayload/include/sys/types.h b/payloads/libpayload/include/sys/types.h new file mode 100644 index 0000000000..ae143d702c --- /dev/null +++ b/payloads/libpayload/include/sys/types.h @@ -0,0 +1,30 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2010 coresystems GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include diff --git a/payloads/libpayload/include/unistd.h b/payloads/libpayload/include/unistd.h new file mode 100644 index 0000000000..878638d419 --- /dev/null +++ b/payloads/libpayload/include/unistd.h @@ -0,0 +1,30 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2010 coresystems GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +int getpagesize(void); diff --git a/payloads/libpayload/libc/lib.c b/payloads/libpayload/libc/lib.c index 3666265d36..baccc07701 100644 --- a/payloads/libpayload/libc/lib.c +++ b/payloads/libpayload/libc/lib.c @@ -113,3 +113,10 @@ void fatal(const char *msg) halt(); } +void exit(int status) +{ + printf("exited with status %d\n", status); + halt(); +} + +int errno; diff --git a/payloads/libpayload/libc/printf.c b/payloads/libpayload/libc/printf.c index d53e99a8fe..04d39319bc 100644 --- a/payloads/libpayload/libc/printf.c +++ b/payloads/libpayload/libc/printf.c @@ -34,6 +34,7 @@ */ #include +#include /** Structure for specifying output methods for different printf clones. */ struct printf_spec { diff --git a/payloads/libpayload/libc/string.c b/payloads/libpayload/libc/string.c index b69fab6815..1ca8bc12e8 100644 --- a/payloads/libpayload/libc/string.c +++ b/payloads/libpayload/libc/string.c @@ -3,6 +3,7 @@ * * Copyright (C) 2007 Uwe Hermann * Copyright (C) 2008 Advanced Micro Devices, Inc. + * Copyright (C) 2010 coresystems GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -29,6 +30,9 @@ */ #include +#include +#include +#include /** * Calculate the length of a fixed-size string. @@ -74,6 +78,47 @@ size_t strlen(const char *str) return len; } +/** + * Compare two strings. + * + * @param s1 The first string. + * @param s2 The second string. + * @return Returns a value less than zero, if s1 is shorter than s2. Returns + * zero, if s1 equals s2. Returns a value greater than zero, if + * s1 is longer than s2. + */ +int strcasecmp(const char *s1, const char *s2) +{ + int i; + + for (i = 0; 1; i++) { + if (tolower(s1[i]) != tolower(s2[i])) + return s1[i] - s2[i]; + } + + return 0; +} + +/** + * Compare two strings with fixed length. + * + * @param s1 The first string. + * @param s2 The second string. + * @param maxlen Return at most maxlen characters as length of the string. + * @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2. + */ +int strncasecmp(const char *s1, const char *s2, size_t maxlen) +{ + int i; + + for (i = 0; i < maxlen; i++) { + if (tolower(s1[i]) != tolower(s2[i])) + return s1[i] - s2[i]; + } + + return 0; +} + /** * Compare two strings. * @@ -85,27 +130,14 @@ size_t strlen(const char *str) */ int strcmp(const char *s1, const char *s2) { - char c1, c2; - - /* Set c1 == c2, so that we can enter the while loop. */ - c1 = 0; - c2 = 0; - - /* Compare characters until they differ, or one of the strings ends. */ - while (c1 == c2) { - /* Read the next character from each string. */ - c1 = *s1++; - c2 = *s2++; - - /* Return something negative (if s1 is shorter than s2), or - zero (if s1 equals s2). */ - if (c1 == '\0') - return c1 - c2; + int i; + + for (i = 0; 1; i++) { + if (s1[i] != s2[i]) + return s1[i] - s2[i]; } - /* Return something positive (if s1 is longer than s2), or zero (if s1 - and s2 are equal). */ - return c1 - c2; + return 0; } /** @@ -160,6 +192,26 @@ char *strcpy(char *d, const char *s) return strncpy(d, s, strlen(s) + 1); } +/** + * Concatenates two strings + * + * @param d The destination string. + * @param s The source string. + * @return A pointer to the destination string. + */ +char *strcat(char *d, const char *s) +{ + char *p = d + strlen(d); + int sl = strlen(s); + int i; + + for (i = 0; i < sl; i++) + p[i] = s[i]; + + p[i] = '\0'; + return d; +} + /** * Concatenates two strings with a maximum length. * @@ -352,6 +404,69 @@ static int _offset(char ch, int base) return tolower(ch) - 'a'; } +/** + * Convert the initial portion of a string into a signed int + * @param ptr A pointer to the string to convert + * @param endptr A pointer to the unconverted part of the string + * @param base The base of the number to convert, or 0 for auto + * @return A signed integer representation of the string + */ + +long int strtol(const char *ptr, char **endptr, int base) +{ + int ret = 0; + int negative = 1; + + if (endptr != NULL) + *endptr = (char *) ptr; + + /* Purge whitespace */ + + for( ; *ptr && isspace(*ptr); ptr++); + + if (ptr[0] == '-') { + negative = -1; + ptr++; + } + + if (!*ptr) + return 0; + + /* Determine the base */ + + if (base == 0) { + if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) + base = 16; + else if (ptr[0] == '0') { + base = 8; + ptr++; + } + else + base = 10; + } + + /* Base 16 allows the 0x on front - so skip over it */ + + if (base == 16) { + if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) + ptr += 2; + } + + /* If the first character isn't valid, then don't + * bother */ + + if (!*ptr || !_valid(*ptr, base)) + return 0; + + for( ; *ptr && _valid(*ptr, base); ptr++) + ret = (ret * base) + _offset(*ptr, base); + + if (endptr != NULL) + *endptr = (char *) ptr; + + return ret * negative; +} + /** * Convert the initial portion of a string into an unsigned int * @param ptr A pointer to the string to convert @@ -360,7 +475,7 @@ static int _offset(char ch, int base) * @return An unsigned integer representation of the string */ -unsigned int strtoul(const char *ptr, char **endptr, int base) +unsigned long int strtoul(const char *ptr, char **endptr, int base) { int ret = 0; @@ -409,4 +524,102 @@ unsigned int strtoul(const char *ptr, char **endptr, int base) return ret; } +/** + * Determine the number of leading characters in s that match characters in a + * @param s A pointer to the string to analyse + * @param a A pointer to an array of characters that match the prefix + * @return The number of matching characters + */ + +size_t strspn(const char *s, const char *a) +{ + int i, j; + int al = strlen(a); + for (i = 0; s[i] != 0; i++) { + int found = 0; + for (j = 0; j < al; j++) { + if (s[i] == a[j]) { + found = 1; + break; + } + } + if (!found) + break; + } + return i; +} +/** + * Determine the number of leading characters in s that do not match characters in a + * @param s A pointer to the string to analyse + * @param a A pointer to an array of characters that do not match the prefix + * @return The number of not matching characters + */ + +size_t strcspn(const char *s, const char *a) +{ + int i, j; + int al = strlen(a); + for (i = 0; s[i] != 0; i++) { + int found = 0; + for (j = 0; j < al; j++) { + if (s[i] == a[j]) { + found = 1; + break; + } + } + if (found) + break; + } + return i; +} + +/** + * Extract first token in string str that is delimited by a character in tokens. + * Destroys str and eliminates the token delimiter. + * @param str A pointer to the string to tokenize. + * @param delim A pointer to an array of characters that delimit the token + * @param ptr A pointer to a string pointer to keep state of the tokenizer + * @return Pointer to token + */ + +char* strtok_r(char *str, const char *delim, char **ptr) +{ + /* start new tokenizing job or continue existing one? */ + if (str == NULL) + str = *ptr; + + /* skip over prefix delimiters */ + char *start = str + strspn(str, delim); + + /* find first delimiter character */ + char *end = start + strcspn(start, delim); + end[0] = '\0'; + + *ptr = end+1; + return start; +} + +static char **strtok_global; + +/** + * Extract first token in string str that is delimited by a character in tokens. + * Destroys str, eliminates the token delimiter and uses global state. + * @param str A pointer to the string to tokenize. + * @param delim A pointer to an array of characters that delimit the token + * @return Pointer to token + */ + +char* strtok(char *str, const char *delim) +{ + return strtok_r(str, delim, strtok_global); +} + +/** + * Print error message and error number + * @param s Error message to print + */ +void perror(const char *s) +{ + printf("%s: %d\n", s?s:"(none)", errno); +} -- cgit v1.2.3