summaryrefslogtreecommitdiff
path: root/src/arch/amd64/mboot.c
blob: a6ee4655a0bcba44b723a206ee2536427c78a340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "mboot.h"

#include <stddef.h>
#include <lib.h>

static void read_cmdline(struct mboot_info *info, struct mboot_tag *tag, char *data, uint8_t len) {
    if (len >= CMDLINE_MAX)
		len = CMDLINE_MAX; // truncate :(
    memcpy(tag->data.cmdline, data, len);
    tag->data.cmdline[len] = '\0';
    info->tags[MBOOT_CMDLINE] = *tag;
}

static void read_xsdp(struct mboot_info *info, struct mboot_tag *tag, char *data) {
	tag->data.rootsdp = (void *) data;
    info->tags[MBOOT_XSDP] = *tag;
}

static uint32_t *read_tag(struct mboot_info *info, uint32_t *data) {
    struct mboot_tag tag;
    tag.type = ((uint16_t*)data)[0];
    tag.size = data[1];
    tag.valid = 1;

    uint8_t data_len = tag.size - 2 * sizeof(uint32_t);

    switch (tag.type) {
        case MBOOT_CMDLINE:
            read_cmdline(info, &tag, (char *)(data + 2), data_len);
            break;
        case MBOOT_XSDP:
            read_xsdp(info, &tag, (char *) (data + 2));
            break;
        default:
            break;
    }

    if(tag.size % 8 != 0) {
		tag.size += 8 - (tag.size % 8);
	}

    return data + tag.size / sizeof(uint32_t);
}

struct mboot_info mboot_load_info(void *mboot_info) {
	struct mboot_info info = {0};

    uint32_t* data = (uint32_t*) mboot_info;
    info.total_size = *data++;
    info.reserved = *data++;

    while((uint8_t*) data < (uint8_t*) mboot_info + info.total_size) {
        data = read_tag(&info, data);
    }
	
	return info;
}

struct mboot_tag *mboot_get_tag(struct mboot_info *info, enum mboot_tag_type type) {
	if (info->tags[type].valid) {
		return &info->tags[type];
	} else {
		return NULL;
	}
}