summaryrefslogtreecommitdiff
path: root/util/sconfig/main.c
blob: f066d7bad2466202e0cf7ea9954188fa06852c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
 * sconfig, coreboot device tree compiler
 *
 * Copyright (C) 2010 coresystems GmbH
 *                 written by Patrick Georgi <patrick.georgi@coresystems.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
 */

#include <ctype.h>
#include "sconfig.h"
#include "sconfig.tab.h"

extern int linenum;

struct device *head, *lastdev;

struct header headers;

static int devcount = 0;

typedef enum {
	STATIC_MODE,
	BOOTBLOCK_MODE,
	KCONFIG_MODE
} scan_t;

static scan_t scan_mode = STATIC_MODE;

static struct device root;
static struct device mainboard = {
	.name = "mainboard",
	.name_underscore = "mainboard",
	.id = 0,
	.chip = &mainboard,
	.type = chip,
#ifdef MAINBOARDS_HAVE_CHIP_H
	.chiph_exists = 1,
#else
	.chiph_exists = 0,
#endif
	.children = &root
};

static struct device root = {
	.name = "dev_root",
	.name_underscore = "dev_root",
	.id = 0,
	.chip = &mainboard,
	.type = device,
	.path = " .type = DEVICE_PATH_ROOT ",
	.ops = "&default_dev_ops_root",
	.parent = &root,
	.bus = &root,
	.enabled = 1
};

static struct device *new_dev(struct device *parent, struct device *bus) {
	struct device *dev = malloc(sizeof(struct device));
	memset(dev, 0, sizeof(struct device));
	dev->id = ++devcount;
	dev->parent = parent;
	dev->bus = bus;
	dev->subsystem_vendor = -1;
	dev->subsystem_device = -1;
	head->next = dev;
	head = dev;
	return dev;
}

static int device_match(struct device *a, struct device *b) {
	if ((a->bustype == b->bustype) && (a->bus == b->bus) && (a->path_a == b->path_a) && (a->path_b == b->path_b))
		return 1;
	return 0;
}

void fold_in(struct device *parent) {
	struct device *child = parent->children;
	struct device *latest = 0;
	while (child != latest) {
		if (child->children) {
			if (!latest) latest = child->children;
			parent->latestchild->next_sibling = child->children;
			parent->latestchild = child->latestchild;
		}
		child = child->next_sibling;
	}
}

int yywrap(void) {
	return 1;
}

void yyerror (char const *str)
{
	extern char *yytext;
	fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
	exit(1);
}

void postprocess_devtree(void) {
	root.next_sibling = root.children;
	root.next_sibling->next_sibling = root.next_sibling->children;

	struct device *dev = &root;
	while (dev) {
		/* skip "chip" elements in children chain */
		while (dev->children && (dev->children->type == chip)) dev->children = dev->children->children;
		/* skip "chip" elements and functions of the same device in sibling chain */
		while (dev->sibling && dev->sibling->used) dev->sibling = dev->sibling->sibling;
		/* If end of chain, and parent is a chip, move on */
		if (!dev->sibling && (dev->parent->type == chip)) dev->sibling = dev->parent->sibling;
		/* skip chips */
		while (dev->sibling && dev->sibling->type == chip) dev->sibling = dev->sibling->children;
		/* skip duplicate function elements in nextdev chain */
		while (dev->nextdev && dev->nextdev->used) dev->nextdev = dev->nextdev->nextdev;
		dev = dev->next_sibling;
	}
}

void translate_name(char *str, int uppercase)
{
	char *c;
	for (c = str; *c; c++) {
		if (*c == '/') *c = '_';
		if (*c == '-') *c = '_';
		if (uppercase)
			*c = toupper(*c);
	}
}

