[PATCH]: libpayload: Document readline
[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 /** @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
43 #ifndef _LIBPAYLOAD_H
44 #define _LIBPAYLOAD_H
45
46 #include <stddef.h>
47 #include <arch/types.h>
48 #include <arch/io.h>
49 #include <arch/virtual.h>
50 #include <sysinfo.h>
51 #include <stdarg.h>
52 #include <lar.h>
53 #include <pci.h>
54
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
59 #define LITTLE_ENDIAN   1234
60 #define BIG_ENDIAN      4321
61
62 #define EXIT_SUCCESS 0
63 #define EXIT_FAILURE 1
64
65 #define RAND_MAX 0x7fffffff
66
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!")
71  */
72
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
78
79 /**
80  * @defgroup nvram NVRAM and RTC functions
81  * @{
82  */
83
84 #define NVRAM_RTC_SECONDS        0      /**< RTC Seconds offset in CMOS */
85 #define NVRAM_RTC_MINUTES        2      /**< RTC Minutes offset in CMOS */
86 #define NVRAM_RTC_HOURS          4      /**< RTC Hours offset in CMOS */
87 #define NVRAM_RTC_DAY            7      /**< RTC Days offset in CMOS */
88 #define NVRAM_RTC_MONTH          8      /**< RTC Month offset in CMOS */
89 #define NVRAM_RTC_YEAR           9      /**< RTC Year offset in CMOS */
90 #define NVRAM_RTC_FREQ_SELECT    10     /**< RTC Update Status Register */
91 #define  NVRAM_RTC_UIP           0x80
92
93 /** @struct tm
94  *  \brief Broken down time structure
95  */
96 struct tm {
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 */
106 };
107
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);
112 /** @} */
113
114 /**
115  * @defgroup input Device functions
116  * @{ @}
117  */
118
119 /**
120  * @defgroup keyboard Keyboard functions
121  * @ingroup input
122  * @{
123  */
124 void keyboard_init(void);
125 int keyboard_havechar(void);
126 unsigned char keyboard_get_scancode(void);
127 int keyboard_getchar(void);
128 /** @} */
129
130 /**
131  * @defgroup serial Serial functions
132  * @ingroup input
133  * @{
134  */
135 void serial_init(void);
136 void serial_putchar(unsigned char c);
137 int serial_havechar(void);
138 int serial_getchar(void);
139
140 void serial_clear(void);
141 void serial_start_bold(void);
142 void serial_end_bold(void);
143 void serial_start_altcharset(void);
144 void serial_end_altcharset(void);
145 void serial_set_cursor(int y, int x);
146 /** @} */
147
148 /**
149  * @defgroup speaker Speaker functions
150  * @ingroup input
151  * @{
152  */
153 void speaker_enable(u16 freq);
154 void speaker_disable(void);
155 void speaker_tone(u16 freq, unsigned int duration);
156 /** @} */
157
158 /**
159  * @defgroup video Video functions
160  * @ingroup input
161  * @{
162  */
163 int video_console_init(void);
164 void video_console_putchar(unsigned int ch);
165 void video_console_putc(u8 row, u8 col, unsigned int ch);
166 void video_console_clear(void);
167 void video_console_cursor_enable(int state);
168 void video_console_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en);
169 void video_console_set_cursor(unsigned int cursorx, unsigned int cursory);
170 /** @} */
171
172 /* drivers/option.c */
173 int get_option(void *dest, char *name);
174
175 /**
176  * @defgroup console Console functions
177  * @{
178  */
179 void console_init(void);
180 int putchar(int c);
181 int puts(const char *s);
182 int havekey(void);
183 int getchar(void);
184 int getchar_timeout(int *ms);
185
186 extern int last_putchar;
187
188 #define havechar havekey
189 /** @} */
190
191 /**
192  * @defgroup ctype Character Type Functions
193  * @{
194  */
195 int isalnum(int c);
196 int isalpha(int c);
197 int isascii(int c);
198 int isblank(int c);
199 int iscntrl(int c);
200 int isdigit(int c);
201 int isgraph(int c);
202 int islower(int c);
203 int isprint(int c);
204 int ispunct(int c);
205 int isspace(int c);
206 int isupper(int c);
207 int isxdigit(int c);
208 int tolower(int c);
209 int toupper(int c);
210 /** @} */
211
212 /**
213  * @defgroup ipchecksum IP Checksum Functions
214  * @{
215  */
216 unsigned short ipchksum(const void *ptr, unsigned long nbytes);
217 /** @} */
218
219 /**
220  * @defgroup malloc Memory Allocation Functions
221  * @{
222  */
223 void free(void *ptr);
224 void *malloc(size_t size);
225 void *calloc(size_t nmemb, size_t size);
226 void *realloc(void *ptr, size_t size);
227 /** @} */
228
229 /**
230  * @defgroup exec Execution Functions
231  * @{
232  */
233 int exec(long addr, int argc, char **argv);
234 /** @} */
235
236 /**
237  * @defgroup misc Misc Functions
238  * @{
239  */
240 int bcd2dec(int b);
241 int dec2bcd(int d);
242 int abs(int j);
243 long int labs(long int j);
244 long long int llabs(long long int j);
245 u8 bin2hex(u8 b);
246 u8 hex2bin(u8 h);
247 /** @} */
248
249 /**
250  * @defgroup memory Memory Manipulation Functions
251  * @{
252  */
253 void *memset(void *s, int c, size_t n);
254 void *memcpy(void *dst, const void *src, size_t n);
255 void *memmove(void *dst, const void *src, size_t n);
256 int memcmp(const void *s1, const void *s2, size_t len);
257 /** @} */
258
259 /**
260  * @defgroup printf Print Functions
261  * @{
262  */
263 int snprintf(char *str, size_t size, const char *fmt, ...);
264 int sprintf(char *str, const char *fmt, ...);
265 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
266 int vsprintf(char *str, const char *fmt, va_list ap);
267 int printf(const char *fmt, ...);
268 int vprintf(const char *fmt, va_list ap);
269 /** @} */
270
271 /**
272  * @defgroup rand Random Number Generator Functions
273  * @{
274  */
275 int rand_r(unsigned int *seed);
276 int rand(void);
277 void srand(unsigned int seed);
278 /** @} */
279
280 /**
281  * @defgroup hash Hashing Functions
282  * @{
283  */
284 #define SHA1_BLOCK_LENGTH       64
285 #define SHA1_DIGEST_LENGTH      20
286 typedef struct {
287         u32 state[5];
288         u64 count;
289         u8 buffer[SHA1_BLOCK_LENGTH];
290 } SHA1_CTX;
291 void SHA1Init(SHA1_CTX *context);
292 void SHA1Transform(u32 state[5], const u8 buffer[SHA1_BLOCK_LENGTH]);
293 void SHA1Update(SHA1_CTX *context, const u8 *data, size_t len);
294 void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
295 u8 *sha1(const u8 *data, size_t len, u8 *buf);
296 /** @} */
297
298 /**
299  * @defgroup string String Functions
300  * @{
301  */
302 size_t strnlen(const char *str, size_t maxlen);
303 size_t strlen(const char *str);
304 int strcmp(const char *s1, const char *s2);
305 int strncmp(const char *s1, const char *s2, size_t maxlen);
306 char *strncpy(char *d, const char *s, size_t n);
307 char *strcpy(char *d, const char *s);
308 char *strncat(char *d, const char *s, size_t n);
309 char *strchr(const char *s, int c);
310 char *strdup(const char *s);
311 char *strstr(const char *h, const char *n);
312 /** @} */
313
314 /**
315  * @defgroup time Time Functions
316  * @{
317  */
318
319 /** @struct timeval
320  * @brief System time structure
321  */
322
323 struct timeval {
324         time_t tv_sec;       /**< Seconds */
325         suseconds_t tv_usec; /**< Microseconds */
326 };
327
328 int gettimeofday(struct timeval *tv, void *tz);
329 /** @} */
330
331 /**
332  * @defgroup lar LAR Functions
333  * @{
334  */
335
336 /** @struct LAR
337  * @brief LAR File Header
338  */
339 struct LAR {
340         void * start;   /**< Location of the LAR in memory */
341         int cindex;     /**< Next file to return in readlar() */
342         int count;      /**< Number of entries in the header cache */
343         int alloc;      /**< Number of slots in the header cache */
344         int eof;        /**< Last entry in the header cache */
345         void **headers; /**< Pointer to the header cache */
346 };
347
348 /** @struct larent
349  * @brief A structure representing the next LAR entry
350  */
351 struct larent {
352         u8 name[LAR_MAX_PATHLEN]; /**< The name of the next LAR entry */
353 };
354
355 /** @struct larstat
356  * @brief A structure containing information about a LAR file
357  */
358 struct larstat {
359         u32 len;           /**< Length of the file in the LAR */
360         u32 reallen;       /**< Length of the uncompressed file */
361         u32 checksum;      /**< Checksum of the uncompressed file */
362         u32 compchecksum;  /**< Checksum of the compressed file in the LAR */
363         u32 offset;        /**< Offset of the file in the LAR */
364         u32 compression;   /**< Compression type of the file */
365         u64 entry;         /**< Entry point of the file for executables */
366         u64 loadaddress;   /**< Address in memory to put the uncompressed file */
367 };
368
369 /** @struct LFILE
370  * @brief A structure representing a LAR file
371  */
372
373 struct LFILE {
374         struct LAR *lar;           /**< Pointer to the LAR struct */
375         struct lar_header *header; /**< Pointer to the header struct */
376         u32 size;                  /**< Size of the file */
377         void *start;               /**< Start of the file in memory */
378         u32 offset;                /**< Offset of the file in the LAR */
379 };
380
381 struct LAR *openlar(void *addr);
382 int closelar(struct LAR *lar);
383 struct larent *readlar(struct LAR *lar);
384 void rewindlar(struct LAR *lar);
385 int larstat(struct LAR *lar, const char *path, struct larstat *buf);
386 void *larfptr(struct LAR *lar, const char *filename);
387 int lfverify(struct LAR *lar, const char *filename);
388 struct LFILE * lfopen(struct LAR *lar, const char *filename);
389 int lfread(void *ptr, size_t size, size_t nmemb, struct LFILE *stream);
390
391 #define SEEK_SET 0 /**@def The seek offset is absolute  */
392 #define SEEK_CUR 1 /**@def The seek offset is against the current position */
393 #define SEEK_END 2 /**@def The see offset is against the end of the file */
394
395 int lfseek(struct LFILE *stream, long offset, int whence);
396 int lfclose(struct LFILE *file);
397 /** @} */
398
399 /**
400  * @defgroup arch Architecture Specific Functions
401  * @{
402  */
403 int get_coreboot_info(struct sysinfo_t *info);
404
405 void lib_get_sysinfo(void);
406
407 /* Timer functions - defined by each architecture. */
408 unsigned int get_cpu_speed(void);
409 void ndelay(unsigned int n);
410 void udelay(unsigned int n);
411 void mdelay(unsigned int n);
412 void delay(unsigned int n);
413
414 #define abort() halt()
415 void halt(void) __attribute__ ((noreturn));
416 void fatal(const char* msg) __attribute__ ((noreturn));
417 /** @} */
418
419 /**
420  * @defgroup readline Readline Functions
421  * This interface provides a simple implementation of the standard
422  * readline and getline functions.  They are suitable for reading a
423  * line of input from the console.
424  * @{
425  */
426 char * readline(const char * prompt);
427 int getline(char *buffer, int len);
428 /** @} */
429
430 #endif