blob: 0dbe5a5d63c320302a496be83f75311480c60827 [file] [log] [blame]
oliviermartin1e57a462013-01-25 11:28:06 +00001/** @file
2*
Olivier Martin3e8576d2014-02-12 15:11:29 +00003* Copyright (c) 2011-2014, ARM Limited. All rights reserved.
oliviermartin1e57a462013-01-25 11:28:06 +00004*
Ronald Cron3402aac2014-08-19 13:29:52 +00005* This program and the accompanying materials
6* are licensed and made available under the terms and conditions of the BSD License
7* which accompanies this distribution. The full text of the license may be found at
8* http://opensource.org/licenses/bsd-license.php
9*
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
oliviermartin1e57a462013-01-25 11:28:06 +000012*
13**/
14
15#ifndef _LIBFDT_ENV_H
16#define _LIBFDT_ENV_H
17
18#include <Library/BaseLib.h>
19#include <Library/BaseMemoryLib.h>
Vijay Kumar Pendoti19aa5bd2017-03-15 19:27:50 +053020#include <Library/MemoryAllocationLib.h>
21#include <Uefi.h>
oliviermartin1e57a462013-01-25 11:28:06 +000022
Olivier Martin3e8576d2014-02-12 15:11:29 +000023typedef UINT16 fdt16_t;
24typedef UINT32 fdt32_t;
25typedef UINT64 fdt64_t;
26
oliviermartin1e57a462013-01-25 11:28:06 +000027typedef UINT8 uint8_t;
28typedef UINT16 uint16_t;
29typedef UINT32 uint32_t;
30typedef UINT64 uint64_t;
31typedef UINTN uintptr_t;
32typedef UINTN size_t;
Vijay Kumar Pendoti19aa5bd2017-03-15 19:27:50 +053033typedef BOOLEAN bool;
oliviermartin1e57a462013-01-25 11:28:06 +000034
Olivier Martin3e8576d2014-02-12 15:11:29 +000035static inline uint16_t fdt16_to_cpu(fdt16_t x)
oliviermartin1e57a462013-01-25 11:28:06 +000036{
37 return SwapBytes16 (x);
38}
39#define cpu_to_fdt16(x) fdt16_to_cpu(x)
40
Olivier Martin3e8576d2014-02-12 15:11:29 +000041static inline uint32_t fdt32_to_cpu(fdt32_t x)
oliviermartin1e57a462013-01-25 11:28:06 +000042{
43 return SwapBytes32 (x);
44}
45#define cpu_to_fdt32(x) fdt32_to_cpu(x)
46
Olivier Martin3e8576d2014-02-12 15:11:29 +000047static inline uint64_t fdt64_to_cpu(fdt64_t x)
oliviermartin1e57a462013-01-25 11:28:06 +000048{
49 return SwapBytes64 (x);
50}
51#define cpu_to_fdt64(x) fdt64_to_cpu(x)
52
53static inline void* memcpy(void* dest, const void* src, size_t len) {
54 return CopyMem (dest, src, len);
55}
56
57static inline void *memmove(void *dest, const void *src, size_t n) {
58 return CopyMem (dest, src, n);
59}
60
61static inline void *memset(void *s, int c, size_t n) {
62 return SetMem (s, n, c);
63}
64
65static inline int memcmp(const void* dest, const void* src, int len) {
66 return CompareMem (dest, src, len);
67}
68
69static inline void *memchr(const void *s, int c, size_t n) {
70 return ScanMem8 (s, n, c);
71}
72
73static inline size_t strlen (const char* str) {
74 return AsciiStrLen (str);
75}
76
77static inline char *strchr(const char *s, int c) {
78 char pattern[2];
79 pattern[0] = c;
80 pattern[1] = 0;
81 return AsciiStrStr (s, pattern);
82}
83
Vijay Kumar Pendoti19aa5bd2017-03-15 19:27:50 +053084static inline int strcmp(const char *s1, const char *s2)
85{
86 return (int)AsciiStrCmp( s1, s2);
87}
88
89static inline int strncmp(const char *s1, const char *s2, size_t n)
90{
91 return (int)AsciiStrnCmp( s1, s2, n);
92}
93
94/**
95 Simple character classification routines, corresponding to POSIX class names
96 and ASCII encoding.
97**/
98#define toupper(a) ((((a) >= 'a') && ((a) <= 'z')) ? ((a) - 'a' + 'A') : (a))
99
100#define isalpha(chr) (('a' <= chr && chr <= 'z') || ('A' <= chr && chr <= 'Z'))
101STATIC
102BOOLEAN
103isalnum (
104 IN CHAR8 Chr
105 )
106{
107 return (('0' <= Chr && Chr <= '9') ||
108 ('A' <= Chr && Chr <= 'Z') ||
109 ('a' <= Chr && Chr <= 'z')
110 );
111}
112
113static int
114Digit2Val( int c)
115{
116 if(isalpha(c)) { /* If c is one of [A-Za-z]... */
117 c = toupper(c) - 7; // Adjust so 'A' is ('9' + 1)
118 }
119 return c - '0'; // Value returned is between 0 and 35, inclusive.
120}
121
122/* Determines if a particular character represents a space character */
123static inline int isspace (int c)
124{
125 //
126 // <space> ::= [ ]
127 //
128 return ((c) == ' ');
129}
130
131#define UINT32_MAX 0xffffffffU /* uint32_t */
132/** The strtoul function converts the initial portion of the string pointed to
133 by nptr to unsigned long int representation.
134
135 See the description for strtol for more information.
136
137 @return The strtoul function returns the converted value, if any. If no
138 conversion could be performed, zero is returned. If the correct
139 value is outside the range of representable values, ULONG_MAX is
140 returned and the value of the macro ERANGE is stored in errno.
141**/
142
143static inline unsigned long
144strtoul(const char * __restrict nptr, char ** __restrict endptr, int base)
145{
146 const char *pEnd;
147 unsigned long Result = 0;
148 unsigned long Previous;
149 int temp;
150
151 pEnd = nptr;
152
153 if((base < 0) || (base == 1) || (base > 36)) {
154 if(endptr != NULL) {
155 *endptr = NULL;
156 }
157 return 0;
158 }
159 // Skip leading spaces.
160 while(isspace(*nptr)) ++nptr;
161
162 // Process Subject sequence: optional + sign followed by digits.
163 if(*nptr == '+') {
164 ++nptr;
165 }
166
167 if(*nptr == '0') { /* Might be Octal or Hex */
168 if(toupper(nptr[1]) == 'X') { /* Looks like Hex */
169 if((base == 0) || (base == 16)) {
170 nptr += 2; /* Skip the "0X" */
171 base = 16; /* In case base was 0 */
172 }
173 }
174 else { /* Looks like Octal */
175 if((base == 0) || (base == 8)) {
176 ++nptr; /* Skip the leading "0" */
177 base = 8; /* In case base was 0 */
178 }
179 }
180 }
181 if(base == 0) { /* If still zero then must be decimal */
182 base = 10;
183 }
184 if(*nptr == '0') {
185 for( ; *nptr == '0'; ++nptr); /* Skip any remaining leading zeros */
186 pEnd = nptr;
187 }
188
189 while( isalnum(*nptr) && ((temp = Digit2Val(*nptr)) < base)) {
190 Previous = Result;
191 Result = (Result * base) + (unsigned long)temp;
192 if( Result < Previous) { // If we overflowed
193 Result = UINT32_MAX;
194 //errno = -1;
195 break;
196 }
197 pEnd = ++nptr;
198 }
199
200 // Save pointer to final sequence
201 if(endptr != NULL) {
202 *endptr = (char *)pEnd;
203 }
204 return Result;
205}
oliviermartin1e57a462013-01-25 11:28:06 +0000206#endif /* _LIBFDT_ENV_H */