struct device *new_chip(struct device *parent, struct device *bus, char *path) {
	struct device *new_chip = new_dev(parent, bus);
	new_chip->chiph_exists = 1;
	new_chip->name = path;
	new_chip->name_underscore = strdup(new_chip->name);
	translate_name(new_chip->name_underscore, 0);
	new_chip->type = chip;
	new_chip->chip = new_chip;

	struct stat st;
	char *chip_h = malloc(strlen(path)+18);
	sprintf(chip_h, "src/%s", path);
	if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
		fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
				path);
		exit(1);
	}

	if (scan_mode == STATIC_MODE)
		sprintf(chip_h, "src/%s/chip.h", path);
	else if (scan_mode == BOOTBLOCK_MODE)
		sprintf(chip_h, "src/%s/bootblock.c", path);

	if ((scan_mode == STATIC_MODE) || (scan_mode == BOOTBLOCK_MODE)) {
		if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
			new_chip->chiph_exists = 0;
	}

	if (parent->latestchild) {
		parent->latestchild->next_sibling = new_chip;
		parent->latestchild->sibling = new_chip;
	}
	parent->latestchild = new_chip;
	if (!parent->children)
		parent->children = new_chip;
	return new_chip;
}

void add_header(struct device *dev) {
	int include_exists = 0;
	struct header *h = &headers;
	while (h->next) {
		int result = strcmp(dev->name, h->next->name);
		if (result == 0) {
			include_exists = 1;
			break;
		}
		if (result < 0) break;
		h = h->next;
	}
	if (!include_exists) {
		struct header *tmp = h->next;
		h->next = malloc(sizeof(struct header));
		memset(h->next, 0, sizeof(struct header));
		h->next->chiph_exists = dev->chiph_exists;
		h->next->name = dev->name;
		h->next->next = tmp;
	}
}

struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) {
	struct device *new_d = new_dev(parent, busdev);
	new_d->bustype = bus;

	char *tmp;
	new_d->path_a = strtol(strdup(devnum), &tmp, 16);
	if (*tmp == '.') {
		tmp++;
		new_d->path_b = strtol(tmp, NULL, 16);
	}

	char *name = malloc(10);
	sprintf(name, "_dev%d", new_d->id);
	new_d->name = name;
	new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr
	new_d->type = device;
	new_d->enabled = enabled;
	new_d->chip = new_d->parent->chip;

	if (parent->latestchild) {
		parent->latestchild->next_sibling = new_d;
		parent->latestchild->sibling = new_d;
	}
	parent->latestchild = new_d;
	if (!parent->children)
		parent->children = new_d;

	lastdev->nextdev = new_d;
	lastdev = new_d;

	switch(bus) {
	case PCI:
		new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
		break;

	case PNP:
		new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
		break;

	case I2C:
		new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}";
		break;

	case APIC:
		new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
		break;

	case APIC_CLUSTER:
		new_d->path = ".type=DEVICE_PATH_APIC_CLUSTER,{.apic_cluster={ .cluster = 0x%x }}";
		break;

	case PCI_DOMAIN:
		new_d->path = ".type=DEVICE_PATH_PCI_DOMAIN,{.pci_domain={ .domain = 0x%x }}";
		break;

	case IOAPIC:
		new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
		break;
	}
	return new_d;
}

void alias_siblings(struct device *d) {
	while (d) {
		int link = 0;
		struct device *cmp = d->next_sibling;
		while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) {
			if (cmp->type==device && !cmp->used) {
				if (device_match(d, cmp)) {
					d->multidev = 1;

					cmp->id = d->id;
					cmp->name = d->name;
					cmp->used = 1;
					cmp->link = ++link;
				}
			}
			cmp = cmp->next_sibling;
		}
		d = d->next_sibling;
	}
}

void add_resource(struct device *dev, int type, int index, int base) {
	struct resource *r = malloc(sizeof(struct resource));
	memset (r, 0, sizeof(struct resource));
	r->type = type;
	r->index = index;
	r->base = base;
	if (dev->res) {
		struct resource *head = dev->res;
		while (head->next) head = head->next;
		head->next = r;
	} else {
		dev->res = r;
	}
	dev->rescnt++;
}

