smbios.c 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * SMBIOS Support
 *
 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
 *
 * Authors:
 *  Alex Williamson <alex.williamson@hp.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
12 13
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
14 15
 */

16
#include "qemu/error-report.h"
17
#include "sysemu/sysemu.h"
P
Paolo Bonzini 已提交
18
#include "hw/i386/smbios.h"
19
#include "hw/loader.h"
20 21 22 23 24 25 26

/*
 * Structures shared with the BIOS
 */
struct smbios_header {
    uint16_t length;
    uint8_t type;
27
} QEMU_PACKED;
28 29 30 31 32 33

struct smbios_field {
    struct smbios_header header;
    uint8_t type;
    uint16_t offset;
    uint8_t data[];
34
} QEMU_PACKED;
35 36 37 38

struct smbios_table {
    struct smbios_header header;
    uint8_t data[];
39
} QEMU_PACKED;
40 41 42 43 44 45 46

#define SMBIOS_FIELD_ENTRY 0
#define SMBIOS_TABLE_ENTRY 1


static uint8_t *smbios_entries;
static size_t smbios_entries_len;
47 48 49 50 51
static int smbios_type4_count = 0;

static void smbios_validate_table(void)
{
    if (smbios_type4_count && smbios_type4_count != smp_cpus) {
52
        error_report("Number of SMBIOS Type 4 tables must match cpu count");
53 54 55
        exit(1);
    }
}
56 57 58

uint8_t *smbios_get_table(size_t *length)
{
59
    smbios_validate_table();
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    *length = smbios_entries_len;
    return smbios_entries;
}

/*
 * To avoid unresolvable overlaps in data, don't allow both
 * tables and fields for the same smbios type.
 */
static void smbios_check_collision(int type, int entry)
{
    uint16_t *num_entries = (uint16_t *)smbios_entries;
    struct smbios_header *header;
    char *p;
    int i;

    if (!num_entries)
        return;

    p = (char *)(num_entries + 1);

    for (i = 0; i < *num_entries; i++) {
        header = (struct smbios_header *)p;
        if (entry == SMBIOS_TABLE_ENTRY && header->type == SMBIOS_FIELD_ENTRY) {
            struct smbios_field *field = (void *)header;
            if (type == field->type) {
85 86
                error_report("SMBIOS type %d field already defined, "
                             "cannot add table", type);
87 88 89 90 91 92
                exit(1);
            }
        } else if (entry == SMBIOS_FIELD_ENTRY &&
                   header->type == SMBIOS_TABLE_ENTRY) {
            struct smbios_structure_header *table = (void *)(header + 1);
            if (type == table->type) {
93 94
                error_report("SMBIOS type %d table already defined, "
                             "cannot add field", type);
95 96 97 98 99 100 101
                exit(1);
            }
        }
        p += le16_to_cpu(header->length);
    }
}

102
void smbios_add_field(int type, int offset, const void *data, size_t len)
103 104 105 106 107 108 109
{
    struct smbios_field *field;

    smbios_check_collision(type, SMBIOS_FIELD_ENTRY);

    if (!smbios_entries) {
        smbios_entries_len = sizeof(uint16_t);
110
        smbios_entries = g_malloc0(smbios_entries_len);
111
    }
112
    smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
                                                  sizeof(*field) + len);
    field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
    field->header.type = SMBIOS_FIELD_ENTRY;
    field->header.length = cpu_to_le16(sizeof(*field) + len);

    field->type = type;
    field->offset = cpu_to_le16(offset);
    memcpy(field->data, data, len);

    smbios_entries_len += sizeof(*field) + len;
    (*(uint16_t *)smbios_entries) =
            cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
}

