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