Andrew Geissler | 9347dd4 | 2023-03-03 12:38:41 -0600 | [diff] [blame^] | 1 | From e2eff4f80e65cb3fcbe6345b5376a6bf7de7e2cc Mon Sep 17 00:00:00 2001 |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 2 | From: Jaxson Han <jaxson.han@arm.com> |
| 3 | Date: Tue, 28 Dec 2021 17:28:25 +0800 |
| 4 | Subject: [PATCH] common: Add essential libc functions |
| 5 | |
| 6 | The libfdt uses some of the libc functions, e.g. memcmp, memmove, |
| 7 | strlen .etc. Add them in lib.c. |
| 8 | |
| 9 | The code is copied from TF-A (v2.5) [1] project, which is under the |
| 10 | terms of BSD license. It is the same with boot-wrapper. |
| 11 | |
| 12 | [1]: https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git |
| 13 | |
| 14 | Issue-Id: SCM-3814 |
| 15 | Upstream-Status: Inappropriate [other] |
| 16 | Implementation pending further discussion |
| 17 | Signed-off-by: Jaxson Han <jaxson.han@arm.com> |
| 18 | Change-Id: If3b55b00afa8694c7522df989a41e0b38eda1d38 |
| 19 | --- |
| 20 | common/lib.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++- |
| 21 | 1 file changed, 70 insertions(+), 1 deletion(-) |
| 22 | |
| 23 | diff --git a/common/lib.c b/common/lib.c |
| 24 | index fcf5f69..0be1c4a 100644 |
| 25 | --- a/common/lib.c |
| 26 | +++ b/common/lib.c |
| 27 | @@ -32,4 +32,73 @@ void *memset(void *s, int c, size_t n) |
| 28 | return s; |
| 29 | } |
| 30 | |
| 31 | -/* TODO: memmove and memcmp could also be called */ |
| 32 | +int memcmp(const void *s1, const void *s2, size_t len) |
| 33 | +{ |
| 34 | + const unsigned char *s = s1; |
| 35 | + const unsigned char *d = s2; |
| 36 | + unsigned char sc; |
| 37 | + unsigned char dc; |
| 38 | + |
| 39 | + while (len--) { |
| 40 | + sc = *s++; |
| 41 | + dc = *d++; |
| 42 | + if (sc - dc) |
| 43 | + return (sc - dc); |
| 44 | + } |
| 45 | + |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +void *memmove(void *dst, const void *src, size_t len) |
| 50 | +{ |
| 51 | + if ((size_t)dst - (size_t)src >= len) { |
| 52 | + /* destination not in source data, so can safely use memcpy */ |
| 53 | + return memcpy(dst, src, len); |
| 54 | + } else { |
| 55 | + /* copy backwards... */ |
| 56 | + const char *end = dst; |
| 57 | + const char *s = (const char *)src + len; |
| 58 | + char *d = (char *)dst + len; |
| 59 | + while (d != end) |
| 60 | + *--d = *--s; |
| 61 | + } |
| 62 | + return dst; |
| 63 | +} |
| 64 | + |
| 65 | +void *memchr(const void *src, int c, size_t len) |
| 66 | +{ |
| 67 | + const unsigned char *s = src; |
| 68 | + |
| 69 | + while (len--) { |
| 70 | + if (*s == (unsigned char)c) |
| 71 | + return (void *) s; |
| 72 | + s++; |
| 73 | + } |
| 74 | + |
| 75 | + return NULL; |
| 76 | +} |
| 77 | + |
| 78 | +char *strrchr(const char *p, int ch) |
| 79 | +{ |
| 80 | + char *save; |
| 81 | + char c; |
| 82 | + |
| 83 | + c = ch; |
| 84 | + for (save = NULL;; ++p) { |
| 85 | + if (*p == c) |
| 86 | + save = (char *)p; |
| 87 | + if (*p == '\0') |
| 88 | + return (save); |
| 89 | + } |
| 90 | + /* NOTREACHED */ |
| 91 | +} |
| 92 | + |
| 93 | +size_t strlen(const char *s) |
| 94 | +{ |
| 95 | + const char *cursor = s; |
| 96 | + |
| 97 | + while (*cursor) |
| 98 | + cursor++; |
| 99 | + |
| 100 | + return cursor - s; |
| 101 | +} |