2003-04-22 21:02:15 +02:00
|
|
|
#include <console/console.h>
|
|
|
|
#include <ip_checksum.h>
|
|
|
|
#include <boot/linuxbios_tables.h>
|
|
|
|
#include "linuxbios_table.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <version.h>
|
2004-10-14 22:54:17 +02:00
|
|
|
#include <device/device.h>
|
|
|
|
#include <stdlib.h>
|
2003-04-22 21:02:15 +02:00
|
|
|
|
|
|
|
struct lb_header *lb_table_init(unsigned long addr)
|
|
|
|
{
|
|
|
|
struct lb_header *header;
|
|
|
|
|
|
|
|
/* 16 byte align the address */
|
|
|
|
addr += 15;
|
|
|
|
addr &= ~15;
|
|
|
|
|
|
|
|
header = (void *)addr;
|
|
|
|
header->signature[0] = 'L';
|
|
|
|
header->signature[1] = 'B';
|
|
|
|
header->signature[2] = 'I';
|
|
|
|
header->signature[3] = 'O';
|
|
|
|
header->header_bytes = sizeof(*header);
|
|
|
|
header->header_checksum = 0;
|
|
|
|
header->table_bytes = 0;
|
|
|
|
header->table_checksum = 0;
|
|
|
|
header->table_entries = 0;
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct lb_record *lb_first_record(struct lb_header *header)
|
|
|
|
{
|
|
|
|
struct lb_record *rec;
|
|
|
|
rec = (void *)(((char *)header) + sizeof(*header));
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct lb_record *lb_last_record(struct lb_header *header)
|
|
|
|
{
|
|
|
|
struct lb_record *rec;
|
|
|
|
rec = (void *)(((char *)header) + sizeof(*header) + header->table_bytes);
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct lb_record *lb_next_record(struct lb_record *rec)
|
|
|
|
{
|
|
|
|
rec = (void *)(((char *)rec) + rec->size);
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct lb_record *lb_new_record(struct lb_header *header)
|
|
|
|
{
|
|
|
|
struct lb_record *rec;
|
|
|
|
rec = lb_last_record(header);
|
|
|
|
if (header->table_entries) {
|
|
|
|
header->table_bytes += rec->size;
|
|
|
|
}
|
|
|
|
rec = lb_last_record(header);
|
|
|
|
header->table_entries++;
|
|
|
|
rec->tag = LB_TAG_UNUSED;
|
|
|
|
rec->size = sizeof(*rec);
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct lb_memory *lb_memory(struct lb_header *header)
|
|
|
|
{
|
|
|
|
struct lb_record *rec;
|
|
|
|
struct lb_memory *mem;
|
|
|
|
rec = lb_new_record(header);
|
|
|
|
mem = (struct lb_memory *)rec;
|
|
|
|
mem->tag = LB_TAG_MEMORY;
|
|
|
|
mem->size = sizeof(*mem);
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct lb_mainboard *lb_mainboard(struct lb_header *header)
|
|
|
|
{
|
|
|
|
struct lb_record *rec;
|
|
|
|
struct lb_mainboard *mainboard;
|
|
|
|
rec = lb_new_record(header);
|
|
|
|
mainboard = (struct lb_mainboard *)rec;
|
|
|
|
mainboard->tag = LB_TAG_MAINBOARD;
|
|
|
|
|
|
|
|
mainboard->size = (sizeof(*mainboard) +
|
|
|
|
strlen(mainboard_vendor) + 1 +
|
|
|
|
strlen(mainboard_part_number) + 1 +
|
|
|
|
3) & ~3;
|
|
|
|
|
|
|
|
mainboard->vendor_idx = 0;
|
|
|
|
mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
|
|
|
|
|
|
|
|
memcpy(mainboard->strings + mainboard->vendor_idx,
|
|
|
|
mainboard_vendor, strlen(mainboard_vendor) + 1);
|
|
|
|
memcpy(mainboard->strings + mainboard->part_number_idx,
|
|
|
|
mainboard_part_number, strlen(mainboard_part_number) + 1);
|
|
|
|
|
|
|
|
return mainboard;
|
|
|
|
}
|
|
|
|
|
2005-12-03 23:39:23 +01:00
|
|
|
struct cmos_checksum *lb_cmos_checksum(struct lb_header *header)
|
|
|
|
{
|
|
|
|
struct lb_record *rec;
|
|
|
|
struct cmos_checksum *cmos_checksum;
|
|
|
|
rec = lb_new_record(header);
|
|
|
|
cmos_checksum = (struct cmos_checksum *)rec;
|
|
|
|
cmos_checksum->tag = LB_TAG_OPTION_CHECKSUM;
|
|
|
|
|
|
|
|
cmos_checksum->size = (sizeof(*cmos_checksum));
|
|
|
|
|
|
|
|
cmos_checksum->range_start = LB_CKS_RANGE_START * 8;
|
|
|
|
cmos_checksum->range_end = ( LB_CKS_RANGE_END * 8 ) + 7;
|
|
|
|
cmos_checksum->location = LB_CKS_LOC * 8;
|
|
|
|
cmos_checksum->type = CHECKSUM_PCBIOS;
|
|
|
|
|
|
|
|
return cmos_checksum;
|
|
|
|
}
|
|
|
|
|
2003-04-22 21:02:15 +02:00
|
|
|
void lb_strings(struct lb_header *header)
|
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
uint32_t tag;
|
|
|
|
const uint8_t *string;
|
|
|
|
} strings[] = {
|
|
|
|
{ LB_TAG_VERSION, linuxbios_version, },
|
|
|
|
{ LB_TAG_EXTRA_VERSION, linuxbios_extra_version, },
|
|
|
|
{ LB_TAG_BUILD, linuxbios_build, },
|
|
|
|
{ LB_TAG_COMPILE_TIME, linuxbios_compile_time, },
|
|
|
|
{ LB_TAG_COMPILE_BY, linuxbios_compile_by, },
|
|
|
|
{ LB_TAG_COMPILE_HOST, linuxbios_compile_host, },
|
|
|
|
{ LB_TAG_COMPILE_DOMAIN, linuxbios_compile_domain, },
|
|
|
|
{ LB_TAG_COMPILER, linuxbios_compiler, },
|
|
|
|
{ LB_TAG_LINKER, linuxbios_linker, },
|
|
|
|
{ LB_TAG_ASSEMBLER, linuxbios_assembler, },
|
|
|
|
};
|
2003-05-19 21:16:21 +02:00
|
|
|
unsigned int i;
|
2003-04-22 21:02:15 +02:00
|
|
|
for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
|
|
|
|
struct lb_string *rec;
|
|
|
|
size_t len;
|
|
|
|
rec = (struct lb_string *)lb_new_record(header);
|
|
|
|
len = strlen(strings[i].string);
|
|
|
|
rec->tag = strings[i].tag;
|
|
|
|
rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
|
|
|
|
memcpy(rec->string, strings[i].string, len+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void lb_memory_range(struct lb_memory *mem,
|
2004-10-27 10:53:57 +02:00
|
|
|
uint32_t type, uint64_t start, uint64_t size)
|
2003-04-22 21:02:15 +02:00
|
|
|
{
|
|
|
|
int entries;
|
|
|
|
entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
|
2004-12-10 21:50:43 +01:00
|
|
|
mem->map[entries].start = pack_lb64(start);
|
|
|
|
mem->map[entries].size = pack_lb64(size);
|
2003-04-22 21:02:15 +02:00
|
|
|
mem->map[entries].type = type;
|
|
|
|
mem->size += sizeof(mem->map[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lb_reserve_table_memory(struct lb_header *head)
|
|
|
|
{
|
|
|
|
struct lb_record *last_rec;
|
|
|
|
struct lb_memory *mem;
|
|
|
|
uint64_t start;
|
|
|
|
uint64_t end;
|
|
|
|
int i, entries;
|
|
|
|
|
|
|
|
last_rec = lb_last_record(head);
|
|
|
|
mem = get_lb_mem();
|
|
|
|
start = (unsigned long)head;
|
|
|
|
end = (unsigned long)last_rec;
|
|
|
|
entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
|
|
|
|
/* Resize the right two memory areas so this table is in
|
|
|
|
* a reserved area of memory. Everything has been carefully
|
|
|
|
* setup so that is all we need to do.
|
|
|
|
*/
|
|
|
|
for(i = 0; i < entries; i++ ) {
|
2004-12-10 21:50:43 +01:00
|
|
|
uint64_t map_start = unpack_lb64(mem->map[i].start);
|
|
|
|
uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
|
2003-04-22 21:02:15 +02:00
|
|
|
/* Does this area need to be expanded? */
|
|
|
|
if (map_end == start) {
|
2004-12-10 21:50:43 +01:00
|
|
|
mem->map[i].size = pack_lb64(end - map_start);
|
2003-04-22 21:02:15 +02:00
|
|
|
}
|
|
|
|
/* Does this area need to be contracted? */
|
|
|
|
else if (map_start == start) {
|
2004-12-10 21:50:43 +01:00
|
|
|
mem->map[i].start = pack_lb64(end);
|
|
|
|
mem->map[i].size = pack_lb64(map_end - end);
|
2003-04-22 21:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long lb_table_fini(struct lb_header *head)
|
|
|
|
{
|
|
|
|
struct lb_record *rec, *first_rec;
|
|
|
|
rec = lb_last_record(head);
|
|
|
|
if (head->table_entries) {
|
|
|
|
head->table_bytes += rec->size;
|
|
|
|
}
|
|
|
|
lb_reserve_table_memory(head);
|
|
|
|
first_rec = lb_first_record(head);
|
|
|
|
head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
|
|
|
|
head->header_checksum = 0;
|
|
|
|
head->header_checksum = compute_ip_checksum(head, sizeof(*head));
|
|
|
|
printk_debug("Wrote linuxbios table at: %p - %p checksum %lx\n",
|
|
|
|
head, rec, head->table_checksum);
|
|
|
|
return (unsigned long)rec;
|
|
|
|
}
|
|
|
|
|
2004-10-27 10:53:57 +02:00
|
|
|
static void lb_cleanup_memory_ranges(struct lb_memory *mem)
|
|
|
|
{
|
|
|
|
int entries;
|
|
|
|
int i, j;
|
|
|
|
entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
|
|
|
|
|
|
|
|
/* Sort the lb memory ranges */
|
|
|
|
for(i = 0; i < entries; i++) {
|
2004-12-10 21:50:43 +01:00
|
|
|
uint64_t entry_start = unpack_lb64(mem->map[i].start);
|
2004-10-27 10:53:57 +02:00
|
|
|
for(j = i; j < entries; j++) {
|
2004-12-10 21:50:43 +01:00
|
|
|
uint64_t temp_start = unpack_lb64(mem->map[j].start);
|
|
|
|
if (temp_start < entry_start) {
|
2004-10-27 10:53:57 +02:00
|
|
|
struct lb_memory_range tmp;
|
|
|
|
tmp = mem->map[i];
|
|
|
|
mem->map[i] = mem->map[j];
|
|
|
|
mem->map[j] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge adjacent entries */
|
|
|
|
for(i = 0; (i + 1) < entries; i++) {
|
|
|
|
uint64_t start, end, nstart, nend;
|
|
|
|
if (mem->map[i].type != mem->map[i + 1].type) {
|
|
|
|
continue;
|
|
|
|
}
|
2004-12-10 21:50:43 +01:00
|
|
|
start = unpack_lb64(mem->map[i].start);
|
|
|
|
end = start + unpack_lb64(mem->map[i].size);
|
|
|
|
nstart = unpack_lb64(mem->map[i + 1].start);
|
|
|
|
nend = nstart + unpack_lb64(mem->map[i + 1].size);
|
2004-10-27 10:53:57 +02:00
|
|
|
if ((start <= nstart) && (end > nstart)) {
|
|
|
|
if (start > nstart) {
|
|
|
|
start = nstart;
|
|
|
|
}
|
|
|
|
if (end < nend) {
|
|
|
|
end = nend;
|
|
|
|
}
|
|
|
|
/* Record the new region size */
|
2004-12-10 21:50:43 +01:00
|
|
|
mem->map[i].start = pack_lb64(start);
|
|
|
|
mem->map[i].size = pack_lb64(end - start);
|
2004-10-27 10:53:57 +02:00
|
|
|
|
|
|
|
/* Delete the entry I have merged with */
|
|
|
|
memmove(&mem->map[i + 1], &mem->map[i + 2],
|
|
|
|
((entries - i - 2) * sizeof(mem->map[0])));
|
|
|
|
mem->size -= sizeof(mem->map[0]);
|
|
|
|
entries -= 1;
|
|
|
|
/* See if I can merge with the next entry as well */
|
|
|
|
i -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lb_remove_memory_range(struct lb_memory *mem,
|
|
|
|
uint64_t start, uint64_t size)
|
|
|
|
{
|
|
|
|
uint64_t end;
|
|
|
|
int entries;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
end = start + size;
|
|
|
|
entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
|
|
|
|
|
|
|
|
/* Remove a reserved area from the memory map */
|
|
|
|
for(i = 0; i < entries; i++) {
|
2004-12-10 21:50:43 +01:00
|
|
|
uint64_t map_start = unpack_lb64(mem->map[i].start);
|
|
|
|
uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
|
2004-10-27 10:53:57 +02:00
|
|
|
if ((start <= map_start) && (end >= map_end)) {
|
|
|
|
/* Remove the completely covered range */
|
|
|
|
memmove(&mem->map[i], &mem->map[i + 1],
|
|
|
|
((entries - i - 1) * sizeof(mem->map[0])));
|
|
|
|
mem->size -= sizeof(mem->map[0]);
|
|
|
|
entries -= 1;
|
|
|
|
/* Since the index will disappear revisit what will appear here */
|
|
|
|
i -= 1;
|
|
|
|
}
|
|
|
|
else if ((start > map_start) && (end < map_end)) {
|
|
|
|
/* Split the memory range */
|
|
|
|
memmove(&mem->map[i + 1], &mem->map[i],
|
|
|
|
((entries - i) * sizeof(mem->map[0])));
|
|
|
|
mem->size += sizeof(mem->map[0]);
|
|
|
|
entries += 1;
|
|
|
|
/* Update the first map entry */
|
2004-12-10 21:50:43 +01:00
|
|
|
mem->map[i].size = pack_lb64(start - map_start);
|
2004-10-27 10:53:57 +02:00
|
|
|
/* Update the second map entry */
|
2004-12-10 21:50:43 +01:00
|
|
|
mem->map[i + 1].start = pack_lb64(end);
|
|
|
|
mem->map[i + 1].size = pack_lb64(map_end - end);
|
2004-10-27 10:53:57 +02:00
|
|
|
/* Don't bother with this map entry again */
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
else if ((start <= map_start) && (end > map_start)) {
|
|
|
|
/* Shrink the start of the memory range */
|
2004-12-10 21:50:43 +01:00
|
|
|
mem->map[i].start = pack_lb64(end);
|
|
|
|
mem->map[i].size = pack_lb64(map_end - end);
|
2004-10-27 10:53:57 +02:00
|
|
|
}
|
|
|
|
else if ((start < map_end) && (start > map_start)) {
|
|
|
|
/* Shrink the end of the memory range */
|
2004-12-10 21:50:43 +01:00
|
|
|
mem->map[i].size = pack_lb64(start - map_start);
|
2004-10-27 10:53:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lb_add_memory_range(struct lb_memory *mem,
|
|
|
|
uint32_t type, uint64_t start, uint64_t size)
|
|
|
|
{
|
|
|
|
lb_remove_memory_range(mem, start, size);
|
|
|
|
lb_memory_range(mem, type, start, size);
|
|
|
|
lb_cleanup_memory_ranges(mem);
|
|
|
|
}
|
2003-04-22 21:02:15 +02:00
|
|
|
|
|
|
|
/* Routines to extract part so the linuxBIOS table or
|
|
|
|
* information from the linuxBIOS table after we have written it.
|
|
|
|
* Currently get_lb_mem relies on a global we can change the
|
|
|
|
* implementaiton.
|
|
|
|
*/
|
|
|
|
static struct lb_memory *mem_ranges = 0;
|
|
|
|
struct lb_memory *get_lb_mem(void)
|
|
|
|
{
|
|
|
|
return mem_ranges;
|
|
|
|
}
|
|
|
|
|
2004-10-30 10:05:41 +02:00
|
|
|
static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
|
|
|
|
{
|
|
|
|
struct lb_memory *mem = gp;
|
|
|
|
lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
|
|
|
|
}
|
|
|
|
|
2004-10-27 10:53:57 +02:00
|
|
|
static struct lb_memory *build_lb_mem(struct lb_header *head)
|
2004-10-14 22:54:17 +02:00
|
|
|
{
|
2004-10-27 10:53:57 +02:00
|
|
|
struct lb_memory *mem;
|
|
|
|
|
|
|
|
/* Record where the lb memory ranges will live */
|
|
|
|
mem = lb_memory(head);
|
|
|
|
mem_ranges = mem;
|
|
|
|
|
|
|
|
/* Build the raw table of memory */
|
2004-10-30 10:05:41 +02:00
|
|
|
search_global_resources(
|
|
|
|
IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
|
|
|
|
build_lb_mem_range, mem);
|
2004-10-27 10:53:57 +02:00
|
|
|
lb_cleanup_memory_ranges(mem);
|
2004-10-14 22:54:17 +02:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2003-04-22 21:02:15 +02:00
|
|
|
unsigned long write_linuxbios_table(
|
2006-07-19 17:32:49 +02:00
|
|
|
unsigned long low_table_start, unsigned long low_table_end,
|
|
|
|
unsigned long rom_table_start, unsigned long rom_table_end)
|
2003-04-22 21:02:15 +02:00
|
|
|
{
|
|
|
|
unsigned long table_size;
|
|
|
|
struct lb_header *head;
|
|
|
|
struct lb_memory *mem;
|
|
|
|
|
2006-07-19 17:32:49 +02:00
|
|
|
if(low_table_end > (0x1000 - sizeof(struct lb_header))) { /* after 4K */
|
|
|
|
/* We need to put lbtable on to [0xf0000,0x100000) */
|
|
|
|
head = lb_table_init(rom_table_end);
|
|
|
|
rom_table_end = (unsigned long)head;
|
|
|
|
} else {
|
|
|
|
head = lb_table_init(low_table_end);
|
|
|
|
low_table_end = (unsigned long)head;
|
|
|
|
}
|
|
|
|
|
2007-04-06 14:14:51 +02:00
|
|
|
#if (HAVE_OPTION_TABLE == 1)
|
|
|
|
{
|
2004-10-27 10:53:57 +02:00
|
|
|
struct lb_record *rec_dest, *rec_src;
|
|
|
|
/* Write the option config table... */
|
|
|
|
rec_dest = lb_new_record(head);
|
2004-11-18 23:38:08 +01:00
|
|
|
rec_src = (struct lb_record *)(void *)&option_table;
|
2004-10-27 10:53:57 +02:00
|
|
|
memcpy(rec_dest, rec_src, rec_src->size);
|
2005-12-03 23:39:23 +01:00
|
|
|
/* Create cmos checksum entry in linuxbios table */
|
|
|
|
lb_cmos_checksum(head);
|
2004-10-27 10:53:57 +02:00
|
|
|
}
|
2007-04-06 14:14:51 +02:00
|
|
|
#endif
|
2004-10-27 10:53:57 +02:00
|
|
|
/* Record where RAM is located */
|
|
|
|
mem = build_lb_mem(head);
|
|
|
|
|
|
|
|
/* Record the mptable and the the lb_table (This will be adjusted later) */
|
|
|
|
lb_add_memory_range(mem, LB_MEM_TABLE,
|
2006-07-19 17:32:49 +02:00
|
|
|
low_table_start, low_table_end - low_table_start);
|
2004-10-27 10:53:57 +02:00
|
|
|
|
2006-07-19 17:32:49 +02:00
|
|
|
/* Record the pirq table, acpi tables, and maybe the mptable */
|
2007-02-01 01:44:27 +01:00
|
|
|
table_size=rom_table_end-rom_table_start;
|
2004-10-27 10:53:57 +02:00
|
|
|
lb_add_memory_range(mem, LB_MEM_TABLE,
|
2007-02-01 01:44:27 +01:00
|
|
|
rom_table_start, table_size<0x10000?0x10000:table_size);
|
2004-10-27 10:53:57 +02:00
|
|
|
|
|
|
|
/* Note:
|
|
|
|
* I assume that there is always memory at immediately after
|
|
|
|
* the low_table_end. This means that after I setup the linuxbios table.
|
|
|
|
* I can trivially fixup the reserved memory ranges to hold the correct
|
|
|
|
* size of the linuxbios table.
|
2003-04-22 21:02:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Record our motheboard */
|
|
|
|
lb_mainboard(head);
|
|
|
|
/* Record our various random string information */
|
|
|
|
lb_strings(head);
|
|
|
|
|
|
|
|
/* Remember where my valid memory ranges are */
|
2006-07-19 17:32:49 +02:00
|
|
|
return lb_table_fini(head);
|
|
|
|
|
2003-04-22 21:02:15 +02:00
|
|
|
}
|