1 #include <console/console.h>
3 #include <ip_checksum.h>
4 #include <boot/linuxbios_tables.h>
5 #include "linuxbios_table.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 void lb_strings(struct lb_header *header)
103 static const struct {
105 const uint8_t *string;
107 { LB_TAG_VERSION, linuxbios_version, },
108 { LB_TAG_EXTRA_VERSION, linuxbios_extra_version, },
109 { LB_TAG_BUILD, linuxbios_build, },
110 { LB_TAG_COMPILE_TIME, linuxbios_compile_time, },
111 { LB_TAG_COMPILE_BY, linuxbios_compile_by, },
112 { LB_TAG_COMPILE_HOST, linuxbios_compile_host, },
113 { LB_TAG_COMPILE_DOMAIN, linuxbios_compile_domain, },
114 { LB_TAG_COMPILER, linuxbios_compiler, },
115 { LB_TAG_LINKER, linuxbios_linker, },
116 { LB_TAG_ASSEMBLER, linuxbios_assembler, },
119 for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
120 struct lb_string *rec;
122 rec = (struct lb_string *)lb_new_record(header);
123 len = strlen(strings[i].string);
124 rec->tag = strings[i].tag;
125 rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
126 memcpy(rec->string, strings[i].string, len+1);
131 /* Some version of gcc have problems with 64 bit types so
132 * take an unsigned long instead of a uint64_t for now.
134 void lb_memory_range(struct lb_memory *mem,
135 uint32_t type, unsigned long start, unsigned long size)
138 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
139 mem->map[entries].start = start;
140 mem->map[entries].size = size;
141 mem->map[entries].type = type;
142 mem->size += sizeof(mem->map[0]);
145 static void lb_memory_rangek(struct lb_memory *mem,
146 uint32_t type, unsigned long startk, unsigned long endk)
149 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
150 mem->map[entries].start = startk;
151 mem->map[entries].start <<= 10;
152 mem->map[entries].size = endk - startk;
153 mem->map[entries].size <<= 10;
154 mem->map[entries].type = type;
155 mem->size += sizeof(mem->map[0]);
158 static void lb_reserve_table_memory(struct lb_header *head)
160 struct lb_record *last_rec;
161 struct lb_memory *mem;
166 last_rec = lb_last_record(head);
168 start = (unsigned long)head;
169 end = (unsigned long)last_rec;
170 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
171 /* Resize the right two memory areas so this table is in
172 * a reserved area of memory. Everything has been carefully
173 * setup so that is all we need to do.
175 for(i = 0; i < entries; i++ ) {
176 uint64_t map_start = mem->map[i].start;
177 uint64_t map_end = map_start + mem->map[i].size;
178 /* Does this area need to be expanded? */
179 if (map_end == start) {
180 mem->map[i].size = end - map_start;
182 /* Does this area need to be contracted? */
183 else if (map_start == start) {
184 mem->map[i].start = end;
185 mem->map[i].size = map_end - end;
191 unsigned long lb_table_fini(struct lb_header *head)
193 struct lb_record *rec, *first_rec;
194 rec = lb_last_record(head);
195 if (head->table_entries) {
196 head->table_bytes += rec->size;
198 lb_reserve_table_memory(head);
199 first_rec = lb_first_record(head);
200 head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
201 head->header_checksum = 0;
202 head->header_checksum = compute_ip_checksum(head, sizeof(*head));
203 printk_debug("Wrote linuxbios table at: %p - %p checksum %lx\n",
204 head, rec, head->table_checksum);
205 return (unsigned long)rec;
209 /* Routines to extract part so the linuxBIOS table or
210 * information from the linuxBIOS table after we have written it.
211 * Currently get_lb_mem relies on a global we can change the
214 static struct lb_memory *mem_ranges = 0;
215 struct lb_memory *get_lb_mem(void)
220 unsigned long write_linuxbios_table(
221 unsigned long *processor_map,
222 struct mem_range *ram,
223 unsigned long low_table_start, unsigned long low_table_end,
224 unsigned long rom_table_startk, unsigned long rom_table_endk)
226 unsigned long table_size;
227 struct mem_range *ramp;
228 struct lb_header *head;
229 struct lb_memory *mem;
230 struct lb_record *rec_dest, *rec_src;
232 head = lb_table_init(low_table_end);
233 low_table_end = (unsigned long)head;
234 #if HAVE_OPTION_TABLE == 1
235 /* Write the option config table... */
236 rec_dest = lb_new_record(head);
237 rec_src = (struct lb_record *)&option_table;
238 memcpy(rec_dest, rec_src, rec_src->size);
240 mem = lb_memory(head);
242 /* I assume there is always ram at address 0 */
243 /* Reserve our tables in low memory */
244 table_size = (low_table_end - low_table_start);
245 lb_memory_range(mem, LB_MEM_TABLE, 0, table_size);
246 lb_memory_range(mem, LB_MEM_RAM, table_size, (ram[0].sizek << 10) - table_size);
247 /* Reserving pci memory mapped space will keep the kernel from booting seeing
250 for(ramp = &ram[1]; ramp->sizek; ramp++) {
251 unsigned long startk, endk;
252 startk = ramp->basek;
253 endk = startk + ramp->sizek;
254 if ((startk < rom_table_startk) && (endk > rom_table_startk)) {
255 lb_memory_rangek(mem, LB_MEM_RAM, startk, rom_table_startk);
256 startk = rom_table_startk;
258 if ((startk == rom_table_startk) && (endk > startk)) {
260 tend = rom_table_endk;
264 lb_memory_rangek(mem, LB_MEM_TABLE, rom_table_startk, tend);
268 lb_memory_rangek(mem, LB_MEM_RAM, startk, endk);
272 /* Record our motheboard */
274 /* Record our various random string information */
277 low_table_end = lb_table_fini(head);
279 /* Remember where my valid memory ranges are */
280 return low_table_end;