* Add strsep (since strtok is considered obsolete)
[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 /**
31  * @mainpage
32  *
33  * @section intro Introduction
34  * libpayload is a small BSD-licensed static library (a lightweight
35  * implementation of common and useful functions) intended to be used
36  * as a basis for coreboot payloads.
37  *
38  * @section example Example
39  * Here is an example of a very simple payload:
40  * @include sample/hello.c
41  */
42
43 #ifndef _LIBPAYLOAD_H
44 #define _LIBPAYLOAD_H
45
46 #include <stddef.h>
47 #include <arch/types.h>
48 #include <arch/io.h>
49 #include <arch/virtual.h>
50 #include <sysinfo.h>
51 #include <stdarg.h>
52 #include <lar.h>
53 #include <pci.h>
54
55 #define MIN(a,b) ((a) < (b) ? (a) : (b))
56 #define MAX(a,b) ((a) > (b) ? (a) : (b))
57 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
58
59 #define LITTLE_ENDIAN   1234
60 #define BIG_ENDIAN      4321
61
62 #define EXIT_SUCCESS 0
63 #define EXIT_FAILURE 1
64
65 #define RAND_MAX 0x7fffffff
66
67 /*
68  * Payload information parameters - these are used to pass information
69  * to the entity loading the payload.
70  * Usage:   PAYLOAD_INFO(key, value)
71  * Example: PAYLOAD_INFO(name, "CoreInfo!")
72  */
73 #define _pstruct(key) __pinfo_ ##key
74 #define PAYLOAD_INFO(key, value)                                        \
75 static const char _pstruct(key)[]                                        \
76   __attribute__((__used__))                                              \
77   __attribute__((section(".note.pinfo"),unused)) = #key "=" value
78
79 /**
80  * @defgroup nvram NVRAM and RTC functions
81  * @{
82  */
83
84 #define NVRAM_RTC_SECONDS        0      /**< RTC Seconds offset in CMOS */
85 #define NVRAM_RTC_MINUTES        2      /**< RTC Minutes offset in CMOS */
86 #define NVRAM_RTC_HOURS          4      /**< RTC Hours offset in CMOS */
87 #define NVRAM_RTC_DAY            7      /**< RTC Days offset in CMOS */
88 #define NVRAM_RTC_MONTH          8      /**< RTC Month offset in CMOS */
89 #define NVRAM_RTC_YEAR           9      /**< RTC Year offset in CMOS */
90 #define NVRAM_RTC_FREQ_SELECT    10     /**< RTC Update Status Register */
91 #define  NVRAM_RTC_UIP           0x80
92
93 /** Broken down time structure */
94 struct tm {
95         int tm_sec;   /**< Number of seconds after the minute */
96         int tm_min;   /**< Number of minutes after the hour */
97         int tm_hour;  /**< Number of hours past midnight */
98         int tm_mday;  /**< The day of the month */
99         int tm_mon;   /**< The month of the year */
100         int tm_year;  /**< The number of years since 1900 */
101         int tm_wday;  /**< The day of the week */
102         int tm_yday;  /**< The number of days since January 1 */
103         int tm_isdst; /**< A flag indicating daylight savings time */
104 };
105
106 u8 nvram_read(u8 addr);
107 void nvram_write(u8 val, u8 addr);
108 int nvram_updating(void);
109 void rtc_read_clock(struct tm *tm);
110 /** @} */
111
112 /**
113  * @defgroup usb USB functions
114  * @{
115  */
116 int usb_initialize(void);                                                      
117 /** @} */
118
119 /**
120  * @defgroup input Device functions
121  * @{ @}
122  */
123
124 /**
125  * @defgroup keyboard Keyboard functions
126  * @ingroup input
127  * @{
128  */
129 void keyboard_init(void);
130 int keyboard_havechar(void);
131 unsigned char keyboard_get_scancode(void);
132 int keyboard_getchar(void);
133 int keyboard_set_layout(char *country);
134 int keyboard_add_reset_handler(void (*new_handler)(void));
135 /** @} */
136
137 /**
138  * @defgroup serial Serial functions
139  * @ingroup input
140  * @{
141  */
142 void serial_init(void);
143 void serial_hardware_init(int port, int speed, int word_bits, int parity, int stop_bits);
144 void serial_putchar(unsigned char c);
145 int serial_havechar(void);
146 int serial_getchar(void);
147 void serial_clear(void);
148 void serial_start_bold(void);
149 void serial_end_bold(void);
150 void serial_start_reverse(void);
151 void serial_end_reverse(void);
152 void serial_start_altcharset(void);
153 void serial_end_altcharset(void);
154 void serial_set_color(short fg, short bg);
155 void serial_cursor_enable(int state);
156 void serial_set_cursor(int y, int x);
157 /** @} */
158
159 /**
160  * @defgroup speaker Speaker functions
161  * @ingroup input
162  * @{
163  */
164 void speaker_enable(u16 freq);
165 void speaker_disable(void);
166 void speaker_tone(u16 freq, unsigned int duration);
167 /** @} */
168
169 /**
170  * @defgroup video Video functions
171  * @ingroup input
172  * @{
173  */
174 int video_console_init(void);
175 void video_console_putchar(unsigned int ch);
176 void video_console_putc(u8 row, u8 col, unsigned int ch);
177 void video_console_clear(void);
178 void video_console_cursor_enable(int state);
179 void video_console_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en);
180 void video_console_set_cursor(unsigned int cursorx, unsigned int cursory);
181 /** @} */
182
183 /* drivers/option.c */
184 int get_option(void *dest, char *name);
185
186 /**
187  * @defgroup console Console functions
188  * @{
189  */
190 void console_init(void);
191 int putchar(int c);
192 int puts(const char *s);
193 int havekey(void);
194 int getchar(void);
195 int getchar_timeout(int *ms);
196
197 extern int last_putchar;
198
199 #define havechar havekey
200 /** @} */
201
202 /**
203  * @defgroup ctype Character type functions
204  * @{
205  */
206 int isalnum(int c);
207 int isalpha(int c);
208 int isascii(int c);
209 int isblank(int c);
210 int iscntrl(int c);
211 int isdigit(int c);
212 int isgraph(int c);
213 int islower(int c);
214 int isprint(int c);
215 int ispunct(int c);
216 int isspace(int c);
217 int isupper(int c);
218 int isxdigit(int c);
219 int tolower(int c);
220 int toupper(int c);
221 /** @} */
222
223 /**
224  * @defgroup ipchecksum IP checksum functions
225  * @{
226  */
227 unsigned short ipchksum(const void *ptr, unsigned long nbytes);
228 /** @} */
229
230 /**
231  * @defgroup malloc Memory allocation functions
232  * @{
233  */
234 void free(void *ptr);
235 void *malloc(size_t size);
236 void *calloc(size_t nmemb, size_t size);
237 void *realloc(void *ptr, size_t size);
238 void *memalign(size_t align, size_t size);
239 /** @} */
240
241 /**
242  * @defgroup exec Execution functions
243  * @{
244  */
245 int exec(long addr, int argc, char **argv);
246 /** @} */
247
248 /**
249  * @defgroup misc Misc functions
250  * @{
251  */
252 int bcd2dec(int b);
253 int dec2bcd(int d);
254 int abs(int j);
255 long int labs(long int j);
256 long long int llabs(long long int j);
257 u8 bin2hex(u8 b);
258 u8 hex2bin(u8 h);
259 void fatal(const char *msg) __attribute__ ((noreturn));
260 /** @} */
261
262 /**
263  * @defgroup memory Memory manipulation functions
264  * @{
265  */
266 void *memset(void *s, int c, size_t n);
267 void *memcpy(void *dst, const void *src, size_t n);
268 void *memmove(void *dst, const void *src, size_t n);
269 int memcmp(const void *s1, const void *s2, size_t len);
270 /** @} */
271
272 /**
273  * @defgroup printf Print functions
274  * @{
275  */
276 int snprintf(char *str, size_t size, const char *fmt, ...);
277 int sprintf(char *str, const char *fmt, ...);
278 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
279 int vsprintf(char *str, const char *fmt, va_list ap);
280 int printf(const char *fmt, ...);
281 int vprintf(const char *fmt, va_list ap);
282 /** @} */
283
284 /**
285  * @defgroup rand Random number generator functions
286  * @{
287  */
288 int rand_r(unsigned int *seed);
289 int rand(void);
290 void srand(unsigned int seed);
291 /** @} */
292
293 /**
294  * @defgroup hash Hashing functions
295  * @{
296  */
297 #define SHA1_BLOCK_LENGTH       64
298 #define SHA1_DIGEST_LENGTH      20
299 typedef struct {
300         u32 state[5];
301         u64 count;
302         u8 buffer[SHA1_BLOCK_LENGTH];
303 } SHA1_CTX;
304 void SHA1Init(SHA1_CTX *context);
305 void SHA1Transform(u32 state[5], const u8 buffer[SHA1_BLOCK_LENGTH]);
306 void SHA1Update(SHA1_CTX *context, const u8 *data, size_t len);
307 void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
308 u8 *sha1(const u8 *data, size_t len, u8 *buf);
309 /** @} */
310
311 /**
312  * @defgroup string String functions
313  * @{
314  */
315 size_t strnlen(const char *str, size_t maxlen);
316 size_t strlen(const char *str);
317 int strcmp(const char *s1, const char *s2);
318 int strncmp(const char *s1, const char *s2, size_t maxlen);
319 char *strncpy(char *d, const char *s, size_t n);
320 char *strcpy(char *d, const char *s);
321 char *strncat(char *d, const char *s, size_t n);
322 char *strchr(const char *s, int c);
323 char *strdup(const char *s);
324 char *strstr(const char *h, const char *n);
325 char *strsep(char **stringp, const char *delim);
326 /** @} */
327
328 /**
329  * @defgroup time Time functions
330  * @{
331  */
332
333 /** System time structure */
334 struct timeval {
335         time_t tv_sec;       /**< Seconds */
336         suseconds_t tv_usec; /**< Microseconds */
337 };
338
339 int gettimeofday(struct timeval *tv, void *tz);
340 /** @} */
341
342 /**
343  * @defgroup lar LAR functions
344  * @{
345  */
346
347 /** LAR file header */
348 struct LAR {
349         void *start;    /**< Location of the LAR in memory */
350         int cindex;     /**< Next file to return in readlar() */
351         int count;      /**< Number of entries in the header cache */
352         int alloc;      /**< Number of slots in the header cache */
353         int eof;        /**< Last entry in the header cache */
354         void **headers; /**< Pointer to the header cache */
355 };
356
357 /** A structure representing the next LAR entry */
358 struct larent {
359         u8 name[LAR_MAX_PATHLEN]; /**< The name of the next LAR entry */
360 };
361
362 /** A structure containing information about a LAR file */
363 struct larstat {
364         u32 len;           /**< Length of the file in the LAR */
365         u32 reallen;       /**< Length of the uncompressed file */
366         u32 checksum;      /**< Checksum of the uncompressed file */
367         u32 compchecksum;  /**< Checksum of the compressed file in the LAR */
368         u32 offset;        /**< Offset of the file in the LAR */
369         u32 compression;   /**< Compression type of the file */
370         u64 entry;         /**< Entry point of the file for executables */
371         u64 loadaddress;   /**< Address in memory to put the uncompressed file */
372 };
373
374 /** A structure representing a LAR file */
375 struct LFILE {
376         struct LAR *lar;           /**< Pointer to the LAR struct */
377         struct lar_header *header; /**< Pointer to the header struct */
378         u32 size;                  /**< Size of the file */
379         void *start;               /**< Start of the file in memory */
380         u32 offset;                /**< Offset of the file in the LAR */
381 };
382
383 struct LAR *openlar(void *addr);
384 int closelar(struct LAR *lar);
385 struct larent *readlar(struct LAR *lar);
386 void rewindlar(struct LAR *lar);
387 int larstat(struct LAR *lar, const char *path, struct larstat *buf);
388 void *larfptr(struct LAR *lar, const char *filename);
389 int lfverify(struct LAR *lar, const char *filename);
390 struct LFILE *lfopen(struct LAR *lar, const char *filename);
391 int lfread(void *ptr, size_t size, size_t nmemb, struct LFILE *stream);
392
393 #define SEEK_SET 0 /**< The seek offset is absolute. */
394 #define SEEK_CUR 1 /**< The seek offset is against the current position. */
395 #define SEEK_END 2 /**< The seek offset is against the end of the file. */
396
397 int lfseek(struct LFILE *stream, long offset, int whence);
398 int lfclose(struct LFILE *file);
399 /** @} */
400
401 /**
402  * @defgroup arch Architecture specific functions
403  * This module contains global architecture specific functions.
404  * All architectures are expected to define these functions.
405  * @{
406  */
407 int get_coreboot_info(struct sysinfo_t *info);
408
409 void lib_get_sysinfo(void);
410
411 /* Timer functions - defined by each architecture. */
412 unsigned int get_cpu_speed(void);
413 void ndelay(unsigned int n);
414 void udelay(unsigned int n);
415 void mdelay(unsigned int n);
416 void delay(unsigned int n);
417
418 #define abort() halt()    /**< Alias for the halt() function */
419
420 /**
421  * Stop execution and halt the processor (this function does not return).
422  */
423 void halt(void) __attribute__ ((noreturn));
424 /** @} */
425
426 /**
427  * @defgroup readline Readline functions
428  * This interface provides a simple implementation of the standard readline()
429  * and getline() functions. They read a line of input from the console.
430  * @{
431  */
432 char *readline(const char *prompt);
433 int getline(char *buffer, int len);
434 /** @} */
435
436 #endif