David Gibson | ede25de | 2006-12-01 15:02:10 +1100 | [diff] [blame] | 1 | #ifndef _LIBFDT_ENV_H |
| 2 | #define _LIBFDT_ENV_H |
| 3 | |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 4 | #include <stddef.h> |
| 5 | #include <stdint.h> |
| 6 | #include <string.h> |
Mayank Grover | f3b7818 | 2017-12-19 13:31:30 +0530 | [diff] [blame] | 7 | #include <km_main.h> |
| 8 | |
| 9 | typedef uint32_t fdt32_t; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 10 | |
Bert Kenward | 37b167f | 2012-04-10 08:00:15 -0700 | [diff] [blame] | 11 | #define EXTRACT_BYTE(n) ((unsigned long long)((uint8_t *)&x)[n]) |
Anton Staaf | 2cd4c8d | 2011-10-11 10:22:27 -0700 | [diff] [blame] | 12 | static inline uint16_t fdt16_to_cpu(uint16_t x) |
| 13 | { |
Bert Kenward | 37b167f | 2012-04-10 08:00:15 -0700 | [diff] [blame] | 14 | return (EXTRACT_BYTE(0) << 8) | EXTRACT_BYTE(1); |
Anton Staaf | 2cd4c8d | 2011-10-11 10:22:27 -0700 | [diff] [blame] | 15 | } |
| 16 | #define cpu_to_fdt16(x) fdt16_to_cpu(x) |
| 17 | |
David Gibson | cdcb415 | 2008-06-26 11:03:49 +1000 | [diff] [blame] | 18 | static inline uint32_t fdt32_to_cpu(uint32_t x) |
| 19 | { |
Bert Kenward | 37b167f | 2012-04-10 08:00:15 -0700 | [diff] [blame] | 20 | return (EXTRACT_BYTE(0) << 24) | (EXTRACT_BYTE(1) << 16) | (EXTRACT_BYTE(2) << 8) | EXTRACT_BYTE(3); |
David Gibson | cdcb415 | 2008-06-26 11:03:49 +1000 | [diff] [blame] | 21 | } |
| 22 | #define cpu_to_fdt32(x) fdt32_to_cpu(x) |
| 23 | |
| 24 | static inline uint64_t fdt64_to_cpu(uint64_t x) |
| 25 | { |
Bert Kenward | 37b167f | 2012-04-10 08:00:15 -0700 | [diff] [blame] | 26 | return (EXTRACT_BYTE(0) << 56) | (EXTRACT_BYTE(1) << 48) | (EXTRACT_BYTE(2) << 40) | (EXTRACT_BYTE(3) << 32) |
| 27 | | (EXTRACT_BYTE(4) << 24) | (EXTRACT_BYTE(5) << 16) | (EXTRACT_BYTE(6) << 8) | EXTRACT_BYTE(7); |
David Gibson | cdcb415 | 2008-06-26 11:03:49 +1000 | [diff] [blame] | 28 | } |
| 29 | #define cpu_to_fdt64(x) fdt64_to_cpu(x) |
Bert Kenward | 37b167f | 2012-04-10 08:00:15 -0700 | [diff] [blame] | 30 | #undef EXTRACT_BYTE |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 31 | |
Mayank Grover | f3b7818 | 2017-12-19 13:31:30 +0530 | [diff] [blame] | 32 | #define toupper(a) ((((a) >= 'a') && ((a) <= 'z')) ? ((a) - 'a' + 'A') : (a)) |
| 33 | #define isalpha(chr) (('a' <= chr && chr <= 'z') || ('A' <= chr && chr <= 'Z')) |
| 34 | |
| 35 | static bool isalnum(unsigned char Chr) |
| 36 | { |
| 37 | return (('0' <= Chr && Chr <= '9') || |
| 38 | ('A' <= Chr && Chr <= 'Z') || |
| 39 | ('a' <= Chr && Chr <= 'z') |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | static int Digit2Val( int c) |
| 44 | { |
| 45 | if(isalpha(c)) { /* If c is one of [A-Za-z]... */ |
| 46 | c = toupper(c) - 7; // Adjust so 'A' is ('9' + 1) |
| 47 | } |
| 48 | return c - '0'; // Value returned is between 0 and 35, inclusive. |
| 49 | } |
| 50 | |
| 51 | |
| 52 | static inline int isspace (int c) |
| 53 | { |
| 54 | // |
| 55 | // <space> ::= [ ] |
| 56 | // |
| 57 | return ((c) == ' '); |
| 58 | } |
| 59 | |
| 60 | /** The strtoul function converts the initial portion of the string pointed to |
| 61 | by nptr to unsigned long int representation. |
| 62 | |
| 63 | See the description for strtol for more information. |
| 64 | |
| 65 | @return The strtoul function returns the converted value, if any. If no |
| 66 | conversion could be performed, zero is returned. If the correct |
| 67 | value is outside the range of representable values, ULONG_MAX is |
| 68 | returned and the value of the macro ERANGE is stored in errno. |
| 69 | **/ |
| 70 | static inline unsigned long |
| 71 | strtoul(const char * __restrict nptr, char ** __restrict endptr, int base) |
| 72 | { |
| 73 | const char *pEnd; |
| 74 | unsigned long Result = 0; |
| 75 | unsigned long Previous; |
| 76 | int temp; |
| 77 | |
| 78 | pEnd = nptr; |
| 79 | |
| 80 | if((base < 0) || (base == 1) || (base > 36)) { |
| 81 | if(endptr != NULL) { |
| 82 | *endptr = NULL; |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | // Skip leading spaces. |
| 87 | while(isspace(*nptr)) ++nptr; |
| 88 | |
| 89 | // Process Subject sequence: optional + sign followed by digits. |
| 90 | if(*nptr == '+') { |
| 91 | ++nptr; |
| 92 | } |
| 93 | |
| 94 | if(*nptr == '0') { /* Might be Octal or Hex */ |
| 95 | if(toupper(nptr[1]) == 'X') { /* Looks like Hex */ |
| 96 | if((base == 0) || (base == 16)) { |
| 97 | nptr += 2; /* Skip the "0X" */ |
| 98 | base = 16; /* In case base was 0 */ |
| 99 | } |
| 100 | } |
| 101 | else { /* Looks like Octal */ |
| 102 | if((base == 0) || (base == 8)) { |
| 103 | ++nptr; /* Skip the leading "0" */ |
| 104 | base = 8; /* In case base was 0 */ |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | if(base == 0) { /* If still zero then must be decimal */ |
| 109 | base = 10; |
| 110 | |
| 111 | } |
| 112 | if(*nptr == '0') { |
| 113 | for( ; *nptr == '0'; ++nptr); /* Skip any remaining leading zeros */ |
| 114 | pEnd = nptr; |
| 115 | } |
| 116 | |
| 117 | while( isalnum(*nptr) && ((temp = Digit2Val(*nptr)) < base)) { |
| 118 | Previous = Result; |
| 119 | Result = (Result * base) + (unsigned long)temp; |
| 120 | if( Result < Previous) { // If we overflowed |
| 121 | Result = UINT32_MAX; |
| 122 | //errno = -1; |
| 123 | break; |
| 124 | } |
| 125 | pEnd = ++nptr; |
| 126 | } |
| 127 | // Save pointer to final sequence |
| 128 | if(endptr != NULL) { |
| 129 | *endptr = (char *)pEnd; |
| 130 | } |
| 131 | return Result; |
| 132 | } |
| 133 | |
David Gibson | ede25de | 2006-12-01 15:02:10 +1100 | [diff] [blame] | 134 | #endif /* _LIBFDT_ENV_H */ |