[HVM] [Firmware] Refactor hvmloader.c and acpi_madt.c to allow other files to use some utility functions (memcpy, puts, get_hvm_info_table). Add some string manipulation functions, memcmp, and memset for SMBIOS generation code. Signed-off-by: Andrew D. Ball diff -r 5610d916ad1b tools/firmware/hvmloader/Makefile --- a/tools/firmware/hvmloader/Makefile Tue Jun 27 08:51:18 2006 +++ b/tools/firmware/hvmloader/Makefile Fri Jul 7 14:17:15 2006 @@ -31,7 +31,7 @@ DEFINES =-DDEBUG XENINC =-I$(XEN_ROOT)/tools/libxc -OBJECTS = hvmloader.o acpi_madt.o +OBJECTS = hvmloader.o acpi_madt.o util.o # Disable PIE/SSP if GCC supports them. They can break us. CFLAGS += $(call test-gcc-flag,$(CC),-nopie) @@ -45,9 +45,9 @@ .PHONY: all all: hvmloader -hvmloader: roms.h hvmloader.c acpi_madt.c - $(CC) $(CFLAGS) -c hvmloader.c acpi_madt.c - $(CC) $(LDFLAGS) -o hvmloader.tmp hvmloader.o acpi_madt.o +hvmloader: roms.h hvmloader.c acpi_madt.c util.c + $(CC) $(CFLAGS) -c hvmloader.c acpi_madt.c util.c + $(CC) $(LDFLAGS) -o hvmloader.tmp hvmloader.o acpi_madt.o util.o $(OBJCOPY) hvmloader.tmp hvmloader rm -f hvmloader.tmp diff -r 5610d916ad1b tools/firmware/hvmloader/acpi_madt.c --- a/tools/firmware/hvmloader/acpi_madt.c Tue Jun 27 08:51:18 2006 +++ b/tools/firmware/hvmloader/acpi_madt.c Fri Jul 7 14:17:15 2006 @@ -20,71 +20,27 @@ #include "../acpi/acpi2_0.h" #include "../acpi/acpi_madt.h" +#include "util.h" #include -#define NULL ((void*)0) - -extern int puts(const char *s); - static struct hvm_info_table *table = NULL; - -static int validate_hvm_info(struct hvm_info_table *t) -{ - char signature[] = "HVM INFO"; - uint8_t *ptr = (uint8_t *)t; - uint8_t sum = 0; - int i; - - /* strncmp(t->signature, "HVM INFO", 8) */ - for (i = 0; i < 8; i++) { - if (signature[i] != t->signature[i]) { - puts("Bad hvm info signature\n"); - return 0; - } - } - - for (i = 0; i < t->length; i++) - sum += ptr[i]; - - return (sum == 0); -} - -/* xc_vmx_builder wrote hvm info at 0x9F800. Return it. */ -static struct hvm_info_table * -get_hvm_info_table(void) -{ - struct hvm_info_table *t; - - if (table != NULL) - return table; - - t = (struct hvm_info_table *)HVM_INFO_PADDR; - - if (!validate_hvm_info(t)) { - puts("Bad hvm info table\n"); - return NULL; - } - - table = t; - - return table; -} int get_vcpu_nr(void) { - struct hvm_info_table *t = get_hvm_info_table(); - return (t ? t->nr_vcpus : 1); /* default 1 vcpu */ + if (table == NULL) + table = get_hvm_info_table(); + return (table ? table->nr_vcpus : 1); /* default 1 vcpu */ } int get_acpi_enabled(void) { - struct hvm_info_table *t = get_hvm_info_table(); - return (t ? t->acpi_enabled : 0); /* default no acpi */ + if (table == NULL) + table = get_hvm_info_table(); + return (table ? table->acpi_enabled : 0); /* default no acpi */ } - static void * acpi_madt_get_madt(unsigned char *acpi_start) diff -r 5610d916ad1b tools/firmware/hvmloader/hvmloader.c --- a/tools/firmware/hvmloader/hvmloader.c Tue Jun 27 08:51:18 2006 +++ b/tools/firmware/hvmloader/hvmloader.c Fri Jul 7 14:17:15 2006 @@ -23,6 +23,7 @@ */ #include "roms.h" #include "../acpi/acpi2_0.h" /* for ACPI_PHYSICAL_ADDRESS */ +#include "util.h" /* memory map */ #define VGABIOS_PHYSICAL_ADDRESS 0x000C0000 @@ -91,28 +92,6 @@ __asm__ __volatile__ ("inb %w1,%0" : "=a" (val) : "Nd" (addr)); return val; -} - -void * -memcpy(void *dest, const void *src, unsigned n) -{ - int t0, t1, t2; - - __asm__ __volatile__( - "cld\n" - "rep; movsl\n" - "testb $2,%b4\n" - "je 1f\n" - "movsw\n" - "1: testb $1,%b4\n" - "je 2f\n" - "movsb\n" - "2:" - : "=&c" (t0), "=&D" (t1), "=&S" (t2) - : "0" (n/4), "q" (n), "1" ((long) dest), "2" ((long) src) - : "memory" - ); - return dest; } int diff -r 5610d916ad1b tools/firmware/hvmloader/util.c --- /dev/null Tue Jun 27 08:51:18 2006 +++ b/tools/firmware/hvmloader/util.c Fri Jul 7 14:17:15 2006 @@ -0,0 +1,155 @@ +/* + * util.c: Utilities for hvmloader. + * + * Most of this is just refactored from acpi_madt.c and hvmloader.c . + * + * validate_hvm_info(), get_hvm_info_table() are taken from acpi_madt.c. + * Yu Ke, ke.yu@xxxxxxxxx + * Copyright (c) 2005, Intel Corporation. + * + * memcpy() is from hvmloader.c. + * Leendert van Doorn, leendert@xxxxxxxxxxxxxx + * Copyright (c) 2005, International Business Machines Corporation. + * + * Andrew D. Ball, aball@xxxxxxxxxx + * Copyright (c) 2006, International Business Machines Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place - Suite 330, Boston, MA 02111-1307 USA. + */ + +#include "util.h" + +int validate_hvm_info(struct hvm_info_table *t) +{ + char signature[] = "HVM INFO"; + uint8_t *ptr = (uint8_t *)t; + uint8_t sum = 0; + int i; + + /* strncmp(t->signature, "HVM INFO", 8) */ + for (i = 0; i < 8; i++) { + if (signature[i] != t->signature[i]) { + puts("Bad hvm info signature\n"); + return 0; + } + } + + for (i = 0; i < t->length; i++) + sum += ptr[i]; + + return (sum == 0); +} + +/* xc_hvm_builder wrote hvm info at 0x9F800. Return it. */ +struct hvm_info_table * +get_hvm_info_table(void) +{ + struct hvm_info_table *t; + + t = (struct hvm_info_table *)HVM_INFO_PADDR; + + if (!validate_hvm_info(t)) { + puts("Bad hvm info table\n"); + return NULL; + } + + return t; +} + +void * +memcpy(void *dest, const void *src, size_t n) +{ + int t0, t1, t2; + + __asm__ __volatile__( + "cld\n" + "rep; movsl\n" + "testb $2,%b4\n" + "je 1f\n" + "movsw\n" + "1: testb $1,%b4\n" + "je 2f\n" + "movsb\n" + "2:" + : "=&c" (t0), "=&D" (t1), "=&S" (t2) + : "0" (n/4), "q" (n), "1" ((long) dest), "2" ((long) src) + : "memory" + ); + return dest; +} + +void * +memset(void *s, int c, size_t n) +{ + char b = (char) c; + char *p = (char *)s; + int i; + for (i = 0; i < n; ++i) + *p++ = b; + return s; +} + +size_t +strlen(const char *s) +{ + int i = 0; + while (*s++) + ++i; + return i; +} + +char * +strncpy(char *dest, const char *src, size_t n) +{ + int i = 0; + + /* write non-NUL characters from src into dest until we run + out of room in dest or encounter a NUL in src */ + while (i < n && *src) { + *dest++ = *src++; + ++i; + } + + /* only add NUL if we have room for it in dest */ + if (i < n) + *dest = 0; + + return dest; +} + +char * +strcpy(char *dest, const char *src) +{ + while (*src) + *dest++ = *src++; + *dest = 0; + return dest; +} + +int +memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + uint8_t *p1 = (uint8_t *) s1; + uint8_t *p2 = (uint8_t *) s2; + + for (i = 0; i < n; ++i) { + if (p1[i] < p2[i]) + return -1; + else if (p1[i] > p2[i]) + return 1; + } + + return 0; +} diff -r 5610d916ad1b tools/firmware/hvmloader/util.h --- /dev/null Tue Jun 27 08:51:18 2006 +++ b/tools/firmware/hvmloader/util.h Fri Jul 7 14:17:15 2006 @@ -0,0 +1,66 @@ +/* + * util.c: Utilities for hvmloader. + * + * Most of this is just refactored from acpi_madt.c and hvmloader.c . + * + * validate_hvm_info(), get_hvm_info_table() are taken from acpi_madt.c. + * Yu Ke, ke.yu@xxxxxxxxx + * Copyright (c) 2005, Intel Corporation. + * + * memcpy() is from hvmloader.c. + * Leendert van Doorn, leendert@xxxxxxxxxxxxxx + * Copyright (c) 2005, International Business Machines Corporation. + * + * Andrew D. Ball, aball@xxxxxxxxxx + * Copyright (c) 2006, International Business Machines Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place - Suite 330, Boston, MA 02111-1307 USA. + */ + +#include +#include + +#ifndef size_t +#define size_t unsigned int +#endif + +#ifndef NULL +#define NULL ((void *)0) +#endif + +extern int puts(const char *s); + +int validate_hvm_info(struct hvm_info_table *t); + +/* xc_vmx_builder wrote hvm info at 0x9F800. Return it. */ +struct hvm_info_table * +get_hvm_info_table(void); + +void * +memcpy(void *dest, const void *src, size_t n); + +void * +memset(void *s, int c, size_t n); + +int memcmp(const void *s1, const void *s2, size_t n); + +size_t +strlen(const char *s); + +char * +strcpy(char *dest, const char *src); + +char * +strncpy(char *dest, const char *src, size_t n); +