void add_register(struct device *dev, char *name, char *val) {
	struct reg *r = malloc(sizeof(struct reg));
	memset (r, 0, sizeof(struct reg));
	r->key = name;
	r->value = val;
	if (dev->reg) {
		struct reg *head = dev->reg;
		// sorting to be equal to sconfig's behaviour
		int sort = strcmp(r->key, head->key);
		if (sort == 0) {
			printf("ERROR: duplicate 'register' key.\n");
			exit(1);
		}
		if (sort<0) {
			r->next = head;
			dev->reg = r;
		} else {
			while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
			r->next = head->next;
			head->next = r;
		}
	} else {
		dev->reg = r;
	}
}

void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
{
	if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
		printf("ERROR: 'subsystem' only allowed for PCI devices\n");
		exit(1);
	}

	dev->subsystem_vendor = vendor;
	dev->subsystem_device = device;
	dev->inherit_subsystem = inherit;
}

void add_ioapic_info(struct device *dev, int apicid, const char *_srcpin, int irqpin)
{

	int srcpin;

	if (!_srcpin || strlen(_srcpin) < 4 ||strncasecmp(_srcpin, "INT", 3) ||
		   _srcpin[3] < 'A' || _srcpin[3] > 'D') {
		printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
		exit(1);
	}

	srcpin = _srcpin[3] - 'A';

	if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
		printf("ERROR: ioapic config only allowed for PCI devices\n");
		exit(1);
	}

	if (srcpin > 3) {
		printf("ERROR: srcpin '%d' invalid\n", srcpin);
		exit(1);
	}
	dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
	dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
}

static void pass0(FILE *fil, struct device *ptr) {
	if (ptr->type == device && ptr->id == 0)
		fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[];\n", ptr->name);

	if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
		fprintf(fil, "ROMSTAGE_CONST static struct device %s;\n", ptr->name);
		if (ptr->rescnt > 0)
			fprintf(fil, "ROMSTAGE_CONST struct resource %s_res[];\n", ptr->name);
		if (ptr->children || ptr->multidev)
			fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[];\n",
					ptr->name);
	}
}