static void smbios_build_type_0_fields(const char *t)
{
    char buf[1024];
130
    unsigned char major, minor;
131 132 133

    if (get_param_value(buf, sizeof(buf), "vendor", t))
        smbios_add_field(0, offsetof(struct smbios_type_0, vendor_str),
134
                         buf, strlen(buf) + 1);
135 136
    if (get_param_value(buf, sizeof(buf), "version", t))
        smbios_add_field(0, offsetof(struct smbios_type_0, bios_version_str),
137
                         buf, strlen(buf) + 1);
138 139 140
    if (get_param_value(buf, sizeof(buf), "date", t))
        smbios_add_field(0, offsetof(struct smbios_type_0,
                                     bios_release_date_str),
141
                         buf, strlen(buf) + 1);
142
    if (get_param_value(buf, sizeof(buf), "release", t)) {
143 144 145 146
        if (sscanf(buf, "%hhu.%hhu", &major, &minor) != 2) {
            error_report("Invalid release");
            exit(1);
        }
147
        smbios_add_field(0, offsetof(struct smbios_type_0,
148 149
                                     system_bios_major_release),
                         &major, 1);
150
        smbios_add_field(0, offsetof(struct smbios_type_0,
151 152
                                     system_bios_minor_release),
                         &minor, 1);
153 154 155 156 157 158 159 160 161
    }
}

static void smbios_build_type_1_fields(const char *t)
{
    char buf[1024];

    if (get_param_value(buf, sizeof(buf), "manufacturer", t))
        smbios_add_field(1, offsetof(struct smbios_type_1, manufacturer_str),
162
                         buf, strlen(buf) + 1);
163 164
    if (get_param_value(buf, sizeof(buf), "product", t))
        smbios_add_field(1, offsetof(struct smbios_type_1, product_name_str),
165
                         buf, strlen(buf) + 1);
166 167
    if (get_param_value(buf, sizeof(buf), "version", t))
        smbios_add_field(1, offsetof(struct smbios_type_1, version_str),
168
                         buf, strlen(buf) + 1);
169 170
    if (get_param_value(buf, sizeof(buf), "serial", t))
        smbios_add_field(1, offsetof(struct smbios_type_1, serial_number_str),
171
                         buf, strlen(buf) + 1);
172 173
    if (get_param_value(buf, sizeof(buf), "uuid", t)) {
        if (qemu_uuid_parse(buf, qemu_uuid) != 0) {
174
            error_report("Invalid UUID");
175 176 177 178 179
            exit(1);
        }
    }
    if (get_param_value(buf, sizeof(buf), "sku", t))
        smbios_add_field(1, offsetof(struct smbios_type_1, sku_number_str),
180
                         buf, strlen(buf) + 1);
181 182
    if (get_param_value(buf, sizeof(buf), "family", t))
        smbios_add_field(1, offsetof(struct smbios_type_1, family_str),
183
                         buf, strlen(buf) + 1);
184 185 186 187 188 189 190 191 192 193 194
}

int smbios_entry_add(const char *t)
{
    char buf[1024];

    if (get_param_value(buf, sizeof(buf), "file", t)) {
        struct smbios_structure_header *header;
        struct smbios_table *table;
        int size = get_image_size(buf);

195
        if (size == -1 || size < sizeof(struct smbios_structure_header)) {
196
            error_report("Cannot read SMBIOS file %s", buf);
197 198 199 200 201
            exit(1);
        }

        if (!smbios_entries) {
            smbios_entries_len = sizeof(uint16_t);
202
            smbios_entries = g_malloc0(smbios_entries_len);
203 204
        }

205
        smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
206 207 208 209 210 211
                                                      sizeof(*table) + size);
        table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
        table->header.type = SMBIOS_TABLE_ENTRY;
        table->header.length = cpu_to_le16(sizeof(*table) + size);

        if (load_image(buf, table->data) != size) {
212
            error_report("Failed to load SMBIOS file %s", buf);
213 214 215 216 217
            exit(1);
        }

        header = (struct smbios_structure_header *)(table->data);
        smbios_check_collision(header->type, SMBIOS_TABLE_ENTRY);
218 219 220
        if (header->type == 4) {
            smbios_type4_count++;
        }
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

        smbios_entries_len += sizeof(*table) + size;
        (*(uint16_t *)smbios_entries) =
                cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
        return 0;
    }

    if (get_param_value(buf, sizeof(buf), "type", t)) {
        unsigned long type = strtoul(buf, NULL, 0);
        switch (type) {
        case 0:
            smbios_build_type_0_fields(t);
            return 0;
        case 1:
            smbios_build_type_1_fields(t);
            return 0;
        default:
238 239
            error_report("Don't know how to build fields for SMBIOS type %ld",
                         type);
240 241 242 243
            exit(1);
        }
    }

244
    error_report("Must specify type= or file=");
245 246
    return -1;
}