libpayload: Add a function to verify the checksum on a LAR file
[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 #include <lar.h>
40
41 #define MIN(a,b) ((a) < (b) ? (a) : (b))
42 #define MAX(a,b) ((a) > (b) ? (a) : (b))
43 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
44
45 #define LITTLE_ENDIAN   1234
46 #define BIG_ENDIAN      4321
47 #ifdef CONFIG_TARGET_I386
48 #define BYTE_ORDER      LITTLE_ENDIAN
49 #else
50 #define BYTE_ORDER      BIG_ENDIAN
51 #endif
52
53 #define EXIT_SUCCESS 0
54 #define EXIT_FAILURE 1
55
56 #define RAND_MAX 0x7fffffff
57
58 /* Some NVRAM byte definitions */
59 #define NVRAM_RTC_SECONDS        0
60 #define NVRAM_RTC_MINUTES        2
61 #define NVRAM_RTC_HOURS          4
62 #define NVRAM_RTC_DAY            7
63 #define NVRAM_RTC_MONTH          8
64 #define NVRAM_RTC_YEAR           9
65 #define NVRAM_RTC_FREQ_SELECT    10
66 #define  NVRAM_RTC_UIP           0x80
67
68 struct tm {
69         int tm_sec;
70         int tm_min;
71         int tm_hour;
72         int tm_mday;
73         int tm_mon;
74         int tm_year;
75         int tm_wday;
76         int tm_yday;
77         int tm_isdst;
78 };
79
80 /* drivers/nvram.c */
81 u8 nvram_read(u8 addr);
82 void nvram_write(u8 val, u8 addr);
83 int nvram_updating(void);
84 void rtc_read_clock(struct tm *tm);
85
86 /* drivers/keyboard.c */
87 void keyboard_init(void);
88 int keyboard_havechar(void);
89 unsigned char keyboard_get_scancode(void);
90 int keyboard_getchar(void);
91
92 /* drivers/serial.c */
93 void serial_init(void);
94 void serial_putchar(unsigned char c);
95 int serial_havechar(void);
96 int serial_getchar(void);
97
98 /* drivers/speaker.c */
99 void speaker_enable(u16 freq);
100 void speaker_disable(void);
101 void speaker_tone(u16 freq, unsigned int duration);
102
103 /* video/video.c */
104 int video_console_init(void);
105 void video_console_putchar(unsigned int ch);
106 void video_console_putc(u8 row, u8 col, unsigned int ch);
107 void video_console_clear(void);
108 void video_console_cursor_enable(int state);
109
110 /* libc/console.c */
111 void console_init(void);
112 int putchar(int c);
113 int puts(const char *s);
114 int havekey(void);
115 int getchar(void);
116 int getchar_timeout(int *ms);
117
118 extern int last_putchar;
119
120 #define havechar havekey
121
122 /* libc/ctype.c */
123 int isalnum(int c);
124 int isalpha(int c);
125 int isascii(int c);
126 int isblank(int c);
127 int iscntrl(int c);
128 int isdigit(int c);
129 int isgraph(int c);
130 int islower(int c);
131 int isprint(int c);
132 int ispunct(int c);
133 int isspace(int c);
134 int isupper(int c);
135 int isxdigit(int c);
136 int tolower(int c);
137 int toupper(int c);
138
139 /* libc/ipchecksum.c */
140 unsigned short ipchksum(const unsigned short *ptr, unsigned long nbytes);
141
142 /* libc/malloc.c */
143 void free(void *ptr);
144 void *malloc(size_t size);
145 void *calloc(size_t nmemb, size_t size);
146 void *realloc(void *ptr, size_t size);
147
148 /* libc/exec.c */
149 int exec(long addr, int argc, char **argv);
150
151 /* libc/lib.c */
152 int bcd2dec(int b);
153 int dec2bcd(int d);
154 int abs(int j);
155 long int labs(long int j);
156 long long int llabs(long long int j);
157 u8 bin2hex(u8 b);
158 u8 hex2bin(u8 h);
159
160 /* libc/memory.c */
161 void *memset(void *s, int c, size_t n);
162 void *memcpy(void *dst, const void *src, size_t n);
163 void *memmove(void *dst, const void *src, size_t n);
164 int memcmp(const char *s1, const char *s2, size_t len);
165
166 /* libc/printf.c */
167 int snprintf(char *str, size_t size, const char *fmt, ...);
168 int sprintf(char *str, const char *fmt, ...);
169 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
170 int vsprintf(char *str, const char *fmt, va_list ap);
171 int printf(const char *fmt, ...);
172 int vprintf(const char *fmt, va_list ap);
173
174 /* libc/rand.c */
175 int rand_r(unsigned int *seed);
176 int rand(void);
177 void srand(unsigned int seed);
178
179 /* libc/sha1.c */
180 #define SHA1_BLOCK_LENGTH       64
181 #define SHA1_DIGEST_LENGTH      20
182 typedef struct {
183         u32 state[5];
184         u64 count;
185         u8 buffer[SHA1_BLOCK_LENGTH];
186 } SHA1_CTX;
187 void SHA1Init(SHA1_CTX *context);
188 void SHA1Transform(u32 state[5], const u8 buffer[SHA1_BLOCK_LENGTH]);
189 void SHA1Update(SHA1_CTX *context, const u8 *data, size_t len);
190 void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
191 u8 *sha1(const u8 *data, size_t len, u8 *buf);
192
193 /* libc/string.c */
194 size_t strnlen(const char *str, size_t maxlen);
195 size_t strlen(const char *str);
196 int strcmp(const char *s1, const char *s2);
197 int strncmp(const char *s1, const char *s2, int maxlen);
198 char *strncpy(char *d, const char *s, int n);
199 char *strcpy(char *d, const char *s);
200 char *strncat(char *d, const char *s, int n);
201 char *strchr(const char *s, int c);
202 char *strdup(const char *s);
203 char *strstr(const char *h, const char *n);
204
205 /* libc/time.c */
206
207 struct timeval {
208         time_t tv_sec;
209         suseconds_t tv_usec;
210 };
211
212 int gettimeofday(struct timeval *tv, void *tz);
213
214 /* libc/lar.c */
215
216 struct LAR {
217         void * start;
218         int cindex;
219         int count;
220         int alloc;
221         int eof;
222         void **headers;
223 };
224
225 struct larent {
226         u8 name[LAR_MAX_PATHLEN];
227 };
228
229 struct larstat {
230         u32 len;
231         u32 reallen;
232         u32 checksum;
233         u32 compchecksum;
234         u32 offset;
235         u32 compression;
236         u64 entry;
237         u64 loadaddress;
238 };
239
240 struct LFILE {
241         struct LAR *lar;
242         struct lar_header *header;
243         u32 size;
244         void *start;
245         u32 offset;
246 };
247
248 struct LAR *openlar(void *addr);
249 int closelar(struct LAR *lar);
250 struct larent *readlar(struct LAR *lar);
251 void rewindlar(struct LAR *lar);
252 int larstat(struct LAR *lar, const char *path, struct larstat *buf);
253 void *larfptr(struct LAR *lar, const char *filename);
254 int lfverify(struct LAR *lar, const char *filename);
255 struct LFILE * lfopen(struct LAR *lar, const char *filename);
256 int lfread(void *ptr, size_t size, size_t nmemb, struct LFILE *stream);
257
258 #define SEEK_SET 0
259 #define SEEK_CUR 1
260 #define SEEK_END 2
261
262 int lfseek(struct LFILE *stream, long offset, int whence);
263 int lfclose(struct LFILE *file);
264
265 /* i386/coreboot.c */
266 int get_coreboot_info(struct sysinfo_t *info);
267
268 /* i386/sysinfo.c */
269 void lib_get_sysinfo(void);
270
271 /* i386/timer.c */
272 /* Timer functions - defined by each architecture. */
273 unsigned int get_cpu_speed(void);
274 void ndelay(unsigned int n);
275 void udelay(unsigned int n);
276 void mdelay(unsigned int n);
277 void delay(unsigned int n);
278
279 /* i386/util.S */
280 #define abort() halt()
281 void halt(void) __attribute__ ((noreturn));
282
283 #endif