static void pass1(FILE *fil, struct device *ptr)
{
	int pin;
	if (!ptr->used && (ptr->type == device)) {
		if (ptr->id != 0)
			fprintf(fil, "static ");
		fprintf(fil, "ROMSTAGE_CONST struct device %s = {\n", ptr->name);
		fprintf(fil, "#ifndef __PRE_RAM__\n");
		fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
		fprintf(fil, "#endif\n");
		fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
		fprintf(fil, "\t.path = {");
		fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
		fprintf(fil, "},\n");
		fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
		fprintf(fil, "\t.on_mainboard = 1,\n");
		if (ptr->subsystem_vendor > 0)
			fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor);

		for(pin = 0; pin < 4; pin++) {
			if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
				fprintf(fil, "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_irq_pin);

			if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
				fprintf(fil, "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_dst_id);
		}

		if (ptr->subsystem_device > 0)
			fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);

		if (ptr->rescnt > 0) {
			fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
		}
		int link = 0;
		if (ptr->children || ptr->multidev)
			fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name);
		else
			fprintf(fil, "\t.link_list = NULL,\n");
		if (ptr->sibling)
			fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
		fprintf(fil, "#ifndef __PRE_RAM__\n");
		fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
		fprintf(fil, "#endif\n");
		if (ptr->chip->chiph_exists)
			fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
		if (ptr->nextdev)
			fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
		fprintf(fil, "};\n");
	}
	if (ptr->rescnt > 0) {
		int i=1;
		fprintf(fil, "ROMSTAGE_CONST struct resource %s_res[] = {\n",
				ptr->name);
		struct resource *r = ptr->res;
		while (r) {
			fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
			if (r->type == IRQ) fprintf(fil, "IRQ");
			if (r->type == DRQ) fprintf(fil, "DRQ");
			if (r->type == IO) fprintf(fil, "IO");
			fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base);
			if (r->next)
				fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++);
			else
				fprintf(fil, ".next=NULL },\n");
			r = r->next;
		}
		fprintf(fil, "\t };\n");
	}
	if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
		fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[] = {\n", ptr->name);
		if (ptr->multidev) {
			struct device *d = ptr;
			while (d) {
				if (device_match(d, ptr)) {
					fprintf(fil, "\t\t[%d] = {\n", d->link);
					fprintf(fil, "\t\t\t.link_num = %d,\n", d->link);
					fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
					if (d->children)
						fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
					if (d->next_sibling && device_match(d->next_sibling, ptr))
						fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1);
					else
						fprintf(fil, "\t\t\t.next = NULL,\n");
					fprintf(fil, "\t\t},\n");
				}
				d = d->next_sibling;
			}
		} else {
			if (ptr->children) {
				fprintf(fil, "\t\t[0] = {\n");
				fprintf(fil, "\t\t\t.link_num = 0,\n");
				fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
				fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
				fprintf(fil, "\t\t\t.next = NULL,\n");
				fprintf(fil, "\t\t},\n");
			}
		}
		fprintf(fil, "\t};\n");
	}
	if ((ptr->type == chip) && (ptr->chiph_exists)) {
		if (ptr->reg) {
			fprintf(fil, "ROMSTAGE_CONST struct %s_config ROMSTAGE_CONST %s_info_%d = {\n",
				ptr->name_underscore, ptr->name_underscore,
				ptr->id);
			struct reg *r = ptr->reg;
			while (r) {
				fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
				r = r->next;
			}
			fprintf(fil, "};\n\n");
		} else {
			fprintf(fil, "ROMSTAGE_CONST struct %s_config ROMSTAGE_CONST %s_info_%d = { };\n",
				ptr->name_underscore, ptr->name_underscore, ptr->id);
		}
	}
}

static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) {
	do {
		func(fil, ptr);
		ptr = ptr->next_sibling;
	} while (ptr);
}

static void inherit_subsystem_ids(FILE *file, struct device *dev)
{
	struct device *p;

	if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
		/* user already gave us a subsystem vendor/device */
		return;
	}

	for(p = dev; p && p != p->parent; p = p->parent) {

		if (p->bustype != PCI && p->bustype != PCI_DOMAIN)
			continue;

		if (p->inherit_subsystem) {
			dev->subsystem_vendor = p->subsystem_vendor;
			dev->subsystem_device = p->subsystem_device;
			break;
		}
	}
}

static void usage(void)
{
	printf("usage: sconfig vendor/mainboard outputdir [-{s|b|k} outputfile]\n");
	printf("\t-s file\tcreate ramstage static device map\n");
	printf("\t-b file\tcreate bootblock init_mainboard()\n");
	printf("\t-k file\tcreate Kconfig devicetree section\n");
	printf("Defaults to \"-s static.c\" if no {s|b|k} specified.\n");
	exit (1);
}


