308d95c667f1122c9d39676fc52d1b4d94153b3a
[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 /**
31  * @mainpage
32  *
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.
37  *
38  * @section example Example
39  * Here is an example of a very simple payload:
40  * @include sample/hello.c
41  */
42
43 #ifndef _LIBPAYLOAD_H
44 #define _LIBPAYLOAD_H
45
46 #include <libpayload-config.h>
47 #include <ctype.h>
48 #include <stddef.h>
49 #include <stdio.h>
50 #include <stdarg.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <arch/types.h>
54 #include <arch/io.h>
55 #include <arch/virtual.h>
56 #include <sysinfo.h>
57 #include <pci.h>
58 #ifdef CONFIG_LAR
59 #include <lar.h>
60 #endif
61
62 #define MIN(a,b) ((a) < (b) ? (a) : (b))
63 #define MAX(a,b) ((a) > (b) ? (a) : (b))
64 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
65
66 #define LITTLE_ENDIAN   1234
67 #define BIG_ENDIAN      4321
68
69 #define EXIT_SUCCESS 0
70 #define EXIT_FAILURE 1
71
72 #define RAND_MAX 0x7fffffff
73
74 #define MAX_ARGC_COUNT 10
75
76 /*
77  * Payload information parameters - these are used to pass information
78  * to the entity loading the payload.
79  * Usage:   PAYLOAD_INFO(key, value)
80  * Example: PAYLOAD_INFO(name, "CoreInfo!")
81  */
82 #define _pstruct(key) __pinfo_ ##key
83 #define PAYLOAD_INFO(key, value)                                        \
84 static const char _pstruct(key)[]                                        \
85   __attribute__((__used__))                                              \
86   __attribute__((section(".note.pinfo"),unused)) = #key "=" value
87
88 /**
89  * @defgroup nvram NVRAM and RTC functions
90  * @{
91  */
92
93 #define NVRAM_RTC_SECONDS        0      /**< RTC Seconds offset in CMOS */
94 #define NVRAM_RTC_MINUTES        2      /**< RTC Minutes offset in CMOS */
95 #define NVRAM_RTC_HOURS          4      /**< RTC Hours offset in CMOS */
96 #define NVRAM_RTC_DAY            7      /**< RTC Days offset in CMOS */
97 #define NVRAM_RTC_MONTH          8      /**< RTC Month offset in CMOS */
98 #define NVRAM_RTC_YEAR           9      /**< RTC Year offset in CMOS */
99 #define NVRAM_RTC_FREQ_SELECT    10     /**< RTC Update Status Register */
100 #define  NVRAM_RTC_UIP           0x80
101
102 /** Broken down time structure */
103 struct tm {
104         int tm_sec;   /**< Number of seconds after the minute */
105         int tm_min;   /**< Number of minutes after the hour */
106         int tm_hour;  /**< Number of hours past midnight */
107         int tm_mday;  /**< The day of the month */
108         int tm_mon;   /**< The month of the year */
109         int tm_year;  /**< The number of years since 1900 */
110         int tm_wday;  /**< The day of the week */
111         int tm_yday;  /**< The number of days since January 1 */
112         int tm_isdst; /**< A flag indicating daylight savings time */
113 };
114
115 u8 nvram_read(u8 addr);
116 void nvram_write(u8 val, u8 addr);
117 int nvram_updating(void);
118 void rtc_read_clock(struct tm *tm);
119 /** @} */
120
121 /**
122  * @defgroup usb USB functions
123  * @{
124  */
125 int usb_initialize(void);
126 int usb_exit (void);
127 int usbhid_havechar(void);
128 int usbhid_getchar(void);
129 /** @} */
130
131 /**
132  * @defgroup input Device functions
133  * @{ @}
134  */
135
136 extern void (*reset_handler)(void);
137 int add_reset_handler(void (*new_handler)(void));
138
139 /**
140  * @defgroup keyboard Keyboard functions
141  * @ingroup input
142  * @{
143  */
144 void keyboard_init(void);
145 int keyboard_havechar(void);
146 unsigned char keyboard_get_scancode(void);
147 int keyboard_getchar(void);
148 int keyboard_set_layout(char *country);
149 /** @} */
150
151 /**
152  * @defgroup serial Serial functions
153  * @ingroup input
154  * @{
155  */
156 void serial_init(void);
157 void serial_putchar(unsigned int c);
158 int serial_havechar(void);
159 int serial_getchar(void);
160 void serial_clear(void);
161 void serial_start_bold(void);
162 void serial_end_bold(void);
163 void serial_start_reverse(void);
164 void serial_end_reverse(void);
165 void serial_start_altcharset(void);
166 void serial_end_altcharset(void);
167 void serial_set_color(short fg, short bg);
168 void serial_cursor_enable(int state);
169 void serial_set_cursor(int y, int x);
170 /** @} */
171
172 /**
173  * @defgroup speaker Speaker functions
174  * @ingroup input
175  * @{
176  */
177 void speaker_enable(u16 freq);
178 void speaker_disable(void);
179 void speaker_tone(u16 freq, unsigned int duration);
180 /** @} */
181
182 /**
183  * @defgroup video Video functions
184  * @ingroup input
185  * @{
186  */
187 int video_console_init(void);
188 void video_console_putchar(unsigned int ch);
189 void video_console_putc(u8 row, u8 col, unsigned int ch);
190 void video_console_clear(void);
191 void video_console_cursor_enable(int state);
192 void video_console_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en);
193 void video_console_set_cursor(unsigned int cursorx, unsigned int cursory);
194 /** @} */
195
196 /* drivers/option.c */
197 struct nvram_accessor {
198         u8 (*read)(u8 reg);
199         void (*write)(u8 val, u8 reg);
200 };
201
202 extern u8 *mem_accessor_base;
203 extern struct nvram_accessor *use_nvram, *use_mem;
204
205 struct cb_cmos_option_table *get_system_option_table(void);
206 void fix_options_checksum_with(const struct nvram_accessor *nvram);
207 void fix_options_checksum(void);
208 int get_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *dest, char *name);
209 int get_option_from(struct cb_cmos_option_table *option_table, void *dest, char *name);
210 int get_option(void *dest, char *name);
211 int set_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *value, char *name);
212 int set_option(void *value, char *name);
213
214 /**
215  * @defgroup console Console functions
216  * @{
217  */
218 void console_init(void);
219 int putchar(unsigned int c);
220 int puts(const char *s);
221 int havekey(void);
222 int getchar(void);
223 int getchar_timeout(int *ms);
224
225 extern int last_putchar;
226
227 struct console_input_driver;
228 struct console_input_driver {
229         struct console_input_driver *next;
230         int (*havekey) (void);
231         int (*getchar) (void);
232 };
233
234 struct console_output_driver;
235 struct console_output_driver {
236         struct console_output_driver *next;
237         void (*putchar) (unsigned int);
238 };
239
240 void console_add_output_driver(struct console_output_driver *out);
241 void console_add_input_driver(struct console_input_driver *in);
242
243 #define havechar havekey
244 /** @} */
245
246 /**
247  * @defgroup ipchecksum IP checksum functions
248  * @{
249  */
250 unsigned short ipchksum(const void *ptr, unsigned long nbytes);
251 /** @} */
252
253
254 /**
255  * @defgroup exec Execution functions
256  * @{
257  */
258 int exec(long addr, int argc, char **argv);
259 /** @} */
260
261 /**
262  * @defgroup misc Misc functions
263  * @{
264  */
265 int bcd2dec(int b);
266 int dec2bcd(int d);
267 int abs(int j);
268 long int labs(long int j);
269 long long int llabs(long long int j);
270 u8 bin2hex(u8 b);
271 u8 hex2bin(u8 h);
272 void fatal(const char *msg) __attribute__ ((noreturn));
273 /** @} */
274
275
276 /**
277  * @defgroup hash Hashing functions
278  * @{
279  */
280 #define SHA1_BLOCK_LENGTH       64
281 #define SHA1_DIGEST_LENGTH      20
282 typedef struct {
283         u32 state[5];
284         u64 count;
285         u8 buffer[SHA1_BLOCK_LENGTH];
286 } SHA1_CTX;
287 void SHA1Init(SHA1_CTX *context);
288 void SHA1Transform(u32 state[5], const u8 buffer[SHA1_BLOCK_LENGTH]);
289 void SHA1Update(SHA1_CTX *context, const u8 *data, size_t len);
290 void SHA1Pad(SHA1_CTX *context);
291 void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
292 u8 *sha1(const u8 *data, size_t len, u8 *buf);
293 /** @} */
294
295 /**
296  * @defgroup time Time functions
297  * @{
298  */
299
300 /** System time structure */
301 struct timeval {
302         time_t tv_sec;       /**< Seconds */
303         suseconds_t tv_usec; /**< Microseconds */
304 };
305
306 int gettimeofday(struct timeval *tv, void *tz);
307 /** @} */
308
309 #ifdef CONFIG_LAR
310 /**
311  * @defgroup lar LAR functions
312  * @{
313  */
314
315 /** LAR file header */
316 struct LAR {
317         void *start;    /**< Location of the LAR in memory */
318         int cindex;     /**< Next file to return in readlar() */
319         int count;      /**< Number of entries in the header cache */
320         int alloc;      /**< Number of slots in the header cache */
321         int eof;        /**< Last entry in the header cache */
322         void **headers; /**< Pointer to the header cache */
323 };
324
325 /** A structure representing the next LAR entry */
326 struct larent {
327         u8 name[LAR_MAX_PATHLEN]; /**< The name of the next LAR entry */
328 };
329
330 /** A structure containing information about a LAR file */
331 struct larstat {
332         u32 len;           /**< Length of the file in the LAR */
333         u32 reallen;       /**< Length of the uncompressed file */
334         u32 checksum;      /**< Checksum of the uncompressed file */
335         u32 compchecksum;  /**< Checksum of the compressed file in the LAR */
336         u32 offset;        /**< Offset of the file in the LAR */
337         u32 compression;   /**< Compression type of the file */
338         u64 entry;         /**< Entry point of the file for executables */
339         u64 loadaddress;   /**< Address in memory to put the uncompressed file */
340 };
341
342 /** A structure representing a LAR file */
343 struct LFILE {
344         struct LAR *lar;           /**< Pointer to the LAR struct */
345         struct lar_header *header; /**< Pointer to the header struct */
346         u32 size;                  /**< Size of the file */
347         void *start;               /**< Start of the file in memory */
348         u32 offset;                /**< Offset of the file in the LAR */
349 };
350
351 struct LAR *openlar(void *addr);
352 int closelar(struct LAR *lar);
353 struct larent *readlar(struct LAR *lar);
354 void rewindlar(struct LAR *lar);
355 int larstat(struct LAR *lar, const char *path, struct larstat *buf);
356 void *larfptr(struct LAR *lar, const char *filename);
357 int lfverify(struct LAR *lar, const char *filename);
358 struct LFILE *lfopen(struct LAR *lar, const char *filename);
359 int lfread(void *ptr, size_t size, size_t nmemb, struct LFILE *stream);
360
361 int lfseek(struct LFILE *stream, long offset, int whence);
362 int lfclose(struct LFILE *file);
363 /** @} */
364 #endif
365
366 /**
367  * @defgroup info System information functions
368  * This module contains functions that return information about the system
369  * @{
370  */
371
372 int sysinfo_have_multiboot(unsigned long *addr);
373 /** @} */
374
375 /**
376  * @defgroup arch Architecture specific functions
377  * This module contains global architecture specific functions.
378  * All architectures are expected to define these functions.
379  * @{
380  */
381 int get_coreboot_info(struct sysinfo_t *info);
382 int get_multiboot_info(struct sysinfo_t *info);
383
384 int lib_get_sysinfo(void);
385
386 /* Timer functions - defined by each architecture. */
387 unsigned int get_cpu_speed(void);
388 void ndelay(unsigned int n);
389 void udelay(unsigned int n);
390 void mdelay(unsigned int n);
391 void delay(unsigned int n);
392
393 /**
394  * @defgroup readline Readline functions
395  * This interface provides a simple implementation of the standard readline()
396  * and getline() functions. They read a line of input from the console.
397  * @{
398  */
399 char *readline(const char *prompt);
400 int getline(char *buffer, int len);
401 /** @} */
402
403 #endif