Use MaxCountCPUs during building of per cpu tables.
[seabios.git] / src / smbios.c
index e2f21901420581b528b4e473e15b5f64e1c05c71..ad0d0c45a6b7b2a128f6adad33a4e33f7f425263 100644 (file)
@@ -1,57 +1,13 @@
 // smbios table generation (on emulators)
 //
-// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
 // Copyright (C) 2006 Fabrice Bellard
 //
 // This file may be distributed under the terms of the GNU LGPLv3 license.
 
 #include "util.h" // dprintf
-#include "memmap.h" // bios_table_cur_addr
 #include "biosvar.h" // GET_EBDA
-
-
-/****************************************************************
- * UUID probe
- ****************************************************************/
-
-#define QEMU_CFG_SIGNATURE  0x00
-#define QEMU_CFG_ID         0x01
-#define QEMU_CFG_UUID       0x02
-
-static void
-qemu_cfg_read(u8 *buf, u16 f, int len)
-{
-    outw(f, PORT_QEMU_CFG_CTL);
-    while (len--)
-        *(buf++) = inb(PORT_QEMU_CFG_DATA);
-}
-
-static int
-qemu_cfg_port_probe()
-{
-    u8 sig[4] = "QEMU";
-    u8 buf[4];
-    qemu_cfg_read(buf, QEMU_CFG_SIGNATURE, 4);
-    return *(u32*)buf == *(u32*)sig;
-}
-
-static void
-uuid_probe(u8 *bios_uuid)
-{
-    // Default to UUID not set
-    memset(bios_uuid, 0, 16);
-
-    if (! CONFIG_UUID_BACKDOOR)
-        return;
-    if (CONFIG_COREBOOT)
-        return;
-    if (! qemu_cfg_port_probe())
-        // Feature not available
-        return;
-
-    qemu_cfg_read(bios_uuid, QEMU_CFG_UUID, 16);
-}
-
+#include "paravirt.h"
 
 /****************************************************************
  * smbios tables
@@ -222,13 +178,16 @@ struct smbios_type_127 {
  ****************************************************************/
 
 static void
