blob: 3ef05f2b3c574e70e524bea7b3e7aa80ec255916 [file] [log] [blame]
Jordan Crousef6145c32008-03-19 23:56:58 +00001/*
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
Jordan Crouse3a48bdc2008-08-28 16:53:24 +000030/** @mainpage
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.
35 *
36 * @section example Example
37 * Here is an example of a very simple payload:
38 * @include sample/hello.c
39 *
40 */
41
42
Uwe Hermannfad8c2b2008-04-11 18:01:50 +000043#ifndef _LIBPAYLOAD_H
44#define _LIBPAYLOAD_H
Jordan Crousef6145c32008-03-19 23:56:58 +000045
Jordan Crousef6145c32008-03-19 23:56:58 +000046#include <stddef.h>
47#include <arch/types.h>
48#include <arch/io.h>
Stefan Reinauer99c08562008-08-19 17:49:53 +000049#include <arch/virtual.h>
Uwe Hermanndc69e052008-03-20 01:53:30 +000050#include <sysinfo.h>
Jordan Crousef6145c32008-03-19 23:56:58 +000051#include <stdarg.h>
Jordan Crouse681ec272008-05-07 20:34:02 +000052#include <lar.h>
Uwe Hermann83233d02008-08-04 21:00:49 +000053#include <pci.h>
Jordan Crousef6145c32008-03-19 23:56:58 +000054
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
Uwe Hermann39955932008-04-03 23:01:23 +000059#define LITTLE_ENDIAN 1234
60#define BIG_ENDIAN 4321
Uwe Hermann39955932008-04-03 23:01:23 +000061
Uwe Hermannbe504d12008-04-11 20:16:24 +000062#define EXIT_SUCCESS 0
63#define EXIT_FAILURE 1
64
Uwe Hermann4eb50892008-04-07 23:33:50 +000065#define RAND_MAX 0x7fffffff
66
Jordan Crousefc697ad2008-05-27 20:06:54 +000067/* 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!")
71 */
72
73#define _pstruct(key) __pinfo_ ##key
74#define PAYLOAD_INFO(key, value) \
75static const char _pstruct(key)[] \
76 __attribute__((__used__)) \
77 __attribute__((section(".note.pinfo"),unused)) = #key "=" value
78
Uwe Hermannc16d24e2008-03-31 15:17:39 +000079/* 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
Jordan Crousee2271432008-04-25 23:11:02 +000086#define NVRAM_RTC_FREQ_SELECT 10
87#define NVRAM_RTC_UIP 0x80
88
Jordan Crouse3a48bdc2008-08-28 16:53:24 +000089/**
90 * @defgroup nvram NVRAM and RTC functions
91 * @{
92 */
Jordan Crousee2271432008-04-25 23:11:02 +000093struct tm {
94 int tm_sec;
95 int tm_min;
96 int tm_hour;
97 int tm_mday;
98 int tm_mon;
99 int tm_year;
100 int tm_wday;
101 int tm_yday;
102 int tm_isdst;
103};
Uwe Hermann8cc38d22008-03-27 23:26:40 +0000104
Uwe Hermannc16d24e2008-03-31 15:17:39 +0000105u8 nvram_read(u8 addr);
106void nvram_write(u8 val, u8 addr);
Jordan Crousee2271432008-04-25 23:11:02 +0000107int nvram_updating(void);
108void rtc_read_clock(struct tm *tm);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000109/** @} */
Uwe Hermann8cc38d22008-03-27 23:26:40 +0000110
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000111/**
112 * @defgroup input Device functions
113 * @{ @}
114 */
115
116/**
117 * @defgroup keyboard Keyboard functions
118 * @ingroup input
119 * @{
120 */
Jordan Crouse63f181f2008-04-25 23:09:39 +0000121void keyboard_init(void);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000122int keyboard_havechar(void);
123unsigned char keyboard_get_scancode(void);
124int keyboard_getchar(void);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000125/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000126
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000127/**
128 * @defgroup serial Serial functions
129 * @ingroup input
130 * @{
131 */
Uwe Hermanndc69e052008-03-20 01:53:30 +0000132void serial_init(void);
133void serial_putchar(unsigned char c);
134int serial_havechar(void);
135int serial_getchar(void);
136
Jordan Crouse3b470652008-06-20 00:01:42 +0000137void serial_clear(void);
138void serial_start_bold(void);
139void serial_end_bold(void);
Ulf Jordanb4d4bac2008-08-11 20:34:28 +0000140void serial_start_altcharset(void);
141void serial_end_altcharset(void);
Jordan Crouse3b470652008-06-20 00:01:42 +0000142void serial_set_cursor(int y, int x);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000143/** @} */
Jordan Crouse3b470652008-06-20 00:01:42 +0000144
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000145/**
146 * @defgroup speaker Speaker functions
147 * @ingroup input
148 * @{
149 */
Uwe Hermannfad8c2b2008-04-11 18:01:50 +0000150void speaker_enable(u16 freq);
151void speaker_disable(void);
152void speaker_tone(u16 freq, unsigned int duration);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000153/** @} */
Uwe Hermannfad8c2b2008-04-11 18:01:50 +0000154
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000155/**
156 * @defgroup video Video functions
157 * @ingroup input
158 * @{
159 */
Jordan Crouse30939bd2008-04-10 22:49:02 +0000160int video_console_init(void);
161void video_console_putchar(unsigned int ch);
162void video_console_putc(u8 row, u8 col, unsigned int ch);
163void video_console_clear(void);
164void video_console_cursor_enable(int state);
Stefan Reinauer59fb9a2b2008-08-19 17:44:49 +0000165void video_console_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en);
166void video_console_set_cursor(unsigned int cursorx, unsigned int cursory);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000167/** @} */
Stefan Reinauer59fb9a2b2008-08-19 17:44:49 +0000168
169/* drivers/option.c */
170int get_option(void *dest, char *name);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000171
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000172/**
173 * @defgroup console Console functions
174 * @{
175 */
Uwe Hermanndc69e052008-03-20 01:53:30 +0000176void console_init(void);
Jordan Crousef6145c32008-03-19 23:56:58 +0000177int putchar(int c);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000178int puts(const char *s);
179int havekey(void);
180int getchar(void);
Jordan Crouse672d0ae2008-04-08 23:21:33 +0000181int getchar_timeout(int *ms);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000182
Jordan Crousef6145c32008-03-19 23:56:58 +0000183extern int last_putchar;
184
185#define havechar havekey
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000186/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000187
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000188/**
189 * @defgroup ctype Character Type Functions
190 * @{
191 */
Uwe Hermann72199bc2008-04-11 19:43:55 +0000192int isalnum(int c);
193int isalpha(int c);
194int isascii(int c);
195int isblank(int c);
196int iscntrl(int c);
Jordan Crousef6145c32008-03-19 23:56:58 +0000197int isdigit(int c);
Uwe Hermann72199bc2008-04-11 19:43:55 +0000198int isgraph(int c);
199int islower(int c);
200int isprint(int c);
201int ispunct(int c);
202int isspace(int c);
203int isupper(int c);
204int isxdigit(int c);
Jordan Crousef6145c32008-03-19 23:56:58 +0000205int tolower(int c);
Uwe Hermann72199bc2008-04-11 19:43:55 +0000206int toupper(int c);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000207/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000208
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000209/**
210 * @defgroup ipchecksum IP Checksum Functions
211 * @{
212 */
Stefan Reinauer11e45cd2008-08-16 15:16:36 +0000213unsigned short ipchksum(const void *ptr, unsigned long nbytes);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000214/** @} */
Uwe Hermanndc69e052008-03-20 01:53:30 +0000215
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000216/**
217 * @defgroup malloc Memory Allocation Functions
218 * @{
219 */
Jordan Crousef6145c32008-03-19 23:56:58 +0000220void free(void *ptr);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000221void *malloc(size_t size);
Jordan Crousef6145c32008-03-19 23:56:58 +0000222void *calloc(size_t nmemb, size_t size);
223void *realloc(void *ptr, size_t size);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000224/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000225
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000226/**
227 * @defgroup exec Execution Functions
228 * @{
229 */
Jordan Crouse9dac1b42008-05-20 20:10:49 +0000230int exec(long addr, int argc, char **argv);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000231/** @} */
Jordan Crouse9dac1b42008-05-20 20:10:49 +0000232
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000233/**
234 * @defgroup misc Misc Functions
235 * @{
236 */
Uwe Hermann8cc38d22008-03-27 23:26:40 +0000237int bcd2dec(int b);
238int dec2bcd(int d);
Uwe Hermannb1033452008-04-11 18:38:04 +0000239int abs(int j);
240long int labs(long int j);
241long long int llabs(long long int j);
242u8 bin2hex(u8 b);
243u8 hex2bin(u8 h);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000244/** @} */
Uwe Hermann8cc38d22008-03-27 23:26:40 +0000245
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000246/**
247 * @defgroup memory Memory Manipulation Functions
248 * @{
249 */
Jordan Crousef6145c32008-03-19 23:56:58 +0000250void *memset(void *s, int c, size_t n);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000251void *memcpy(void *dst, const void *src, size_t n);
Jordan Crousef6145c32008-03-19 23:56:58 +0000252void *memmove(void *dst, const void *src, size_t n);
Ulf Jordan68285692008-08-09 19:34:56 +0000253int memcmp(const void *s1, const void *s2, size_t len);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000254/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000255
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000256/**
257 * @defgroup printf Print Functions
258 * @{
259 */
Uwe Hermann0a896252008-04-02 12:35:45 +0000260int snprintf(char *str, size_t size, const char *fmt, ...);
Jordan Crousef6145c32008-03-19 23:56:58 +0000261int sprintf(char *str, const char *fmt, ...);
262int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
263int vsprintf(char *str, const char *fmt, va_list ap);
264int printf(const char *fmt, ...);
265int vprintf(const char *fmt, va_list ap);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000266/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000267
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000268/**
269 * @defgroup rand Random Number Generator Functions
270 * @{
271 */
Uwe Hermannc5a78ac2008-04-08 23:38:15 +0000272int rand_r(unsigned int *seed);
273int rand(void);
274void srand(unsigned int seed);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000275/** @} */
Uwe Hermannc5a78ac2008-04-08 23:38:15 +0000276
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000277/**
278 * @defgroup hash Hashing Functions
279 * @{
280 */
Uwe Hermann39955932008-04-03 23:01:23 +0000281#define SHA1_BLOCK_LENGTH 64
282#define SHA1_DIGEST_LENGTH 20
283typedef struct {
284 u32 state[5];
285 u64 count;
286 u8 buffer[SHA1_BLOCK_LENGTH];
287} SHA1_CTX;
288void SHA1Init(SHA1_CTX *context);
289void SHA1Transform(u32 state[5], const u8 buffer[SHA1_BLOCK_LENGTH]);
290void SHA1Update(SHA1_CTX *context, const u8 *data, size_t len);
291void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
292u8 *sha1(const u8 *data, size_t len, u8 *buf);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000293/** @} */
Uwe Hermann39955932008-04-03 23:01:23 +0000294
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000295/**
296 * @defgroup string String Functions
297 * @{
298 */
Uwe Hermanndc69e052008-03-20 01:53:30 +0000299size_t strnlen(const char *str, size_t maxlen);
300size_t strlen(const char *str);
Jordan Crousef6145c32008-03-19 23:56:58 +0000301int strcmp(const char *s1, const char *s2);
Stefan Reinauer3b9d1b82008-08-26 21:51:04 +0000302int strncmp(const char *s1, const char *s2, size_t maxlen);
303char *strncpy(char *d, const char *s, size_t n);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000304char *strcpy(char *d, const char *s);
Stefan Reinauer3b9d1b82008-08-26 21:51:04 +0000305char *strncat(char *d, const char *s, size_t n);
Jordan Crousef6145c32008-03-19 23:56:58 +0000306char *strchr(const char *s, int c);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000307char *strdup(const char *s);
308char *strstr(const char *h, const char *n);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000309/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000310
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000311/**
312 * @defgroup time Time Functions
313 * @{
314 */
Jordan Crousee2271432008-04-25 23:11:02 +0000315struct timeval {
316 time_t tv_sec;
317 suseconds_t tv_usec;
318};
319
320int gettimeofday(struct timeval *tv, void *tz);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000321/** @} */
Jordan Crousee2271432008-04-25 23:11:02 +0000322
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000323/**
324 * @defgroup lar LAR Functions
325 * @{
326 */
Jordan Crouse681ec272008-05-07 20:34:02 +0000327struct LAR {
328 void * start;
329 int cindex;
330 int count;
331 int alloc;
332 int eof;
333 void **headers;
334};
335
336struct larent {
337 u8 name[LAR_MAX_PATHLEN];
338};
339
340struct larstat {
341 u32 len;
342 u32 reallen;
343 u32 checksum;
344 u32 compchecksum;
345 u32 offset;
346 u32 compression;
347 u64 entry;
348 u64 loadaddress;
349};
350
351struct LFILE {
352 struct LAR *lar;
353 struct lar_header *header;
354 u32 size;
355 void *start;
356 u32 offset;
357};
358
359struct LAR *openlar(void *addr);
360int closelar(struct LAR *lar);
361struct larent *readlar(struct LAR *lar);
362void rewindlar(struct LAR *lar);
363int larstat(struct LAR *lar, const char *path, struct larstat *buf);
Jordan Crouse50698082008-05-20 20:09:42 +0000364void *larfptr(struct LAR *lar, const char *filename);
Jordan Crouse9e734c22008-05-27 19:57:53 +0000365int lfverify(struct LAR *lar, const char *filename);
Jordan Crouse681ec272008-05-07 20:34:02 +0000366struct LFILE * lfopen(struct LAR *lar, const char *filename);
367int lfread(void *ptr, size_t size, size_t nmemb, struct LFILE *stream);
368
369#define SEEK_SET 0
370#define SEEK_CUR 1
371#define SEEK_END 2
372
373int lfseek(struct LFILE *stream, long offset, int whence);
374int lfclose(struct LFILE *file);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000375/** @} */
Jordan Crouse681ec272008-05-07 20:34:02 +0000376
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000377/**
378 * @defgroup arch Architecture Specific Functions
379 * @{
380 */
Uwe Hermanndc69e052008-03-20 01:53:30 +0000381int get_coreboot_info(struct sysinfo_t *info);
Jordan Crousef6145c32008-03-19 23:56:58 +0000382
Uwe Hermanndc69e052008-03-20 01:53:30 +0000383void lib_get_sysinfo(void);
384
Uwe Hermann661e3802008-03-21 18:37:23 +0000385/* Timer functions - defined by each architecture. */
Uwe Hermanndc69e052008-03-20 01:53:30 +0000386unsigned int get_cpu_speed(void);
387void ndelay(unsigned int n);
Jordan Crouse234e87f2008-04-10 22:50:44 +0000388void udelay(unsigned int n);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000389void mdelay(unsigned int n);
390void delay(unsigned int n);
391
Jordan Crousef6145c32008-03-19 23:56:58 +0000392#define abort() halt()
393void halt(void) __attribute__ ((noreturn));
Stefan Reinauer6e51cee2008-08-19 17:47:18 +0000394void fatal(const char* msg) __attribute__ ((noreturn));
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000395/** @} */
Stefan Reinauer6e51cee2008-08-19 17:47:18 +0000396
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000397/**
398 * @defgroup readline Readline Functions
399 * @{
400 */
Stefan Reinauer6e51cee2008-08-19 17:47:18 +0000401char * readline(const char * prompt);
Jakob Bornecrantzb0d3e712008-08-23 12:17:46 +0000402int getline(char *buffer, int len);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000403/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000404
Jordan Crousef6145c32008-03-19 23:56:58 +0000405#endif