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