1 /*
2  * HVM SeaBIOS support.
3  *
4  * Leendert van Doorn, leendert@watson.ibm.com
5  * Copyright (c) 2005, International Business Machines Corporation.
6  * Copyright (c) 2006, Keir Fraser, XenSource Inc.
7  * Copyright (c) 2011, Citrix Inc.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms and conditions of the GNU General Public License,
11  * version 2, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program; If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "config.h"
23 #include "config-seabios.h"
24 
25 #include "util.h"
26 
27 #include "smbios_types.h"
28 
29 #include <acpi2_0.h>
30 #include <libacpi.h>
31 
32 struct seabios_info {
33     char signature[14]; /* XenHVMSeaBIOS\0 */
34     uint8_t length;     /* Length of this struct */
35     uint8_t checksum;   /* Set such that the sum over bytes 0..length == 0 */
36     /*
37      * Physical address of an array of tables_nr elements.
38      *
39      * Each element is a 32 bit value contianing the physical address
40      * of a BIOS table.
41      */
42     uint32_t tables;
43     uint32_t tables_nr;
44     /*
45      * Physical address of the e820 table, contains e820_nr entries.
46      */
47     uint32_t e820;
48     uint32_t e820_nr;
49 } __attribute__ ((packed));
50 
51 #define MAX_TABLES 4
52 
seabios_setup_bios_info(void)53 static void seabios_setup_bios_info(void)
54 {
55     struct seabios_info *info = (void *)BIOS_INFO_PHYSICAL_ADDRESS;
56 
57     *info = (struct seabios_info) {
58         .signature = "XenHVMSeaBIOS",
59         .length = sizeof(*info)
60     };
61 
62     info->tables = (uint32_t)scratch_alloc(MAX_TABLES*sizeof(uint32_t), 0);
63 }
64 
seabios_finish_bios_info(void)65 static void seabios_finish_bios_info(void)
66 {
67     struct seabios_info *info = (void *)BIOS_INFO_PHYSICAL_ADDRESS;
68     uint32_t i;
69     uint8_t checksum;
70 
71     checksum = 0;
72     for ( i = 0; i < info->length; i++ )
73         checksum += ((uint8_t *)(info))[i];
74 
75     info->checksum = -checksum;
76 }
77 
add_table(uint32_t t)78 static void add_table(uint32_t t)
79 {
80     struct seabios_info *info = (void *)BIOS_INFO_PHYSICAL_ADDRESS;
81     uint32_t *ts = (uint32_t *)info->tables;
82 
83     ASSERT(info->tables_nr < MAX_TABLES);
84 
85     ts[info->tables_nr] = t;
86     info->tables_nr++;
87 }
88 
seabios_acpi_build_tables(void)89 static void seabios_acpi_build_tables(void)
90 {
91     uint32_t rsdp = (uint32_t)scratch_alloc(sizeof(struct acpi_20_rsdp), 0);
92     struct acpi_config config = {
93         .dsdt_anycpu = dsdt_anycpu_qemu_xen,
94         .dsdt_anycpu_len = dsdt_anycpu_qemu_xen_len,
95         .dsdt_15cpu = NULL,
96         .dsdt_15cpu_len = 0,
97     };
98 
99     hvmloader_acpi_build_tables(&config, rsdp);
100     add_table(rsdp);
101 }
102 
seabios_create_mp_tables(void)103 static void seabios_create_mp_tables(void)
104 {
105     add_table(create_mp_tables(NULL));
106 }
107 
seabios_create_smbios_tables(void)108 static void seabios_create_smbios_tables(void)
109 {
110     uint32_t ep = (uint32_t)scratch_alloc(sizeof(struct smbios_entry_point), 0);
111     hvm_write_smbios_tables(ep, 0UL, 0UL);
112     add_table(ep);
113 }
114 
seabios_create_pir_tables(void)115 static void seabios_create_pir_tables(void)
116 {
117     add_table(create_pir_tables());
118 }
119 
seabios_setup_e820(void)120 static void seabios_setup_e820(void)
121 {
122     struct seabios_info *info = (void *)BIOS_INFO_PHYSICAL_ADDRESS;
123     struct e820entry *e820 = scratch_alloc(sizeof(struct e820entry)*16, 0);
124     info->e820 = (uint32_t)e820;
125 
126     /* Upper boundary already checked by seabios_load(). */
127     BUG_ON(seabios_config.bios_address < 0x000c0000);
128     /* SeaBIOS reserves memory in e820 as necessary so no low reservation. */
129     info->e820_nr = build_e820_table(e820, 0, seabios_config.bios_address);
130     dump_e820_table(e820, info->e820_nr);
131 }
132 
seabios_load(const struct bios_config * bios,void * bios_addr,uint32_t bios_length)133 static void seabios_load(const struct bios_config *bios,
134                          void *bios_addr, uint32_t bios_length)
135 {
136     unsigned int bios_dest = 0x100000 - bios_length;
137 
138     BUG_ON(bios_dest + bios_length > HVMLOADER_PHYSICAL_ADDRESS);
139     memcpy((void *)bios_dest, bios_addr, bios_length);
140     seabios_config.bios_address = bios_dest;
141     seabios_config.image_size = bios_length;
142 }
143 
144 struct bios_config seabios_config = {
145     .name = "SeaBIOS",
146 
147     .load_roms = NULL,
148 
149     .bios_load = seabios_load,
150 
151     .bios_info_setup = seabios_setup_bios_info,
152     .bios_info_finish = seabios_finish_bios_info,
153 
154     .e820_setup = seabios_setup_e820,
155 
156     .acpi_build_tables = seabios_acpi_build_tables,
157     .create_mp_tables = seabios_create_mp_tables,
158     .create_smbios_tables = seabios_create_smbios_tables,
159     .create_pir_tables = seabios_create_pir_tables,
160 };
161 
162 /*
163  * Local variables:
164  * mode: C
165  * c-file-style: "BSD"
166  * c-basic-offset: 4
167  * tab-width: 4
168  * indent-tabs-mode: nil
169  * End:
170  */
171