copied for i386
[coreboot.git] / src / arch / ppc / boot / linuxbios_table.c
1 #include <console/console.h>
2 #include <mem.h>
3 #include <ip_checksum.h>
4 #include <boot/linuxbios_tables.h>
5 #include "linuxbios_table.h"
6 #include <string.h>
7 #include <version.h>
8
9
10 struct lb_header *lb_table_init(unsigned long addr)
11 {
12         struct lb_header *header;
13
14         /* 16 byte align the address */
15         addr += 15;
16         addr &= ~15;
17
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;
28         return header;
29 }
30
31 struct lb_record *lb_first_record(struct lb_header *header)
32 {
33         struct lb_record *rec;
34         rec = (void *)(((char *)header) + sizeof(*header));
35         return rec;
36 }
37
38 struct lb_record *lb_last_record(struct lb_header *header)
39 {
40         struct lb_record *rec;
41         rec = (void *)(((char *)header) + sizeof(*header) + header->table_bytes);
42         return rec;
43 }
44
45 struct lb_record *lb_next_record(struct lb_record *rec)
46 {
47         rec = (void *)(((char *)rec) + rec->size);      
48         return rec;
49 }
50
51 struct lb_record *lb_new_record(struct lb_header *header)
52 {
53         struct lb_record *rec;
54         rec = lb_last_record(header);
55         if (header->table_entries) {
56                 header->table_bytes += rec->size;
57         }
58         rec = lb_last_record(header);
59         header->table_entries++;
60         rec->tag = LB_TAG_UNUSED;
61         rec->size = sizeof(*rec);
62         return rec;
63 }
64
65
66 struct lb_memory *lb_memory(struct lb_header *header)
67 {
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);
74         return mem;
75 }
76
77 struct lb_mainboard *lb_mainboard(struct lb_header *header)
78 {
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;
84
85         mainboard->size = (sizeof(*mainboard) +
86                 strlen(mainboard_vendor) + 1 + 
87                 strlen(mainboard_part_number) + 1 +
88                 3) & ~3;
89
90         mainboard->vendor_idx = 0;
91         mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
92
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);
97
98         return mainboard;
99 }
100
101 void lb_strings(struct lb_header *header)
102 {
103         static const struct {
104                 uint32_t tag;
105                 const uint8_t *string;
106         } strings[] = {
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,      },
117         };
118         unsigned int i;
119         for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
120                 struct lb_string *rec;
121                 size_t len;
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);
127         }
128
129 }
130
131 /* Some version of gcc have problems with 64 bit types so
132  * take an unsigned long instead of a uint64_t for now.
133  */
134 void lb_memory_range(struct lb_memory *mem,
135         uint32_t type, unsigned long start, unsigned long size)
136 {
137         int entries;
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]);
143 }
144
145 static void lb_memory_rangek(struct lb_memory *mem,
146         uint32_t type, unsigned long startk, unsigned long endk)
147 {
148         int entries;
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]);
156 }
157
158 static void lb_reserve_table_memory(struct lb_header *head)
159 {
160         struct lb_record *last_rec;
161         struct lb_memory *mem;
162         uint64_t start;
163         uint64_t end;
164         int i, entries;
165
166         last_rec = lb_last_record(head);
167         mem = get_lb_mem();
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.
174          */
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;
181                 }
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;
186                 }
187         }
188 }
189
190
191 unsigned long lb_table_fini(struct lb_header *head)
192 {
193         struct lb_record *rec, *first_rec;
194         rec = lb_last_record(head);
195         if (head->table_entries) {
196                 head->table_bytes += rec->size;
197         }
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;
206 }
207
208
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
212  * implementaiton.
213  */
214 static struct lb_memory *mem_ranges = 0;
215 struct lb_memory *get_lb_mem(void)
216 {
217         return mem_ranges;
218 }
219
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)
225 {
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;
231
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);
239 #endif  
240         mem = lb_memory(head);
241         mem_ranges = mem;
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
248          * any pci resources.
249          */
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;
257                 }
258                 if ((startk == rom_table_startk) && (endk > startk)) {
259                         unsigned long tend;
260                         tend = rom_table_endk;
261                         if (tend > endk) {
262                                 tend = endk;
263                         }
264                         lb_memory_rangek(mem, LB_MEM_TABLE, rom_table_startk, tend);
265                         startk = tend;
266                 }
267                 if (endk > startk) {
268                         lb_memory_rangek(mem, LB_MEM_RAM, startk, endk);
269                 }
270         }
271
272         /* Record our motheboard */
273         lb_mainboard(head);
274         /* Record our various random string information */
275         lb_strings(head);
276
277         low_table_end = lb_table_fini(head);
278
279         /* Remember where my valid memory ranges are */
280         return low_table_end;
281 }