blob: 19ba671a5ad5e9d97979dea7f9a4d18d27c34021 [file] [log] [blame]
Jordan Crousef6145c32008-03-19 23:56:58 +00001/*
Jordan Crousef6145c32008-03-19 23:56:58 +00002 *
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Uwe Hermann31538632008-08-31 22:10:35 +000029/**
30 * @mainpage
31 *
Jordan Crouse3a48bdc2008-08-28 16:53:24 +000032 * @section intro Introduction
33 * libpayload is a small BSD-licensed static library (a lightweight
34 * implementation of common and useful functions) intended to be used
35 * as a basis for coreboot payloads.
36 *
37 * @section example Example
38 * Here is an example of a very simple payload:
39 * @include sample/hello.c
Jordan Crouse3a48bdc2008-08-28 16:53:24 +000040 */
41
Uwe Hermannfad8c2b2008-04-11 18:01:50 +000042#ifndef _LIBPAYLOAD_H
43#define _LIBPAYLOAD_H
Jordan Crousef6145c32008-03-19 23:56:58 +000044
Nico Huberd9e543a2020-10-24 17:19:15 +020045#include <stdbool.h>
Stefan Reinauere5d30b72010-03-25 22:15:19 +000046#include <libpayload-config.h>
Daisuke Nojiri3f663982015-07-29 16:03:52 -070047#include <cbgfx.h>
Hsuan Ting Chen607b39c2022-03-25 17:23:21 +080048#include <commonlib/bsd/elog.h>
Jakub Czapiga8fac6622021-11-12 13:45:29 +000049#include <commonlib/bsd/fmap_serialized.h>
Jakub Czapiga97602642022-03-18 14:13:29 +010050#include <commonlib/bsd/helpers.h>
Ravi Kumar Bokka42fcb2a2021-11-10 05:22:47 +053051#include <commonlib/bsd/mem_chip_info.h>
Patrick Georgi980a69b2010-06-24 11:16:10 +000052#include <ctype.h>
Gabe Black7c7b5ff2013-11-23 00:38:49 -080053#include <die.h>
Julius Werner8a1d11f2014-07-17 10:43:15 -070054#include <endian.h>
Jakub Czapiga8fac6622021-11-12 13:45:29 +000055#include <fmap.h>
Gabe Black54c800a2012-08-28 16:31:09 -070056#include <ipchksum.h>
Daisuke Nojirie0a8a882015-06-29 13:33:34 -070057#include <kconfig.h>
Jordan Crousef6145c32008-03-19 23:56:58 +000058#include <stddef.h>
Patrick Georgi980a69b2010-06-24 11:16:10 +000059#include <stdio.h>
60#include <stdarg.h>
61#include <stdlib.h>
62#include <string.h>
Stef van Osc0124282016-04-20 15:22:00 +020063#include <time.h>
Nico Huber1653cc72018-12-10 15:10:58 +010064#include <sys/types.h>
Jordan Crousef6145c32008-03-19 23:56:58 +000065#include <arch/types.h>
66#include <arch/io.h>
Stefan Reinauer99c08562008-08-19 17:49:53 +000067#include <arch/virtual.h>
Uwe Hermanndc69e052008-03-20 01:53:30 +000068#include <sysinfo.h>
Uwe Hermann83233d02008-08-04 21:00:49 +000069#include <pci.h>
Daisuke Nojiri6d2c7222015-11-05 10:05:38 -080070#include <archive.h>
Thomas Heijligen303a8952022-11-29 19:53:31 +010071#include <delay.h>
Jordan Crousef6145c32008-03-19 23:56:58 +000072
satya priya5c44c4a2019-11-25 15:10:08 +053073#define BIT(x) (1ul << (x))
Jordan Crousef6145c32008-03-19 23:56:58 +000074
Julius Werner6df355d2015-07-09 15:47:07 -070075static inline u32 div_round_up(u32 n, u32 d) { return (n + d - 1) / d; }
Julius Werner68bdd002015-05-22 18:18:46 -070076
Uwe Hermann39955932008-04-03 23:01:23 +000077#define LITTLE_ENDIAN 1234
78#define BIG_ENDIAN 4321
Uwe Hermann39955932008-04-03 23:01:23 +000079
Uwe Hermannbe504d12008-04-11 20:16:24 +000080#define EXIT_SUCCESS 0
81#define EXIT_FAILURE 1
82
Uwe Hermann4eb50892008-04-07 23:33:50 +000083#define RAND_MAX 0x7fffffff
84
Jeremy Compostellabf618cb2016-11-18 13:40:32 +010085#define MAX_ARGC_COUNT 32
Jordan Crouse20c9cf12008-10-20 16:51:43 +000086
Uwe Hermann31538632008-08-31 22:10:35 +000087/*
88 * Payload information parameters - these are used to pass information
89 * to the entity loading the payload.
90 * Usage: PAYLOAD_INFO(key, value)
91 * Example: PAYLOAD_INFO(name, "CoreInfo!")
Jordan Crousefc697ad2008-05-27 20:06:54 +000092 */
Jordan Crousefc697ad2008-05-27 20:06:54 +000093#define _pstruct(key) __pinfo_ ##key
94#define PAYLOAD_INFO(key, value) \
95static const char _pstruct(key)[] \
96 __attribute__((__used__)) \
97 __attribute__((section(".note.pinfo"),unused)) = #key "=" value
98
Jordan Crouse3a48bdc2008-08-28 16:53:24 +000099/**
100 * @defgroup nvram NVRAM and RTC functions
101 * @{
102 */
Jordan Crousee2ad8062008-08-28 23:10:55 +0000103
104#define NVRAM_RTC_SECONDS 0 /**< RTC Seconds offset in CMOS */
105#define NVRAM_RTC_MINUTES 2 /**< RTC Minutes offset in CMOS */
106#define NVRAM_RTC_HOURS 4 /**< RTC Hours offset in CMOS */
107#define NVRAM_RTC_DAY 7 /**< RTC Days offset in CMOS */
108#define NVRAM_RTC_MONTH 8 /**< RTC Month offset in CMOS */
109#define NVRAM_RTC_YEAR 9 /**< RTC Year offset in CMOS */
110#define NVRAM_RTC_FREQ_SELECT 10 /**< RTC Update Status Register */
111#define NVRAM_RTC_UIP 0x80
Patrick Rudolph9f3e7342017-02-25 09:56:53 +0100112#define NVRAM_RTC_STATUSB 11 /**< RTC Status Register B */
113#define NVRAM_RTC_FORMAT_24HOUR 0x02
114#define NVRAM_RTC_FORMAT_BINARY 0x04
Jordan Crousee2ad8062008-08-28 23:10:55 +0000115
Uwe Hermann31538632008-08-31 22:10:35 +0000116/** Broken down time structure */
Jordan Crousee2271432008-04-25 23:11:02 +0000117struct tm {
Jordan Crousee2ad8062008-08-28 23:10:55 +0000118 int tm_sec; /**< Number of seconds after the minute */
119 int tm_min; /**< Number of minutes after the hour */
120 int tm_hour; /**< Number of hours past midnight */
121 int tm_mday; /**< The day of the month */
122 int tm_mon; /**< The month of the year */
123 int tm_year; /**< The number of years since 1900 */
124 int tm_wday; /**< The day of the week */
125 int tm_yday; /**< The number of days since January 1 */
126 int tm_isdst; /**< A flag indicating daylight savings time */
Jordan Crousee2271432008-04-25 23:11:02 +0000127};
Uwe Hermann8cc38d22008-03-27 23:26:40 +0000128
Uwe Hermannc16d24e2008-03-31 15:17:39 +0000129u8 nvram_read(u8 addr);
130void nvram_write(u8 val, u8 addr);
Jordan Crousee2271432008-04-25 23:11:02 +0000131int nvram_updating(void);
132void rtc_read_clock(struct tm *tm);
Patrick Rudolph77e0baa2018-02-03 11:50:56 +0100133void rtc_write_clock(const struct tm *tm);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000134/** @} */
Uwe Hermann8cc38d22008-03-27 23:26:40 +0000135
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000136/**
Nico Huber1f6bd942012-08-30 15:36:57 +0200137 * @defgroup storage driver functions
138 * @{
139 */
140void storage_initialize(void);
141/** @} */
142
143/**
Stefan Reinauer41514392008-09-26 18:42:40 +0000144 * @defgroup usb USB functions
145 * @{
146 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000147int usb_initialize(void);
Patrick Georgibbc52312011-11-04 12:06:06 +0100148int usb_exit (void);
Patrick Georgi4727c072008-10-16 19:20:51 +0000149int usbhid_havechar(void);
150int usbhid_getchar(void);
Patrick Rudolph1f5ebf72017-03-06 18:37:00 +0100151int usbhid_getmodifiers(void);
Stefan Reinauer41514392008-09-26 18:42:40 +0000152/** @} */
153
154/**
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000155 * @defgroup input Device functions
156 * @{ @}
157 */
158
Stefan Reinauer1beabe12010-03-25 18:52:24 +0000159extern void (*reset_handler)(void);
160int add_reset_handler(void (*new_handler)(void));
161
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000162/**
163 * @defgroup keyboard Keyboard functions
164 * @ingroup input
165 * @{
166 */
Jordan Crouse63f181f2008-04-25 23:09:39 +0000167void keyboard_init(void);
Martin Roth3ee59f72013-07-26 16:31:21 -0600168void keyboard_disconnect(void);
Nico Huberd9e543a2020-10-24 17:19:15 +0200169bool keyboard_havechar(void);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000170unsigned char keyboard_get_scancode(void);
171int keyboard_getchar(void);
Stefan Reinauerd84ef1e2008-09-26 18:37:26 +0000172int keyboard_set_layout(char *country);
Patrick Rudolphae2cb2d2017-03-05 17:29:18 +0100173int keyboard_getmodifier(void);
Thejaswani Putta3557f122019-11-05 17:51:58 -0800174void initialize_keyboard_media_key_mapping_callback(int (*media_key_mapper)(char));
Patrick Rudolphae2cb2d2017-03-05 17:29:18 +0100175
176enum KEYBOARD_MODIFIERS {
177 KB_MOD_SHIFT = (1 << 0),
178 KB_MOD_ALT = (1 << 1),
179 KB_MOD_CTRL = (1 << 2),
180 KB_MOD_CAPSLOCK = (1 << 3),
181};
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000182/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000183
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000184/**
Patrick Rudolph837da6a2017-02-06 15:02:25 +0100185 * @defgroup mouse Mouse cursor functions
186 * @ingroup input
187 * @{
188 */
189void mouse_cursor_poll(void);
190void mouse_cursor_get_rel(int *x, int *y, int *z);
191u32 mouse_cursor_get_buttons(void);
192void mouse_cursor_set_speed(u32 val);
193u32 mouse_cursor_get_speed(void);
194void mouse_cursor_set_acceleration(u8 val);
195u8 mouse_cursor_get_acceleration(void);
196/** @} */
197
198/**
Patrick Rudolphe6a38212017-03-01 19:07:37 +0100199 * @defgroup i8042 controller functions
200 * @ingroup input
201 * @{
202 */
203size_t i8042_has_ps2(void);
204size_t i8042_has_aux(void);
205
206u8 i8042_probe(void);
207void i8042_close(void);
208
209int i8042_cmd(u8 cmd);
210void i8042_write_data(u8 data);
211
212u8 i8042_data_ready_ps2(void);
213u8 i8042_data_ready_aux(void);
214
215u8 i8042_read_data_ps2(void);
Nico Huber4f7b6872020-11-08 23:22:32 +0100216u8 i8042_peek_data_ps2(void);
Patrick Rudolphe6a38212017-03-01 19:07:37 +0100217u8 i8042_read_data_aux(void);
218
219int i8042_wait_read_ps2(void);
220int i8042_wait_read_aux(void);
221
Nico Huber0e1d19b2020-11-08 14:04:39 +0100222int i8042_get_kbd_translation(void);
223int i8042_set_kbd_translation(bool xlate);
224
Patrick Rudolphe6a38212017-03-01 19:07:37 +0100225/** @} */
226
227/**
Patrick Rudolph5afc2932017-02-06 15:26:58 +0100228 * @defgroup i8042 PS2 Mouse functions
229 * @ingroup input
230 * @{
231 */
232void i8042_mouse_init(void);
233void i8042_mouse_disconnect(void);
234/** @} */
235
236/**
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000237 * @defgroup serial Serial functions
238 * @ingroup input
239 * @{
240 */
Uwe Hermanndc69e052008-03-20 01:53:30 +0000241void serial_init(void);
Gabe Black9ed8a822013-12-03 21:25:35 -0800242void serial_console_init(void);
Patrick Georgi657a6dc2008-10-21 15:08:18 +0000243void serial_putchar(unsigned int c);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000244int serial_havechar(void);
245int serial_getchar(void);
Jordan Crouse3b470652008-06-20 00:01:42 +0000246void serial_clear(void);
247void serial_start_bold(void);
248void serial_end_bold(void);
Stefan Reinauere75c3d82008-09-26 18:36:26 +0000249void serial_start_reverse(void);
250void serial_end_reverse(void);
Ulf Jordanb4d4bac2008-08-11 20:34:28 +0000251void serial_start_altcharset(void);
252void serial_end_altcharset(void);
Ulf Jordand57a6802008-09-03 19:59:44 +0000253void serial_set_color(short fg, short bg);
Stefan Reinauere75c3d82008-09-26 18:36:26 +0000254void serial_cursor_enable(int state);
Jordan Crouse3b470652008-06-20 00:01:42 +0000255void serial_set_cursor(int y, int x);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000256/** @} */
Jordan Crouse3b470652008-06-20 00:01:42 +0000257
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000258/**
259 * @defgroup speaker Speaker functions
260 * @ingroup input
261 * @{
262 */
Uwe Hermannfad8c2b2008-04-11 18:01:50 +0000263void speaker_enable(u16 freq);
264void speaker_disable(void);
265void speaker_tone(u16 freq, unsigned int duration);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000266/** @} */
Uwe Hermannfad8c2b2008-04-11 18:01:50 +0000267
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000268/**
269 * @defgroup video Video functions
270 * @ingroup input
271 * @{
272 */
Gabe Blackdd9e4e52012-10-05 23:35:04 -0700273int video_init(void);
Jordan Crouse30939bd2008-04-10 22:49:02 +0000274int video_console_init(void);
Gabe Black8e7d7fd2012-10-05 23:43:37 -0700275void video_get_rows_cols(unsigned int *rows, unsigned int *cols);
Jordan Crouse30939bd2008-04-10 22:49:02 +0000276void video_console_putchar(unsigned int ch);
277void video_console_putc(u8 row, u8 col, unsigned int ch);
278void video_console_clear(void);
279void video_console_cursor_enable(int state);
Stefan Reinauer59fb9a2b2008-08-19 17:44:49 +0000280void video_console_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en);
281void video_console_set_cursor(unsigned int cursorx, unsigned int cursory);
Daisuke Nojiriabe03d22015-07-31 15:22:58 -0700282/*
283 * print characters on video console with colors. note that there is a size
284 * restriction for the internal buffer. so, output string can be truncated.
285 */
Daisuke Nojiriccda4462015-08-12 18:49:50 -0700286enum video_printf_align {
287 VIDEO_PRINTF_ALIGN_KEEP = 0,
288 VIDEO_PRINTF_ALIGN_LEFT,
289 VIDEO_PRINTF_ALIGN_CENTER,
290 VIDEO_PRINTF_ALIGN_RIGHT,
291};
292void video_printf(int foreground, int background, enum video_printf_align align,
293 const char *fmt, ...);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000294/** @} */
Stefan Reinauer59fb9a2b2008-08-19 17:44:49 +0000295
Gabe Blacka54b6a62012-09-29 00:21:27 -0700296/**
297 * @defgroup cbmem_console CBMEM memory console.
298 * @ingroup input
299 * @{
300 */
301void cbmem_console_init(void);
Julius Werner2fe505b2014-04-17 20:00:20 -0700302void cbmem_console_write(const void *buffer, size_t count);
Yu-Ping Wu41956b52019-12-02 11:11:53 +0800303/**
304 * Take a snapshot of the CBMEM memory console. This function will allocate a
305 * range of memory. Callers must free the returned buffer by themselves.
306 *
307 * @return The allocated buffer on success, NULL on failure.
308 */
309char *cbmem_console_snapshot(void);
Gabe Blacka54b6a62012-09-29 00:21:27 -0700310/** @} */
311
Stefan Reinauer59fb9a2b2008-08-19 17:44:49 +0000312/* drivers/option.c */
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100313struct nvram_accessor {
314 u8 (*read)(u8 reg);
315 void (*write)(u8 val, u8 reg);
316};
317
Patrick Georgi5febb002012-02-02 15:51:29 +0100318extern u8 *mem_accessor_base;
319extern struct nvram_accessor *use_nvram, *use_mem;
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100320
321struct cb_cmos_option_table *get_system_option_table(void);
Patrick Georgi56f468d2012-01-16 15:03:11 +0100322int options_checksum_valid(const struct nvram_accessor *nvram);
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100323void fix_options_checksum_with(const struct nvram_accessor *nvram);
Stefan Reinauer59a1b7f2010-08-17 10:14:50 +0000324void fix_options_checksum(void);
Patrick Georgi08ed5a82012-09-24 20:06:27 +0200325
Patrick Georgi0a595112012-01-16 15:39:57 +0100326struct cb_cmos_entries *first_cmos_entry(struct cb_cmos_option_table *option_table);
327struct cb_cmos_entries *next_cmos_entry(struct cb_cmos_entries *cur);
Patrick Georgi08ed5a82012-09-24 20:06:27 +0200328
329struct cb_cmos_enums *first_cmos_enum(struct cb_cmos_option_table *option_table);
330struct cb_cmos_enums *next_cmos_enum(struct cb_cmos_enums *cmos_enum);
331struct cb_cmos_enums *first_cmos_enum_of_id(struct cb_cmos_option_table *option_table, int id);
332struct cb_cmos_enums *next_cmos_enum_of_id(struct cb_cmos_enums *cmos_enum, int id);
333
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100334int get_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *dest, const char *name);
335int get_option_from(struct cb_cmos_option_table *option_table, void *dest, const char *name);
336int get_option(void *dest, const char *name);
337int set_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, const void *value, const char *name);
338int set_option(const void *value, const char *name);
339int get_option_as_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, char **dest, const char *name);
340int set_option_from_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, const char *value, const char *name);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000341
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000342/**
343 * @defgroup console Console functions
344 * @{
345 */
Luigi Semenzato562db3b2014-01-13 17:45:54 -0800346typedef enum {
347 CONSOLE_INPUT_TYPE_UNKNOWN = 0,
348 CONSOLE_INPUT_TYPE_USB,
Matt Delcoa20e59d2019-04-22 13:38:13 -0700349 CONSOLE_INPUT_TYPE_EC,
350 CONSOLE_INPUT_TYPE_UART,
351 CONSOLE_INPUT_TYPE_GPIO,
Luigi Semenzato562db3b2014-01-13 17:45:54 -0800352} console_input_type;
353
Uwe Hermanndc69e052008-03-20 01:53:30 +0000354void console_init(void);
Julius Werner2fe505b2014-04-17 20:00:20 -0700355void console_write(const void *buffer, size_t count);
Patrick Georgi657a6dc2008-10-21 15:08:18 +0000356int putchar(unsigned int c);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000357int puts(const char *s);
358int havekey(void);
359int getchar(void);
Jordan Crouse672d0ae2008-04-08 23:21:33 +0000360int getchar_timeout(int *ms);
Luigi Semenzato562db3b2014-01-13 17:45:54 -0800361console_input_type last_key_input_type(void);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000362
Jordan Crousef6145c32008-03-19 23:56:58 +0000363extern int last_putchar;
364
Patrick Georgi657a6dc2008-10-21 15:08:18 +0000365struct console_input_driver;
366struct console_input_driver {
367 struct console_input_driver *next;
368 int (*havekey) (void);
369 int (*getchar) (void);
Luigi Semenzato562db3b2014-01-13 17:45:54 -0800370 console_input_type input_type;
Patrick Georgi657a6dc2008-10-21 15:08:18 +0000371};
372
373struct console_output_driver;
374struct console_output_driver {
375 struct console_output_driver *next;
376 void (*putchar) (unsigned int);
Julius Werner2fe505b2014-04-17 20:00:20 -0700377 void (*write) (const void *, size_t);
Patrick Georgi657a6dc2008-10-21 15:08:18 +0000378};
379
380void console_add_output_driver(struct console_output_driver *out);
381void console_add_input_driver(struct console_input_driver *in);
Julius Werner43e10302014-06-02 20:13:51 -0700382int console_remove_output_driver(void *function);
Patrick Georgi657a6dc2008-10-21 15:08:18 +0000383
Jordan Crousef6145c32008-03-19 23:56:58 +0000384#define havechar havekey
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000385/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000386
Patrick Rudolph837da6a2017-02-06 15:02:25 +0100387/**
388 * @defgroup mouse_cursor Mouse cursor functions
389 * @{
390 */
391typedef enum {
392 CURSOR_INPUT_TYPE_UNKNOWN = 0,
393 CURSOR_INPUT_TYPE_USB,
394 CURSOR_INPUT_TYPE_PS2,
395} cursor_input_type;
396
397void mouse_cursor_init(void);
398
399struct mouse_cursor_input_driver;
400struct mouse_cursor_input_driver {
401 struct mouse_cursor_input_driver *next;
402 /* X,Y,Z axis and buttons */
403 void (*get_state)(int *, int *, int *, u32 *);
404 cursor_input_type input_type;
405};
406
407void mouse_cursor_add_input_driver(struct mouse_cursor_input_driver *in);
408
409/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000410
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000411/**
Uwe Hermann31538632008-08-31 22:10:35 +0000412 * @defgroup exec Execution functions
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000413 * @{
414 */
Jordan Crouse9dac1b42008-05-20 20:10:49 +0000415int exec(long addr, int argc, char **argv);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000416/** @} */
Jordan Crouse9dac1b42008-05-20 20:10:49 +0000417
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000418/**
Uwe Hermann31538632008-08-31 22:10:35 +0000419 * @defgroup misc Misc functions
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000420 * @{
421 */
Uwe Hermann8cc38d22008-03-27 23:26:40 +0000422int bcd2dec(int b);
423int dec2bcd(int d);
Uwe Hermannb1033452008-04-11 18:38:04 +0000424u8 bin2hex(u8 b);
425u8 hex2bin(u8 h);
Paul Menzelccf53af2014-03-24 00:42:36 +0100426void hexdump(const void *memory, size_t length);
Stefan Reinauer6a001132017-07-13 02:20:27 +0200427void fatal(const char *msg) __attribute__((noreturn));
Julius Werner7a8a4ab2015-05-22 16:26:40 -0700428
Angel Ponse0ce60c2020-10-14 17:48:05 +0200429/* Population Count: number of bits that are one */
430static inline int popcnt(u32 x) { return __builtin_popcount(x); }
Julius Werner7a8a4ab2015-05-22 16:26:40 -0700431/* Count Leading Zeroes: clz(0) == 32, clz(0xf) == 28, clz(1 << 31) == 0 */
Patrick Georgid8cd2e92019-04-04 18:07:52 +0200432static inline int clz(u32 x)
433{
434 return x ? __builtin_clz(x) : (int)sizeof(x) * 8;
435}
Julius Werner7a8a4ab2015-05-22 16:26:40 -0700436/* Integer binary logarithm (rounding down): log2(0) == -1, log2(5) == 2 */
Patrick Georgid8cd2e92019-04-04 18:07:52 +0200437static inline int log2(u32 x) { return (int)sizeof(x) * 8 - clz(x) - 1; }
Julius Werner7a8a4ab2015-05-22 16:26:40 -0700438/* Find First Set: __ffs(0xf) == 0, __ffs(0) == -1, __ffs(1 << 31) == 31 */
439static inline int __ffs(u32 x) { return log2(x & (u32)(-(s32)x)); }
Jianjun Wang8bb59ca2021-11-30 10:51:53 +0800440/* Find Last Set: __fls(1) == 0, __fls(5) == 2, __fls(1 << 31) == 31 */
441static inline int __fls(u32 x) { return log2(x); }
Tim Wawrzynczak7ded1af2020-10-01 15:36:42 -0600442
Angel Ponse0ce60c2020-10-14 17:48:05 +0200443static inline int popcnt64(u64 x) { return __builtin_popcountll(x); }
Tim Wawrzynczak7ded1af2020-10-01 15:36:42 -0600444static inline int clz64(u64 x)
445{
446 return x ? __builtin_clzll(x) : sizeof(x) * 8;
447}
448
449static inline int log2_64(u64 x) { return sizeof(x) * 8 - clz64(x) - 1; }
450static inline int __ffs64(u64 x) { return log2_64(x & (u64)(-(s64)x)); }
Jianjun Wang8bb59ca2021-11-30 10:51:53 +0800451static inline int __fls64(u64 x) { return log2_64(x); }
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000452/** @} */
Uwe Hermann8cc38d22008-03-27 23:26:40 +0000453
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000454/**
Julius Wernerdb7f6fb2019-08-12 16:45:21 -0700455 * @defgroup mmio MMIO helper functions
456 * @{
457 */
Julius Wernerdb7f6fb2019-08-12 16:45:21 -0700458void buffer_from_fifo32(void *buffer, size_t size, void *fifo,
459 int fifo_stride, int fifo_width);
Julius Wernerea03d002021-09-16 15:53:32 -0700460void buffer_to_fifo32_prefix(const void *buffer, u32 prefix, int prefsz, size_t size,
Julius Wernerdb7f6fb2019-08-12 16:45:21 -0700461 void *fifo, int fifo_stride, int fifo_width);
Julius Wernerea03d002021-09-16 15:53:32 -0700462static inline void buffer_to_fifo32(const void *buffer, size_t size, void *fifo,
Julius Wernerdb7f6fb2019-08-12 16:45:21 -0700463 int fifo_stride, int fifo_width)
464{
Julius Werner9f19dd92019-11-18 18:21:27 -0800465 buffer_to_fifo32_prefix(buffer, 0, 0, size, fifo,
Julius Wernerdb7f6fb2019-08-12 16:45:21 -0700466 fifo_stride, fifo_width);
467}
Julius Wernerdb7f6fb2019-08-12 16:45:21 -0700468/** @} */
469
Julius Wernerdb7f6fb2019-08-12 16:45:21 -0700470/**
Uwe Hermann31538632008-08-31 22:10:35 +0000471 * @defgroup hash Hashing functions
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000472 * @{
473 */
Uwe Hermann39955932008-04-03 23:01:23 +0000474#define SHA1_BLOCK_LENGTH 64
475#define SHA1_DIGEST_LENGTH 20
476typedef struct {
477 u32 state[5];
478 u64 count;
479 u8 buffer[SHA1_BLOCK_LENGTH];
480} SHA1_CTX;
481void SHA1Init(SHA1_CTX *context);
482void SHA1Transform(u32 state[5], const u8 buffer[SHA1_BLOCK_LENGTH]);
483void SHA1Update(SHA1_CTX *context, const u8 *data, size_t len);
Patrick Georgi7f965832011-04-21 18:57:16 +0200484void SHA1Pad(SHA1_CTX *context);
Uwe Hermann39955932008-04-03 23:01:23 +0000485void SHA1Final(u8 digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
486u8 *sha1(const u8 *data, size_t len, u8 *buf);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000487/** @} */
Uwe Hermann39955932008-04-03 23:01:23 +0000488
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000489/**
Jordan Crouse6c6e4332008-11-11 19:51:14 +0000490 * @defgroup info System information functions
491 * This module contains functions that return information about the system
492 * @{
493 */
494
495int sysinfo_have_multiboot(unsigned long *addr);
496/** @} */
497
498/**
Uwe Hermann31538632008-08-31 22:10:35 +0000499 * @defgroup arch Architecture specific functions
500 * This module contains global architecture specific functions.
Jordan Crouseb7461782008-08-28 23:12:22 +0000501 * All architectures are expected to define these functions.
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000502 * @{
503 */
Uwe Hermanndc69e052008-03-20 01:53:30 +0000504int get_coreboot_info(struct sysinfo_t *info);
Jordan Crouse20c9cf12008-10-20 16:51:43 +0000505int get_multiboot_info(struct sysinfo_t *info);
Furquan Shaikh943d6232014-09-11 14:20:35 -0700506void *get_cb_header_ptr(void);
Jordan Crousef6145c32008-03-19 23:56:58 +0000507
Philip Prindeville44bf6fc2011-12-23 18:33:05 -0700508int lib_get_sysinfo(void);
Furquan Shaikh8e159632014-09-04 15:21:12 -0700509void lib_sysinfo_get_memranges(struct memrange **ranges,
510 uint64_t *nranges);
Uwe Hermanndc69e052008-03-20 01:53:30 +0000511
Gabe Black5c0b7ab2013-02-22 16:38:53 -0800512/* Timer functions. */
513/* Defined by each architecture. */
Gabe Black5c0b7ab2013-02-22 16:38:53 -0800514uint64_t timer_hz(void);
515uint64_t timer_raw_value(void);
Gabe Black125a6a22013-12-06 23:30:10 -0800516uint64_t timer_us(uint64_t base);
Gabe Black5c0b7ab2013-02-22 16:38:53 -0800517/* Generic. */
Raul E Rangeld63627f2018-08-20 12:47:19 -0600518
519/**
Uwe Hermann31538632008-08-31 22:10:35 +0000520 * @defgroup readline Readline functions
521 * This interface provides a simple implementation of the standard readline()
522 * and getline() functions. They read a line of input from the console.
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000523 * @{
524 */
Uwe Hermann31538632008-08-31 22:10:35 +0000525char *readline(const char *prompt);
Jakob Bornecrantzb0d3e712008-08-23 12:17:46 +0000526int getline(char *buffer, int len);
Jordan Crouse3a48bdc2008-08-28 16:53:24 +0000527/** @} */
Jordan Crousef6145c32008-03-19 23:56:58 +0000528
Vadim Bendebury66fdbce2014-05-23 14:37:10 -0700529/* Defined in arch/${ARCH}/selfboot.c */
530void selfboot(void *entry);
531
Patrick Georgi89f73dc2015-07-09 13:57:00 +0200532/* look for area "name" in "fmap", setting offset and size to describe it.
533 Returns 0 on success, < 0 on error. */
534int fmap_region_by_name(const uint32_t fmap_offset, const char * const name,
535 uint32_t * const offset, uint32_t * const size);
Jordan Crousef6145c32008-03-19 23:56:58 +0000536#endif