Convert BIN2HEX/HEX2BIN to functions and add the abs() family
[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 RAND_MAX 0x7fffffff
53
54 /* Some NVRAM byte definitions */
55 #define NVRAM_RTC_SECONDS        0
56 #define NVRAM_RTC_MINUTES        2
57 #define NVRAM_RTC_HOURS          4
58 #define NVRAM_RTC_DAY            7
59 #define NVRAM_RTC_MONTH          8
60 #define NVRAM_RTC_YEAR           9
61
62 /* drivers/nvram.c */
63 u8 nvram_read(u8 addr);
64 void nvram_write(u8 val, u8 addr);
65
66 /* drivers/keyboard.c */
67 int keyboard_havechar(void);
68 unsigned char keyboard_get_scancode(void);
69 int keyboard_getchar(void);
70
71 /* drivers/serial.c */
72 void serial_init(void);
73 void serial_putchar(unsigned char c);
74 int serial_havechar(void);
75 int serial_getchar(void);
76
77 /* drivers/speaker.c */
78 void speaker_enable(u16 freq);
79 void speaker_disable(void);
80 void speaker_tone(u16 freq, unsigned int duration);
81
82 /* video/video.c */
83 int video_console_init(void);
84 void video_console_putchar(unsigned int ch);
85 void video_console_putc(u8 row, u8 col, unsigned int ch);
86 void video_console_clear(void);
87 void video_console_cursor_enable(int state);
88
89 /* libc/console.c */
90 void console_init(void);
91 int putchar(int c);
92 int puts(const char *s);
93 int havekey(void);
94 int getchar(void);
95 int getchar_timeout(int *ms);
96
97 extern int last_putchar;
98
99 #define havechar havekey
100
101 /* libc/ctype.c */
102 int isspace(int c);
103 int isdigit(int c);
104 int tolower(int c);
105
106 /* libc/ipchecksum.c */
107 unsigned short ipchksum(const unsigned short *ptr, unsigned long nbytes);
108
109 /* libc/malloc.c */
110 void free(void *ptr);
111 void *malloc(size_t size);
112 void *calloc(size_t nmemb, size_t size);
113 void *realloc(void *ptr, size_t size);
114
115 /* libc/lib.c */
116 int bcd2dec(int b);
117 int dec2bcd(int d);
118 int abs(int j);
119 long int labs(long int j);
120 long long int llabs(long long int j);
121 u8 bin2hex(u8 b);
122 u8 hex2bin(u8 h);
123
124 /* libc/memory.c */
125 void *memset(void *s, int c, size_t n);
126 void *memcpy(void *dst, const void *src, size_t n);
127 void *memmove(void *dst, const void *src, size_t n);
128 int memcmp(const char *s1, const char *s2, size_t len);
129
130 /* libc/printf.c */
131 int snprintf(char *str, size_t size, const char *fmt, ...);
132 int sprintf(char *str, const char *fmt, ...);
133 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
134 int vsprintf(char *str, const char *fmt, va_list ap);
135 int printf(const char *fmt, ...);
136 int vprintf(const char *fmt, va_list ap);
137
138 /* libc/rand.c */
139 int rand_r(unsigned int *seed);
140 int rand(void);
141 void srand(unsigned int seed);
142
143 /* libc/sha1.c */
144 #define SHA1_BLOCK_LENGTH       64
145 #define SHA1_DIGEST_LENGTH      20
146 typedef struct {
147         u32 state[5];
148         u64 count;
149         u8 buffer[SHA1_BLOCK_LENGTH];
150 } SHA1_CTX;
151 void SHA1Init(SHA1_CTX *context);
152 void SHA1Transform(u32 state[5], const u8 buffer[SHA1_BLOCK_LENGTH]);
153 void SHA1Update(SHA1_CTX *context, const u8 *data, size_t len);
154 void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
155 u8 *sha1(const u8 *data, size_t len, u8 *buf);
156
157 /* libc/string.c */
158 size_t strnlen(const char *str, size_t maxlen);
159 size_t strlen(const char *str);
160 int strcmp(const char *s1, const char *s2);
161 int strncmp(const char *s1, const char *s2, int maxlen);
162 char *strncpy(char *d, const char *s, int n);
163 char *strcpy(char *d, const char *s);
164 char *strncat(char *d, const char *s, int n);
165 char *strchr(const char *s, int c);
166 char *strdup(const char *s);
167 char *strstr(const char *h, const char *n);
168
169 /* i386/coreboot.c */
170 int get_coreboot_info(struct sysinfo_t *info);
171
172 /* i386/sysinfo.c */
173 void lib_get_sysinfo(void);
174
175 /* i386/timer.c */
176 /* Timer functions - defined by each architecture. */
177 unsigned int get_cpu_speed(void);
178 void ndelay(unsigned int n);
179 void udelay(unsigned int n);
180 void mdelay(unsigned int n);
181 void delay(unsigned int n);
182
183 /* i386/util.S */
184 #define abort() halt()
185 void halt(void) __attribute__ ((noreturn));
186
187 #endif