libpayload: Add gettimeofday() and friends
[coreboot.git] / payloads / libpayload / include / libpayload.h
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #ifndef _LIBPAYLOAD_H
31 #define _LIBPAYLOAD_H
32
33 #include <autoconf.h>
34 #include <stddef.h>
35 #include <arch/types.h>
36 #include <arch/io.h>
37 #include <sysinfo.h>
38 #include <stdarg.h>
39
40 #define MIN(a,b) ((a) < (b) ? (a) : (b))
41 #define MAX(a,b) ((a) > (b) ? (a) : (b))
42 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
43
44 #define LITTLE_ENDIAN   1234
45 #define BIG_ENDIAN      4321
46 #ifdef CONFIG_TARGET_I386
47 #define BYTE_ORDER      LITTLE_ENDIAN
48 #else
49 #define BYTE_ORDER      BIG_ENDIAN
50 #endif
51
52 #define EXIT_SUCCESS 0
53 #define EXIT_FAILURE 1
54
55 #define RAND_MAX 0x7fffffff
56
57 /* Some NVRAM byte definitions */
58 #define NVRAM_RTC_SECONDS        0
59 #define NVRAM_RTC_MINUTES        2
60 #define NVRAM_RTC_HOURS          4
61 #define NVRAM_RTC_DAY            7
62 #define NVRAM_RTC_MONTH          8
63 #define NVRAM_RTC_YEAR           9
64 #define NVRAM_RTC_FREQ_SELECT    10
65 #define  NVRAM_RTC_UIP           0x80
66
67 struct tm {
68         int tm_sec;
69         int tm_min;
70         int tm_hour;
71         int tm_mday;
72         int tm_mon;
73         int tm_year;
74         int tm_wday;
75         int tm_yday;
76         int tm_isdst;
77 };
78
79 /* drivers/nvram.c */
80 u8 nvram_read(u8 addr);
81 void nvram_write(u8 val, u8 addr);
82 int nvram_updating(void);
83 void rtc_read_clock(struct tm *tm);
84
85 /* drivers/keyboard.c */
86 void keyboard_init(void);
87 int keyboard_havechar(void);
88 unsigned char keyboard_get_scancode(void);
89 int keyboard_getchar(void);
90
91 /* drivers/serial.c */
92 void serial_init(void);
93 void serial_putchar(unsigned char c);
94 int serial_havechar(void);
95 int serial_getchar(void);
96
97 /* drivers/speaker.c */
98 void speaker_enable(u16 freq);
99 void speaker_disable(void);
100 void speaker_tone(u16 freq, unsigned int duration);
101
102 /* video/video.c */
103 int video_console_init(void);
104 void video_console_putchar(unsigned int ch);
105 void video_console_putc(u8 row, u8 col, unsigned int ch);
106 void video_console_clear(void);
107 void video_console_cursor_enable(int state);
108
109 /* libc/console.c */
110 void console_init(void);
111 int putchar(int c);
112 int puts(const char *s);
113 int havekey(void);
114 int getchar(void);
115 int getchar_timeout(int *ms);
116
117 extern int last_putchar;
118
119 #define havechar havekey
120
121 /* libc/ctype.c */
122 int isalnum(int c);
123 int isalpha(int c);
124 int isascii(int c);
125 int isblank(int c);
126 int iscntrl(int c);
127 int isdigit(int c);
128 int isgraph(int c);
129 int islower(int c);
130 int isprint(int c);
131 int ispunct(int c);
132 int isspace(int c);
133 int isupper(int c);
134 int isxdigit(int c);
135 int tolower(int c);
136 int toupper(int c);
137
138 /* libc/ipchecksum.c */
139 unsigned short ipchksum(const unsigned short *ptr, unsigned long nbytes);
140
141 /* libc/malloc.c */
142 void free(void *ptr);
143 void *malloc(size_t size);
144 void *calloc(size_t nmemb, size_t size);
145 void *realloc(void *ptr, size_t size);
146
147 /* libc/lib.c */
148 int bcd2dec(int b);
149 int dec2bcd(int d);
150 int abs(int j);
151 long int labs(long int j);
152 long long int llabs(long long int j);
153 u8 bin2hex(u8 b);
154 u8 hex2bin(u8 h);
155
156 /* libc/memory.c */
157 void *memset(void *s, int c, size_t n);
158 void *memcpy(void *dst, const void *src, size_t n);
159 void *memmove(void *dst, const void *src, size_t n);
160 int memcmp(const char *s1, const char *s2, size_t len);
161
162 /* libc/printf.c */
163 int snprintf(char *str, size_t size, const char *fmt, ...);
164 int sprintf(char *str, const char *fmt, ...);
165 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
166 int vsprintf(char *str, const char *fmt, va_list ap);
167 int printf(const char *fmt, ...);
168 int vprintf(const char *fmt, va_list ap);
169
170 /* libc/rand.c */
171 int rand_r(unsigned int *seed);
172 int rand(void);
173 void srand(unsigned int seed);
174
175 /* libc/sha1.c */
176 #define SHA1_BLOCK_LENGTH       64
177 #define SHA1_DIGEST_LENGTH      20
178 typedef struct {
179         u32 state[5];
180         u64 count;
181         u8 buffer[SHA1_BLOCK_LENGTH];
182 } SHA1_CTX;
183 void SHA1Init(SHA1_CTX *context);
184 void SHA1Transform(u32 state[5], const u8 buffer[SHA1_BLOCK_LENGTH]);
185 void SHA1Update(SHA1_CTX *context, const u8 *data, size_t len);
186 void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
187 u8 *sha1(const u8 *data, size_t len, u8 *buf);
188
189 /* libc/string.c */
190 size_t strnlen(const char *str, size_t maxlen);
191 size_t strlen(const char *str);
192 int strcmp(const char *s1, const char *s2);
193 int strncmp(const char *s1, const char *s2, int maxlen);
194 char *strncpy(char *d, const char *s, int n);
195 char *strcpy(char *d, const char *s);
196 char *strncat(char *d, const char *s, int n);
197 char *strchr(const char *s, int c);
198 char *strdup(const char *s);
199 char *strstr(const char *h, const char *n);
200
201 /* libc/time.c */
202
203 struct timeval {
204         time_t tv_sec;
205         suseconds_t tv_usec;
206 };
207
208 int gettimeofday(struct timeval *tv, void *tz);
209
210 /* i386/coreboot.c */
211 int get_coreboot_info(struct sysinfo_t *info);
212
213 /* i386/sysinfo.c */
214 void lib_get_sysinfo(void);
215
216 /* i386/timer.c */
217 /* Timer functions - defined by each architecture. */
218 unsigned int get_cpu_speed(void);
219 void ndelay(unsigned int n);
220 void udelay(unsigned int n);
221 void mdelay(unsigned int n);
222 void delay(unsigned int n);
223
224 /* i386/util.S */
225 #define abort() halt()
226 void halt(void) __attribute__ ((noreturn));
227
228 #endif