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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* Various utility functions and macros */
#ifndef __CROS_EC_UTIL_H
#define __CROS_EC_UTIL_H
#include "common.h"
#include "compile_time_macros.h"
#include "panic.h"
#include "builtin/assert.h" /* For ASSERT(). */
#include <stddef.h>
/* Standard macros / definitions */
#ifndef MAX
#define MAX(a, b) \
({ \
__typeof__(a) temp_a = (a); \
__typeof__(b) temp_b = (b); \
\
temp_a > temp_b ? temp_a : temp_b; \
})
#endif
#ifndef MIN
#define MIN(a, b) \
({ \
__typeof__(a) temp_a = (a); \
__typeof__(b) temp_b = (b); \
\
temp_a < temp_b ? temp_a : temp_b; \
})
#endif
#ifndef NULL
#define NULL ((void *)0)
#endif
/*
* Convert a pointer to a base struct into a pointer to the struct that
* contains the base struct. This requires knowing where in the contained
* struct the base struct resides, this is the member parameter to downcast.
*/
#define DOWNCAST(pointer, type, member) \
((type *)(((uint8_t *) pointer) - offsetof(type, member)))
/* True of x is a power of two */
#define POWER_OF_TWO(x) (x && !(x & (x - 1)))
/*
* macros for integer division with various rounding variants
* default integer division rounds down.
*/
#define DIV_ROUND_UP(x, y) (((x) + ((y) - 1)) / (y))
#define DIV_ROUND_NEAREST(x, y) (((x) + ((y) / 2)) / (y))
#ifndef HIDE_EC_STDLIB
/* Standard library functions */
__stdlib_compat int atoi(const char *nptr);
__stdlib_compat int isdigit(int c);
__stdlib_compat int isspace(int c);
__stdlib_compat int isalpha(int c);
__stdlib_compat int isprint(int c);
__stdlib_compat int memcmp(const void *s1, const void *s2, size_t len);
__stdlib_compat void *memcpy(void *dest, const void *src, size_t len);
__stdlib_compat __visible void *memset(void *dest, int c, size_t len);
__stdlib_compat void *memmove(void *dest, const void *src, size_t len);
__stdlib_compat void *memchr(const void *buffer, int c, size_t n);
__stdlib_compat int strcasecmp(const char *s1, const char *s2);
__stdlib_compat int strncasecmp(const char *s1, const char *s2, size_t size);
__stdlib_compat size_t strlen(const char *s);
__stdlib_compat size_t strnlen(const char *s, size_t maxlen);
__stdlib_compat char *strncpy(char *dest, const char *src, size_t n);
__stdlib_compat int strncmp(const char *s1, const char *s2, size_t n);
/* Like strtol(), but for integers. */
__stdlib_compat int strtoi(const char *nptr, char **endptr, int base);
__stdlib_compat uint64_t strtoul(const char *nptr, char **endptr, int base);
/* Like strncpy(), but guarantees null termination. */
__stdlib_compat char *strzcpy(char *dest, const char *src, int len);
/**
* Parses a boolean option from a string.
*
* Strings that set *dest=0 and return 1 (all case-insensitive):
* "off"
* "dis*"
* "n*"
* "f*"
*
* Strings that set *dest=1 and return 1 (all case-insensitive):
* "on"
* "ena*"
* "y*"
* "t*"
*
* Other strings return 0 and leave *dest unchanged.
*/
__stdlib_compat int parse_bool(const char *s, int *dest);
__stdlib_compat int tolower(int c);
#endif /* !HIDE_EC_STDLIB */
/**
* Constant time implementation of memcmp to avoid timing side channels.
*/
int safe_memcmp(const void *s1, const void *s2, size_t len);
/* 64-bit divide-and-modulo. Does the equivalent of:
*
* r = *n % d;
* *n /= d;
* return r;
*/
int uint64divmod(uint64_t *v, int by);
/**
* Get-and-clear next bit from mask.
*
* Starts with most significant bit.
*
* @param mask Bitmask to extract next bit from. Must NOT be zero.
* @return bit position (0..31)
*/
int get_next_bit(uint32_t *mask);
/**
* Reverse's the byte-order of the provided buffer.
*/
void reverse(void *dest, size_t len);
/****************************************************************************/
/* Conditional stuff.
*
* We often need to watch for transitions between one state and another, so
* that we can issue warnings or take action ONCE. This abstracts that "have I
* already reacted to this" stuff into a single set of functions.
*
* For example:
*
* cond_t c;
*
* cond_init_false(&c);
*
* while(1) {
* int val = read_some_gpio();
* cond_set(&c, val);
*
* if (cond_went_true(&c))
* host_event(SOMETHING_HAPPENED);
* }
*
*/
typedef uint8_t cond_t;
/* Initialize a conditional to a specific state. Do this first. */
void cond_init(cond_t *c, int boolean);
static inline void cond_init_false(cond_t *c) { cond_init(c, 0); }
static inline void cond_init_true(cond_t *c) { cond_init(c, 1); }
/* Set the current state. Do this as often as you like. */
void cond_set(cond_t *c, int boolean);
static inline void cond_set_false(cond_t *c) { cond_set(c, 0); }
static inline void cond_set_true(cond_t *c) { cond_set(c, 1); }
/* Get the current state. Do this as often as you like. */
int cond_is(cond_t *c, int boolean);
static inline int cond_is_false(cond_t *c) { return cond_is(c, 0); }
static inline int cond_is_true(cond_t *c) { return cond_is(c, 1); }
/* See if the state has transitioned. If it has, the corresponding function
* will return true ONCE only, until it's changed back.
*/
int cond_went(cond_t *c, int boolean);
static inline int cond_went_false(cond_t *c) { return cond_went(c, 0); }
static inline int cond_went_true(cond_t *c) { return cond_went(c, 1); }
/****************************************************************************/
/* Console command parsing */
/* Parse command-line arguments given integer shift value to obtain
* offset and size.
*/
int parse_offset_size(int argc, char **argv, int shift,
int *offset, int *size);
#ifdef CONFIG_ASSEMBLY_MULA32
/*
* Compute (a*b)+c[+d], where a, b, c[, d] are 32-bit integers, and the result
* is 64-bit long.
*/
uint64_t mula32(uint32_t a, uint32_t b, uint32_t c);
uint64_t mulaa32(uint32_t a, uint32_t b, uint32_t c, uint32_t d);
#else
static inline uint64_t mula32(uint32_t a, uint32_t b, uint32_t c)
{
uint64_t ret = a;
ret *= b;
ret += c;
return ret;
}
static inline uint64_t mulaa32(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
{
uint64_t ret = a;
ret *= b;
ret += c;
ret += d;
return ret;
}
#endif
#endif /* __CROS_EC_UTIL_H */
|