-smbios_entry_point_init(void *start,
-                        u16 max_structure_size,
+smbios_entry_point_init(u16 max_structure_size,
                         u16 structure_table_length,
-                        u32 structure_table_address,
+                        void *structure_table_address,
                         u16 number_of_structures)
 {
-    struct smbios_entry_point *ep = (struct smbios_entry_point *)start;
+    struct smbios_entry_point *ep = malloc_fseg(sizeof(*ep));
+    if (! ep) {
+        dprintf(1, "No space for smbios entry table!\n");
+        return;
+    }
 
     memcpy(ep->anchor_string, "_SM_", 4);
     ep->length = 0x1f;
@@ -240,84 +199,132 @@ smbios_entry_point_init(void *start,
     memcpy(ep->intermediate_anchor_string, "_DMI_", 5);
 
     ep->structure_table_length = structure_table_length;
-    ep->structure_table_address = structure_table_address;
+    ep->structure_table_address = (u32)structure_table_address;
     ep->number_of_structures = number_of_structures;
     ep->smbios_bcd_revision = 0x24;
 
-    ep->checksum = 0;
-    ep->intermediate_checksum = 0;
+    ep->checksum -= checksum(ep, 0x10);
 
-    ep->checksum = -checksum(start, 0x10);
+    ep->intermediate_checksum -= checksum((void*)ep + 0x10, ep->length - 0x10);
 
-    ep->intermediate_checksum = -checksum(start + 0x10, ep->length - 0x10);
+    dprintf(1, "SMBIOS ptr=%p table=%p\n", ep, structure_table_address);
 }
 
+#define load_str_field_with_default(type, field, def)                   \
+    do {                                                                \
+        size = qemu_cfg_smbios_load_field(type,                         \
+                                 offsetof(struct smbios_type_##type,    \
+                                          field), end);                 \
+        if (size > 0) {                                                 \
+            end += size;                                                \
+        } else {                                                        \
+            memcpy(end, def, sizeof(def));                              \
+            end += sizeof(def);                                         \
+        }                                                               \
+        p->field = ++str_index;                                         \
+    } while (0)
+
+#define load_str_field_or_skip(type, field) \
+    do {                                                                \
+        size = qemu_cfg_smbios_load_field(type,                         \
+                                 offsetof(struct smbios_type_##type,    \
+                                          field), end);                 \
+        if (size > 0) {                                                 \
+            end += size;                                                \
+            p->field = ++str_index;                                     \
+        } else {                                                        \
+            p->field = 0;                                               \
+        }                                                               \
+    } while (0)
+
 /* Type 0 -- BIOS Information */
 #define RELEASE_DATE_STR "01/01/2007"
 static void *
-smbios_type_0_init(void *start)
+smbios_init_type_0(void *start)
 {
     struct smbios_type_0 *p = (struct smbios_type_0 *)start;
+    char *end = (char *)start + sizeof(struct smbios_type_0);
+    size_t size;
+    int str_index = 0;
 
     p->header.type = 0;
     p->header.length = sizeof(struct smbios_type_0);
     p->header.handle = 0;
 
-    p->vendor_str = 1;
-    p->bios_version_str = 1;
+    load_str_field_with_default(0, vendor_str, CONFIG_APPNAME);
+    load_str_field_with_default(0, bios_version_str, CONFIG_APPNAME);
+
     p->bios_starting_address_segment = 0xe800;
-    p->bios_release_date_str = 2;
+
+    load_str_field_with_default(0, bios_release_date_str, RELEASE_DATE_STR);
+
     p->bios_rom_size = 0; /* FIXME */
 
-    memset(p->bios_characteristics, 0, sizeof(p->bios_characteristics));
+    memset(p->bios_characteristics, 0, 8);
     p->bios_characteristics[0] = 0x08; /* BIOS characteristics not supported */
     p->bios_characteristics_extension_bytes[0] = 0;
     p->bios_characteristics_extension_bytes[1] = 0;
 
-    p->system_bios_major_release = 1;
-    p->system_bios_minor_release = 0;
+    if (!qemu_cfg_smbios_load_field(0, offsetof(struct smbios_type_0,
+                                                system_bios_major_release),
+                                    &p->system_bios_major_release))
+        p->system_bios_major_release = 1;
+
+    if (!qemu_cfg_smbios_load_field(0, offsetof(struct smbios_type_0,
+                                                system_bios_minor_release),
+                                    &p->system_bios_minor_release))
+        p->system_bios_minor_release = 0;
+
     p->embedded_controller_major_release = 0xff;
     p->embedded_controller_minor_release = 0xff;
 
-    start += sizeof(struct smbios_type_0);
-    memcpy((char *)start, CONFIG_APPNAME, sizeof(CONFIG_APPNAME));
-    start += sizeof(CONFIG_APPNAME);
-    memcpy((char *)start, RELEASE_DATE_STR, sizeof(RELEASE_DATE_STR));
-    start += sizeof(RELEASE_DATE_STR);
-    *((u8 *)start) = 0;
+    *end = 0;
+    end++;
 
-    return start+1;
+    return end;
 }
 
 /* Type 1 -- System Information */
 static void *
-smbios_type_1_init(void *start)
+smbios_init_type_1(void *start)
 {
     struct smbios_type_1 *p = (struct smbios_type_1 *)start;
+    char *end = (char *)start + sizeof(struct smbios_type_1);
+    size_t size;
+    int str_index = 0;
+
     p->header.type = 1;
     p->header.length = sizeof(struct smbios_type_1);
     p->header.handle = 0x100;
 
-    p->manufacturer_str = 0;
-    p->product_name_str = 0;
-    p->version_str = 0;
-    p->serial_number_str = 0;
+    load_str_field_or_skip(1, manufacturer_str);
+    load_str_field_or_skip(1, product_name_str);
+    load_str_field_or_skip(1, version_str);
+    load_str_field_or_skip(1, serial_number_str);
 
-    uuid_probe(p->uuid);
+    size = qemu_cfg_smbios_load_field(1, offsetof(struct smbios_type_1,
+                                                  uuid), &p->uuid);
+    if (size == 0)
+        memset(p->uuid, 0, 16);
 
     p->wake_up_type = 0x06; /* power switch */
-    p->sku_number_str = 0;
-    p->family_str = 0;
 
-    start += sizeof(struct smbios_type_1);
-    *((u16 *)start) = 0;
+    load_str_field_or_skip(1, sku_number_str);
+    load_str_field_or_skip(1, family_str);
 
-    return start+2;
+    *end = 0;
+    end++;
+    if (!str_index) {
+        *end = 0;
+        end++;
+    }
+
+    return end;
 }
 
 /* Type 3 -- System Enclosure */
 static void *
-smbios_type_3_init(void *start)
+smbios_init_type_3(void *start)
 {
     struct smbios_type_3 *p = (struct smbios_type_3 *)start;
 
@@ -347,7 +354,7 @@ smbios_type_3_init(void *start)
 
 /* Type 4 -- Processor Information */
 static void *
-smbios_type_4_init(void *start, unsigned int cpu_number)
+smbios_init_type_4(void *start, unsigned int cpu_number)
 {
     struct smbios_type_4 *p = (struct smbios_type_4 *)start;
 
@@ -389,7 +396,7 @@ smbios_type_4_init(void *start, unsigned int cpu_number)
 
 /* Type 16 -- Physical Memory Array */
 static void *
-smbios_type_16_init(void *start)
+smbios_init_type_16(void *start, u32 memory_size_mb, int nr_mem_devs)
 {
     struct smbios_type_16 *p = (struct smbios_type_16*)start;
 
@@ -400,10 +407,9 @@ smbios_type_16_init(void *start)
     p->location = 0x01; /* other */
     p->use = 0x03; /* system memory */
     p->error_correction = 0x01; /* other */
-    u64 memsize = RamSize + RamSizeOver4G;
-    p->maximum_capacity = memsize / 1024;
+    p->maximum_capacity = memory_size_mb * 1024;
     p->memory_error_information_handle = 0xfffe; /* none provided */
-    p->number_of_memory_devices = 1;
+    p->number_of_memory_devices = nr_mem_devs;
 
     start += sizeof(struct smbios_type_16);
     *((u16 *)start) = 0;
@@ -413,21 +419,19 @@ smbios_type_16_init(void *start)
 
 /* Type 17 -- Memory Device */
 static void *
-smbios_type_17_init(void *start)
+smbios_init_type_17(void *start, u32 memory_size_mb, int instance)
 {
     struct smbios_type_17 *p = (struct smbios_type_17 *)start;
 
     p->header.type = 17;
     p->header.length = sizeof(struct smbios_type_17);
-    p->header.handle = 0x1100;
+    p->header.handle = 0x1100 + instance;
 
     p->physical_memory_array_handle = 0x1000;
     p->total_width = 64;
     p->data_width = 64;
-    /* truncate memory_size_mb to 16 bits and clear most significant
-       bit [indicates size in MB] */
-    u64 memsize = RamSize + RamSizeOver4G;
-    p->size = (u16) (memsize / (1024*1024)) & 0x7fff;
+/* TODO: should assert in case something is wrong   ASSERT((memory_size_mb & ~0x7fff) == 0); */
+    p->size = memory_size_mb;
     p->form_factor = 0x09; /* DIMM */
     p->device_set = 0;
     p->device_locator_str = 1;
@@ -436,7 +440,8 @@ smbios_type_17_init(void *start)
     p->type_detail = 0;
 
     start += sizeof(struct smbios_type_17);
-    memcpy((char *)start, "DIMM 1", 7);
+    memcpy((char *)start, "DIMM 0", 7);
+    ((char*)start)[5] += instance;
     start += 7;
     *((u8 *)start) = 0;
 
@@ -445,21 +450,16 @@ smbios_type_17_init(void *start)
 
 /* Type 19 -- Memory Array Mapped Address */
 static void *
-smbios_type_19_init(void *start)
+smbios_init_type_19(void *start, u32 memory_size_mb, int instance)
 {
     struct smbios_type_19 *p = (struct smbios_type_19 *)start;
 
     p->header.type = 19;
     p->header.length = sizeof(struct smbios_type_19);
-    p->header.handle = 0x1300;
+    p->header.handle = 0x1300 + instance;
 
-    p->starting_address = 0;
-    u64 memsize = RamSizeOver4G;
-    if (memsize)
-        memsize += 0x100000000ull;
-    else
-        memsize = RamSize;
-    p->ending_address = memsize / 1024 - 1;
+    p->starting_address = instance << 24;
+    p->ending_address = p->starting_address + (memory_size_mb << 10) - 1;
     p->memory_array_handle = 0x1000;
     p->partition_width = 1;
 
@@ -471,23 +471,18 @@ smbios_type_19_init(void *start)
 
 /* Type 20 -- Memory Device Mapped Address */
 static void *
-smbios_type_20_init(void *start)
+smbios_init_type_20(void *start, u32 memory_size_mb, int instance)
 {
     struct smbios_type_20 *p = (struct smbios_type_20 *)start;
 
     p->header.type = 20;
     p->header.length = sizeof(struct smbios_type_20);
-    p->header.handle = 0x1400;
+    p->header.handle = 0x1400 + instance;
 
-    p->starting_address = 0;
-    u64 memsize = RamSizeOver4G;
-    if (memsize)
-        memsize += 0x100000000ull;
-    else
-        memsize = RamSize;
-    p->ending_address = memsize / 1024 - 1;
-    p->memory_device_handle = 0x1100;
-    p->memory_array_mapped_address_handle = 0x1300;
+    p->starting_address = instance << 24;
+    p->ending_address = p->starting_address + (memory_size_mb << 10) - 1;
+    p->memory_device_handle = 0x1100 + instance;
+    p->memory_array_mapped_address_handle = 0x1300 + instance;
     p->partition_row_position = 1;
     p->interleave_position = 0;
     p->interleaved_data_depth = 0;
@@ -500,7 +495,7 @@ smbios_type_20_init(void *start)
 
 /* Type 32 -- System Boot Information */
 static void *
-smbios_type_32_init(void *start)
+smbios_init_type_32(void *start)
 {
     struct smbios_type_32 *p = (struct smbios_type_32 *)start;
 
@@ -518,7 +513,7 @@ smbios_type_32_init(void *start)
 
 /* Type 127 -- End of Table */
 static void *
-smbios_type_127_init(void *start)
+smbios_init_type_127(void *start)
 {
     struct smbios_type_127 *p = (struct smbios_type_127 *)start;
 
@@ -540,44 +535,59 @@ smbios_init(void)
 
     dprintf(3, "init SMBIOS tables\n");
 
-    unsigned cpu_num, nr_structs = 0, max_struct_size = 0;
-    char *start, *p, *q;
-
-    bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
-    start = (void *)(bios_table_cur_addr);
-
-    p = (char *)start + sizeof(struct smbios_entry_point);
-
-#define add_struct(fn) { \
-    q = (fn); \
-    nr_structs++; \
-    if ((q - p) > max_struct_size) \
-        max_struct_size = q - p; \
-    p = q; \
-}
-
-    add_struct(smbios_type_0_init(p));
-    add_struct(smbios_type_1_init(p));
-    add_struct(smbios_type_3_init(p));
-    int smp_cpus = smp_probe();
-    for (cpu_num = 1; cpu_num <= smp_cpus; cpu_num++)
-        add_struct(smbios_type_4_init(p, cpu_num));
-    add_struct(smbios_type_16_init(p));
-    add_struct(smbios_type_17_init(p));
-    add_struct(smbios_type_19_init(p));
-    add_struct(smbios_type_20_init(p));
-    add_struct(smbios_type_32_init(p));
-    add_struct(smbios_type_127_init(p));
+    char *start = malloc_high(2048); // XXX - determine real size
+    if (! start) {
+        dprintf(1, "No memory for smbios tables\n");
+        return;
+    }
+
+    u32 nr_structs = 0, max_struct_size = 0;
+    char *q, *p = start, *end = start + 2048 - sizeof(struct smbios_type_127);
+
+#define add_struct(type, args...)                                       \
+    do {                                                                \
+        if (!qemu_cfg_smbios_load_external(type, &p, &nr_structs,       \
+                                           &max_struct_size, end)) {    \
+            q = smbios_init_type_##type(args);                          \
+            nr_structs++;                                               \
+            if ((q - p) > max_struct_size)                              \
+                max_struct_size = q - p;                                \
+            p = q;                                                      \
+        }                                                               \
+    } while (0)
+
+    add_struct(0, p);
+    add_struct(1, p);
+    add_struct(3, p);
+
+    int cpu_num;
+    for (cpu_num = 1; cpu_num <= MaxCountCPUs; cpu_num++)
+        add_struct(4, p, cpu_num);
+    u64 memsize = RamSizeOver4G;
+    if (memsize)
+        memsize += 0x100000000ull;
+    else
+        memsize = RamSize;
+    memsize = memsize / (1024 * 1024);
+    int nr_mem_devs = (memsize + 0x3fff) >> 14;
+    add_struct(16, p, memsize, nr_mem_devs);
+    int i;
+    for (i = 0; i < nr_mem_devs; i++) {
+        u32 dev_memsize = ((i == (nr_mem_devs - 1))
+                           ? (((memsize-1) & 0x3fff)+1) : 0x4000);
+        add_struct(17, p, dev_memsize, i);
+        add_struct(19, p, dev_memsize, i);
+        add_struct(20, p, dev_memsize, i);
+    }
+
+    add_struct(32, p);
+    /* Add any remaining provided entries before the end marker */
+    for (i = 0; i < 256; i++)
+        qemu_cfg_smbios_load_external(i, &p, &nr_structs, &max_struct_size,
+                                      end);
+    add_struct(127, p);
 
 #undef add_struct
 
-    smbios_entry_point_init(
-        start, max_struct_size,
-        (p - (char *)start) - sizeof(struct smbios_entry_point),
-        (u32)(start + sizeof(struct smbios_entry_point)),
-        nr_structs);
-
-    bios_table_cur_addr += (p - (char *)start);
-
-    dprintf(1, "SMBIOS table addr=0x%08lx\n", (unsigned long)start);
+    smbios_entry_point_init(max_struct_size, p - start, start, nr_structs);
 }