oliviermartin | 1e57a46 | 2013-01-25 11:28:06 +0000 | [diff] [blame] | 1 | /** @file
|
| 2 | *
|
Olivier Martin | 3e8576d | 2014-02-12 15:11:29 +0000 | [diff] [blame] | 3 | * Copyright (c) 2011-2014, ARM Limited. All rights reserved.
|
oliviermartin | 1e57a46 | 2013-01-25 11:28:06 +0000 | [diff] [blame] | 4 | *
|
Ronald Cron | 3402aac | 2014-08-19 13:29:52 +0000 | [diff] [blame] | 5 | * 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.
|
oliviermartin | 1e57a46 | 2013-01-25 11:28:06 +0000 | [diff] [blame] | 12 | *
|
| 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 Pendoti | 19aa5bd | 2017-03-15 19:27:50 +0530 | [diff] [blame] | 20 | #include <Library/MemoryAllocationLib.h>
|
| 21 | #include <Uefi.h>
|
oliviermartin | 1e57a46 | 2013-01-25 11:28:06 +0000 | [diff] [blame] | 22 |
|
Olivier Martin | 3e8576d | 2014-02-12 15:11:29 +0000 | [diff] [blame] | 23 | typedef UINT16 fdt16_t;
|
| 24 | typedef UINT32 fdt32_t;
|
| 25 | typedef UINT64 fdt64_t;
|
| 26 |
|
oliviermartin | 1e57a46 | 2013-01-25 11:28:06 +0000 | [diff] [blame] | 27 | typedef UINT8 uint8_t;
|
| 28 | typedef UINT16 uint16_t;
|
| 29 | typedef UINT32 uint32_t;
|
| 30 | typedef UINT64 uint64_t;
|
| 31 | typedef UINTN uintptr_t;
|
| 32 | typedef UINTN size_t;
|
Vijay Kumar Pendoti | 19aa5bd | 2017-03-15 19:27:50 +0530 | [diff] [blame] | 33 | typedef BOOLEAN bool;
|
oliviermartin | 1e57a46 | 2013-01-25 11:28:06 +0000 | [diff] [blame] | 34 |
|
Olivier Martin | 3e8576d | 2014-02-12 15:11:29 +0000 | [diff] [blame] | 35 | static inline uint16_t fdt16_to_cpu(fdt16_t x)
|
oliviermartin | 1e57a46 | 2013-01-25 11:28:06 +0000 | [diff] [blame] | 36 | {
|
| 37 | return SwapBytes16 (x);
|
| 38 | }
|
| 39 | #define cpu_to_fdt16(x) fdt16_to_cpu(x)
|
| 40 |
|
Olivier Martin | 3e8576d | 2014-02-12 15:11:29 +0000 | [diff] [blame] | 41 | static inline uint32_t fdt32_to_cpu(fdt32_t x)
|
oliviermartin | 1e57a46 | 2013-01-25 11:28:06 +0000 | [diff] [blame] | 42 | {
|
| 43 | return SwapBytes32 (x);
|
| 44 | }
|
| 45 | #define cpu_to_fdt32(x) fdt32_to_cpu(x)
|
| 46 |
|
Olivier Martin | 3e8576d | 2014-02-12 15:11:29 +0000 | [diff] [blame] | 47 | static inline uint64_t fdt64_to_cpu(fdt64_t x)
|
oliviermartin | 1e57a46 | 2013-01-25 11:28:06 +0000 | [diff] [blame] | 48 | {
|
| 49 | return SwapBytes64 (x);
|
| 50 | }
|
| 51 | #define cpu_to_fdt64(x) fdt64_to_cpu(x)
|
| 52 |
|
| 53 | static inline void* memcpy(void* dest, const void* src, size_t len) {
|
| 54 | return CopyMem (dest, src, len);
|
| 55 | }
|
| 56 |
|
| 57 | static inline void *memmove(void *dest, const void *src, size_t n) {
|
| 58 | return CopyMem (dest, src, n);
|
| 59 | }
|
| 60 |
|
| 61 | static inline void *memset(void *s, int c, size_t n) {
|
| 62 | return SetMem (s, n, c);
|
| 63 | }
|
| 64 |
|
| 65 | static inline int memcmp(const void* dest, const void* src, int len) {
|
| 66 | return CompareMem (dest, src, len);
|
| 67 | }
|
| 68 |
|
| 69 | static inline void *memchr(const void *s, int c, size_t n) {
|
| 70 | return ScanMem8 (s, n, c);
|
| 71 | }
|
| 72 |
|
| 73 | static inline size_t strlen (const char* str) {
|
| 74 | return AsciiStrLen (str);
|
| 75 | }
|
| 76 |
|
| 77 | static 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 Pendoti | 19aa5bd | 2017-03-15 19:27:50 +0530 | [diff] [blame] | 84 | static inline int strcmp(const char *s1, const char *s2)
|
| 85 | {
|
| 86 | return (int)AsciiStrCmp( s1, s2);
|
| 87 | }
|
| 88 |
|
| 89 | static 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'))
|
| 101 | STATIC
|
| 102 | BOOLEAN
|
| 103 | isalnum (
|
| 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 |
|
| 113 | static int
|
| 114 | Digit2Val( 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 */
|
| 123 | static 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 |
|
| 143 | static inline unsigned long
|
| 144 | strtoul(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 | }
|
oliviermartin | 1e57a46 | 2013-01-25 11:28:06 +0000 | [diff] [blame] | 206 | #endif /* _LIBFDT_ENV_H */
|