summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig14
-rw-r--r--lib/aes.c2
-rw-r--r--lib/bch.c48
-rw-r--r--lib/div64.c141
-rw-r--r--lib/fdtdec.c2
-rw-r--r--lib/libfdt/fdt_overlay.c23
-rw-r--r--lib/libfdt/fdt_rw.c3
-rw-r--r--lib/libfdt/fdt_strerror.c3
-rw-r--r--lib/libfdt/setup.py2
-rw-r--r--lib/rsa/Kconfig4
-rw-r--r--lib/string.c6
-rw-r--r--lib/tiny-printf.c154
-rw-r--r--lib/tpm.c40
13 files changed, 418 insertions, 24 deletions
diff --git a/lib/Kconfig b/lib/Kconfig
index 65c01573e1..a0d5d926eb 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -52,6 +52,18 @@ config LIB_RAND
help
This library provides pseudo-random number generator functions.
+config SPL_TINY_MEMSET
+ bool "Use a very small memset() in SPL"
+ help
+ The faster memset() is the arch-specific one (if available) enabled
+ by CONFIG_USE_ARCH_MEMSET. If that is not enabled, we can still get
+ better performance by writing a word at a time. But in very
+ size-constrained envrionments even this may be too big. Enable this
+ option to reduce code size slightly at the cost of some speed.
+
+config RBTREE
+ bool
+
source lib/dhry/Kconfig
source lib/rsa/Kconfig
@@ -123,6 +135,8 @@ config LZ4
frame format currently (2015) implemented in the Linux kernel
(generated by 'lz4 -l'). The two formats are incompatible.
+config LZO
+ bool
endmenu
config ERRNO_STR
diff --git a/lib/aes.c b/lib/aes.c
index 9d7a0a1c11..d6144e61d6 100644
--- a/lib/aes.c
+++ b/lib/aes.c
@@ -27,7 +27,7 @@
#else
#include <string.h>
#endif
-#include "aes.h"
+#include "uboot_aes.h"
/* forward s-box */
static const u8 sbox[256] = {
diff --git a/lib/bch.c b/lib/bch.c
index 147715afd0..ec53483774 100644
--- a/lib/bch.c
+++ b/lib/bch.c
@@ -54,10 +54,27 @@
* finite fields GF(2^q). In Rapport de recherche INRIA no 2829, 1996.
*/
+#ifndef USE_HOSTCC
#include <common.h>
#include <ubi_uboot.h>
#include <linux/bitops.h>
+#else
+#include <errno.h>
+#include <endian.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#undef cpu_to_be32
+#define cpu_to_be32 htobe32
+#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
+#define kmalloc(size, flags) malloc(size)
+#define kzalloc(size, flags) calloc(1, size)
+#define kfree free
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
+
#include <asm/byteorder.h>
#include <linux/bch.h>
@@ -95,6 +112,37 @@ struct gf_poly_deg1 {
unsigned int c[2];
};
+#ifdef USE_HOSTCC
+static int fls(int x)
+{
+ int r = 32;
+
+ if (!x)
+ return 0;
+ if (!(x & 0xffff0000u)) {
+ x <<= 16;
+ r -= 16;
+ }
+ if (!(x & 0xff000000u)) {
+ x <<= 8;
+ r -= 8;
+ }
+ if (!(x & 0xf0000000u)) {
+ x <<= 4;
+ r -= 4;
+ }
+ if (!(x & 0xc0000000u)) {
+ x <<= 2;
+ r -= 2;
+ }
+ if (!(x & 0x80000000u)) {
+ x <<= 1;
+ r -= 1;
+ }
+ return r;
+}
+#endif
+
/*
* same as encode_bch(), but process input data one byte at a time
*/
diff --git a/lib/div64.c b/lib/div64.c
index 319fca50fa..206f582ca9 100644
--- a/lib/div64.c
+++ b/lib/div64.c
@@ -13,14 +13,19 @@
*
* Code generated for this function might be very inefficient
* for some CPUs. __div64_32() can be overridden by linking arch-specific
- * assembly versions such as arch/powerpc/lib/div64.S and arch/sh/lib/div64.S.
+ * assembly versions such as arch/ppc/lib/div64.S and arch/sh/lib/div64.S
+ * or by defining a preprocessor macro in arch/include/asm/div64.h.
*/
-#include <div64.h>
-#include <linux/types.h>
-#include <linux/compiler.h>
+#include <linux/compat.h>
+#include <linux/kernel.h>
+#include <linux/math64.h>
-uint32_t notrace __div64_32(uint64_t *n, uint32_t base)
+/* Not needed on 64bit architectures */
+#if BITS_PER_LONG == 32
+
+#ifndef __div64_32
+uint32_t __attribute__((weak)) __div64_32(uint64_t *n, uint32_t base)
{
uint64_t rem = *n;
uint64_t b = base;
@@ -52,3 +57,129 @@ uint32_t notrace __div64_32(uint64_t *n, uint32_t base)
*n = res;
return rem;
}
+EXPORT_SYMBOL(__div64_32);
+#endif
+
+#ifndef div_s64_rem
+s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
+{
+ u64 quotient;
+
+ if (dividend < 0) {
+ quotient = div_u64_rem(-dividend, abs(divisor), (u32 *)remainder);
+ *remainder = -*remainder;
+ if (divisor > 0)
+ quotient = -quotient;
+ } else {
+ quotient = div_u64_rem(dividend, abs(divisor), (u32 *)remainder);
+ if (divisor < 0)
+ quotient = -quotient;
+ }
+ return quotient;
+}
+EXPORT_SYMBOL(div_s64_rem);
+#endif
+
+/**
+ * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
+ * @dividend: 64bit dividend
+ * @divisor: 64bit divisor
+ * @remainder: 64bit remainder
+ *
+ * This implementation is a comparable to algorithm used by div64_u64.
+ * But this operation, which includes math for calculating the remainder,
+ * is kept distinct to avoid slowing down the div64_u64 operation on 32bit
+ * systems.
+ */
+#ifndef div64_u64_rem
+u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
+{
+ u32 high = divisor >> 32;
+ u64 quot;
+
+ if (high == 0) {
+ u32 rem32;
+ quot = div_u64_rem(dividend, divisor, &rem32);
+ *remainder = rem32;
+ } else {
+ int n = 1 + fls(high);
+ quot = div_u64(dividend >> n, divisor >> n);
+
+ if (quot != 0)
+ quot--;
+
+ *remainder = dividend - quot * divisor;
+ if (*remainder >= divisor) {
+ quot++;
+ *remainder -= divisor;
+ }
+ }
+
+ return quot;
+}
+EXPORT_SYMBOL(div64_u64_rem);
+#endif
+
+/**
+ * div64_u64 - unsigned 64bit divide with 64bit divisor
+ * @dividend: 64bit dividend
+ * @divisor: 64bit divisor
+ *
+ * This implementation is a modified version of the algorithm proposed
+ * by the book 'Hacker's Delight'. The original source and full proof
+ * can be found here and is available for use without restriction.
+ *
+ * 'http://www.hackersdelight.org/hdcodetxt/divDouble.c.txt'
+ */
+#ifndef div64_u64
+u64 div64_u64(u64 dividend, u64 divisor)
+{
+ u32 high = divisor >> 32;
+ u64 quot;
+
+ if (high == 0) {
+ quot = div_u64(dividend, divisor);
+ } else {
+ int n = 1 + fls(high);
+ quot = div_u64(dividend >> n, divisor >> n);
+
+ if (quot != 0)
+ quot--;
+ if ((dividend - quot * divisor) >= divisor)
+ quot++;
+ }
+
+ return quot;
+}
+EXPORT_SYMBOL(div64_u64);
+#endif
+
+/**
+ * div64_s64 - signed 64bit divide with 64bit divisor
+ * @dividend: 64bit dividend
+ * @divisor: 64bit divisor
+ */
+#ifndef div64_s64
+s64 div64_s64(s64 dividend, s64 divisor)
+{
+ s64 quot, t;
+
+ quot = div64_u64(abs(dividend), abs(divisor));
+ t = (dividend ^ divisor) >> 63;
+
+ return (quot ^ t) - t;
+}
+EXPORT_SYMBOL(div64_s64);
+#endif
+
+#endif /* BITS_PER_LONG == 32 */
+
+/*
+ * Iterative div/mod for use when dividend is not expected to be much
+ * bigger than divisor.
+ */
+u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
+{
+ return __iter_div_u64_rem(dividend, divisor, remainder);
+}
+EXPORT_SYMBOL(iter_div_u64_rem);
diff --git a/lib/fdtdec.c b/lib/fdtdec.c
index 81f47ef2c7..1edfbf2d39 100644
--- a/lib/fdtdec.c
+++ b/lib/fdtdec.c
@@ -112,7 +112,7 @@ fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
return FDT_ADDR_T_NONE;
}
-#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_OF_LIBFDT)
+#if CONFIG_IS_ENABLED(OF_TRANSLATE)
if (translate)
addr = fdt_translate_address(blob, node, prop_addr);
else
diff --git a/lib/libfdt/fdt_overlay.c b/lib/libfdt/fdt_overlay.c
index 56cb70ed44..ceb968786e 100644
--- a/lib/libfdt/fdt_overlay.c
+++ b/lib/libfdt/fdt_overlay.c
@@ -21,14 +21,14 @@
*/
static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
{
- const uint32_t *val;
+ const fdt32_t *val;
int len;
val = fdt_getprop(fdto, fragment, "target", &len);
if (!val)
return 0;
- if ((len != sizeof(*val)) || (*val == (uint32_t)-1))
+ if ((len != sizeof(*val)) || (fdt32_to_cpu(*val) == (uint32_t)-1))
return (uint32_t)-1;
return fdt32_to_cpu(*val);
@@ -99,7 +99,7 @@ static int overlay_get_target(const void *fdt, const void *fdto,
static int overlay_phandle_add_offset(void *fdt, int node,
const char *name, uint32_t delta)
{
- const uint32_t *val;
+ const fdt32_t *val;
uint32_t adj_val;
int len;
@@ -210,7 +210,7 @@ static int overlay_update_local_node_references(void *fdto,
int ret;
fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
- const uint32_t *fixup_val;
+ const fdt32_t *fixup_val;
const char *tree_val;
const char *name;
int fixup_len;
@@ -234,7 +234,8 @@ static int overlay_update_local_node_references(void *fdto,
}
for (i = 0; i < (fixup_len / sizeof(uint32_t)); i++) {
- uint32_t adj_val, poffset;
+ fdt32_t adj_val;
+ uint32_t poffset;
poffset = fdt32_to_cpu(fixup_val[i]);
@@ -246,9 +247,7 @@ static int overlay_update_local_node_references(void *fdto,
*/
memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
- adj_val = fdt32_to_cpu(adj_val);
- adj_val += delta;
- adj_val = cpu_to_fdt32(adj_val);
+ adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) + delta);
ret = fdt_setprop_inplace_namelen_partial(fdto,
tree_node,
@@ -272,7 +271,7 @@ static int overlay_update_local_node_references(void *fdto,
tree_child = fdt_subnode_offset(fdto, tree_node,
fixup_child_name);
- if (ret == -FDT_ERR_NOTFOUND)
+ if (tree_child == -FDT_ERR_NOTFOUND)
return -FDT_ERR_BADOVERLAY;
if (tree_child < 0)
return tree_child;
@@ -356,6 +355,7 @@ static int overlay_fixup_one_phandle(void *fdt, void *fdto,
{
const char *symbol_path;
uint32_t phandle;
+ fdt32_t phandle_prop;
int symbol_off, fixup_off;
int prop_len;
@@ -381,10 +381,11 @@ static int overlay_fixup_one_phandle(void *fdt, void *fdto,
if (fixup_off < 0)
return fixup_off;
- phandle = cpu_to_fdt32(phandle);
+ phandle_prop = cpu_to_fdt32(phandle);
return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
name, name_len, poffset,
- &phandle, sizeof(phandle));
+ &phandle_prop,
+ sizeof(phandle_prop));
};
/**
diff --git a/lib/libfdt/fdt_rw.c b/lib/libfdt/fdt_rw.c
index 87d4030fb1..80a3212141 100644
--- a/lib/libfdt/fdt_rw.c
+++ b/lib/libfdt/fdt_rw.c
@@ -242,7 +242,8 @@ int fdt_setprop(void *fdt, int nodeoffset, const char *name,
if (err)
return err;
- memcpy(prop->data, val, len);
+ if (len)
+ memcpy(prop->data, val, len);
return 0;
}
diff --git a/lib/libfdt/fdt_strerror.c b/lib/libfdt/fdt_strerror.c
index 7f8e02b1ee..f89004c609 100644
--- a/lib/libfdt/fdt_strerror.c
+++ b/lib/libfdt/fdt_strerror.c
@@ -36,6 +36,9 @@ static struct fdt_errtabent fdt_errtable[] = {
FDT_ERRTABENT(FDT_ERR_BADVERSION),
FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE),
FDT_ERRTABENT(FDT_ERR_BADLAYOUT),
+ FDT_ERRTABENT(FDT_ERR_INTERNAL),
+ FDT_ERRTABENT(FDT_ERR_BADNCELLS),
+ FDT_ERRTABENT(FDT_ERR_BADVALUE),
FDT_ERRTABENT(FDT_ERR_BADOVERLAY),
FDT_ERRTABENT(FDT_ERR_NOPHANDLES),
};
diff --git a/lib/libfdt/setup.py b/lib/libfdt/setup.py
index 62e7bcc1ac..845a0c2b10 100644
--- a/lib/libfdt/setup.py
+++ b/lib/libfdt/setup.py
@@ -27,7 +27,7 @@ libfdt_module = Extension(
extra_compile_args = cflags
)
-sys.argv = [progname, '--quiet', 'build_ext', '--inplace']
+sys.argv = [progname, '--quiet', 'build_ext', '--inplace', '--force']
setup (name = 'libfdt',
version = '0.1',
diff --git a/lib/rsa/Kconfig b/lib/rsa/Kconfig
index 09ec358242..fde1ac108d 100644
--- a/lib/rsa/Kconfig
+++ b/lib/rsa/Kconfig
@@ -1,6 +1,6 @@
config RSA
bool "Use RSA Library"
- select RSA_FREESCALE_EXP if FSL_CAAM
+ select RSA_FREESCALE_EXP if FSL_CAAM && !ARCH_MX7 && !ARCH_MX6 && !ARCH_MX5
select RSA_SOFTWARE_EXP if !RSA_FREESCALE_EXP
help
RSA support. This enables the RSA algorithm used for FIT image
@@ -29,7 +29,7 @@ config RSA_SOFTWARE_EXP
config RSA_FREESCALE_EXP
bool "Enable RSA Modular Exponentiation with FSL crypto accelerator"
- depends on DM && RSA && FSL_CAAM
+ depends on DM && RSA && FSL_CAAM && !ARCH_MX7 && !ARCH_MX6 && !ARCH_MX5
help
Enables driver for RSA modular exponentiation using Freescale cryptographic
accelerator - CAAM.
diff --git a/lib/string.c b/lib/string.c
index 67d5f6a421..c1a28c14ce 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -437,8 +437,10 @@ char *strswab(const char *s)
void * memset(void * s,int c,size_t count)
{
unsigned long *sl = (unsigned long *) s;
- unsigned long cl = 0;
char *s8;
+
+#if !CONFIG_IS_ENABLED(TINY_MEMSET)
+ unsigned long cl = 0;
int i;
/* do it one word at a time (32 bits or 64 bits) while possible */
@@ -452,7 +454,7 @@ void * memset(void * s,int c,size_t count)
count -= sizeof(*sl);
}
}
- /* fill 8 bits at a time */
+#endif /* fill 8 bits at a time */
s8 = (char *)sl;
while (count--)
*s8++ = c;
diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c
index 6def8f98aa..0b04813dc2 100644
--- a/lib/tiny-printf.c
+++ b/lib/tiny-printf.c
@@ -12,6 +12,7 @@
#include <common.h>
#include <stdarg.h>
#include <serial.h>
+#include <linux/ctype.h>
struct printf_info {
char *bf; /* Digit buffer */
@@ -52,6 +53,154 @@ static void div_out(struct printf_info *info, unsigned long *num,
out_dgt(info, dgt);
}
+#ifdef CONFIG_SPL_NET_SUPPORT
+static void string(struct printf_info *info, char *s)
+{
+ char ch;
+
+ while ((ch = *s++))
+ out(info, ch);
+}
+
+static const char hex_asc[] = "0123456789abcdef";
+#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
+#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
+
+static inline char *pack_hex_byte(char *buf, u8 byte)
+{
+ *buf++ = hex_asc_hi(byte);
+ *buf++ = hex_asc_lo(byte);
+ return buf;
+}
+
+static void mac_address_string(struct printf_info *info, u8 *addr,
+ bool separator)
+{
+ /* (6 * 2 hex digits), 5 colons and trailing zero */
+ char mac_addr[6 * 3];
+ char *p = mac_addr;
+ int i;
+
+ for (i = 0; i < 6; i++) {
+ p = pack_hex_byte(p, addr[i]);
+ if (separator && i != 5)
+ *p++ = ':';
+ }
+ *p = '\0';
+
+ string(info, mac_addr);
+}
+
+static char *put_dec_trunc(char *buf, unsigned int q)
+{
+ unsigned int d3, d2, d1, d0;
+ d1 = (q >> 4) & 0xf;
+ d2 = (q >> 8) & 0xf;
+ d3 = (q >> 12);
+
+ d0 = 6 * (d3 + d2 + d1) + (q & 0xf);
+ q = (d0 * 0xcd) >> 11;
+ d0 = d0 - 10 * q;
+ *buf++ = d0 + '0'; /* least significant digit */
+ d1 = q + 9 * d3 + 5 * d2 + d1;
+ if (d1 != 0) {
+ q = (d1 * 0xcd) >> 11;
+ d1 = d1 - 10 * q;
+ *buf++ = d1 + '0'; /* next digit */
+
+ d2 = q + 2 * d2;
+ if ((d2 != 0) || (d3 != 0)) {
+ q = (d2 * 0xd) >> 7;
+ d2 = d2 - 10 * q;
+ *buf++ = d2 + '0'; /* next digit */
+
+ d3 = q + 4 * d3;
+ if (d3 != 0) {
+ q = (d3 * 0xcd) >> 11;
+ d3 = d3 - 10 * q;
+ *buf++ = d3 + '0'; /* next digit */
+ if (q != 0)
+ *buf++ = q + '0'; /* most sign. digit */
+ }
+ }
+ }
+ return buf;
+}
+
+static void ip4_addr_string(struct printf_info *info, u8 *addr)
+{
+ /* (4 * 3 decimal digits), 3 dots and trailing zero */
+ char ip4_addr[4 * 4];
+ char temp[3]; /* hold each IP quad in reverse order */
+ char *p = ip4_addr;
+ int i, digits;
+
+ for (i = 0; i < 4; i++) {
+ digits = put_dec_trunc(temp, addr[i]) - temp;
+ /* reverse the digits in the quad */
+ while (digits--)
+ *p++ = temp[digits];
+ if (i != 3)
+ *p++ = '.';
+ }
+ *p = '\0';
+
+ string(info, ip4_addr);
+}
+#endif
+
+/*
+ * Show a '%p' thing. A kernel extension is that the '%p' is followed
+ * by an extra set of characters that are extended format
+ * specifiers.
+ *
+ * Right now we handle:
+ *
+ * - 'M' For a 6-byte MAC address, it prints the address in the
+ * usual colon-separated hex notation.
+ * - 'm' Same as above except there is no colon-separator.
+ * - 'I4'for IPv4 addresses printed in the usual way (dot-separated
+ * decimal).
+ */
+
+static void pointer(struct printf_info *info, const char *fmt, void *ptr)
+{
+#ifdef DEBUG
+ unsigned long num = (uintptr_t)ptr;
+ unsigned long div;
+#endif
+
+ switch (*fmt) {
+#ifdef DEBUG
+ case 'a':
+
+ switch (fmt[1]) {
+ case 'p':
+ default:
+ num = *(phys_addr_t *)ptr;
+ break;
+ }
+ break;
+#endif
+#ifdef CONFIG_SPL_NET_SUPPORT
+ case 'm':
+ return mac_address_string(info, ptr, false);
+ case 'M':
+ return mac_address_string(info, ptr, true);
+ case 'I':
+ if (fmt[1] == '4')
+ return ip4_addr_string(info, ptr);
+#endif
+ default:
+ break;
+ }
+#ifdef DEBUG
+ div = 1UL << (sizeof(long) * 8 - 4);
+ for (; div; div /= 0x10)
+ div_out(info, &num, div);
+#endif
+}
+
static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
{
char ch;
@@ -144,6 +293,11 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
case 's':
p = va_arg(va, char*);
break;
+ case 'p':
+ pointer(info, fmt, va_arg(va, void *));
+ while (isalnum(fmt[0]))
+ fmt++;
+ break;
case '%':
out(info, '%');
default:
diff --git a/lib/tpm.c b/lib/tpm.c
index fb1221472a..cd7f88f220 100644
--- a/lib/tpm.c
+++ b/lib/tpm.c
@@ -996,4 +996,44 @@ uint32_t tpm_get_pub_key_oiap(uint32_t key_handle, const void *usage_auth,
return 0;
}
+#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
+uint32_t tpm_find_key_sha1(const uint8_t auth[20], const uint8_t
+ pubkey_digest[20], uint32_t *handle)
+{
+ uint16_t key_count;
+ uint32_t key_handles[10];
+ uint8_t buf[288];
+ uint8_t *ptr;
+ uint32_t err;
+ uint8_t digest[20];
+ size_t buf_len;
+ unsigned int i;
+
+ /* fetch list of already loaded keys in the TPM */
+ err = tpm_get_capability(TPM_CAP_HANDLE, TPM_RT_KEY, buf, sizeof(buf));
+ if (err)
+ return -1;
+ key_count = get_unaligned_be16(buf);
+ ptr = buf + 2;
+ for (i = 0; i < key_count; ++i, ptr += 4)
+ key_handles[i] = get_unaligned_be32(ptr);
+
+ /* now search a(/ the) key which we can access with the given auth */
+ for (i = 0; i < key_count; ++i) {
+ buf_len = sizeof(buf);
+ err = tpm_get_pub_key_oiap(key_handles[i], auth, buf, &buf_len);
+ if (err && err != TPM_AUTHFAIL)
+ return -1;
+ if (err)
+ continue;
+ sha1_csum(buf, buf_len, digest);
+ if (!memcmp(digest, pubkey_digest, 20)) {
+ *handle = key_handles[i];
+ return 0;
+ }
+ }
+ return 1;
+}
+#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
+
#endif /* CONFIG_TPM_AUTH_SESSIONS */