efi_freestanding.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // 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. #include <common.h>
  11. /**
  12. * memcmp() - compare memory areas
  13. *
  14. * @s1: pointer to first area
  15. * @s2: pointer to second area
  16. * @n: number of bytes to compare
  17. * Return: 0 if both memory areas are the same, otherwise the sign of the
  18. * result value is the same as the sign of the difference between
  19. * the first differing pair of bytes taken as u8.
  20. */
  21. int memcmp(const void *s1, const void *s2, size_t n)
  22. {
  23. const u8 *pos1 = s1;
  24. const u8 *pos2 = s2;
  25. for (; n; --n) {
  26. if (*pos1 != *pos2)
  27. return *pos1 - *pos2;
  28. ++pos1;
  29. ++pos2;
  30. }
  31. return 0;
  32. }
  33. /**
  34. * memcpy() - copy memory area
  35. *
  36. * @dest: destination buffer
  37. * @src: source buffer
  38. * @n: number of bytes to copy
  39. * Return: pointer to destination buffer
  40. */
  41. void *memmove(void *dest, const void *src, size_t n)
  42. {
  43. u8 *d = dest;
  44. const u8 *s = src;
  45. if (d <= s) {
  46. for (; n; --n)
  47. *d++ = *s++;
  48. } else {
  49. d += n;
  50. s += n;
  51. for (; n; --n)
  52. *--d = *--s;
  53. }
  54. return dest;
  55. }
  56. /**
  57. * memcpy() - copy memory area
  58. *
  59. * @dest: destination buffer
  60. * @src: source buffer
  61. * @n: number of bytes to copy
  62. * Return: pointer to destination buffer
  63. */
  64. void *memcpy(void *dest, const void *src, size_t n)
  65. {
  66. return memmove(dest, src, n);
  67. }
  68. /**
  69. * memset() - fill memory with a constant byte
  70. *
  71. * @s: destination buffer
  72. * @c: byte value
  73. * @n: number of bytes to set
  74. * Return: pointer to destination buffer
  75. */
  76. void *memset(void *s, int c, size_t n)
  77. {
  78. u8 *d = s;
  79. for (; n; --n)
  80. *d++ = c;
  81. return s;
  82. }
  83. /**
  84. * __cyg_profile_func_enter() - record function entry
  85. *
  86. * This is called on every function entry when compiling with
  87. * -finstrument-functions.
  88. *
  89. * We do nothing here.
  90. *
  91. * @param func_ptr Pointer to function being entered
  92. * @param caller Pointer to function which called this function
  93. */
  94. void __attribute__((no_instrument_function))
  95. __cyg_profile_func_enter(void *func_ptr, void *caller)
  96. {
  97. }
  98. /**
  99. * __cyg_profile_func_exit() - record function exit
  100. *
  101. * This is called on every function exit when compiling with
  102. * -finstrument-functions.
  103. *
  104. * We do nothing here.
  105. *
  106. * @param func_ptr Pointer to function being entered
  107. * @param caller Pointer to function which called this function
  108. */
  109. void __attribute__((no_instrument_function))
  110. __cyg_profile_func_exit(void *func_ptr, void *caller)
  111. {
  112. }