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 = pack_lb64(start);
137 mem->map[entries].size = pack_lb64(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 = unpack_lb64(mem->map[i].start);
161 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
162 /* Does this area need to be expanded? */
163 if (map_end == start) {
164 mem->map[i].size = pack_lb64(end - map_start);
166 /* Does this area need to be contracted? */
167 else if (map_start == start) {
168 mem->map[i].start = pack_lb64(end);
169 mem->map[i].size = pack_lb64(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 uint64_t entry_start = unpack_lb64(mem->map[i].start);
200 for(j = i; j < entries; j++) {
201 uint64_t temp_start = unpack_lb64(mem->map[j].start);
202 if (temp_start < entry_start) {
203 struct lb_memory_range tmp;
205 mem->map[i] = mem->map[j];
211 /* Merge adjacent entries */
212 for(i = 0; (i + 1) < entries; i++) {
213 uint64_t start, end, nstart, nend;
214 if (mem->map[i].type != mem->map[i + 1].type) {
217 start = unpack_lb64(mem->map[i].start);
218 end = start + unpack_lb64(mem->map[i].size);
219 nstart = unpack_lb64(mem->map[i + 1].start);
220 nend = nstart + unpack_lb64(mem->map[i + 1].size);
221 if ((start <= nstart) && (end > nstart)) {
222 if (start > nstart) {
228 /* Record the new region size */
229 mem->map[i].start = pack_lb64(start);
230 mem->map[i].size = pack_lb64(end - start);
232 /* Delete the entry I have merged with */
233 memmove(&mem->map[i + 1], &mem->map[i + 2],
234 ((entries - i - 2) * sizeof(mem->map[0])));
235 mem->size -= sizeof(mem->map[0]);
237 /* See if I can merge with the next entry as well */
243 static void lb_remove_memory_range(struct lb_memory *mem,
244 uint64_t start, uint64_t size)
251 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
253 /* Remove a reserved area from the memory map */
254 for(i = 0; i < entries; i++) {
255 uint64_t map_start = unpack_lb64(mem->map[i].start);
256 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
257 if ((start <= map_start) && (end >= map_end)) {
258 /* Remove the completely covered range */
259 memmove(&mem->map[i], &mem->map[i + 1],
260 ((entries - i - 1) * sizeof(mem->map[0])));
261 mem->size -= sizeof(mem->map[0]);
263 /* Since the index will disappear revisit what will appear here */
266 else if ((start > map_start) && (end < map_end)) {
267 /* Split the memory range */
268 memmove(&mem->map[i + 1], &mem->map[i],
269 ((entries - i) * sizeof(mem->map[0])));
270 mem->size += sizeof(mem->map[0]);
272 /* Update the first map entry */
273 mem->map[i].size = pack_lb64(start - map_start);
274 /* Update the second map entry */
275 mem->map[i + 1].start = pack_lb64(end);
276 mem->map[i + 1].size = pack_lb64(map_end - end);
277 /* Don't bother with this map entry again */
280 else if ((start <= map_start) && (end > map_start)) {
281 /* Shrink the start of the memory range */
282 mem->map[i].start = pack_lb64(end);
283 mem->map[i].size = pack_lb64(map_end - end);
285 else if ((start < map_end) && (start > map_start)) {
286 /* Shrink the end of the memory range */
287 mem->map[i].size = pack_lb64(start - map_start);
292 static void lb_add_memory_range(struct lb_memory *mem,
293 uint32_t type, uint64_t start, uint64_t size)
295 lb_remove_memory_range(mem, start, size);
296 lb_memory_range(mem, type, start, size);
297 lb_cleanup_memory_ranges(mem);
300 /* Routines to extract part so the linuxBIOS table or
301 * information from the linuxBIOS table after we have written it.
302 * Currently get_lb_mem relies on a global we can change the
305 static struct lb_memory *mem_ranges = 0;
306 struct lb_memory *get_lb_mem(void)
311 static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
313 struct lb_memory *mem = gp;
314 lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
317 static struct lb_memory *build_lb_mem(struct lb_header *head)
319 struct lb_memory *mem;
321 /* Record where the lb memory ranges will live */
322 mem = lb_memory(head);
325 /* Build the raw table of memory */
326 search_global_resources(
327 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
328 build_lb_mem_range, mem);
329 lb_cleanup_memory_ranges(mem);
333 unsigned long write_linuxbios_table(
334 unsigned long low_table_start, unsigned long low_table_end,
335 unsigned long rom_table_start, unsigned long rom_table_end)
337 unsigned long table_size;
338 struct lb_header *head;
339 struct lb_memory *mem;
341 head = lb_table_init(low_table_end);
342 low_table_end = (unsigned long)head;
343 if (HAVE_OPTION_TABLE == 1) {
344 struct lb_record *rec_dest, *rec_src;
345 /* Write the option config table... */
346 rec_dest = lb_new_record(head);
347 rec_src = (struct lb_record *)(void *)&option_table;
348 memcpy(rec_dest, rec_src, rec_src->size);
350 /* Record where RAM is located */
351 mem = build_lb_mem(head);
353 /* Find the current mptable size */
354 table_size = (low_table_end - low_table_start);
356 /* Record the mptable and the the lb_table (This will be adjusted later) */
357 lb_add_memory_range(mem, LB_MEM_TABLE,
358 low_table_start, table_size);
360 /* Record the pirq table */
361 lb_add_memory_range(mem, LB_MEM_TABLE,
362 rom_table_start, (rom_table_end - rom_table_start));
365 * I assume that there is always memory at immediately after
366 * the low_table_end. This means that after I setup the linuxbios table.
367 * I can trivially fixup the reserved memory ranges to hold the correct
368 * size of the linuxbios table.
371 /* Record our motheboard */
373 /* Record our various random string information */
376 low_table_end = lb_table_fini(head);
378 /* Remember where my valid memory ranges are */
379 return low_table_end;