2 * This file is part of the libpayload project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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.
38 * @section example Example
39 * Here is an example of a very simple payload:
40 * @include sample/hello.c
46 #include <libpayload-config.h>
48 #include <arch/types.h>
50 #include <arch/virtual.h>
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]))
62 #define LITTLE_ENDIAN 1234
63 #define BIG_ENDIAN 4321
65 #define EXIT_SUCCESS 0
66 #define EXIT_FAILURE 1
68 #define RAND_MAX 0x7fffffff
70 #define MAX_ARGC_COUNT 10
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!")
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
85 * @defgroup nvram NVRAM and RTC functions
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
98 /** Broken down time structure */
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 */
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);
118 * @defgroup usb USB functions
121 int usb_initialize(void);
122 int usbhid_havechar(void);
123 int usbhid_getchar(void);
127 * @defgroup input Device functions
131 extern void (*reset_handler)(void);
132 int add_reset_handler(void (*new_handler)(void));
135 * @defgroup keyboard Keyboard functions
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);
147 * @defgroup serial Serial functions
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);
169 * @defgroup speaker Speaker functions
173 void speaker_enable(u16 freq);
174 void speaker_disable(void);
175 void speaker_tone(u16 freq, unsigned int duration);
179 * @defgroup video Video functions
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);
192 /* drivers/option.c */
193 int get_option(void *dest, char *name);
196 * @defgroup console Console functions
199 void console_init(void);
200 int putchar(unsigned int c);
201 int puts(const char *s);
204 int getchar_timeout(int *ms);
206 extern int last_putchar;
208 struct console_input_driver;
209 struct console_input_driver {
210 struct console_input_driver *next;
211 int (*havekey) (void);
212 int (*getchar) (void);
215 struct console_output_driver;
216 struct console_output_driver {
217 struct console_output_driver *next;
218 void (*putchar) (unsigned int);
221 void console_add_output_driver(struct console_output_driver *out);
222 void console_add_input_driver(struct console_input_driver *in);
224 #define havechar havekey
228 * @defgroup ctype Character type functions
249 * @defgroup ipchecksum IP checksum functions
252 unsigned short ipchksum(const void *ptr, unsigned long nbytes);
256 * @defgroup malloc Memory allocation functions
259 #if defined(CONFIG_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
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__, \
266 printf("PRE free()\n"); \
267 print_malloc_map(); \
269 printf("POST free()\n"); \
270 print_malloc_map(); \
274 extern void print_malloc_map(void); \
275 extern void *malloc(size_t); \
277 printf("malloc(%u) called from %s:%s:%d...\n", s, __FILE__, __func__, \
279 printf("PRE malloc\n"); \
280 print_malloc_map(); \
282 printf("POST malloc (ptr = %p)\n", ptr); \
283 print_malloc_map(); \
286 #define calloc(n,s) \
288 extern void print_malloc_map(void); \
289 extern void *calloc(size_t,size_t); \
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(); \
296 printf("POST calloc (ptr = %p)\n", ptr); \
297 print_malloc_map(); \
300 #define realloc(p,s) \
302 extern void print_malloc_map(void); \
303 extern void *realloc(void*,size_t); \
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(); \
314 #define memalign(a,s) \
316 extern void print_malloc_map(void); \
317 extern void *memalign(size_t, size_t); \
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(); \
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);
338 * @defgroup exec Execution functions
341 int exec(long addr, int argc, char **argv);
345 * @defgroup misc Misc functions
351 long int labs(long int j);
352 long long int llabs(long long int j);
355 void fatal(const char *msg) __attribute__ ((noreturn));
359 * @defgroup memory Memory manipulation functions
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);
369 * @defgroup printf Print functions
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);
381 * @defgroup rand Random number generator functions
384 int rand_r(unsigned int *seed);
386 void srand(unsigned int seed);
390 * @defgroup hash Hashing functions
393 #define SHA1_BLOCK_LENGTH 64
394 #define SHA1_DIGEST_LENGTH 20
398 u8 buffer[SHA1_BLOCK_LENGTH];
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);
408 * @defgroup string String functions
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);
429 * @defgroup time Time functions
433 /** System time structure */
435 time_t tv_sec; /**< Seconds */
436 suseconds_t tv_usec; /**< Microseconds */
439 int gettimeofday(struct timeval *tv, void *tz);
444 * @defgroup lar LAR functions
448 /** LAR file header */
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 */
458 /** A structure representing the next LAR entry */
460 u8 name[LAR_MAX_PATHLEN]; /**< The name of the next LAR entry */
463 /** A structure containing information about a LAR file */
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 */
475 /** A structure representing a LAR file */
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 */
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);
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. */
498 int lfseek(struct LFILE *stream, long offset, int whence);
499 int lfclose(struct LFILE *file);
504 * @defgroup info System information functions
505 * This module contains functions that return information about the system
509 int sysinfo_have_multiboot(unsigned long *addr);
513 * @defgroup arch Architecture specific functions
514 * This module contains global architecture specific functions.
515 * All architectures are expected to define these functions.
518 int get_coreboot_info(struct sysinfo_t *info);
519 int get_multiboot_info(struct sysinfo_t *info);
521 void lib_get_sysinfo(void);
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);
530 #define abort() halt() /**< Alias for the halt() function */
533 * Stop execution and halt the processor (this function does not return).
535 void halt(void) __attribute__ ((noreturn));
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.
544 char *readline(const char *prompt);
545 int getline(char *buffer, int len);