int main(int argc, char** argv) {
	if (argc < 3)
		usage();

	char *mainboard=argv[1];
	char *outputdir=argv[2];
	char *devtree=malloc(strlen(mainboard)+30);
	sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
	char *outputc;

	if (argc == 3) {
		scan_mode = STATIC_MODE;
		outputc=malloc(strlen(outputdir)+20);
		sprintf(outputc, "%s/static.c", outputdir);
	} else if ((argc == 5) && (argv[3][0] == '-') && (argv[3][2] == 0)) {

		switch (argv[3][1]) {
		case 's':
			scan_mode = STATIC_MODE;
			break;
		case 'b':
			scan_mode = BOOTBLOCK_MODE;
			break;
		case 'k':
			scan_mode = KCONFIG_MODE;
			break;
		default:
			usage();
			break;
		}
		char *outputfile=argv[4];

		outputc=malloc(strlen(outputdir)+strlen(outputfile)+2);
		sprintf(outputc, "%s/%s", outputdir, outputfile);
	}

	headers.next = 0;
#ifdef MAINBOARDS_HAVE_CHIP_H
	if (scan_mode == STATIC_MODE) {
		headers.next = malloc(sizeof(struct header));
		headers.next->name = malloc(strlen(mainboard)+12);
		headers.next->next = 0;
		sprintf(headers.next->name, "mainboard/%s", mainboard);
	}
#endif

	FILE *filec = fopen(devtree, "r");
	if (!filec) {
		fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
		perror(NULL);
		exit(1);
	}

	yyrestart(filec);

	lastdev = head = &root;

	yyparse();

	fclose(filec);

	if ((head->type == chip) && (!head->chiph_exists)) {
		struct device *tmp = head;
		head = &root;
		while (head->next != tmp) head = head->next;
	}

	FILE *autogen = fopen(outputc, "w");
	if (!autogen) {
		fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
		perror(NULL);
		exit(1);
	}

	struct header *h;
	if (scan_mode == STATIC_MODE) {

		fprintf(autogen, "#include <device/device.h>\n");
		fprintf(autogen, "#include <device/pci.h>\n");
		h = &headers;
		while (h->next) {
			h = h->next;
			if (h->chiph_exists)
				fprintf(autogen, "#include \"%s/chip.h\"\n", h->name);
		}
		fprintf(autogen, "\n#ifndef __PRE_RAM__\n");
		h = &headers;
		while (h->next) {
			h = h->next;
			char *name_underscore = strdup(h->name);
			translate_name(name_underscore, 0);
			fprintf(autogen, "extern struct chip_operations %s_ops;\n", name_underscore);
			free(name_underscore);
		}
		fprintf(autogen, "#endif\n");

		walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL);
		fprintf(autogen, "\n/* pass 0 */\n");
		walk_device_tree(autogen, &root, pass0, NULL);
		fprintf(autogen, "\n/* pass 1 */\n"
			    "ROMSTAGE_CONST struct device * ROMSTAGE_CONST last_dev = &%s;\n", lastdev->name);
#ifdef MAINBOARDS_HAVE_CHIP_H
		fprintf(autogen, "static ROMSTAGE_CONST struct mainboard_config ROMSTAGE_CONST mainboard_info_0;\n");
#else
		fprintf(autogen, "extern struct chip_operations mainboard_ops;\n");
#endif
		walk_device_tree(autogen, &root, pass1, NULL);

	} else if (scan_mode == BOOTBLOCK_MODE) {
		h = &headers;
		while (h->next) {
			h = h->next;
			fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name);
		}

		fprintf(autogen, "\n#if CONFIG_HAS_MAINBOARD_BOOTBLOCK\n");
		fprintf(autogen, "#include \"mainboard/%s/bootblock.c\"\n", mainboard);
		fprintf(autogen, "#else\n");
		fprintf(autogen, "static unsigned long init_mainboard(int bsp_cpu)\n{\n");
		fprintf(autogen, "\tif (! bsp_cpu) return 0;\n");
		h = &headers;
		while (h->next) {
			h = h->next;
			translate_name(h->name, 0);
			fprintf(autogen, "\tinit_%s();\n", h->name);
		}

		fprintf(autogen, "\treturn 0;\n}\n");
		fprintf(autogen, "#endif\n");

	} else if (scan_mode == KCONFIG_MODE) {
		fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n");
		fprintf(autogen, "\tdefault %s\n", mainboard);

		fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n");
		h = &headers;
		while (h->next) {
			h = h->next;
			translate_name(h->name, 1);
			fprintf(autogen, "\tselect %s\n", h->name);
		}
	}

	fclose(autogen);

	return 0;
}