Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 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 | |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 30 | /** |
| 31 | * @mainpage |
| 32 | * |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 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 |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 41 | */ |
| 42 | |
Uwe Hermann | fad8c2b | 2008-04-11 18:01:50 +0000 | [diff] [blame] | 43 | #ifndef _LIBPAYLOAD_H |
| 44 | #define _LIBPAYLOAD_H |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 45 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 46 | #include <stddef.h> |
| 47 | #include <arch/types.h> |
| 48 | #include <arch/io.h> |
Stefan Reinauer | 99c0856 | 2008-08-19 17:49:53 +0000 | [diff] [blame] | 49 | #include <arch/virtual.h> |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 50 | #include <sysinfo.h> |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 51 | #include <stdarg.h> |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 52 | #include <lar.h> |
Uwe Hermann | 83233d0 | 2008-08-04 21:00:49 +0000 | [diff] [blame] | 53 | #include <pci.h> |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 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 | |
Uwe Hermann | 3995593 | 2008-04-03 23:01:23 +0000 | [diff] [blame] | 59 | #define LITTLE_ENDIAN 1234 |
| 60 | #define BIG_ENDIAN 4321 |
Uwe Hermann | 3995593 | 2008-04-03 23:01:23 +0000 | [diff] [blame] | 61 | |
Uwe Hermann | be504d1 | 2008-04-11 20:16:24 +0000 | [diff] [blame] | 62 | #define EXIT_SUCCESS 0 |
| 63 | #define EXIT_FAILURE 1 |
| 64 | |
Uwe Hermann | 4eb5089 | 2008-04-07 23:33:50 +0000 | [diff] [blame] | 65 | #define RAND_MAX 0x7fffffff |
| 66 | |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 67 | /* |
| 68 | * Payload information parameters - these are used to pass information |
| 69 | * to the entity loading the payload. |
| 70 | * Usage: PAYLOAD_INFO(key, value) |
| 71 | * Example: PAYLOAD_INFO(name, "CoreInfo!") |
Jordan Crouse | fc697ad | 2008-05-27 20:06:54 +0000 | [diff] [blame] | 72 | */ |
Jordan Crouse | fc697ad | 2008-05-27 20:06:54 +0000 | [diff] [blame] | 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 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 79 | /** |
| 80 | * @defgroup nvram NVRAM and RTC functions |
| 81 | * @{ |
| 82 | */ |
Jordan Crouse | e2ad806 | 2008-08-28 23:10:55 +0000 | [diff] [blame] | 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 | |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 93 | /** Broken down time structure */ |
Jordan Crouse | e227143 | 2008-04-25 23:11:02 +0000 | [diff] [blame] | 94 | struct tm { |
Jordan Crouse | e2ad806 | 2008-08-28 23:10:55 +0000 | [diff] [blame] | 95 | int tm_sec; /**< Number of seconds after the minute */ |
| 96 | int tm_min; /**< Number of minutes after the hour */ |
| 97 | int tm_hour; /**< Number of hours past midnight */ |
| 98 | int tm_mday; /**< The day of the month */ |
| 99 | int tm_mon; /**< The month of the year */ |
| 100 | int tm_year; /**< The number of years since 1900 */ |
| 101 | int tm_wday; /**< The day of the week */ |
| 102 | int tm_yday; /**< The number of days since January 1 */ |
| 103 | int tm_isdst; /**< A flag indicating daylight savings time */ |
Jordan Crouse | e227143 | 2008-04-25 23:11:02 +0000 | [diff] [blame] | 104 | }; |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 105 | |
Uwe Hermann | c16d24e | 2008-03-31 15:17:39 +0000 | [diff] [blame] | 106 | u8 nvram_read(u8 addr); |
| 107 | void nvram_write(u8 val, u8 addr); |
Jordan Crouse | e227143 | 2008-04-25 23:11:02 +0000 | [diff] [blame] | 108 | int nvram_updating(void); |
| 109 | void rtc_read_clock(struct tm *tm); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 110 | /** @} */ |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 111 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 112 | /** |
| 113 | * @defgroup input Device functions |
| 114 | * @{ @} |
| 115 | */ |
| 116 | |
| 117 | /** |
| 118 | * @defgroup keyboard Keyboard functions |
| 119 | * @ingroup input |
| 120 | * @{ |
| 121 | */ |
Jordan Crouse | 63f181f | 2008-04-25 23:09:39 +0000 | [diff] [blame] | 122 | void keyboard_init(void); |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 123 | int keyboard_havechar(void); |
| 124 | unsigned char keyboard_get_scancode(void); |
| 125 | int keyboard_getchar(void); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 126 | /** @} */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 127 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 128 | /** |
| 129 | * @defgroup serial Serial functions |
| 130 | * @ingroup input |
| 131 | * @{ |
| 132 | */ |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 133 | void serial_init(void); |
| 134 | void serial_putchar(unsigned char c); |
| 135 | int serial_havechar(void); |
| 136 | int serial_getchar(void); |
Jordan Crouse | 3b47065 | 2008-06-20 00:01:42 +0000 | [diff] [blame] | 137 | void serial_clear(void); |
| 138 | void serial_start_bold(void); |
| 139 | void serial_end_bold(void); |
Ulf Jordan | b4d4bac | 2008-08-11 20:34:28 +0000 | [diff] [blame] | 140 | void serial_start_altcharset(void); |
| 141 | void serial_end_altcharset(void); |
Ulf Jordan | d57a680 | 2008-09-03 19:59:44 +0000 | [diff] [blame^] | 142 | void serial_set_color(short fg, short bg); |
Jordan Crouse | 3b47065 | 2008-06-20 00:01:42 +0000 | [diff] [blame] | 143 | void serial_set_cursor(int y, int x); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 144 | /** @} */ |
Jordan Crouse | 3b47065 | 2008-06-20 00:01:42 +0000 | [diff] [blame] | 145 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 146 | /** |
| 147 | * @defgroup speaker Speaker functions |
| 148 | * @ingroup input |
| 149 | * @{ |
| 150 | */ |
Uwe Hermann | fad8c2b | 2008-04-11 18:01:50 +0000 | [diff] [blame] | 151 | void speaker_enable(u16 freq); |
| 152 | void speaker_disable(void); |
| 153 | void speaker_tone(u16 freq, unsigned int duration); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 154 | /** @} */ |
Uwe Hermann | fad8c2b | 2008-04-11 18:01:50 +0000 | [diff] [blame] | 155 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 156 | /** |
| 157 | * @defgroup video Video functions |
| 158 | * @ingroup input |
| 159 | * @{ |
| 160 | */ |
Jordan Crouse | 30939bd | 2008-04-10 22:49:02 +0000 | [diff] [blame] | 161 | int video_console_init(void); |
| 162 | void video_console_putchar(unsigned int ch); |
| 163 | void video_console_putc(u8 row, u8 col, unsigned int ch); |
| 164 | void video_console_clear(void); |
| 165 | void video_console_cursor_enable(int state); |
Stefan Reinauer | 59fb9a2b | 2008-08-19 17:44:49 +0000 | [diff] [blame] | 166 | void video_console_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en); |
| 167 | void video_console_set_cursor(unsigned int cursorx, unsigned int cursory); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 168 | /** @} */ |
Stefan Reinauer | 59fb9a2b | 2008-08-19 17:44:49 +0000 | [diff] [blame] | 169 | |
| 170 | /* drivers/option.c */ |
| 171 | int get_option(void *dest, char *name); |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 172 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 173 | /** |
| 174 | * @defgroup console Console functions |
| 175 | * @{ |
| 176 | */ |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 177 | void console_init(void); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 178 | int putchar(int c); |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 179 | int puts(const char *s); |
| 180 | int havekey(void); |
| 181 | int getchar(void); |
Jordan Crouse | 672d0ae | 2008-04-08 23:21:33 +0000 | [diff] [blame] | 182 | int getchar_timeout(int *ms); |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 183 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 184 | extern int last_putchar; |
| 185 | |
| 186 | #define havechar havekey |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 187 | /** @} */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 188 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 189 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 190 | * @defgroup ctype Character type functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 191 | * @{ |
| 192 | */ |
Uwe Hermann | 72199bc | 2008-04-11 19:43:55 +0000 | [diff] [blame] | 193 | int isalnum(int c); |
| 194 | int isalpha(int c); |
| 195 | int isascii(int c); |
| 196 | int isblank(int c); |
| 197 | int iscntrl(int c); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 198 | int isdigit(int c); |
Uwe Hermann | 72199bc | 2008-04-11 19:43:55 +0000 | [diff] [blame] | 199 | int isgraph(int c); |
| 200 | int islower(int c); |
| 201 | int isprint(int c); |
| 202 | int ispunct(int c); |
| 203 | int isspace(int c); |
| 204 | int isupper(int c); |
| 205 | int isxdigit(int c); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 206 | int tolower(int c); |
Uwe Hermann | 72199bc | 2008-04-11 19:43:55 +0000 | [diff] [blame] | 207 | int toupper(int c); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 208 | /** @} */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 209 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 210 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 211 | * @defgroup ipchecksum IP checksum functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 212 | * @{ |
| 213 | */ |
Stefan Reinauer | 11e45cd | 2008-08-16 15:16:36 +0000 | [diff] [blame] | 214 | unsigned short ipchksum(const void *ptr, unsigned long nbytes); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 215 | /** @} */ |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 216 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 217 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 218 | * @defgroup malloc Memory allocation functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 219 | * @{ |
| 220 | */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 221 | void free(void *ptr); |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 222 | void *malloc(size_t size); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 223 | void *calloc(size_t nmemb, size_t size); |
| 224 | void *realloc(void *ptr, size_t size); |
Patrick Georgi | 5ccfa1a | 2008-09-02 15:49:32 +0000 | [diff] [blame] | 225 | void *memalign(size_t align, size_t size); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 226 | /** @} */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 227 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 228 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 229 | * @defgroup exec Execution functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 230 | * @{ |
| 231 | */ |
Jordan Crouse | 9dac1b4 | 2008-05-20 20:10:49 +0000 | [diff] [blame] | 232 | int exec(long addr, int argc, char **argv); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 233 | /** @} */ |
Jordan Crouse | 9dac1b4 | 2008-05-20 20:10:49 +0000 | [diff] [blame] | 234 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 235 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 236 | * @defgroup misc Misc functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 237 | * @{ |
| 238 | */ |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 239 | int bcd2dec(int b); |
| 240 | int dec2bcd(int d); |
Uwe Hermann | b103345 | 2008-04-11 18:38:04 +0000 | [diff] [blame] | 241 | int abs(int j); |
| 242 | long int labs(long int j); |
| 243 | long long int llabs(long long int j); |
| 244 | u8 bin2hex(u8 b); |
| 245 | u8 hex2bin(u8 h); |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 246 | void fatal(const char *msg) __attribute__ ((noreturn)); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 247 | /** @} */ |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 248 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 249 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 250 | * @defgroup memory Memory manipulation functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 251 | * @{ |
| 252 | */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 253 | void *memset(void *s, int c, size_t n); |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 254 | void *memcpy(void *dst, const void *src, size_t n); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 255 | void *memmove(void *dst, const void *src, size_t n); |
Ulf Jordan | 6828569 | 2008-08-09 19:34:56 +0000 | [diff] [blame] | 256 | int memcmp(const void *s1, const void *s2, size_t len); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 257 | /** @} */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 258 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 259 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 260 | * @defgroup printf Print functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 261 | * @{ |
| 262 | */ |
Uwe Hermann | 0a89625 | 2008-04-02 12:35:45 +0000 | [diff] [blame] | 263 | int snprintf(char *str, size_t size, const char *fmt, ...); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 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); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 269 | /** @} */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 270 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 271 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 272 | * @defgroup rand Random number generator functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 273 | * @{ |
| 274 | */ |
Uwe Hermann | c5a78ac | 2008-04-08 23:38:15 +0000 | [diff] [blame] | 275 | int rand_r(unsigned int *seed); |
| 276 | int rand(void); |
| 277 | void srand(unsigned int seed); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 278 | /** @} */ |
Uwe Hermann | c5a78ac | 2008-04-08 23:38:15 +0000 | [diff] [blame] | 279 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 280 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 281 | * @defgroup hash Hashing functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 282 | * @{ |
| 283 | */ |
Uwe Hermann | 3995593 | 2008-04-03 23:01:23 +0000 | [diff] [blame] | 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); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 296 | /** @} */ |
Uwe Hermann | 3995593 | 2008-04-03 23:01:23 +0000 | [diff] [blame] | 297 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 298 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 299 | * @defgroup string String functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 300 | * @{ |
| 301 | */ |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 302 | size_t strnlen(const char *str, size_t maxlen); |
| 303 | size_t strlen(const char *str); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 304 | int strcmp(const char *s1, const char *s2); |
Stefan Reinauer | 3b9d1b8 | 2008-08-26 21:51:04 +0000 | [diff] [blame] | 305 | int strncmp(const char *s1, const char *s2, size_t maxlen); |
| 306 | char *strncpy(char *d, const char *s, size_t n); |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 307 | char *strcpy(char *d, const char *s); |
Stefan Reinauer | 3b9d1b8 | 2008-08-26 21:51:04 +0000 | [diff] [blame] | 308 | char *strncat(char *d, const char *s, size_t n); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 309 | char *strchr(const char *s, int c); |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 310 | char *strdup(const char *s); |
| 311 | char *strstr(const char *h, const char *n); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 312 | /** @} */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 313 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 314 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 315 | * @defgroup time Time functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 316 | * @{ |
| 317 | */ |
Jordan Crouse | e2ad806 | 2008-08-28 23:10:55 +0000 | [diff] [blame] | 318 | |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 319 | /** System time structure */ |
Jordan Crouse | e227143 | 2008-04-25 23:11:02 +0000 | [diff] [blame] | 320 | struct timeval { |
Jordan Crouse | e2ad806 | 2008-08-28 23:10:55 +0000 | [diff] [blame] | 321 | time_t tv_sec; /**< Seconds */ |
| 322 | suseconds_t tv_usec; /**< Microseconds */ |
Jordan Crouse | e227143 | 2008-04-25 23:11:02 +0000 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | int gettimeofday(struct timeval *tv, void *tz); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 326 | /** @} */ |
Jordan Crouse | e227143 | 2008-04-25 23:11:02 +0000 | [diff] [blame] | 327 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 328 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 329 | * @defgroup lar LAR functions |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 330 | * @{ |
| 331 | */ |
Jordan Crouse | e2ad806 | 2008-08-28 23:10:55 +0000 | [diff] [blame] | 332 | |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 333 | /** LAR file header */ |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 334 | struct LAR { |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 335 | void *start; /**< Location of the LAR in memory */ |
Jordan Crouse | e2ad806 | 2008-08-28 23:10:55 +0000 | [diff] [blame] | 336 | int cindex; /**< Next file to return in readlar() */ |
| 337 | int count; /**< Number of entries in the header cache */ |
| 338 | int alloc; /**< Number of slots in the header cache */ |
| 339 | int eof; /**< Last entry in the header cache */ |
| 340 | void **headers; /**< Pointer to the header cache */ |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 341 | }; |
| 342 | |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 343 | /** A structure representing the next LAR entry */ |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 344 | struct larent { |
Jordan Crouse | e2ad806 | 2008-08-28 23:10:55 +0000 | [diff] [blame] | 345 | u8 name[LAR_MAX_PATHLEN]; /**< The name of the next LAR entry */ |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 346 | }; |
| 347 | |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 348 | /** A structure containing information about a LAR file */ |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 349 | struct larstat { |
Jordan Crouse | e2ad806 | 2008-08-28 23:10:55 +0000 | [diff] [blame] | 350 | u32 len; /**< Length of the file in the LAR */ |
| 351 | u32 reallen; /**< Length of the uncompressed file */ |
| 352 | u32 checksum; /**< Checksum of the uncompressed file */ |
| 353 | u32 compchecksum; /**< Checksum of the compressed file in the LAR */ |
| 354 | u32 offset; /**< Offset of the file in the LAR */ |
| 355 | u32 compression; /**< Compression type of the file */ |
| 356 | u64 entry; /**< Entry point of the file for executables */ |
| 357 | u64 loadaddress; /**< Address in memory to put the uncompressed file */ |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 358 | }; |
| 359 | |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 360 | /** A structure representing a LAR file */ |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 361 | struct LFILE { |
Jordan Crouse | e2ad806 | 2008-08-28 23:10:55 +0000 | [diff] [blame] | 362 | struct LAR *lar; /**< Pointer to the LAR struct */ |
| 363 | struct lar_header *header; /**< Pointer to the header struct */ |
| 364 | u32 size; /**< Size of the file */ |
| 365 | void *start; /**< Start of the file in memory */ |
| 366 | u32 offset; /**< Offset of the file in the LAR */ |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 367 | }; |
| 368 | |
| 369 | struct LAR *openlar(void *addr); |
| 370 | int closelar(struct LAR *lar); |
| 371 | struct larent *readlar(struct LAR *lar); |
| 372 | void rewindlar(struct LAR *lar); |
| 373 | int larstat(struct LAR *lar, const char *path, struct larstat *buf); |
Jordan Crouse | 5069808 | 2008-05-20 20:09:42 +0000 | [diff] [blame] | 374 | void *larfptr(struct LAR *lar, const char *filename); |
Jordan Crouse | 9e734c2 | 2008-05-27 19:57:53 +0000 | [diff] [blame] | 375 | int lfverify(struct LAR *lar, const char *filename); |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 376 | struct LFILE *lfopen(struct LAR *lar, const char *filename); |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 377 | int lfread(void *ptr, size_t size, size_t nmemb, struct LFILE *stream); |
| 378 | |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 379 | #define SEEK_SET 0 /**< The seek offset is absolute. */ |
| 380 | #define SEEK_CUR 1 /**< The seek offset is against the current position. */ |
| 381 | #define SEEK_END 2 /**< The seek offset is against the end of the file. */ |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 382 | |
| 383 | int lfseek(struct LFILE *stream, long offset, int whence); |
| 384 | int lfclose(struct LFILE *file); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 385 | /** @} */ |
Jordan Crouse | 681ec27 | 2008-05-07 20:34:02 +0000 | [diff] [blame] | 386 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 387 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 388 | * @defgroup arch Architecture specific functions |
| 389 | * This module contains global architecture specific functions. |
Jordan Crouse | b746178 | 2008-08-28 23:12:22 +0000 | [diff] [blame] | 390 | * All architectures are expected to define these functions. |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 391 | * @{ |
| 392 | */ |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 393 | int get_coreboot_info(struct sysinfo_t *info); |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 394 | |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 395 | void lib_get_sysinfo(void); |
| 396 | |
Uwe Hermann | 661e380 | 2008-03-21 18:37:23 +0000 | [diff] [blame] | 397 | /* Timer functions - defined by each architecture. */ |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 398 | unsigned int get_cpu_speed(void); |
| 399 | void ndelay(unsigned int n); |
Jordan Crouse | 234e87f | 2008-04-10 22:50:44 +0000 | [diff] [blame] | 400 | void udelay(unsigned int n); |
Uwe Hermann | dc69e05 | 2008-03-20 01:53:30 +0000 | [diff] [blame] | 401 | void mdelay(unsigned int n); |
| 402 | void delay(unsigned int n); |
| 403 | |
Jordan Crouse | b746178 | 2008-08-28 23:12:22 +0000 | [diff] [blame] | 404 | #define abort() halt() /**< Alias for the halt() function */ |
| 405 | |
| 406 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 407 | * Stop execution and halt the processor (this function does not return). |
Jordan Crouse | b746178 | 2008-08-28 23:12:22 +0000 | [diff] [blame] | 408 | */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 409 | void halt(void) __attribute__ ((noreturn)); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 410 | /** @} */ |
Stefan Reinauer | 6e51cee | 2008-08-19 17:47:18 +0000 | [diff] [blame] | 411 | |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 412 | /** |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 413 | * @defgroup readline Readline functions |
| 414 | * This interface provides a simple implementation of the standard readline() |
| 415 | * and getline() functions. They read a line of input from the console. |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 416 | * @{ |
| 417 | */ |
Uwe Hermann | 3153863 | 2008-08-31 22:10:35 +0000 | [diff] [blame] | 418 | char *readline(const char *prompt); |
Jakob Bornecrantz | b0d3e71 | 2008-08-23 12:17:46 +0000 | [diff] [blame] | 419 | int getline(char *buffer, int len); |
Jordan Crouse | 3a48bdc | 2008-08-28 16:53:24 +0000 | [diff] [blame] | 420 | /** @} */ |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 421 | |
Jordan Crouse | f6145c3 | 2008-03-19 23:56:58 +0000 | [diff] [blame] | 422 | #endif |