Elyes Haouas has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/86233?usp=email )
Change subject: include/string.h: Add memcpy_s function ......................................................................
include/string.h: Add memcpy_s function
Change-Id: I0d32c838e94ae760907efe55ed00bab3faaaa8c5 Signed-off-by: Elyes Haouas ehaouas@noos.fr --- M src/include/string.h M src/soc/intel/denverton_ns/include/soc/soc_util.h M src/soc/intel/denverton_ns/soc_util.c 3 files changed, 15 insertions(+), 54 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/33/86233/1
diff --git a/src/include/string.h b/src/include/string.h index bbf1c3a..f3ea668 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -24,6 +24,21 @@ char *strtok_r(char *str, const char *delim, char **ptr); char *strtok(char *str, const char *delim); long atol(const char *str); +static inline int memcpy_s(void *dest, size_t destsz, const void *src, size_t count) +{ + if (!dest || !src) + return EINVAL; + if (count > destsz) + return ERANGE; + + uint8_t *dp = (uint8_t *)dest; + const uint8_t *sp = (const uint8_t *)src; + if (((dp > sp) && (dp < (sp + count))) || ((sp > dp) && (sp < (dp + count)))) + return EINVAL; + + memcpy(dest, src, count); + return 0; +}
/** * Find a character in a string. diff --git a/src/soc/intel/denverton_ns/include/soc/soc_util.h b/src/soc/intel/denverton_ns/include/soc/soc_util.h index 5b7a75d..d38737d 100644 --- a/src/soc/intel/denverton_ns/include/soc/soc_util.h +++ b/src/soc/intel/denverton_ns/include/soc/soc_util.h @@ -35,11 +35,6 @@ uint16_t get_pmbase(void); uint16_t get_tcobase(void);
-/* -* Secure functions. -*/ -void *memcpy_s(void *dest, const void *src, size_t n); - void mmio_andthenor32(void *addr, uint32_t val2and, uint32_t val2or); uint8_t silicon_stepping(void);
diff --git a/src/soc/intel/denverton_ns/soc_util.c b/src/soc/intel/denverton_ns/soc_util.c index dbe924b..22e9471 100644 --- a/src/soc/intel/denverton_ns/soc_util.c +++ b/src/soc/intel/denverton_ns/soc_util.c @@ -238,52 +238,3 @@
return revision_id; } - -void *memcpy_s(void *dest, const void *src, size_t n) -{ - uint8_t *dp; - const uint8_t *sp; - - dp = (uint8_t *)dest; - sp = (uint8_t *)src; - - if (!n) - return dest; - - if (n > UINT32_MAX) - return dest; - - if (!dp) - return dest; - - if (!sp) - return dest; - - /* - * overlap is undefined behavior, do not allow - */ - if (((dp > sp) && (dp < (sp + n))) || ((sp > dp) && (sp < (dp + n)))) - return dest; - - /* - * now perform the copy - */ - - /* Original memcpy() function */ - unsigned long d0, d1, d2; - - asm volatile( -#if ENV_X86_64 - "rep ; movsd\n\t" - "mov %4,%%rcx\n\t" -#else - "rep ; movsl\n\t" - "movl %4,%%ecx\n\t" -#endif - "rep ; movsb\n\t" - : "=&c"(d0), "=&D"(d1), "=&S"(d2) - : "0"(n >> 2), "g"(n & 3), "1"(dest), "2"(src) - : "memory"); - - return dest; -}