summaryrefslogtreecommitdiffhomepage
path: root/dev/boot/src/BootSupport.cc
blob: 93dd1e300adc11ac297d2282bda8ffacba22c6c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -------------------------------------------

  Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.

------------------------------------------- */

#include <BootKit/BootKit.h>
#include <BootKit/Support.h>
#include <FirmwareKit/EFI/API.h>
#include <FirmwareKit/EFI/EFI.h>
#include <FirmwareKit/Handover.h>
#include <KernelKit/MSDOS.h>
#include <KernelKit/PE.h>

#ifdef __BOOTZ_STANDALONE__

/// @brief memset definition in C++.
/// @param dst destination pointer.
/// @param byte value to fill in.
/// @param len length of of src.
EXTERN_C VoidPtr memnset(void* dst, int byte, long long unsigned int len,
                         long long unsigned int dst_size) {
  if (!dst || len > dst_size) {
    // For now, we return nullptr or an error status.
    return nullptr;
  }
  unsigned char* p   = (unsigned char*) dst;
  unsigned char  val = (unsigned char) byte;
  for (size_t i = 0UL; i < len; ++i) {
    p[i] = val;
  }
  return dst;
}

/// @brief memcpy definition in C++.
/// @param dst destination pointer.
/// @param  src source pointer.
/// @param len length of of src.
EXTERN_C VoidPtr memncpy(void* dst, const void* src, long long unsigned int len,
                         long long unsigned int dst_size) {
  if (!dst || !src || len > dst_size) {
    // Similar to memset, this is a critical failure.
    return nullptr;
  }
  unsigned char*       d = (unsigned char*) dst;
  const unsigned char* s = (const unsigned char*) src;
  for (size_t i = 0UL; i < len; ++i) {
    d[i] = s[i];
  }
  return dst;
}

/// @brief strlen definition in C++.
EXTERN_C size_t strnlen(const char* whatToCheck, size_t max_len) {
  size_t len = 0;
  while (len < max_len && whatToCheck[len] != '\0') {
    ++len;
  }
  return len;
}

/// @brief strcmp definition in C++.
EXTERN_C int strncmp(const char* whatToCheck, const char* whatToCheckRight, size_t max_len) {
  size_t i = 0;
  while (i < max_len && whatToCheck[i] == whatToCheckRight[i]) {
    if (whatToCheck[i] == '\0') return 0;
    ++i;
  }
  if (i == max_len) {
    return 0;
  }
  return (unsigned char) whatToCheck[i] - (unsigned char) whatToCheckRight[i];
}

/// @brief something specific to the Microsoft's ABI, When the stack grows too big.
EXTERN_C void ___chkstk_ms(void) {}

/// @note GCC expects them to be here.

/// @brief memset definition in C++.
/// @param dst destination pointer.
/// @param byte value to fill in.
/// @param len length of of src.
EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len) {
  for (size_t i = 0UL; i < len; ++i) {
    ((int*) dst)[i] = byte;
  }

  return dst;
}

/// @brief memcpy definition in C++.
/// @param dst destination pointer.
/// @param  src source pointer.
/// @param len length of of src.
EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len) {
  for (size_t i = 0UL; i < len; ++i) {
    ((int*) dst)[i] = ((int*) src)[i];
  }

  return dst;
}

/// @brief strlen definition in C++.
EXTERN_C size_t strlen(const char* whatToCheck) {
  SizeT len = 0;

  while (whatToCheck[len] != 0) {
    ++len;
  }

  return len;
}

/// @brief strcmp definition in C++.
EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight) {
  SizeT len = 0;

  while (whatToCheck[len] == whatToCheckRight[len]) {
    if (whatToCheck[len] == 0) return 0;

    ++len;
  }

  return len;
}

#endif