diff options
Diffstat (limited to 'payloads')
22 files changed, 349 insertions, 402 deletions
diff --git a/payloads/libpayload/curses/colors.c b/payloads/libpayload/curses/colors.c index 045df83ba3..34091498c0 100644 --- a/payloads/libpayload/curses/colors.c +++ b/payloads/libpayload/curses/colors.c @@ -36,7 +36,8 @@ unsigned char color_pairs[256] = { [0] = 0x07, }; -int start_color(void) { +int start_color(void) +{ return 0; } diff --git a/payloads/libpayload/curses/keyboard.c b/payloads/libpayload/curses/keyboard.c index 6ab5dd0cf7..3ec4d9d7bf 100644 --- a/payloads/libpayload/curses/keyboard.c +++ b/payloads/libpayload/curses/keyboard.c @@ -174,45 +174,38 @@ struct { static int cook_scancodes(unsigned char code) { static int modifiers = 0; - int ch = 0, sc, shift; + int ch = 0, sc, shift; - switch(code) { + switch (code) { case DOWN(SCANCODE_RSHIFT): case DOWN(SCANCODE_LSHIFT): modifiers |= SHIFT_MODIFIER; return 0; - case UP(SCANCODE_RSHIFT): case UP(SCANCODE_LSHIFT): modifiers &= ~SHIFT_MODIFIER; return 0; - case UP(SCANCODE_CAPSLOCK): if (modifiers & CAPSLOCK_MODIFIER) modifiers &= ~CAPSLOCK_MODIFIER; else modifiers |= CAPSLOCK_MODIFIER; return 0; - case DOWN(SCANCODE_LALT): modifiers |= ALT_MODIFIER; return 0; - case UP(SCANCODE_LALT): modifiers &= ~ALT_MODIFIER; return 0; - case DOWN(SCANCODE_LCTRL): modifiers |= CTRL_MODIFIER; return 0; - case UP(SCANCODE_LCTRL): modifiers &= ~CTRL_MODIFIER; return 0; } - /* Only process keys on an upstroke */ - + /* Only process keys on an upstroke. */ if (!ISUP(code)) return 0; @@ -231,8 +224,8 @@ static int cook_scancodes(unsigned char code) return ch; } -static int curses_getchar(int delay) { - +static int curses_getchar(int delay) +{ unsigned char c = 0; int ret; @@ -243,7 +236,7 @@ static int curses_getchar(int delay) { if ((c & 1) == 0) { if ((curses_flags & F_ENABLE_SERIAL) && - serial_havechar()) { + serial_havechar()) { c = serial_getchar(); return cook_serial(c); } @@ -258,9 +251,9 @@ static int curses_getchar(int delay) { if (ret != 0) { return ret; - } + } - } while(1); + } while (1); return ERR; } diff --git a/payloads/libpayload/curses/tinycurses.c b/payloads/libpayload/curses/tinycurses.c index dc6b77818b..946cdeb18b 100644 --- a/payloads/libpayload/curses/tinycurses.c +++ b/payloads/libpayload/curses/tinycurses.c @@ -150,20 +150,22 @@ WINDOW *derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx) int flags = _SUBWIN; /* Make sure window fits inside the original one. */ - if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0) - return NULL; + if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 + || num_columns < 0) + return NULL; + if (begy + num_lines > orig->_maxy + 1 || begx + num_columns > orig->_maxx + 1) - return NULL; + return NULL; if (num_lines == 0) - num_lines = orig->_maxy + 1 - begy; + num_lines = orig->_maxy + 1 - begy; if (num_columns == 0) - num_columns = orig->_maxx + 1 - begx; + num_columns = orig->_maxx + 1 - begx; if (orig->_flags & _ISPAD) - flags |= _ISPAD; + flags |= _ISPAD; //// if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy, //// orig->_begx + begx, flags)) == 0) @@ -175,7 +177,7 @@ WINDOW *derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx) win->_nc_bkgd = orig->_nc_bkgd; for (i = 0; i < num_lines; i++) - win->_line[i].text = &orig->_line[begy++].text[begx]; + win->_line[i].text = &orig->_line[begy++].text[begx]; win->_parent = orig; @@ -217,11 +219,9 @@ WINDOW *initscr(void) // def_prog_mode(); if (curses_flags & F_ENABLE_CONSOLE) { - - /* Clear the screen and kill the cursor */ - - vga_clear(); - vga_cursor_enable(0); + /* Clear the screen and kill the cursor. */ + vga_clear(); + vga_cursor_enable(0); } // Speaker init? @@ -313,7 +313,8 @@ WINDOW *newwin(int num_lines, int num_columns, int begy, int begx) win->_attrs = A_NORMAL; for (i = 0; i < num_lines; i++) - win->_line[i].text = (NCURSES_CH_T *)&linebuf_list[linebuf_count++]; + win->_line[i].text = + (NCURSES_CH_T *)&linebuf_list[linebuf_count++]; return win; } @@ -479,7 +480,7 @@ int wclrtoeol(WINDOW *win) { /* TODO */ return(*(int *)0); } int wcolor_set(WINDOW *win, short color_pair_number, void *opts) { if (!opts && (color_pair_number >= 0) - && (color_pair_number < COLOR_PAIRS)) { + && (color_pair_number < COLOR_PAIRS)) { SET_WINDOW_PAIR(win, color_pair_number); if_EXT_COLORS(win->_color = color_pair_number); return OK; @@ -511,7 +512,7 @@ int whline(WINDOW *win, chtype ch, int n) start = win->_curx; end = start + n - 1; if (end > win->_maxx) - end = win->_maxx; + end = win->_maxx; CHANGED_RANGE(line, start, end); @@ -526,8 +527,8 @@ int whline(WINDOW *win, chtype ch, int n) wch = _nc_render(win, wch); while (end >= start) { - line->text[end] = wch; - end--; + line->text[end] = wch; + end--; } //// _nc_synchook(win); @@ -570,25 +571,21 @@ int wnoutrefresh(WINDOW *win) if (curses_flags & F_ENABLE_CONSOLE) { attr_t attr = win->_line[y].text[x].attr; - unsigned int c = ((int) color_pairs[PAIR_NUMBER(attr)]) << 8; - - /* Handle some of the attributes */ + unsigned int c = + ((int)color_pairs[PAIR_NUMBER(attr)]) << 8; - if (attr & A_BOLD) { + /* Handle some of the attributes. */ + if (attr & A_BOLD) c |= 0x0800; - } - if (attr & A_DIM) { + if (attr & A_DIM) c &= ~0x800; - } if (attr & A_REVERSE) { - unsigned char tmp = (c >> 8) & 0xF; - c = (c >> 4) & 0xF00; + unsigned char tmp = (c >> 8) & 0xf; + c = (c >> 4) & 0xf00; c |= tmp << 12; } c |= win->_line[y].text[x].chars[0]; - - vga_putc(y, x, c); } } diff --git a/payloads/libpayload/drivers/keyboard.c b/payloads/libpayload/drivers/keyboard.c index 0dc93713b8..9a3e4b8d19 100644 --- a/payloads/libpayload/drivers/keyboard.c +++ b/payloads/libpayload/drivers/keyboard.c @@ -31,26 +31,26 @@ #include <libpayload.h> unsigned char map[2][0x57] = { - { - 0x00, 0x1B, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, - 0x37, 0x38, 0x39, 0x30, 0x2D, 0x3D, 0x08, 0x09, - 0x71, 0x77, 0x65, 0x72, 0x74, 0x79, 0x75, 0x69, - 0x6F, 0x70, 0x5B, 0x5D, 0x0A, 0x00, 0x61, 0x73, - 0x64, 0x66, 0x67, 0x68, 0x6A, 0x6B, 0x6C, 0x3B, - 0x27, 0x60, 0x00, 0x5C, 0x7A, 0x78, 0x63, 0x76, - 0x62, 0x6E, 0x6D, 0x2C, 0x2E, 0x2F, 0x00, 0x2A, - 0x00, 0x20, - }, - { - 0x00, 0x1B, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5E, - 0x26, 0x2A, 0x28, 0x29, 0x5F, 0x2B, 0x08, 0x00, - 0x51, 0x57, 0x45, 0x52, 0x54, 0x59, 0x55, 0x49, - 0x4F, 0x50, 0x7B, 0x7D, 0x0A, 0x00, 0x41, 0x53, - 0x44, 0x46, 0x47, 0x48, 0x4A, 0x4B, 0x4C, 0x3A, - 0x22, 0x7E, 0x00, 0x7C, 0x5A, 0x58, 0x43, 0x56, - 0x42, 0x4E, 0x4D, 0x3C, 0x3E, 0x3F, 0x00, 0x2A, - 0x00, 0x20, - } + { + 0x00, 0x1B, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, + 0x37, 0x38, 0x39, 0x30, 0x2D, 0x3D, 0x08, 0x09, + 0x71, 0x77, 0x65, 0x72, 0x74, 0x79, 0x75, 0x69, + 0x6F, 0x70, 0x5B, 0x5D, 0x0A, 0x00, 0x61, 0x73, + 0x64, 0x66, 0x67, 0x68, 0x6A, 0x6B, 0x6C, 0x3B, + 0x27, 0x60, 0x00, 0x5C, 0x7A, 0x78, 0x63, 0x76, + 0x62, 0x6E, 0x6D, 0x2C, 0x2E, 0x2F, 0x00, 0x2A, + 0x00, 0x20, + }, + { + 0x00, 0x1B, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5E, + 0x26, 0x2A, 0x28, 0x29, 0x5F, 0x2B, 0x08, 0x00, + 0x51, 0x57, 0x45, 0x52, 0x54, 0x59, 0x55, 0x49, + 0x4F, 0x50, 0x7B, 0x7D, 0x0A, 0x00, 0x41, 0x53, + 0x44, 0x46, 0x47, 0x48, 0x4A, 0x4B, 0x4C, 0x3A, + 0x22, 0x7E, 0x00, 0x7C, 0x5A, 0x58, 0x43, 0x56, + 0x42, 0x4E, 0x4D, 0x3C, 0x3E, 0x3F, 0x00, 0x2A, + 0x00, 0x20, + } }; #define MOD_SHIFT 1 @@ -78,32 +78,26 @@ int keyboard_getchar(void) static int modifier; unsigned char ch; int shift; - int ret = 0; - while(!keyboard_havechar()) - ; + while (!keyboard_havechar()) ; ch = keyboard_get_scancode(); - switch(ch) { + switch (ch) { case 0x36: case 0x2a: modifier &= ~MOD_SHIFT; break; - case 0x80 | 0x36: case 0x80 | 0x2a: modifier |= MOD_SHIFT; - case 0x1d: modifier &= ~MOD_CTRL; break; - case 0x80 | 0x1d: modifier |= MOD_CTRL; break; - case 0x3a: if (modifier & MOD_CAPSLOCK) modifier &= ~MOD_CAPSLOCK; @@ -112,12 +106,13 @@ int keyboard_getchar(void) break; } - if (!(ch & 0x80) && ch < 0x57) { - shift = (modifier & MOD_SHIFT) ^ (modifier & MOD_CAPSLOCK) ? 1 : 0; + if (!(ch & 0x80) && ch < 0x57) { + shift = + (modifier & MOD_SHIFT) ^ (modifier & MOD_CAPSLOCK) ? 1 : 0; ret = map[shift][ch]; if (modifier & MOD_CTRL) - ret = (ret >= 0x3F && ret <= 0x5F) ? ret & 0x1f : 0; + ret = (ret >= 0x3F && ret <= 0x5F) ? ret & 0x1f : 0; return ret; } diff --git a/payloads/libpayload/drivers/serial.c b/payloads/libpayload/drivers/serial.c index bcf60df614..b3179c756d 100644 --- a/payloads/libpayload/drivers/serial.c +++ b/payloads/libpayload/drivers/serial.c @@ -37,32 +37,32 @@ #endif void serial_init(void) -{ +{ #ifdef CONFIG_SERIAL_SET_SPEED unsigned char reg; - /* Disable interrupts */ + /* Disable interrupts. */ outb(0, IOBASE + 0x01); - /* Assert RTS and DTR */ + /* Assert RTS and DTR. */ outb(3, IOBASE + 0x04); - /* Set the divisor latch */ + /* Set the divisor latch. */ reg = inb(IOBASE + 0x03); outb(reg | 0x80, IOBASE + 0x03); - /* Write the divisor */ + /* Write the divisor. */ outb(DIVISOR & 0xFF, IOBASE); outb(DIVISOR >> 8 & 0xFF, IOBASE + 1); - /* Restore the previous value of the divisor */ + /* Restore the previous value of the divisor. */ outb(reg &= ~0x80, IOBASE + 0x03); #endif } - + void serial_putchar(unsigned char c) { - while((inb(IOBASE + 0x05) & 0x20) == 0); + while ((inb(IOBASE + 0x05) & 0x20) == 0) ; outb(c, IOBASE); } @@ -73,6 +73,6 @@ int serial_havechar(void) int serial_getchar(void) { - while (!serial_havechar()); - return (int) inb(IOBASE); + while (!serial_havechar()) ; + return (int)inb(IOBASE); } diff --git a/payloads/libpayload/drivers/vga.c b/payloads/libpayload/drivers/vga.c index 5e0e406f27..fd1dd35abb 100644 --- a/payloads/libpayload/drivers/vga.c +++ b/payloads/libpayload/drivers/vga.c @@ -38,8 +38,8 @@ #define CRTC_INDEX 0x3d4 #define CRTC_DATA 0x3d5 -#define VIDEO(_r, _c)\ - ((uint16_t *) (0xB8000 + ((_r) * (WIDTH * 2)) + ((_c) * 2))) +#define VIDEO(_r, _c) \ + ((uint16_t *) (0xB8000 + ((_r) * (WIDTH * 2)) + ((_c) * 2))) static int cursor_enabled; static int cursorx; @@ -49,24 +49,25 @@ static void vga_scroll_up(void); static inline uint8_t crtc_read(uint8_t index) { - outb(index, CRTC_INDEX); - return inb(CRTC_DATA); + outb(index, CRTC_INDEX); + return inb(CRTC_DATA); } static inline void crtc_write(uint8_t data, uint8_t index) { - outb(index, CRTC_INDEX); - outb(data, CRTC_DATA); + outb(index, CRTC_INDEX); + outb(data, CRTC_DATA); } static void vga_get_cursor_pos(void) { unsigned int addr; - addr = ((unsigned int) crtc_read(0x0E)) << 8; + + addr = ((unsigned int)crtc_read(0x0E)) << 8; addr += crtc_read(0x0E); cursorx = addr % WIDTH; - cursory = addr / WIDTH; + cursory = addr / WIDTH; } static void vga_fixup_cursor(void) @@ -78,16 +79,16 @@ static void vga_fixup_cursor(void) if (cursorx < 0) cursorx = 0; - + if (cursory < 0) cursory = 0; - + if (cursorx >= WIDTH) { cursorx = 0; cursory++; } - while(cursory >= HEIGHT) + while (cursory >= HEIGHT) vga_scroll_up(); addr = cursorx + (WIDTH * cursory); @@ -102,8 +103,7 @@ void vga_cursor_enable(int state) if (state == 0) { tmp |= (1 << 5); cursor_enabled = 0; - } - else { + } else { tmp &= ~(1 << 5); cursor_enabled = 1; vga_fixup_cursor(); @@ -117,19 +117,19 @@ void vga_clear_line(uint8_t row, uint8_t ch, uint8_t attr) int col; uint16_t *ptr = VIDEO(0, row); - for(col = 0; col < WIDTH; col++) + for (col = 0; col < WIDTH; col++) ptr[col] = ((attr & 0xFF) << 8) | (ch & 0xFF); } - + static void vga_scroll_up(void) { - uint16_t *src = VIDEO(0,1); - uint16_t *dst = VIDEO(0,0); + uint16_t *src = VIDEO(0, 1); + uint16_t *dst = VIDEO(0, 0); int i; - for(i = 0; i < (HEIGHT - 1) * WIDTH; i++) + for (i = 0; i < (HEIGHT - 1) * WIDTH; i++) *dst++ = *src++; - + vga_clear_line(HEIGHT - 1, ' ', VGA_COLOR_WHITE); cursory--; } @@ -137,7 +137,7 @@ static void vga_scroll_up(void) void vga_fill(uint8_t ch, uint8_t attr) { uint8_t row; - for(row = 0; row < HEIGHT; row++) + for (row = 0; row < HEIGHT; row++) vga_clear_line(row, ch, attr); } @@ -153,11 +153,12 @@ void vga_putc(uint8_t row, uint8_t col, unsigned int c) *ptr = (uint16_t) (c & 0xFFFF); } -void vga_putchar(unsigned int ch) { - +void vga_putchar(unsigned int ch) +{ + uint16_t *ptr; - switch(ch & 0xFF) { + switch (ch & 0xFF) { case '\r': cursorx = 0; break; @@ -187,30 +188,25 @@ int vga_move_cursor(int x, int y) { cursorx = x; cursory = y; - + vga_fixup_cursor(); } void vga_init(void) { - /* Get the position of the cursor */ + /* Get the position of the cursor. */ vga_get_cursor_pos(); - /* See if it us currently enabled or not */ + /* See if it currently enabled or not. */ cursor_enabled = !(crtc_read(0x0A) & (1 << 5)); - /* If the cursor is enabled, get us to a sane point */ - + /* If the cursor is enabled, get us to a sane point. */ if (cursor_enabled) { - - /* Go to the next line */ - + /* Go to the next line. */ if (cursorx) { cursorx = 0; cursory++; } - vga_fixup_cursor(); } } - diff --git a/payloads/libpayload/i386/coreboot.c b/payloads/libpayload/i386/coreboot.c index e5d02319a7..0eb76dcf4b 100644 --- a/payloads/libpayload/i386/coreboot.c +++ b/payloads/libpayload/i386/coreboot.c @@ -31,20 +31,19 @@ #include <sysinfo.h> #include <coreboot_tables.h> -/* Some of this is x86 specific, and the rest of it - is generic. Right now, since we only support x86, - we'll avoid trying to make lots of infrastructure - we don't need. If in the future, we want to use - coreboot on some other architecture, then take out - the generic parsing code and move it elsewhere -*/ +/* + * Some of this is x86 specific, and the rest of it is generic. Right now, + * since we only support x86, we'll avoid trying to make lots of infrastructure + * we don't need. If in the future, we want to use coreboot on some other + * architecture, then take out the generic parsing code and move it elsewhere. + */ /* === Parsing code === */ -/* This is the generic parsing code */ +/* This is the generic parsing code. */ static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info) { - struct cb_memory *mem = (struct cb_memory *) ptr; + struct cb_memory *mem = (struct cb_memory *)ptr; int count = MEM_RANGE_COUNT(mem); int i; @@ -53,18 +52,18 @@ static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info) info->n_memranges = 0; - for(i = 0; i < count; i++) { - struct cb_memory_range *range = - (struct cb_memory_range *) MEM_RANGE_PTR(mem, i); - + for (i = 0; i < count; i++) { + struct cb_memory_range *range = + (struct cb_memory_range *)MEM_RANGE_PTR(mem, i); + if (range->type != CB_MEM_RAM) continue; info->memrange[info->n_memranges].base = - UNPACK_CB64(range->start); + UNPACK_CB64(range->start); info->memrange[info->n_memranges].size = - UNPACK_CB64(range->size); + UNPACK_CB64(range->size); info->n_memranges++; } @@ -72,55 +71,48 @@ static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info) static void cb_parse_serial(unsigned char *ptr, struct sysinfo_t *info) { - struct cb_serial *ser = (struct cb_serial *) ptr; + struct cb_serial *ser = (struct cb_serial *)ptr; info->ser_ioport = ser->ioport; } static int cb_parse_header(void *addr, int len, struct sysinfo_t *info) { struct cb_header *header; - unsigned char *ptr = (unsigned char *) addr; + unsigned char *ptr = (unsigned char *)addr; int i; for (i = 0; i < len; i += 16, ptr += 16) { - header = (struct cb_header *) ptr; - + header = (struct cb_header *)ptr; if (!strncmp(header->signature, "LBIO", 4)) break; } - - /* We walked the entire space and didn't find anything */ + /* We walked the entire space and didn't find anything. */ if (i >= len) return -1; if (!header->table_bytes) return 0; - /* Make sure the checksums match */ - + /* Make sure the checksums match. */ if (ipchksum((uint16_t *) header, sizeof(*header)) != 0) return -1; if (ipchksum((uint16_t *) (ptr + sizeof(*header)), - header->table_bytes) != header->table_checksum) + header->table_bytes) != header->table_checksum) return -1; - /* Now, walk the tables */ + /* Now, walk the tables. */ ptr += header->header_bytes; - - for(i = 0; i < header->table_entries; i++) { - struct cb_record *rec = (struct cb_record *) ptr; - /* We only care about a few tags here - maybe - more will be interesting later - */ + for (i = 0; i < header->table_entries; i++) { + struct cb_record *rec = (struct cb_record *)ptr; - switch(rec->tag) { + /* We only care about a few tags here (maybe more later). */ + switch (rec->tag) { case CB_TAG_MEMORY: cb_parse_memory(ptr, info); break; - case CB_TAG_SERIAL: cb_parse_serial(ptr, info); break; @@ -128,19 +120,19 @@ static int cb_parse_header(void *addr, int len, struct sysinfo_t *info) ptr += rec->size; } - + return 1; } -/* == Architecture specific ==*/ -/* This is the x86 specific stuff */ +/* == Architecture specific == */ +/* This is the x86 specific stuff. */ int get_coreboot_info(struct sysinfo_t *info) { - int ret = cb_parse_header((void *) 0x0, 0x1000, info); + int ret = cb_parse_header((void *)0x0, 0x1000, info); if (ret != 1) - ret = cb_parse_header((void *) 0xf0000, 0x1000, info); + ret = cb_parse_header((void *)0xf0000, 0x1000, info); return (ret == 1) ? 0 : -1; } diff --git a/payloads/libpayload/i386/head.S b/payloads/libpayload/i386/head.S index 1278bdf4ed..e05cb3ed30 100644 --- a/payloads/libpayload/i386/head.S +++ b/payloads/libpayload/i386/head.S @@ -26,60 +26,55 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ - + .global _entry, _leave .text .align 4 -/* Our entry point - assume that the CPU is in - * 32 bit protected mode and all segments are in a - * flat model. Thats our operating mode, so we won't - * change anything +/* + * Our entry point - assume that the CPU is in 32 bit protected mode and + * all segments are in a flat model. That's our operating mode, so we won't + * change anything. */ - _entry: call _init - /* We're back - go back to the bootloader */ + /* We're back - go back to the bootloader. */ ret -/* This function saves off the previous stack and - switches us to our own execution enviornment -*/ - +/* + * This function saves off the previous stack and switches us to our + * own execution environment. + */ _init: - /* No interrupts, please */ + /* No interrupts, please. */ cli - /* Get the current stack pointer */ + /* Get the current stack pointer. */ movl %esp, %esi movl _istack, %ebx - /* lret needs %cs in the stack, so copy it over */ + /* lret needs %cs in the stack, so copy it over. */ movw %cs, 4(%ebx) - /* Exchange the current stack pointer for the one in - the initial stack (which happens to be the new - stack pointer) */ - + /* + * Exchange the current stack pointer for the one in the initial + * stack (which happens to be the new stack pointer). + */ xchgl %esi, 16(%ebx) - /* Set the new stack pointer */ + /* Set the new stack pointer. */ movl %esi, %esp - /* Return into the main entry function - and go - */ - + /* Return into the main entry function and go. */ lret _leave: movl _istack, %ebx - /* Restore the stack pointer from the storage area */ + /* Restore the stack pointer from the storage area. */ movl 16(%ebx), %esp - /* Return to the original context */ + /* Return to the original context. */ lret - diff --git a/payloads/libpayload/i386/main.c b/payloads/libpayload/i386/main.c index 846d46a426..ed5c1b9e9c 100644 --- a/payloads/libpayload/i386/main.c +++ b/payloads/libpayload/i386/main.c @@ -29,14 +29,12 @@ #include <arch/types.h> -/* This structure seeds the stack. We provide - the return address of our main function, and - further down, the address of the function - that we call when we leave and try to restore - the original stack. At the very bottom of the - stack we store the orignal stack pointer - from the calling application -*/ +/* + * This structure seeds the stack. We provide the return address of our main + * function, and further down, the address of the function that we call when + * we leave and try to restore the original stack. At the very bottom of the + * stack we store the orignal stack pointer from the calling application. + */ static void start_main(void); extern void _leave(void); @@ -45,35 +43,40 @@ static struct { uint32_t eip[2]; uint32_t raddr[2]; uint32_t esp; -} initial_stack __attribute__((section (".istack"))) = { +} initial_stack __attribute__ ((section(".istack"))) = { { (uint32_t) start_main, 0 }, { (uint32_t) _leave, 0 }, - (uint32_t) &initial_stack, + (uint32_t) & initial_stack, }; -void * _istack = &initial_stack; - -/* This is our C entry function - set up the system - and jump into the payload entry point */ +void *_istack = &initial_stack; +/** + * This is our C entry function - set up the system + * and jump into the payload entry point. + */ static void start_main(void) { extern int main(void); - /* Set up the consoles */ + /* Set up the consoles. */ console_init(); - /* Gather system information */ + /* Gather system information. */ lib_get_sysinfo(); - /* Any other system init that has to happen before the - user gets control goes here. */ + /* + * Any other system init that has to happen before the + * user gets control goes here. + */ - /* Go to the entry point */ + /* Go to the entry point. */ - /* in the future we may care about the return value */ + /* In the future we may care about the return value. */ (void) main(); - /* Returning here will go to the _leave function to return - us to the original context */ + /* + * Returning here will go to the _leave function to return + * us to the original context. + */ } diff --git a/payloads/libpayload/i386/sysinfo.c b/payloads/libpayload/i386/sysinfo.c index 1fdf3fc419..cb149ead3a 100644 --- a/payloads/libpayload/i386/sysinfo.c +++ b/payloads/libpayload/i386/sysinfo.c @@ -30,41 +30,29 @@ #include <libpayload.h> #include <sysinfo.h> -/* This is a global structure that is used through the - library - we set it up initially with some dummy - values - hopefully they will be overridden -*/ - +/** + * This is a global structure that is used through the library - we set it + * up initially with some dummy values - hopefully they will be overridden. + */ struct sysinfo_t lib_sysinfo = { - . cpu_khz = 200, - . ser_ioport = CONFIG_SERIAL_IOBASE, + .cpu_khz = 200, + .ser_ioport = CONFIG_SERIAL_IOBASE, }; - + void lib_get_sysinfo(void) { - /* Get the CPU speed (for delays) */ + /* Get the CPU speed (for delays). */ lib_sysinfo.cpu_khz = get_cpu_speed(); - - /* Get the memory information */ + /* Get the memory information. */ get_coreboot_info(&lib_sysinfo); if (!lib_sysinfo.n_memranges) { - - /* If we couldn't get a good memory range, - then use a hard coded default */ - + /* If we can't get a good memory range, use the default. */ lib_sysinfo.n_memranges = 2; - lib_sysinfo.memrange[0].base = 0; lib_sysinfo.memrange[0].size = 640 * 1024; lib_sysinfo.memrange[1].base = 1024 * 1024; lib_sysinfo.memrange[1].size = 31 * 1024 * 1024; } } - - - - - - diff --git a/payloads/libpayload/i386/timer.c b/payloads/libpayload/i386/timer.c index 852cbe6c03..a787646073 100644 --- a/payloads/libpayload/i386/timer.c +++ b/payloads/libpayload/i386/timer.c @@ -32,53 +32,47 @@ static unsigned int cpu_khz; -/* Calculate the speed of the processor for use in delays */ - +/** + * Calculate the speed of the processor for use in delays. + * + * @return The CPU speed in kHz. + */ unsigned int get_cpu_speed(void) { unsigned long long start, end; - /* Set up the PPC port - disable the speaker, - * enable the T2 gate */ - + /* Set up the PPC port - disable the speaker, enable the T2 gate. */ outb((inb(0x61) & ~0x02) | 0x01, 0x61); - /* Set the PIT to Mode 0, counter 2, word access */ + /* Set the PIT to Mode 0, counter 2, word access. */ outb(0xB0, 0x43); - /* Load the counter with 0xFFFF */ - - outb(0xFF, 0x42); - outb(0xFF, 0x42); - - /* Read the number of ticks during the period */ + /* Load the counter with 0xffff. */ + outb(0xff, 0x42); + outb(0xff, 0x42); + /* Read the number of ticks during the period. */ start = rdtsc(); - while(!(inb(0x61) & 0x20)); + while (!(inb(0x61) & 0x20)) ; end = rdtsc(); - /* The clock rate is 1193180 Hz - * the number of miliseconds for a period - * of 0xFFFF is 1193180 / (0xFFFF * 1000) - * or .0182. Multiply that by the number of - * measured clocks to get the khz value - */ - - cpu_khz = - (unsigned int ) ((end - start) * 1193180U / (1000 * 0xFFFF)); + /* + * The clock rate is 1193180 Hz, the number of miliseconds for a + * period of 0xffff is 1193180 / (0xFFFF * 1000) or .0182. + * Multiply that by the number of measured clocks to get the kHz value. + */ + cpu_khz = (unsigned int)((end - start) * 1193180U / (1000 * 0xffff)); } -/* Global delay functions */ - static inline void _delay(unsigned int delta) { unsigned long long timeout = rdtsc() + delta; - while (rdtsc() < timeout); + while (rdtsc() < timeout) ; } void ndelay(unsigned int n) { - _delay(n * cpu_khz / 1000000); + _delay(n * cpu_khz / 1000000); } void mdelay(unsigned int m) diff --git a/payloads/libpayload/i386/util.S b/payloads/libpayload/i386/util.S index a0b1b0980d..9858d29b05 100644 --- a/payloads/libpayload/i386/util.S +++ b/payloads/libpayload/i386/util.S @@ -26,12 +26,12 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ - .global halt + + .global halt .text .align 4 - -/* This function puts the system into a halt. */ +/* This function puts the system into a halt. */ halt: cli hlt diff --git a/payloads/libpayload/include/coreboot_tables.h b/payloads/libpayload/include/coreboot_tables.h index 1542157ca7..f156383862 100644 --- a/payloads/libpayload/include/coreboot_tables.h +++ b/payloads/libpayload/include/coreboot_tables.h @@ -123,21 +123,21 @@ struct cb_console { #define CB_TAG_CONSOLE_SERIAL8250 0 #define CB_TAG_CONSOLE_VGA 1 -#define CB_TAG_CONSOLE_BTEXT 2 +#define CB_TAG_CONSOLE_BTEXT 2 #define CB_TAG_CONSOLE_LOGBUF 3 #define CB_TAG_CONSOLE_SROM 4 #define CB_TAG_CONSOLE_EHCI 5 -/* Still to come: CMOS information */ +/* Still to come: CMOS information. */ /* Helpful macros */ #define MEM_RANGE_COUNT(_rec) \ - (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0])) + (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0])) #define MEM_RANGE_PTR(_rec, _idx) \ - (((uint8_t *) (_rec)) + sizeof(*(_rec)) \ - + (sizeof((_rec)->map[0]) * (_idx))) + (((uint8_t *) (_rec)) + sizeof(*(_rec)) \ + + (sizeof((_rec)->map[0]) * (_idx))) #define MB_VENDOR_STRING(_mb) \ (((unsigned char *) ((_mb)->strings)) + (_mb)->vendor_idx) diff --git a/payloads/libpayload/include/sysinfo.h b/payloads/libpayload/include/sysinfo.h index ff39765b76..50d331a043 100644 --- a/payloads/libpayload/include/sysinfo.h +++ b/payloads/libpayload/include/sysinfo.h @@ -27,11 +27,10 @@ * SUCH DAMAGE. */ -#ifndef _SYSINFO_H_ -#define _SYSINFO_H_ - -/* Allow a maximum of 16 memory range definitions */ +#ifndef SYSINFO_H +#define SYSINFO_H +/* Allow a maximum of 16 memory range definitions. */ #define SYSINFO_MAX_MEM_RANGES 16 struct sysinfo_t { diff --git a/payloads/libpayload/libc/console.c b/payloads/libpayload/libc/console.c index bb7dec1f94..b2b115b835 100644 --- a/payloads/libpayload/libc/console.c +++ b/payloads/libpayload/libc/console.c @@ -42,7 +42,7 @@ void console_init(void) static void device_putchar(unsigned char c) { #ifdef CONFIG_VGA_CONSOLE - vga_putchar(0x700| c); + vga_putchar(0x700 | c); #endif #ifdef CONFIG_SERIAL_CONSOLE serial_putchar(c); @@ -65,10 +65,10 @@ int puts(const char *s) while (*s) { putchar(*s++); n++; - } - + } + putchar('\n'); - return n+1; + return n + 1; } int havekey(void) @@ -81,13 +81,13 @@ int havekey(void) if (keyboard_havechar()) return 1; #endif - return 0; + return 0; } -/* This returns an ascii value - the two getchar functions - cook the respective input from the device -*/ - +/** + * This returns an ASCII value - the two getchar functions + * cook the respective input from the device. + */ int getchar(void) { while (1) { @@ -101,4 +101,3 @@ int getchar(void) #endif } } - diff --git a/payloads/libpayload/libc/ctype.c b/payloads/libpayload/libc/ctype.c index c2f42a52d2..9853fceb59 100644 --- a/payloads/libpayload/libc/ctype.c +++ b/payloads/libpayload/libc/ctype.c @@ -27,34 +27,36 @@ * SUCH DAMAGE. */ -/* Basic ctype functions */ - #include <libpayload.h> int isspace(int c) { - switch (c) { - case ' ': case '\f': case '\n': - case '\r': case '\t': case '\v': - return 1; - default: - return 0; - } + switch (c) { + case ' ': + case '\f': + case '\n': + case '\r': + case '\t': + case '\v': + return 1; + default: + return 0; + } } int isdigit(int c) { - switch (c) { - case '0'...'9': - return 1; - default: - return 0; - } + switch (c) { + case '0'...'9': + return 1; + default: + return 0; + } } int tolower(int c) { - if (c >= 'A' && c <= 'Z') - return c - 'A' + 'a'; - return c; + if (c >= 'A' && c <= 'Z') + return c - 'A' + 'a'; + return c; } diff --git a/payloads/libpayload/libc/ipchecksum.c b/payloads/libpayload/libc/ipchecksum.c index caba6aaf34..4f712068e5 100644 --- a/payloads/libpayload/libc/ipchecksum.c +++ b/payloads/libpayload/libc/ipchecksum.c @@ -1,5 +1,7 @@ /* - * This file is part of the libpayload project + * This file is part of the libpayload project. + * + * It has orginally been taken from the FreeBSD project. * * Copyright (c) 2001 Charles Mott <cm@linktel.net> * All rights reserved. @@ -30,20 +32,20 @@ unsigned short ipchksum(const unsigned short *ptr, unsigned long nbytes) { - int sum, oddbyte; + int sum, oddbyte; - sum = 0; - while (nbytes > 1) { - sum += *ptr++; - nbytes -= 2; - } - if (nbytes == 1) { - oddbyte = 0; - ((u8 *) &oddbyte)[0] = *(u8 *)ptr; - ((u8 *) &oddbyte)[1] = 0; - sum += oddbyte; - } - sum = (sum >> 16) + (sum & 0xffff); - sum += (sum >> 16); - return (~sum); + sum = 0; + while (nbytes > 1) { + sum += *ptr++; + nbytes -= 2; + } + if (nbytes == 1) { + oddbyte = 0; + ((u8 *) & oddbyte)[0] = *(u8 *) ptr; + ((u8 *) & oddbyte)[1] = 0; + sum += oddbyte; + } + sum = (sum >> 16) + (sum & 0xffff); + sum += (sum >> 16); + return (~sum); } diff --git a/payloads/libpayload/libc/malloc.c b/payloads/libpayload/libc/malloc.c index a4877e67d9..86fb0b13cd 100644 --- a/payloads/libpayload/libc/malloc.c +++ b/payloads/libpayload/libc/malloc.c @@ -27,24 +27,23 @@ * SUCH DAMAGE. */ -/* This is a classically weak malloc() implmentation. - We have a relatively small and static heap, so we take - the easy route with an O(N) loop through the tree for - every malloc() and free(). Obviously, this doesn't scale - past a few hundred K (if that). - - We're also susecptable to the usual buffer overun poisoning, - though the risk is within acceptable ranges for this - implementation (don't overrun your buffers, kids!) -*/ - +/* + * This is a classically weak malloc() implmentation. We have a relatively + * small and static heap, so we take the easy route with an O(N) loop + * through the tree for every malloc() and free(). Obviously, this doesn't + * scale past a few hundred KB (if that). + * + * We're also susecptable to the usual buffer overun poisoning, though the + * risk is within acceptable ranges for this implementation (don't overrun + * your buffers, kids!). + */ + #include <libpayload.h> -/* Defined in the ldscript */ -extern char _heap, _eheap; +extern char _heap, _eheap; /* Defined in the ldscript. */ -static void *hstart = (void *) &_heap; -static void *hend = (void *) &_eheap; +static void *hstart = (void *)&_heap; +static void *hend = (void *)&_eheap; typedef unsigned int hdrtype_t; @@ -66,66 +65,64 @@ typedef unsigned int hdrtype_t; void print_malloc_map(void); -static void setup(void) +static void setup(void) { - int size = (unsigned int) (_heap - _eheap) - HDRSIZE; + int size = (unsigned int)(_heap - _eheap) - HDRSIZE; *((hdrtype_t *) hstart) = FREE_BLOCK(size); } - + static void *alloc(int len) { hdrtype_t header; void *ptr = hstart; - - /* align the size */ + + /* Align the size. */ len = (len + 3) & ~3; - if (!len || len > 0xFFFFFF) - return (void *) NULL; + if (!len || len > 0xffffff) + return (void *)NULL; - /* Make sure the region is setup correctly */ + /* Make sure the region is setup correctly. */ if (!HAS_MAGIC(*((hdrtype_t *) ptr))) setup(); - /* Find some free space */ - + /* Find some free space. */ do { header = *((hdrtype_t *) ptr); int size = SIZE(header); if (header & FLAG_FREE) { - if (len <= size) { void *nptr = ptr + HDRSIZE + len; int nsize = size - (len + 8); - /* Mark the block as used */ + /* Mark the block as used. */ *((hdrtype_t *) ptr) = USED_BLOCK(len); /* If there is still room in this block, - * then mark it as such */ - - if (nsize > 0) + * then mark it as such. + */ + if (nsize > 0) *((hdrtype_t *) nptr) = - FREE_BLOCK(nsize - 4); + FREE_BLOCK(nsize - 4); - return (void *) (ptr + HDRSIZE); + return (void *)(ptr + HDRSIZE); } } ptr += HDRSIZE + size; - } while(ptr < hend); + } while (ptr < hend); - /* Nothing available */ - return (void *) NULL; + /* Nothing available. */ + return (void *)NULL; } static void _consolidate(void) { void *ptr = hstart; - while(ptr < hend) { + while (ptr < hend) { void *nptr; hdrtype_t hdr = *((hdrtype_t *) ptr); unsigned int size = 0; @@ -134,23 +131,23 @@ static void _consolidate(void) ptr += HDRSIZE + SIZE(hdr); continue; } - + size = SIZE(hdr); nptr = ptr + HDRSIZE + SIZE(hdr); while (nptr < hend) { - hdrtype_t nhdr = *((hdrtype_t *) nptr); - + hdrtype_t nhdr = *((hdrtype_t *) nptr); + if (!(IS_FREE(nhdr))) break; - + size += SIZE(nhdr) + HDRSIZE; - *((hdrtype_t *) nptr) = 0; + *((hdrtype_t *) nptr) = 0; nptr += (HDRSIZE + SIZE(nhdr)); } - + *((hdrtype_t *) ptr) = FREE_BLOCK(size); ptr = nptr; } @@ -162,20 +159,20 @@ void free(void *ptr) ptr -= HDRSIZE; - /* Sanity check */ + /* Sanity check. */ if (ptr < hstart || ptr >= hend) return; hdr = *((hdrtype_t *) ptr); - /* Not our header (we're probably poisoned) */ + /* Not our header (we're probably poisoned). */ if (!HAS_MAGIC(hdr)) return; - /* Double free */ + /* Double free. */ if (hdr & FLAG_FREE) return; - + *((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr)); _consolidate(); } @@ -198,8 +195,7 @@ void *calloc(size_t nmemb, size_t size) void *realloc(void *ptr, size_t size) { - void *ret; - void *pptr; + void *ret, *pptr; unsigned int osize; if (ptr == NULL) @@ -209,38 +205,38 @@ void *realloc(void *ptr, size_t size) if (!HAS_MAGIC(*((hdrtype_t *) pptr))) return NULL; - - /* Get the original size of the block */ + + /* Get the original size of the block. */ osize = SIZE(*((hdrtype_t *) pptr)); - - /* Free the memory to update the tables - this - won't touch the actual memory, so we can still - use it for the copy after we have reallocated - the new space - */ - - free(ptr); + + /* + * Free the memory to update the tables - this won't touch the actual + * memory, so we can still use it for the copy after we have + * reallocated the new space. + */ + free(ptr); ret = alloc(size); - /* if ret == NULL, then doh - failure. - if ret == ptr then woo-hoo! no copy needed */ - + /* + * if ret == NULL, then doh - failure. + * if ret == ptr then woo-hoo! no copy needed. + */ if (ret == NULL || ret == ptr) return ret; - - /* Copy the memory to the new location */ + + /* Copy the memory to the new location. */ memcpy(ret, ptr, osize > size ? size : osize); + return ret; } -/* This is for debugging purposes */ +/* This is for debugging purposes. */ #ifdef TEST - void print_malloc_map(void) { void *ptr = hstart; - while(ptr < hend) { + while (ptr < hend) { hdrtype_t hdr = *((hdrtype_t *) ptr); if (!HAS_MAGIC(hdr)) { @@ -248,15 +244,13 @@ void print_malloc_map(void) break; } - /* FIXME: Verify the size of the block */ + /* FIXME: Verify the size of the block. */ printf("%x: %s (%x bytes)\n", - (unsigned int) (ptr - hstart), - hdr & FLAG_FREE ? "FREE" : "USED", - SIZE(hdr)); + (unsigned int)(ptr - hstart), + hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr)); ptr += HDRSIZE + SIZE(hdr); } } - #endif diff --git a/payloads/libpayload/libc/memory.c b/payloads/libpayload/libc/memory.c index 41fa76e343..57c31e0bb3 100644 --- a/payloads/libpayload/libc/memory.c +++ b/payloads/libpayload/libc/memory.c @@ -101,12 +101,11 @@ void *memmove(void *dst, const void *src, size_t n) /** * Compare two memory areas. * - * @param s1 Pointer to the first area to compare. - * @param s2 Pointer to the second area to compare. - * @param len Size of the first area in bytes. Both areas must have the same - * length. - * @return If len is 0, return zero. If the areas match, return zero. - * Otherwise return non-zero. + * @param s1 Pointer to the first area to compare. + * @param s2 Pointer to the second area to compare. + * @param len Size of the first area in bytes (both must have the same length). + * @return If len is 0, return zero. If the areas match, return zero. + * Otherwise return non-zero. */ int memcmp(const char *s1, const char *s2, size_t len) { diff --git a/payloads/libpayload/libc/string.c b/payloads/libpayload/libc/string.c index 87f5789b4c..bbe99933f0 100644 --- a/payloads/libpayload/libc/string.c +++ b/payloads/libpayload/libc/string.c @@ -135,8 +135,8 @@ char *strncpy(char *d, const char *s, int n) int max = n > strlen(s) + 1 ? strlen(s) + 1 : n; int i; - for(i = 0; i < max; i++) - d[i] = (char) s[i]; + for (i = 0; i < max; i++) + d[i] = (char)s[i]; return d; } @@ -152,18 +152,18 @@ char *strncat(char *d, const char *s, int n) int max = n > strlen(s) ? strlen(s) : n; int i; - for(i = 0; i < max; i++) + for (i = 0; i < max; i++) p[i] = s[i]; p[i] = '\0'; return d; } -char * strchr(const char *s, int c) +char *strchr(const char *s, int c) { - char *p = (char *) s; + char *p = (char *)s; - for( ; *p != 0; p++) { + for (; *p != 0; p++) { if (*p == c) return p; } @@ -171,7 +171,6 @@ char * strchr(const char *s, int c) return NULL; } - char *strdup(const char *s) { int n = strlen(s); @@ -189,11 +188,9 @@ char *strstr(const char *h, const char *n) int nn = strlen(n); int i; - for(i = 0; i <= hn - nn; i++) + for (i = 0; i <= hn - nn; i++) if (!strcmp(&h[i], n)) - return (char *) &h[i]; + return (char *)&h[i]; return NULL; } - - diff --git a/payloads/libpayload/sample/Makefile b/payloads/libpayload/sample/Makefile index a2599fbc32..9ab3249e12 100644 --- a/payloads/libpayload/sample/Makefile +++ b/payloads/libpayload/sample/Makefile @@ -29,15 +29,15 @@ # Sample libpayload Makefile. -CC=gcc +CC = gcc CROSS_CFLAGS = -m32 -INCLUDES=-I../include +INCLUDES = -I../include INCLUDES += -I$(shell $(CC) $(CROSS_CFLAGS) -print-search-dirs | head -n 1 | cut -d' ' -f2)include -LIBPAYLOAD=../libpayload.a -LIBGCC:=$(shell $(CC) $(CROSS_CFLAGS) -print-libgcc-file-name) -CFLAGS := -Werror -fno-stack-protector -nostdinc $(INCLUDES) +LIBPAYLOAD = ../libpayload.a +LIBGCC := $(shell $(CC) $(CROSS_CFLAGS) -print-libgcc-file-name) +CFLAGS := -Werror -Os -fno-stack-protector -nostdinc $(INCLUDES) all: hello.elf diff --git a/payloads/libpayload/sample/hello.c b/payloads/libpayload/sample/hello.c index 01948f6d8a..242c0c5f7b 100644 --- a/payloads/libpayload/sample/hello.c +++ b/payloads/libpayload/sample/hello.c @@ -31,7 +31,8 @@ #include <libpayload.h> -int main(void) { +int main(void) +{ printf("Hello world!\n"); halt(); |