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
31 * @section intro Introduction
32 * libpayload is a small BSD-licensed static library (a lightweight
33 * implementation of common and useful functions) intended to be used
34 * as a basis for coreboot payloads.
36 * @section example Example
37 * Here is an example of a very simple payload:
38 * @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 /* Payload information parameters - these are used to pass information
68 * to the entity loading the payload
69 * Usage: PAYLOAD_INFO(key, value)
70 * Example: PAYLOAD_INFO(name, "CoreInfo!")
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
79 /* Some NVRAM byte definitions */
80 #define NVRAM_RTC_SECONDS 0
81 #define NVRAM_RTC_MINUTES 2
82 #define NVRAM_RTC_HOURS 4
83 #define NVRAM_RTC_DAY 7
84 #define NVRAM_RTC_MONTH 8
85 #define NVRAM_RTC_YEAR 9
86 #define NVRAM_RTC_FREQ_SELECT 10
87 #define NVRAM_RTC_UIP 0x80
90 * @defgroup nvram NVRAM and RTC functions
105 u8 nvram_read(u8 addr);
106 void nvram_write(u8 val, u8 addr);
107 int nvram_updating(void);
108 void rtc_read_clock(struct tm *tm);
112 * @defgroup input Device functions
117 * @defgroup keyboard Keyboard functions
121 void keyboard_init(void);
122 int keyboard_havechar(void);
123 unsigned char keyboard_get_scancode(void);
124 int keyboard_getchar(void);
128 * @defgroup serial Serial functions
132 void serial_init(void);
133 void serial_putchar(unsigned char c);
134 int serial_havechar(void);
135 int serial_getchar(void);
137 void serial_clear(void);
138 void serial_start_bold(void);
139 void serial_end_bold(void);
140 void serial_start_altcharset(void);
141 void serial_end_altcharset(void);
142 void serial_set_cursor(int y, int x);
146 * @defgroup speaker Speaker functions
150 void speaker_enable(u16 freq);
151 void speaker_disable(void);
152 void speaker_tone(u16 freq, unsigned int duration);
156 * @defgroup video Video functions
160 int video_console_init(void);
161 void video_console_putchar(unsigned int ch);
162 void video_console_putc(u8 row, u8 col, unsigned int ch);
163 void video_console_clear(void);
164 void video_console_cursor_enable(int state);
165 void video_console_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en);
166 void video_console_set_cursor(unsigned int cursorx, unsigned int cursory);
169 /* drivers/option.c */
170 int get_option(void *dest, char *name);
173 * @defgroup console Console functions
176 void console_init(void);
178 int puts(const char *s);
181 int getchar_timeout(int *ms);
183 extern int last_putchar;
185 #define havechar havekey
189 * @defgroup ctype Character Type Functions
210 * @defgroup ipchecksum IP Checksum Functions
213 unsigned short ipchksum(const void *ptr, unsigned long nbytes);
217 * @defgroup malloc Memory Allocation Functions
220 void free(void *ptr);
221 void *malloc(size_t size);
222 void *calloc(size_t nmemb, size_t size);
223 void *realloc(void *ptr, size_t size);
227 * @defgroup exec Execution Functions
230 int exec(long addr, int argc, char **argv);
234 * @defgroup misc Misc Functions
240 long int labs(long int j);
241 long long int llabs(long long int j);
247 * @defgroup memory Memory Manipulation Functions
250 void *memset(void *s, int c, size_t n);
251 void *memcpy(void *dst, const void *src, size_t n);
252 void *memmove(void *dst, const void *src, size_t n);
253 int memcmp(const void *s1, const void *s2, size_t len);
257 * @defgroup printf Print Functions
260 int snprintf(char *str, size_t size, const char *fmt, ...);
261 int sprintf(char *str, const char *fmt, ...);
262 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
263 int vsprintf(char *str, const char *fmt, va_list ap);
264 int printf(const char *fmt, ...);
265 int vprintf(const char *fmt, va_list ap);
269 * @defgroup rand Random Number Generator Functions
272 int rand_r(unsigned int *seed);
274 void srand(unsigned int seed);
278 * @defgroup hash Hashing Functions
281 #define SHA1_BLOCK_LENGTH 64
282 #define SHA1_DIGEST_LENGTH 20
286 u8 buffer[SHA1_BLOCK_LENGTH];
288 void SHA1Init(SHA1_CTX *context);
289 void SHA1Transform(u32 state[5], const u8 buffer[SHA1_BLOCK_LENGTH]);
290 void SHA1Update(SHA1_CTX *context, const u8 *data, size_t len);
291 void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
292 u8 *sha1(const u8 *data, size_t len, u8 *buf);
296 * @defgroup string String Functions
299 size_t strnlen(const char *str, size_t maxlen);
300 size_t strlen(const char *str);
301 int strcmp(const char *s1, const char *s2);
302 int strncmp(const char *s1, const char *s2, size_t maxlen);
303 char *strncpy(char *d, const char *s, size_t n);
304 char *strcpy(char *d, const char *s);
305 char *strncat(char *d, const char *s, size_t n);
306 char *strchr(const char *s, int c);
307 char *strdup(const char *s);
308 char *strstr(const char *h, const char *n);
312 * @defgroup time Time Functions
320 int gettimeofday(struct timeval *tv, void *tz);
324 * @defgroup lar LAR Functions
337 u8 name[LAR_MAX_PATHLEN];
353 struct lar_header *header;
359 struct LAR *openlar(void *addr);
360 int closelar(struct LAR *lar);
361 struct larent *readlar(struct LAR *lar);
362 void rewindlar(struct LAR *lar);
363 int larstat(struct LAR *lar, const char *path, struct larstat *buf);
364 void *larfptr(struct LAR *lar, const char *filename);
365 int lfverify(struct LAR *lar, const char *filename);
366 struct LFILE * lfopen(struct LAR *lar, const char *filename);
367 int lfread(void *ptr, size_t size, size_t nmemb, struct LFILE *stream);
373 int lfseek(struct LFILE *stream, long offset, int whence);
374 int lfclose(struct LFILE *file);
378 * @defgroup arch Architecture Specific Functions
381 int get_coreboot_info(struct sysinfo_t *info);
383 void lib_get_sysinfo(void);
385 /* Timer functions - defined by each architecture. */
386 unsigned int get_cpu_speed(void);
387 void ndelay(unsigned int n);
388 void udelay(unsigned int n);
389 void mdelay(unsigned int n);
390 void delay(unsigned int n);
392 #define abort() halt()
393 void halt(void) __attribute__ ((noreturn));
394 void fatal(const char* msg) __attribute__ ((noreturn));
398 * @defgroup readline Readline Functions
401 char * readline(const char * prompt);
402 int getline(char *buffer, int len);