aboutsummaryrefslogtreecommitdiff
path: root/tables.c
blob: bb533b61b4781b53ff12addda8ad122dd363d6dd (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "bios.h"
#include "stdio.h"
#include "fw_cfg.h"
#include "string.h"

struct loader_cmd {
	uint32_t cmd;
	union {
#define CMD_QUIT 0
#define CMD_ALLOC 1
		struct {
			char file[56];
			uint32_t align;
			uint8_t zone;
		} alloc;
#define CMD_PTR 2
		struct {
			char dest[56];
			char src[56];
			uint32_t offset;
			uint8_t size;
		} ptr;
#define CMD_CHECKSUM 3
		struct {
			char file[56];
			uint32_t offset;
			uint32_t start;
			uint32_t len;
		} checksum;
		uint8_t pad[124];
	};
} __attribute__((__packed__));

enum {
	ALLOC_HIGH = 1,
	ALLOC_FSEG = 2
};

static uint8_t *file_address[20];

static inline void *id_to_addr(int fw_cfg_id)
{
	return file_address[fw_cfg_id];
}

static inline void set_file_addr(int fw_cfg_id, void *p)
{
	file_address[fw_cfg_id] = p;
}

static void do_alloc(char *file, uint32_t align, uint8_t zone)
{
	int id = fw_cfg_file_id(file);
	int n = fw_cfg_file_size(id);
	char *p;

	if (id == -1)
		panic();

	if (align > 16)
		n += align - 16;

	if (zone == ALLOC_FSEG)
		p = malloc_fseg(n);
	else
		p = malloc(n);

	p = (char *)((uintptr_t)(p + align - 1) & -align);

	set_file_addr(id, p);
	fw_cfg_read_file(id, p, n);
}

static void do_ptr(char *dest, char *src, uint32_t offset, uint8_t size)
{
	char *p, *q;
	int id;
	union {
		long long ll;
		char b[8];
	} data;

	id = fw_cfg_file_id(src);
	p = id_to_addr(id);
	if (!p)
		panic();

	id = fw_cfg_file_id(dest);
	q = id_to_addr(id);
	if (!q)
		panic();

	q += offset;

	/* Assumes little endian */
	data.ll = 0;
	memcpy(&data.b, q, size);
	data.ll += (uintptr_t) p;
	memcpy(q, &data.b, size);
}

static inline uint8_t csum8(uint8_t *buf, uint32_t len)
{
	uint32_t s = 0;
	while (len-- > 0)
		s += *buf++;
	return s;
}
static void do_checksum(char *file, uint32_t offset, uint32_t start, uint32_t len)
{
	uint8_t *p;
	int id;
	int n;

	id = fw_cfg_file_id(file);
	p = id_to_addr(id);
	if (!p)
		panic();

	n = fw_cfg_file_size(id);
	if (offset >= n || n < start || len > n - start)
		panic();

	p[offset] -= csum8(&p[start], len);
}

void extract_acpi(void)
{
	int id = fw_cfg_file_id("etc/table-loader");
	int n = fw_cfg_file_size(id);
	struct loader_cmd script[n / sizeof(struct loader_cmd)];
	int i;

	if (!n)
		return;

	fw_cfg_read_file(id, script, n);

	for (i = 0; i < ARRAY_SIZE(script); i++) {
		struct loader_cmd *s = &script[i];
		switch(script[i].cmd) {
		case CMD_ALLOC:
			do_alloc(s->alloc.file, s->alloc.align, s->alloc.zone);
			break;
		case CMD_PTR:
			do_ptr(s->ptr.dest, s->ptr.src, s->ptr.offset, s->ptr.size);
			break;
		case CMD_CHECKSUM:
			do_checksum(s->checksum.file, s->checksum.offset,
				    s->checksum.start, s->checksum.len);
			break;
		case CMD_QUIT:
			return;
		default:
			panic();
		}
	}
}