File: bootinfo-loader.c

package info (click to toggle)
openbios-sparc 1.0%2Bsvn640-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,412 kB
  • ctags: 12,091
  • sloc: ansic: 57,249; asm: 2,680; xml: 1,335; cpp: 414; makefile: 224; sh: 190
file content (245 lines) | stat: -rw-r--r-- 5,616 bytes parent folder | download | duplicates (2)
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
/*
 *
 *       <bootinfo-loader.c>
 *
 *       bootinfo file loader
 *
 *   Copyright (C) 2009 Laurent Vivier (Laurent@vivier.eu)
 *
 *   Original XML parser by Blue Swirl <blauwirbel@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   version 2
 *
 */

#include "openbios/config.h"
#include "openbios/bindings.h"
#include "modules.h"
#include "ofmem.h"
#include "libc/vsprintf.h"

//#define DEBUG_BOOTINFO

#ifdef DEBUG_BOOTINFO
#define DPRINTF(fmt, args...) \
    do { printk("%s: " fmt, __func__ , ##args); } while (0)
#else
#define DPRINTF(fmt, args...) \
    do { } while (0)
#endif

DECLARE_NODE(bootinfo_loader, INSTALL_OPEN, 0, "+/packages/bootinfo-loader" );

static char *
get_device( const char *path )
{
	int i;
	static char buf[1024];

	for (i = 0; i < sizeof(buf) && path[i] && path[i] != ':'; i++)
		buf[i] = path[i];
	buf[i] = 0;

	return buf;
}

static int
get_partition( const char *path )
{
	while ( *path && *path != ':' )
		path++;

	if (!*path)
		return -1;
	path++;

	if (!strchr(path, ',')) /* check if there is a ',' */
		return -1;

	return atol(path);
}

static char *
get_filename( const char * path , char **dirname)
{
        static char buf[1024];
        char *filename;

        while ( *path && *path != ':' )
                path++;

        if (!*path) {
                *dirname = NULL;
                return NULL;
        }
        path++;

        while ( *path && isdigit(*path) )
                path++;

        if (*path == ',')
                path++;

        strncpy(buf, path, sizeof(buf));
        buf[sizeof(buf) - 1] = 0;

        filename = strrchr(buf, '\\');
        if (filename) {
                *dirname = buf;
                (*filename++) = 0;
        } else {
                *dirname = NULL;
                filename = buf;
        }

        return filename;
}

/*
  Parse SGML structure like:
  <chrp-boot>
  <description>Debian/GNU Linux Installation on IBM CHRP hardware</description>
  <os-name>Debian/GNU Linux for PowerPC</os-name>
  <boot-script>boot &device;:\install\yaboot</boot-script>
  <icon size=64,64 color-space=3,3,2>

  CHRP system bindings are described at:
  http://playground.sun.com/1275/bindings/chrp/chrp1_7a.ps
*/

static void
bootinfo_loader_init_program( void *dummy )
{
	char *base;
	int proplen;
	phandle_t chosen;
	int tag, taglen, script, scriptlen, entity, chrp;
	char tagbuf[128], c;
	char *device, *filename, *directory;
	int partition;
	int current, size;
	char *bootscript;
        char *tmp;
	char bootpath[1024];

	feval("0 state-valid !");

	chosen = find_dev("/chosen");
	tmp = get_property(chosen, "bootpath", &proplen);
	memcpy(bootpath, tmp, proplen);
	bootpath[proplen] = 0;

	DPRINTF("bootpath %s\n", bootpath);

	device = get_device(bootpath);
	partition = get_partition(bootpath);
	filename = get_filename(bootpath, &directory);

	feval("load-base");
	base = (char*)POP();

	feval("load-size");
	size = POP();

	bootscript = malloc(size);
	if (bootscript == NULL) {
		DPRINTF("Can't malloc %d bytes\n", size);
		return;
	}

	chrp = 0;
	tag = 0;
	taglen = 0;
	script = 0;
	scriptlen = 0;
	entity = 0;
	current = 0;
	while (current < size) {

		c = base[current++];

		if (c == '<') {
			script = 0;
			tag = 1;
			taglen = 0;
		} else if (c == '>') {
			tag = 0;
			tagbuf[taglen] = '\0';
			if (strcasecmp(tagbuf, "chrp-boot") == 0) {
				chrp = 1;
			} else if (chrp == 1) {
				if (strcasecmp(tagbuf, "boot-script") == 0) {
					script = 1;
					scriptlen = 0;
				} else if (strcasecmp(tagbuf, "/boot-script") == 0) {

					script = 0;
					bootscript[scriptlen] = '\0';

					DPRINTF("got bootscript %s\n",
						bootscript);

					feval("-1 state-valid !");

					break;
				} else if (strcasecmp(tagbuf, "/chrp-boot") == 0)
					break;
			}
		} else if (tag && taglen < sizeof(tagbuf)) {
			tagbuf[taglen++] = c;
		} else if (script && c == '&') {
			entity = 1;
			taglen = 0;
		} else if (entity && c ==';') {
			entity = 0;
			tagbuf[taglen] = '\0';
			if (strcasecmp(tagbuf, "lt") == 0) {
				bootscript[scriptlen++] = '<';
			} else if (strcasecmp(tagbuf, "gt") == 0) {
				bootscript[scriptlen++] = '>';
			} else if (strcasecmp(tagbuf, "device") == 0) {
				strcpy(bootscript + scriptlen, device);
				scriptlen += strlen(device);
			} else if (strcasecmp(tagbuf, "partition") == 0) {
				if (partition != -1)
			sprintf(bootscript + scriptlen, "%d", partition);
				else
					*(bootscript + scriptlen) = 0;
				scriptlen = strlen(bootscript);
			} else if (strcasecmp(tagbuf, "directory") == 0) {
				strcpy(bootscript + scriptlen, directory);
				scriptlen += strlen(directory);
			} else if (strcasecmp(tagbuf, "filename") == 0) {
				strcpy(bootscript + scriptlen, filename);
				scriptlen += strlen(filename);
			} else if (strcasecmp(tagbuf, "full-path") == 0) {
				strcpy(bootscript + scriptlen, bootpath);
				scriptlen += strlen(bootpath);
			} else { /* unknown, keep it */
				bootscript[scriptlen] = '&';
				strcpy(bootscript + scriptlen + 1, tagbuf);
				scriptlen += taglen + 1;
				bootscript[scriptlen] = ';';
				scriptlen++;
			}
		} else if (entity && taglen < sizeof(tagbuf)) {
			tagbuf[taglen++] = c;
		} else if (script && scriptlen < size) {
			bootscript[scriptlen++] = c;
		}
	}
	/* FIXME: should initialize saved-program-state. */
	push_str(bootscript);
	feval("bootinfo-size ! bootinfo-entry !");
}

NODE_METHODS( bootinfo_loader ) = {
	{ "init-program", bootinfo_loader_init_program },
};

void bootinfo_loader_init( void )
{
	REGISTER_NODE( bootinfo_loader );
}