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
47 #include <arch/types.h>
49 #include <arch/virtual.h>
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]))
59 #define LITTLE_ENDIAN 1234
60 #define BIG_ENDIAN 4321
62 #define EXIT_SUCCESS 0
63 #define EXIT_FAILURE 1
65 #define RAND_MAX 0x7fffffff
67 #define MAX_ARGC_COUNT 10
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!")
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
82 * @defgroup nvram NVRAM and RTC functions
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
95 /** Broken down time structure */
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 */
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);
115 * @defgroup usb USB functions
118 int usb_initialize(void);
119 int usbhid_havechar(void);
120 int usbhid_getchar(void);
124 * @defgroup input Device functions
128 extern void (*reset_handler)(void);
129 int add_reset_handler(void (*new_handler)(void));
132 * @defgroup keyboard Keyboard functions
136 void keyboard_init(void);
137 int keyboard_havechar(void);
138 unsigned char keyboard_get_scancode(void);
139 int keyboard_getchar(void);
140 int keyboard_set_layout(char *country);
144 * @defgroup serial Serial functions
148 void serial_init(void);
149 void serial_hardware_init(int port, int speed, int word_bits, int parity, int stop_bits);
150 void serial_putchar(unsigned int c);
151 int serial_havechar(void);
152 int serial_getchar(void);
153 void serial_clear(void);
154 void serial_start_bold(void);
155 void serial_end_bold(void);
156 void serial_start_reverse(void);
157 void serial_end_reverse(void);
158 void serial_start_altcharset(void);
159 void serial_end_altcharset(void);
160 void serial_set_color(short fg, short bg);
161 void serial_cursor_enable(int state);
162 void serial_set_cursor(int y, int x);
166 * @defgroup speaker Speaker functions
170 void speaker_enable(u16 freq);
171 void speaker_disable(void);
172 void speaker_tone(u16 freq, unsigned int duration);
176 * @defgroup video Video functions
180 int video_console_init(void);
181 void video_console_putchar(unsigned int ch);
182 void video_console_putc(u8 row, u8 col, unsigned int ch);
183 void video_console_clear(void);
184 void video_console_cursor_enable(int state);
185 void video_console_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en);
186 void video_console_set_cursor(unsigned int cursorx, unsigned int cursory);
189 /* drivers/option.c */
190 int get_option(void *dest, char *name);
193 * @defgroup console Console functions
196 void console_init(void);
197 int putchar(unsigned int c);
198 int puts(const char *s);
201 int getchar_timeout(int *ms);
203 extern int last_putchar;
205 struct console_input_driver;
206 struct console_input_driver {
207 struct console_input_driver *next;
208 int (*havekey) (void);
209 int (*getchar) (void);
212 struct console_output_driver;
213 struct console_output_driver {
214 struct console_output_driver *next;
215 void (*putchar) (unsigned int);
218 void console_add_output_driver(struct console_output_driver *out);
219 void console_add_input_driver(struct console_input_driver *in);
221 #define havechar havekey
225 * @defgroup ctype Character type functions
246 * @defgroup ipchecksum IP checksum functions
249 unsigned short ipchksum(const void *ptr, unsigned long nbytes);
253 * @defgroup malloc Memory allocation functions
256 void free(void *ptr);
257 void *malloc(size_t size);
258 void *calloc(size_t nmemb, size_t size);
259 void *realloc(void *ptr, size_t size);
260 void *memalign(size_t align, size_t size);
264 * @defgroup exec Execution functions
267 int exec(long addr, int argc, char **argv);
271 * @defgroup misc Misc functions
277 long int labs(long int j);
278 long long int llabs(long long int j);
281 void fatal(const char *msg) __attribute__ ((noreturn));
285 * @defgroup memory Memory manipulation functions
288 void *memset(void *s, int c, size_t n);
289 void *memcpy(void *dst, const void *src, size_t n);
290 void *memmove(void *dst, const void *src, size_t n);
291 int memcmp(const void *s1, const void *s2, size_t len);
295 * @defgroup printf Print functions
298 int snprintf(char *str, size_t size, const char *fmt, ...);
299 int sprintf(char *str, const char *fmt, ...);
300 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
301 int vsprintf(char *str, const char *fmt, va_list ap);
302 int printf(const char *fmt, ...);
303 int vprintf(const char *fmt, va_list ap);
307 * @defgroup rand Random number generator functions
310 int rand_r(unsigned int *seed);
312 void srand(unsigned int seed);
316 * @defgroup hash Hashing functions
319 #define SHA1_BLOCK_LENGTH 64
320 #define SHA1_DIGEST_LENGTH 20
324 u8 buffer[SHA1_BLOCK_LENGTH];
326 void SHA1Init(SHA1_CTX *context);
327 void SHA1Transform(u32 state[5], const u8 buffer[SHA1_BLOCK_LENGTH]);
328 void SHA1Update(SHA1_CTX *context, const u8 *data, size_t len);
329 void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
330 u8 *sha1(const u8 *data, size_t len, u8 *buf);
334 * @defgroup string String functions
337 size_t strnlen(const char *str, size_t maxlen);
338 size_t strlen(const char *str);
339 int strcmp(const char *s1, const char *s2);
340 int strncmp(const char *s1, const char *s2, size_t maxlen);
341 char *strncpy(char *d, const char *s, size_t n);
342 char *strcpy(char *d, const char *s);
343 char *strncat(char *d, const char *s, size_t n);
344 char *strchr(const char *s, int c);
345 char *strdup(const char *s);
346 char *strstr(const char *h, const char *n);
347 char *strsep(char **stringp, const char *delim);
348 unsigned int strtoul(const char *s, char **nptr, int base);
353 * @defgroup time Time functions
357 /** System time structure */
359 time_t tv_sec; /**< Seconds */
360 suseconds_t tv_usec; /**< Microseconds */
363 int gettimeofday(struct timeval *tv, void *tz);
367 * @defgroup lar LAR functions
371 /** LAR file header */
373 void *start; /**< Location of the LAR in memory */
374 int cindex; /**< Next file to return in readlar() */
375 int count; /**< Number of entries in the header cache */
376 int alloc; /**< Number of slots in the header cache */
377 int eof; /**< Last entry in the header cache */
378 void **headers; /**< Pointer to the header cache */
381 /** A structure representing the next LAR entry */
383 u8 name[LAR_MAX_PATHLEN]; /**< The name of the next LAR entry */
386 /** A structure containing information about a LAR file */
388 u32 len; /**< Length of the file in the LAR */
389 u32 reallen; /**< Length of the uncompressed file */
390 u32 checksum; /**< Checksum of the uncompressed file */
391 u32 compchecksum; /**< Checksum of the compressed file in the LAR */
392 u32 offset; /**< Offset of the file in the LAR */
393 u32 compression; /**< Compression type of the file */
394 u64 entry; /**< Entry point of the file for executables */
395 u64 loadaddress; /**< Address in memory to put the uncompressed file */
398 /** A structure representing a LAR file */
400 struct LAR *lar; /**< Pointer to the LAR struct */
401 struct lar_header *header; /**< Pointer to the header struct */
402 u32 size; /**< Size of the file */
403 void *start; /**< Start of the file in memory */
404 u32 offset; /**< Offset of the file in the LAR */
407 struct LAR *openlar(void *addr);
408 int closelar(struct LAR *lar);
409 struct larent *readlar(struct LAR *lar);
410 void rewindlar(struct LAR *lar);
411 int larstat(struct LAR *lar, const char *path, struct larstat *buf);
412 void *larfptr(struct LAR *lar, const char *filename);
413 int lfverify(struct LAR *lar, const char *filename);
414 struct LFILE *lfopen(struct LAR *lar, const char *filename);
415 int lfread(void *ptr, size_t size, size_t nmemb, struct LFILE *stream);
417 #define SEEK_SET 0 /**< The seek offset is absolute. */
418 #define SEEK_CUR 1 /**< The seek offset is against the current position. */
419 #define SEEK_END 2 /**< The seek offset is against the end of the file. */
421 int lfseek(struct LFILE *stream, long offset, int whence);
422 int lfclose(struct LFILE *file);
426 * @defgroup info System information functions
427 * This module contains functions that return information about the system
431 int sysinfo_have_multiboot(unsigned long *addr);
435 * @defgroup arch Architecture specific functions
436 * This module contains global architecture specific functions.
437 * All architectures are expected to define these functions.
440 int get_coreboot_info(struct sysinfo_t *info);
441 int get_multiboot_info(struct sysinfo_t *info);
443 void lib_get_sysinfo(void);
445 /* Timer functions - defined by each architecture. */
446 unsigned int get_cpu_speed(void);
447 void ndelay(unsigned int n);
448 void udelay(unsigned int n);
449 void mdelay(unsigned int n);
450 void delay(unsigned int n);
452 #define abort() halt() /**< Alias for the halt() function */
455 * Stop execution and halt the processor (this function does not return).
457 void halt(void) __attribute__ ((noreturn));
461 * @defgroup readline Readline functions
462 * This interface provides a simple implementation of the standard readline()
463 * and getline() functions. They read a line of input from the console.
466 char *readline(const char *prompt);
467 int getline(char *buffer, int len);