blob: b2786095c3232694507e90e5a3deb8957f167c15 [file] [log] [blame]
Heinrich Schuchardtf51a2262019-01-20 08:20:30 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Library for freestanding binary
4 *
5 * Copyright 2019, Heinrich Schuchardt <xypron.glpk@gmx.de>
6 *
7 * GCC requires that freestanding programs provide memcpy(), memmove(),
8 * memset(), and memcmp().
9 */
10
Tom Rinic38cb222023-12-14 13:16:55 -050011#include <linux/types.h>
Heinrich Schuchardtf51a2262019-01-20 08:20:30 +010012
13/**
14 * memcmp() - compare memory areas
15 *
16 * @s1: pointer to first area
17 * @s2: pointer to second area
18 * @n: number of bytes to compare
19 * Return: 0 if both memory areas are the same, otherwise the sign of the
20 * result value is the same as the sign of the difference between
21 * the first differing pair of bytes taken as u8.
22 */
23int memcmp(const void *s1, const void *s2, size_t n)
24{
25 const u8 *pos1 = s1;
26 const u8 *pos2 = s2;
27
28 for (; n; --n) {
29 if (*pos1 != *pos2)
30 return *pos1 - *pos2;
31 ++pos1;
32 ++pos2;
33 }
34 return 0;
35}
36
37/**
38 * memcpy() - copy memory area
39 *
40 * @dest: destination buffer
41 * @src: source buffer
42 * @n: number of bytes to copy
43 * Return: pointer to destination buffer
44 */
45void *memmove(void *dest, const void *src, size_t n)
46{
47 u8 *d = dest;
48 const u8 *s = src;
49
Heinrich Schuchardt72291a92020-03-22 09:52:48 +010050 if (d <= s) {
Heinrich Schuchardtf51a2262019-01-20 08:20:30 +010051 for (; n; --n)
52 *d++ = *s++;
53 } else {
54 d += n;
55 s += n;
56 for (; n; --n)
57 *--d = *--s;
58 }
59 return dest;
60}
61
62/**
63 * memcpy() - copy memory area
64 *
65 * @dest: destination buffer
66 * @src: source buffer
67 * @n: number of bytes to copy
68 * Return: pointer to destination buffer
69 */
70void *memcpy(void *dest, const void *src, size_t n)
71{
72 return memmove(dest, src, n);
73}
74
75/**
76 * memset() - fill memory with a constant byte
77 *
78 * @s: destination buffer
79 * @c: byte value
80 * @n: number of bytes to set
81 * Return: pointer to destination buffer
82 */
83void *memset(void *s, int c, size_t n)
84{
85 u8 *d = s;
86
87 for (; n; --n)
88 *d++ = c;
89 return s;
90}
Heinrich Schuchardt9bb758a2020-01-01 13:19:12 +010091
92/**
93 * __cyg_profile_func_enter() - record function entry
94 *
95 * This is called on every function entry when compiling with
96 * -finstrument-functions.
97 *
98 * We do nothing here.
99 *
Heinrich Schuchardt3dd719d2022-01-20 19:48:20 +0100100 * func_ptr: Pointer to function being entered
101 * caller: Pointer to function which called this function
Heinrich Schuchardt9bb758a2020-01-01 13:19:12 +0100102 */
Simon Glass33c60a32022-12-21 16:08:15 -0700103void notrace
Heinrich Schuchardt9bb758a2020-01-01 13:19:12 +0100104__cyg_profile_func_enter(void *func_ptr, void *caller)
105{
106}
107
108/**
109 * __cyg_profile_func_exit() - record function exit
110 *
111 * This is called on every function exit when compiling with
112 * -finstrument-functions.
113 *
114 * We do nothing here.
115 *
Heinrich Schuchardt3dd719d2022-01-20 19:48:20 +0100116 * func_ptr: Pointer to function being entered
117 * caller: Pointer to function which called this function
Heinrich Schuchardt9bb758a2020-01-01 13:19:12 +0100118 */
Simon Glass33c60a32022-12-21 16:08:15 -0700119void notrace
Heinrich Schuchardt9bb758a2020-01-01 13:19:12 +0100120__cyg_profile_func_exit(void *func_ptr, void *caller)
121{
122}