blob: a45ff00abcc3eb5acb779b678f8c28998b8c3bf4 [file] [log] [blame]
David Gibsonede25de2006-12-01 15:02:10 +11001#ifndef _LIBFDT_ENV_H
2#define _LIBFDT_ENV_H
3
David Gibson3da0f9a2006-11-27 16:21:28 +11004#include <stddef.h>
5#include <stdint.h>
6#include <string.h>
Mayank Groverf3b78182017-12-19 13:31:30 +05307#include <km_main.h>
8
9typedef uint32_t fdt32_t;
David Gibson3da0f9a2006-11-27 16:21:28 +110010
Bert Kenward37b167f2012-04-10 08:00:15 -070011#define EXTRACT_BYTE(n) ((unsigned long long)((uint8_t *)&x)[n])
Anton Staaf2cd4c8d2011-10-11 10:22:27 -070012static inline uint16_t fdt16_to_cpu(uint16_t x)
13{
Bert Kenward37b167f2012-04-10 08:00:15 -070014 return (EXTRACT_BYTE(0) << 8) | EXTRACT_BYTE(1);
Anton Staaf2cd4c8d2011-10-11 10:22:27 -070015}
16#define cpu_to_fdt16(x) fdt16_to_cpu(x)
17
David Gibsoncdcb4152008-06-26 11:03:49 +100018static inline uint32_t fdt32_to_cpu(uint32_t x)
19{
Bert Kenward37b167f2012-04-10 08:00:15 -070020 return (EXTRACT_BYTE(0) << 24) | (EXTRACT_BYTE(1) << 16) | (EXTRACT_BYTE(2) << 8) | EXTRACT_BYTE(3);
David Gibsoncdcb4152008-06-26 11:03:49 +100021}
22#define cpu_to_fdt32(x) fdt32_to_cpu(x)
23
24static inline uint64_t fdt64_to_cpu(uint64_t x)
25{
Bert Kenward37b167f2012-04-10 08:00:15 -070026 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 Gibsoncdcb4152008-06-26 11:03:49 +100028}
29#define cpu_to_fdt64(x) fdt64_to_cpu(x)
Bert Kenward37b167f2012-04-10 08:00:15 -070030#undef EXTRACT_BYTE
David Gibson3da0f9a2006-11-27 16:21:28 +110031
Mayank Groverf3b78182017-12-19 13:31:30 +053032#define toupper(a) ((((a) >= 'a') && ((a) <= 'z')) ? ((a) - 'a' + 'A') : (a))
33#define isalpha(chr) (('a' <= chr && chr <= 'z') || ('A' <= chr && chr <= 'Z'))
34
35static 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
43static 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
52static 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**/
70static inline unsigned long
71strtoul(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 Gibsonede25de2006-12-01 15:02:10 +1100134#endif /* _LIBFDT_ENV_H */