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 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 void lb_memory_range(struct lb_memory *mem,
132 uint32_t type, uint64_t start, uint64_t size)
135 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
136 mem->map[entries].start = start;
137 mem->map[entries].size = size;
138 mem->map[entries].type = type;
139 mem->size += sizeof(mem->map[0]);
142 static void lb_reserve_table_memory(struct lb_header *head)
144 struct lb_record *last_rec;
145 struct lb_memory *mem;
150 last_rec = lb_last_record(head);
152 start = (unsigned long)head;
153 end = (unsigned long)last_rec;
154 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
155 /* Resize the right two memory areas so this table is in
156 * a reserved area of memory. Everything has been carefully
157 * setup so that is all we need to do.
159 for(i = 0; i < entries; i++ ) {
160 uint64_t map_start = mem->map[i].start;
161 uint64_t map_end = map_start + mem->map[i].size;
162 /* Does this area need to be expanded? */
163 if (map_end == start) {
164 mem->map[i].size = end - map_start;
166 /* Does this area need to be contracted? */
167 else if (map_start == start) {
168 mem->map[i].start = end;
169 mem->map[i].size = map_end - end;
174 unsigned long lb_table_fini(struct lb_header *head)
176 struct lb_record *rec, *first_rec;
177 rec = lb_last_record(head);
178 if (head->table_entries) {
179 head->table_bytes += rec->size;
181 lb_reserve_table_memory(head);
182 first_rec = lb_first_record(head);
183 head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
184 head->header_checksum = 0;
185 head->header_checksum = compute_ip_checksum(head, sizeof(*head));
186 printk_debug("Wrote linuxbios table at: %p - %p checksum %lx\n",
187 head, rec, head->table_checksum);
188 return (unsigned long)rec;
191 static void lb_cleanup_memory_ranges(struct lb_memory *mem)
195 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
197 /* Sort the lb memory ranges */
198 for(i = 0; i < entries; i++) {
199 for(j = i; j < entries; j++) {
200 if (mem->map[j].start < mem->map[i].start) {
201 struct lb_memory_range tmp;
203 mem->map[i] = mem->map[j];
209 /* Merge adjacent entries */
210 for(i = 0; (i + 1) < entries; i++) {
211 uint64_t start, end, nstart, nend;
212 if (mem->map[i].type != mem->map[i + 1].type) {
215 start = mem->map[i].start;
216 end = start + mem->map[i].size;
217 nstart = mem->map[i + 1].start;
218 nend = nstart + mem->map[i + 1].size;
219 if ((start <= nstart) && (end > nstart)) {
220 if (start > nstart) {
226 /* Record the new region size */
227 mem->map[i].start = start;
228 mem->map[i].size = end - start;
230 /* Delete the entry I have merged with */
231 memmove(&mem->map[i + 1], &mem->map[i + 2],
232 ((entries - i - 2) * sizeof(mem->map[0])));
233 mem->size -= sizeof(mem->map[0]);
235 /* See if I can merge with the next entry as well */
241 static void lb_remove_memory_range(struct lb_memory *mem,
242 uint64_t start, uint64_t size)
249 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
251 /* Remove a reserved area from the memory map */
252 for(i = 0; i < entries; i++) {
253 uint64_t map_start = mem->map[i].start;
254 uint64_t map_end = map_start + mem->map[i].size;
255 if ((start <= map_start) && (end >= map_end)) {
256 /* Remove the completely covered range */
257 memmove(&mem->map[i], &mem->map[i + 1],
258 ((entries - i - 1) * sizeof(mem->map[0])));
259 mem->size -= sizeof(mem->map[0]);
261 /* Since the index will disappear revisit what will appear here */
264 else if ((start > map_start) && (end < map_end)) {
265 /* Split the memory range */
266 memmove(&mem->map[i + 1], &mem->map[i],
267 ((entries - i) * sizeof(mem->map[0])));
268 mem->size += sizeof(mem->map[0]);
270 /* Update the first map entry */
271 mem->map[i].size = start - map_start;
272 /* Update the second map entry */
273 mem->map[i + 1].start = end;
274 mem->map[i + 1].size = map_end - end;
275 /* Don't bother with this map entry again */
278 else if ((start <= map_start) && (end > map_start)) {
279 /* Shrink the start of the memory range */
280 mem->map[i].start = end;
281 mem->map[i].size = map_end - end;
283 else if ((start < map_end) && (start > map_start)) {
284 /* Shrink the end of the memory range */
285 mem->map[i].size = start - map_start;
290 static void lb_add_memory_range(struct lb_memory *mem,
291 uint32_t type, uint64_t start, uint64_t size)
293 lb_remove_memory_range(mem, start, size);
294 lb_memory_range(mem, type, start, size);
295 lb_cleanup_memory_ranges(mem);
298 /* Routines to extract part so the linuxBIOS table or
299 * information from the linuxBIOS table after we have written it.
300 * Currently get_lb_mem relies on a global we can change the
303 static struct lb_memory *mem_ranges = 0;
304 struct lb_memory *get_lb_mem(void)
309 static struct lb_memory *build_lb_mem(struct lb_header *head)
311 struct lb_memory *mem;
314 /* Record where the lb memory ranges will live */
315 mem = lb_memory(head);
318 /* Build the raw table of memory */
319 for(dev = all_devices; dev; dev = dev->next) {
320 struct resource *res, *last;
321 last = &dev->resource[dev->resources];
322 for(res = &dev->resource[0]; res < last; res++) {
323 if (!(res->flags & IORESOURCE_MEM) ||
324 !(res->flags & IORESOURCE_CACHEABLE)) {
327 lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
330 lb_cleanup_memory_ranges(mem);
334 unsigned long write_linuxbios_table(
335 unsigned long low_table_start, unsigned long low_table_end,
336 unsigned long rom_table_startk, unsigned long rom_table_endk)
338 unsigned long table_size;
339 struct lb_header *head;
340 struct lb_memory *mem;
342 head = lb_table_init(low_table_end);
343 low_table_end = (unsigned long)head;
344 if (HAVE_OPTION_TABLE == 1) {
345 struct lb_record *rec_dest, *rec_src;
346 /* Write the option config table... */
347 rec_dest = lb_new_record(head);
348 rec_src = (struct lb_record *)&option_table;
349 memcpy(rec_dest, rec_src, rec_src->size);
351 /* Record where RAM is located */
352 mem = build_lb_mem(head);
354 /* Find the current mptable size */
355 table_size = (low_table_end - low_table_start);
357 /* Record the mptable and the the lb_table (This will be adjusted later) */
358 lb_add_memory_range(mem, LB_MEM_TABLE,
359 low_table_start, table_size);
361 /* Record the pirq table */
362 lb_add_memory_range(mem, LB_MEM_TABLE,
363 rom_table_startk << 10, (rom_table_endk - rom_table_startk) << 10);
366 * I assume that there is always memory at immediately after
367 * the low_table_end. This means that after I setup the linuxbios table.
368 * I can trivially fixup the reserved memory ranges to hold the correct
369 * size of the linuxbios table.
372 /* Record our motheboard */
374 /* Record our various random string information */
377 low_table_end = lb_table_fini(head);
379 /* Remember where my valid memory ranges are */
380 return low_table_end;