2 #include <ip_checksum.h>
3 #include <boot/linuxbios_tables.h>
4 #include "linuxbios_table.h"
5 #include <console/console.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_record *rec;
121 struct lb_string *str;
123 rec = lb_new_record(header);
124 str = (struct lb_string *)rec;
125 len = strlen(strings[i].string);
126 str->tag = strings[i].tag;
127 str->size = (sizeof(*rec) + len + 1 + 3) & ~3;
128 memcpy(str->string, strings[i].string, len+1);
133 /* Some version of gcc have problems with 64 bit types so
134 * take an unsigned long instead of a uint64_t for now.
136 void lb_memory_range(struct lb_memory *mem,
137 uint32_t type, unsigned long start, unsigned long size)
140 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
141 mem->map[entries].start = start;
142 mem->map[entries].size = size;
143 mem->map[entries].type = type;
144 mem->size += sizeof(mem->map[0]);
147 static void lb_memory_rangek(struct lb_memory *mem,
148 uint32_t type, unsigned long startk, unsigned long endk)
151 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
152 mem->map[entries].start = startk;
153 mem->map[entries].start <<= 10;
154 mem->map[entries].size = endk - startk;
155 mem->map[entries].size <<= 10;
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 = mem->map[i].start;
179 uint64_t map_end = map_start + mem->map[i].size;
180 /* Does this area need to be expanded? */
181 if (map_end == start) {
182 mem->map[i].size = end - map_start;
184 /* Does this area need to be contracted? */
185 else if (map_start == start) {
186 mem->map[i].start = end;
187 mem->map[i].size = map_end - end;
193 unsigned long lb_table_fini(struct lb_header *head)
195 struct lb_record *rec, *first_rec;
196 rec = lb_last_record(head);
197 if (head->table_entries) {
198 head->table_bytes += rec->size;
200 lb_reserve_table_memory(head);
201 first_rec = lb_first_record(head);
202 head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
203 head->header_checksum = 0;
204 head->header_checksum = compute_ip_checksum(head, sizeof(*head));
205 printk_debug("Wrote linuxbios table at: %p - %p checksum %lx\n",
206 head, rec, head->table_checksum);
207 return (unsigned long)rec;
211 /* Routines to extract part so the linuxBIOS table or
212 * information from the linuxBIOS table after we have written it.
213 * Currently get_lb_mem relies on a global we can change the
216 static struct lb_memory *mem_ranges = 0;
217 struct lb_memory *get_lb_mem(void)
222 unsigned long write_linuxbios_table(
223 unsigned long *processor_map,
224 struct mem_range *ram,
225 unsigned long low_table_start, unsigned long low_table_end,
226 unsigned long rom_table_startk, unsigned long rom_table_endk)
228 unsigned long table_size;
229 struct mem_range *ramp;
230 struct lb_header *head;
231 struct lb_memory *mem;
232 #if HAVE_OPTION_TABLE == 1
233 struct lb_record *rec_dest, *rec_src;
236 head = lb_table_init(low_table_end);
237 low_table_end = (unsigned long)head;
238 #if HAVE_OPTION_TABLE == 1
239 /* Write the option config table... */
240 rec_dest = lb_new_record(head);
241 rec_src = (struct lb_record *)&option_table;
242 memcpy(rec_dest, rec_src, rec_src->size);
244 mem = lb_memory(head);
246 /* I assume there is always ram at address 0 */
247 /* Reserve our tables in low memory */
248 table_size = (low_table_end - low_table_start);
249 lb_memory_range(mem, LB_MEM_TABLE, 0, table_size);
250 lb_memory_range(mem, LB_MEM_RAM, table_size, (ram[0].sizek << 10) - table_size);
251 /* Reserving pci memory mapped space will keep the kernel from booting seeing
254 for(ramp = &ram[1]; ramp->sizek; ramp++) {
255 unsigned long startk, endk;
256 startk = ramp->basek;
257 endk = startk + ramp->sizek;
258 if ((startk < rom_table_startk) && (endk > rom_table_startk)) {
259 lb_memory_rangek(mem, LB_MEM_RAM, startk, rom_table_startk);
260 startk = rom_table_startk;
262 if ((startk == rom_table_startk) && (endk > startk)) {
264 tend = rom_table_endk;
268 lb_memory_rangek(mem, LB_MEM_TABLE, rom_table_startk, tend);
272 lb_memory_rangek(mem, LB_MEM_RAM, startk, endk);
276 /* Record our motheboard */
278 /* Record our various random string information */
281 low_table_end = lb_table_fini(head);
283 /* Remember where my valid memory ranges are */
284 return low_table_end;