1 #include <console/console.h>
2 #include <ip_checksum.h>
3 #include <boot/linuxbios_tables.h>
4 #include "linuxbios_table.h"
7 #include <device/device.h>
10 struct lb_header *lb_table_init(unsigned long addr)
12 struct lb_header *header;
14 /* 16 byte align the address */
18 header = (void *)addr;
19 header->signature[0] = 'L';
20 header->signature[1] = 'B';
21 header->signature[2] = 'I';
22 header->signature[3] = 'O';
23 header->header_bytes = sizeof(*header);
24 header->header_checksum = 0;
25 header->table_bytes = 0;
26 header->table_checksum = 0;
27 header->table_entries = 0;
31 struct lb_record *lb_first_record(struct lb_header *header)
33 struct lb_record *rec;
34 rec = (void *)(((char *)header) + sizeof(*header));
38 struct lb_record *lb_last_record(struct lb_header *header)
40 struct lb_record *rec;
41 rec = (void *)(((char *)header) + sizeof(*header) + header->table_bytes);
45 struct lb_record *lb_next_record(struct lb_record *rec)
47 rec = (void *)(((char *)rec) + rec->size);
51 struct lb_record *lb_new_record(struct lb_header *header)
53 struct lb_record *rec;
54 rec = lb_last_record(header);
55 if (header->table_entries) {
56 header->table_bytes += rec->size;
58 rec = lb_last_record(header);
59 header->table_entries++;
60 rec->tag = LB_TAG_UNUSED;
61 rec->size = sizeof(*rec);
66 struct lb_memory *lb_memory(struct lb_header *header)
68 struct lb_record *rec;
69 struct lb_memory *mem;
70 rec = lb_new_record(header);
71 mem = (struct lb_memory *)rec;
72 mem->tag = LB_TAG_MEMORY;
73 mem->size = sizeof(*mem);
77 struct lb_mainboard *lb_mainboard(struct lb_header *header)
79 struct lb_record *rec;
80 struct lb_mainboard *mainboard;
81 rec = lb_new_record(header);
82 mainboard = (struct lb_mainboard *)rec;
83 mainboard->tag = LB_TAG_MAINBOARD;
85 mainboard->size = (sizeof(*mainboard) +
86 strlen(mainboard_vendor) + 1 +
87 strlen(mainboard_part_number) + 1 +
90 mainboard->vendor_idx = 0;
91 mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
93 memcpy(mainboard->strings + mainboard->vendor_idx,
94 mainboard_vendor, strlen(mainboard_vendor) + 1);
95 memcpy(mainboard->strings + mainboard->part_number_idx,
96 mainboard_part_number, strlen(mainboard_part_number) + 1);
101 struct cmos_checksum *lb_cmos_checksum(struct lb_header *header)
103 struct lb_record *rec;
104 struct cmos_checksum *cmos_checksum;
105 rec = lb_new_record(header);
106 cmos_checksum = (struct cmos_checksum *)rec;
107 cmos_checksum->tag = LB_TAG_OPTION_CHECKSUM;
109 cmos_checksum->size = (sizeof(*cmos_checksum));
111 cmos_checksum->range_start = LB_CKS_RANGE_START * 8;
112 cmos_checksum->range_end = ( LB_CKS_RANGE_END * 8 ) + 7;
113 cmos_checksum->location = LB_CKS_LOC * 8;
114 cmos_checksum->type = CHECKSUM_PCBIOS;
116 return cmos_checksum;
119 void lb_strings(struct lb_header *header)
121 static const struct {
125 { LB_TAG_VERSION, coreboot_version, },
126 { LB_TAG_EXTRA_VERSION, coreboot_extra_version, },
127 { LB_TAG_BUILD, coreboot_build, },
128 { LB_TAG_COMPILE_TIME, coreboot_compile_time, },
129 { LB_TAG_COMPILE_BY, coreboot_compile_by, },
130 { LB_TAG_COMPILE_HOST, coreboot_compile_host, },
131 { LB_TAG_COMPILE_DOMAIN, coreboot_compile_domain, },
132 { LB_TAG_COMPILER, coreboot_compiler, },
133 { LB_TAG_LINKER, coreboot_linker, },
134 { LB_TAG_ASSEMBLER, coreboot_assembler, },
137 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
138 struct lb_string *rec;
140 rec = (struct lb_string *)lb_new_record(header);
141 len = strlen(strings[i].string);
142 rec->tag = strings[i].tag;
143 rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
144 memcpy(rec->string, strings[i].string, len+1);
149 void lb_memory_range(struct lb_memory *mem,
150 uint32_t type, uint64_t start, uint64_t size)
153 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
154 mem->map[entries].start = pack_lb64(start);
155 mem->map[entries].size = pack_lb64(size);
156 mem->map[entries].type = type;
157 mem->size += sizeof(mem->map[0]);
160 static void lb_reserve_table_memory(struct lb_header *head)
162 struct lb_record *last_rec;
163 struct lb_memory *mem;
168 last_rec = lb_last_record(head);
170 start = (unsigned long)head;
171 end = (unsigned long)last_rec;
172 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
173 /* Resize the right two memory areas so this table is in
174 * a reserved area of memory. Everything has been carefully
175 * setup so that is all we need to do.
177 for(i = 0; i < entries; i++ ) {
178 uint64_t map_start = unpack_lb64(mem->map[i].start);
179 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
180 /* Does this area need to be expanded? */
181 if (map_end == start) {
182 mem->map[i].size = pack_lb64(end - map_start);
184 /* Does this area need to be contracted? */
185 else if (map_start == start) {
186 mem->map[i].start = pack_lb64(end);
187 mem->map[i].size = pack_lb64(map_end - end);
192 unsigned long lb_table_fini(struct lb_header *head)
194 struct lb_record *rec, *first_rec;
195 rec = lb_last_record(head);
196 if (head->table_entries) {
197 head->table_bytes += rec->size;
199 lb_reserve_table_memory(head);
200 first_rec = lb_first_record(head);
201 head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
202 head->header_checksum = 0;
203 head->header_checksum = compute_ip_checksum(head, sizeof(*head));
204 printk_debug("Wrote coreboot table at: %p - %p checksum %lx\n",
205 head, rec, head->table_checksum);
206 return (unsigned long)rec;
209 static void lb_cleanup_memory_ranges(struct lb_memory *mem)
213 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
215 /* Sort the lb memory ranges */
216 for(i = 0; i < entries; i++) {
217 uint64_t entry_start = unpack_lb64(mem->map[i].start);
218 for(j = i; j < entries; j++) {
219 uint64_t temp_start = unpack_lb64(mem->map[j].start);
220 if (temp_start < entry_start) {
221 struct lb_memory_range tmp;
223 mem->map[i] = mem->map[j];
229 /* Merge adjacent entries */
230 for(i = 0; (i + 1) < entries; i++) {
231 uint64_t start, end, nstart, nend;
232 if (mem->map[i].type != mem->map[i + 1].type) {
235 start = unpack_lb64(mem->map[i].start);
236 end = start + unpack_lb64(mem->map[i].size);
237 nstart = unpack_lb64(mem->map[i + 1].start);
238 nend = nstart + unpack_lb64(mem->map[i + 1].size);
239 if ((start <= nstart) && (end > nstart)) {
240 if (start > nstart) {
246 /* Record the new region size */
247 mem->map[i].start = pack_lb64(start);
248 mem->map[i].size = pack_lb64(end - start);
250 /* Delete the entry I have merged with */
251 memmove(&mem->map[i + 1], &mem->map[i + 2],
252 ((entries - i - 2) * sizeof(mem->map[0])));
253 mem->size -= sizeof(mem->map[0]);
255 /* See if I can merge with the next entry as well */
261 static void lb_remove_memory_range(struct lb_memory *mem,
262 uint64_t start, uint64_t size)
269 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
271 /* Remove a reserved area from the memory map */
272 for(i = 0; i < entries; i++) {
273 uint64_t map_start = unpack_lb64(mem->map[i].start);
274 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
275 if ((start <= map_start) && (end >= map_end)) {
276 /* Remove the completely covered range */
277 memmove(&mem->map[i], &mem->map[i + 1],
278 ((entries - i - 1) * sizeof(mem->map[0])));
279 mem->size -= sizeof(mem->map[0]);
281 /* Since the index will disappear revisit what will appear here */
284 else if ((start > map_start) && (end < map_end)) {
285 /* Split the memory range */
286 memmove(&mem->map[i + 1], &mem->map[i],
287 ((entries - i) * sizeof(mem->map[0])));
288 mem->size += sizeof(mem->map[0]);
290 /* Update the first map entry */
291 mem->map[i].size = pack_lb64(start - map_start);
292 /* Update the second map entry */
293 mem->map[i + 1].start = pack_lb64(end);
294 mem->map[i + 1].size = pack_lb64(map_end - end);
295 /* Don't bother with this map entry again */
298 else if ((start <= map_start) && (end > map_start)) {
299 /* Shrink the start of the memory range */
300 mem->map[i].start = pack_lb64(end);
301 mem->map[i].size = pack_lb64(map_end - end);
303 else if ((start < map_end) && (start > map_start)) {
304 /* Shrink the end of the memory range */
305 mem->map[i].size = pack_lb64(start - map_start);
310 static void lb_add_memory_range(struct lb_memory *mem,
311 uint32_t type, uint64_t start, uint64_t size)
313 lb_remove_memory_range(mem, start, size);
314 lb_memory_range(mem, type, start, size);
315 lb_cleanup_memory_ranges(mem);
318 /* Routines to extract part so the coreboot table or
319 * information from the coreboot table after we have written it.
320 * Currently get_lb_mem relies on a global we can change the
323 static struct lb_memory *mem_ranges = 0;
324 struct lb_memory *get_lb_mem(void)
329 static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
331 struct lb_memory *mem = gp;
332 lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
335 static struct lb_memory *build_lb_mem(struct lb_header *head)
337 struct lb_memory *mem;
339 /* Record where the lb memory ranges will live */
340 mem = lb_memory(head);
343 /* Build the raw table of memory */
344 search_global_resources(
345 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
346 build_lb_mem_range, mem);
347 lb_cleanup_memory_ranges(mem);
351 unsigned long write_coreboot_table(
352 unsigned long low_table_start, unsigned long low_table_end,
353 unsigned long rom_table_start, unsigned long rom_table_end)
355 unsigned long table_size;
356 struct lb_header *head;
357 struct lb_memory *mem;
359 if(low_table_end > (0x1000 - sizeof(struct lb_header))) { /* after 4K */
360 /* We need to put lbtable on to [0xf0000,0x100000) */
361 head = lb_table_init(rom_table_end);
362 rom_table_end = (unsigned long)head;
364 head = lb_table_init(low_table_end);
365 low_table_end = (unsigned long)head;
368 printk_debug("Adjust low_table_end from 0x%08x to ", low_table_end);
369 low_table_end += 0xfff; // 4K aligned
370 low_table_end &= ~0xfff;
371 printk_debug("0x%08x \n", low_table_end);
373 /* The Linux kernel assumes this region is reserved */
374 printk_debug("Adjust rom_table_end from 0x%08x to ", rom_table_end);
375 rom_table_end += 0xffff; // 64K align
376 rom_table_end &= ~0xffff;
377 printk_debug("0x%08x \n", rom_table_end);
379 #if (HAVE_OPTION_TABLE == 1)
381 struct lb_record *rec_dest, *rec_src;
382 /* Write the option config table... */
383 rec_dest = lb_new_record(head);
384 rec_src = (struct lb_record *)(void *)&option_table;
385 memcpy(rec_dest, rec_src, rec_src->size);
386 /* Create cmos checksum entry in coreboot table */
387 lb_cmos_checksum(head);
390 /* Record where RAM is located */
391 mem = build_lb_mem(head);
393 /* Record the mptable and the the lb_table (This will be adjusted later) */
394 lb_add_memory_range(mem, LB_MEM_TABLE,
395 low_table_start, low_table_end - low_table_start);
397 /* Record the pirq table, acpi tables, and maybe the mptable */
398 table_size=rom_table_end-rom_table_start;
399 lb_add_memory_range(mem, LB_MEM_TABLE,
400 rom_table_start, table_size<0x10000?0x10000:table_size);
403 * I assume that there is always memory at immediately after
404 * the low_table_end. This means that after I setup the coreboot table.
405 * I can trivially fixup the reserved memory ranges to hold the correct
406 * size of the coreboot table.
409 /* Record our motheboard */
411 /* Record our various random string information */
414 /* Remember where my valid memory ranges are */
415 return lb_table_fini(head);