aboutsummaryrefslogtreecommitdiff
path: root/ext2load/conffile.c
blob: 518ccf199d2e59bf5aec9e7f0e2eda415e78d079 (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
/*
 * Copyright 2001-2004 Guido Guenther <agx@sigxcpu.org>
 *
 * load arcboots configuration file and process the arguments
 *
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <types.h>

#include <sys/types.h>
#include <ext2_fs.h>
#include <ext2fs.h>
#include "arcboot.h"
#include <arc.h>

#define _PARM_LIMIT	32

static	char	*carray[_PARM_LIMIT+3]; /* 0 is the name, 
					   1 the boofile, ...
					   X is OSLoadOptions 
					   X+1 ... _PARM_LIMIT are options given on the
					   command line */

CHAR** GetConfig(char* config, char* name)
{
	char *t, *start, *end;
	int		i;

	/* Loop on lines */
	while(*config != 0x0) {

		start=config;

		while(*config != 0xa && *config != 0x0) {
			/* Delete comments */
			if (*config == '#') 
				*config=0x0;
			config++;
		}

		/* Did we stop at the end of a line ? */
		if (*config == 0xa) {
			/* Terminate Line */
			*config=0x0;
			config++;
		}		

		/* Skip leading spaces and tabs */
		while(*start == ' ' || *start == '\t') 
			start++;

		/* If the start of a line is the end - Next line */
		if (*start == 0x0)
			continue;

		/* get the end pointer */
		end=&start[strlen(start)-1];

		/* Delete spaces and tabs at the end of a line */
		while(*end == ' ' || *end == '\t')
			*end--=0x0;

		if (strncmp("label=",start,6) == 0) {
			/* If we found the right profile or want the first */
			if (carray[0])
				if (((strcmp(carray[0], name) == 0) && (strcmp(name, carray[0]) == 0))) {
					return carray;
				}
			/* Reset image & append */
			carray[1]=carray[2]=0;
			carray[0]=&start[6];
		} else if (strncmp("image=",start,6) == 0) {
			carray[1]=&start[6];
		} else if (strncmp("append=",start,7) == 0) {
			t=&start[7];
			/* Does append start with " */
			if (*t == '"') {
				t++;
				/* If so - append starts +1 */
				carray[2]=t;
				/* Search ending quote */
				while(*t != '"' && *t != 0x0)
					t++;
				/* And delete */
				if (*t == '"') 
					*t=0x0;
			} else 
				carray[2]=&start[7];
			t=carray[2];
			i=3;
			while(i<_PARM_LIMIT && *t != 0x0) {
				t++;
					
				if (*t == ' ' || *t == '\t') {
					*t++=0x0;
					if (*t != 0x0)
						carray[i++]=t;
				}
			}
		}
	}
	if (carray[0])
		if ((name == NULL) ||
			(strcmp(carray[0], name) == 0)) {
			return carray;
		}
	/* Found nothing appropriate: */
	return NULL;
}

CHAR** ReadConfFile(char** partition, const char *filename, char* label)
{
	ext2_file_t file;
	unsigned size, num_read;
	errcode_t status;
	char *conf_file;

	if(!OpenFile( *partition, filename, &file )){
		/* OSLoadPartition seems to be wrong, but don't give up now */
		int npart,i;
		char *part,*spart;

		/* the user wants to boot a file directly out of the filesystem
		 * don't try to fixup OSLoadPartition for him in this case */
		if(label[0] == '/') {
			return False;
		}
		printf("Can't open configuration file. Trying other partitions\n\r");
		spart = ArcGetEnvironmentVariable("SystemPartition");
		if(! spart ) {
			printf("Couldn't get SystemPartition, weird.");
			return False;
		}
		part = strdup(spart);
		npart = part[strlen(part)-2] - '0';
		for(i = 0; i < npart; i++) {
			part[strlen(part)-2] = '0' + i;
#if DEBUG
			printf("Trying %s\n\r", part);
#endif
			/* we found it, good */
			if(OpenFile( part, filename, &file )) {
				printf("Please adjust OSLoadPartition to %s\n\r", part);
				*partition = part;
				break;
			}
		}
		if( i == npart )
			return False;
	}

	size = ext2fs_file_get_size(file);
	conf_file = malloc(size);
	if( !conf_file ) {
		printf("Can't read configuration file - not enough memory\n\r");
		return False;
	}
	status = ext2fs_file_read(file,(char*) conf_file, size, &num_read);
	if( status ) {
		print_ext2fs_error(status);
		return False;
	}
	if( size != num_read ) {
		printf("Wanted: %u, got %u bytes of configuration file\n\r", size, num_read);
		return False;
	}
	return GetConfig(conf_file, label); 
}