Source

Open PS2 Loader / src / config.c

Full commit
  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
/*
  Copyright 2009, Ifcaro & volca
  Licenced under Academic Free License version 3.0
  Review OpenUsbLd README & LICENSE files for further details.  
*/

#include "include/usbld.h"
#include "include/util.h"
#include <string.h>
#include "fileio.h"
#include <sys/stat.h>

static char IPconfig_path[] = "mc?:/SYS-CONF/IPCONFIG.DAT";
int filesize=0;

int strToColor(const char *string, unsigned char *color) {
	int cnt=0, n=0;
	color[0]=0;
	color[1]=0;
	color[2]=0;

	if (!string || !*string) return 0;
	if (string[0]!='#') return 0;
	
	string++;

	while (*string) {
		int fh = fromHex(*string);
		if (fh >= 0) {
			color[n] = color[n] * 16 + fh;
		} else {
			break;
		}

		// Two characters per color
		if(cnt==1) {
			cnt=0;
			n++;
		}else{
			cnt++;
		}
		
		string++;
	}
	
	return 1;
}

int splitAssignment(const char* line, char* key, char* val) {
	// find "=". 
	// If found, the text before is key, after is val. 
	// Otherwise malformed string is encountered
	int eqpos = strpos(line, '=');
	
	if (eqpos < 0)
		return 0;
	
	// copy the name and the value
	strncpy(key, line, eqpos);
	eqpos++;
	strncpy(val, &line[eqpos], strlen(line) - eqpos);
	
	return 1;
}

int configKeyValidate(const char* key) {
	if (strlen(key) == 0)
		return 0;
	
	int eqpos = strpos(key, '=');
	
	if (eqpos < 0)
		return 1;
	
	return 0;
}

void configValueToText(struct TConfigValue* val, char* buf, unsigned int size) {
	snprintf(buf, size, "%s=%s", val->key, val->val);
}

struct TConfigValue* allocConfigItem(const char* key, const char* val) {
	struct TConfigValue* it = (struct TConfigValue*)malloc(sizeof(struct TConfigValue));
	strncpy(it->key, key, 32);
	it->key[min(strlen(key), 31)] = '\0';
	strncpy(it->val, val, 255);
	it->val[min(strlen(val), 254)] = '\0';
	it->next = NULL;
	
	return it;
}

/// Low level key addition. Does not check for uniqueness.
void addConfigValue(struct TConfigSet* config, const char* key, const char* val) {
	if (!config->tail) {
		config->head = allocConfigItem(key, val);
		config->tail = config->head;
	} else {
		config->tail->next = allocConfigItem(key, val);
		config->tail = config->tail->next;
	}
}

struct TConfigValue* getConfigItemForName(struct TConfigSet* config, const char* name) {
	struct TConfigValue* val = config->head;
	
	while (val) {
		if (strncmp(val->key, name, 32) == 0)
			break;
		
		val = val->next;
	}
	
	return val;
}

// --------------------------------------------------------------------------------------
// ------------------------------ Config getters and setters ----------------------------
// --------------------------------------------------------------------------------------
int setConfigStr(struct TConfigSet* config, const char* key, const char* value) {
	if (!configKeyValidate(key))
		return 0;
	
	struct TConfigValue *it = getConfigItemForName(config, key);
	
	if (it) {
		strncpy(it->val, value, 255);
		it->val[min(strlen(value), 254)] = '\0';
	} else {
		addConfigValue(config, key, value);
	}
	
	return 1;
}

// sets the value to point to the value str in the config. Do not overwrite - it will overwrite the string in config
int getConfigStr(struct TConfigSet* config, const char* key, char** value) {
	if (!configKeyValidate(key))
		return 0;
	
	struct TConfigValue *it = getConfigItemForName(config, key);
	
	if (it) {
		*value = it->val;
		return 1;
	} else {
		return 0;
	}
}
	
int setConfigInt(struct TConfigSet* config, const char* key, const int value) {
	char tmp[12];
	
	snprintf(tmp, 12, "%d", value);
	return setConfigStr(config, key, tmp);
}

int getConfigInt(struct TConfigSet* config, char* key, int* value) {
	char *valref = NULL;
	
	if (getConfigStr(config, key, &valref)) {
		// convert to int
		*value = atoi(valref);
		return 1;
	} else {
		return 0;
	}
}

int setConfigColor(struct TConfigSet* config, const char* key, unsigned char* color) {
	char tmp[8];
	
	snprintf(tmp, 8, "#%02X%02X%02X", color[0], color[1], color[2]);
	return setConfigStr(config, key, tmp);
}

int getConfigColor(struct TConfigSet* config, const char* key, unsigned char* color) {
	char *valref = NULL;
	
	if (getConfigStr(config, key, &valref)) {
		// convert to color
		strToColor(valref, color);
		return 1;
	} else {
		return 0;
	}
}

int configRemoveKey(struct TConfigSet* config, const char* key) {
	// remove config key from config set
	if (!configKeyValidate(key))
		return 0;
	
	struct TConfigValue* val = config->head;
	struct TConfigValue* prev = NULL;
	
	while (val) {
		if (strncmp(val->key, key, 32) == 0) {
			if (prev)
				prev->next = val->next;
			
			if (val == config->head)
				config->head = val->next;
			
			if (val == config->tail)
				config->tail = val->next;
			
			free(val);
		}
		
		prev = val;
		val = val->next;
	}
	
	return 1;
}

void readIPConfig() {
	char ipconfig[255];
	
	IPconfig_path[2] = '0';
	int fd=fioOpen(IPconfig_path, O_RDONLY);
	if (fd<0) {
		IPconfig_path[2] = '1';
		fd=fioOpen(IPconfig_path, O_RDONLY);
		if (fd<0) {
			//DEBUG: printf("No config. Exiting...\n");
			IPconfig_path[2] = '?';
			return;
		}
	}
	filesize = lseek(fd, 0, SEEK_END);
	lseek(fd, 0, SEEK_SET);
	
	fioRead(fd, &ipconfig, filesize);
		
	sscanf(ipconfig, "%d.%d.%d.%d %d.%d.%d.%d %d.%d.%d.%d", &ps2_ip[0], &ps2_ip[1], &ps2_ip[2], &ps2_ip[3],
															&ps2_netmask[0], &ps2_netmask[1], &ps2_netmask[2], &ps2_netmask[3],
															&ps2_gateway[0], &ps2_gateway[1], &ps2_gateway[2], &ps2_gateway[3]);

	
	fioClose(fd);
	
	return;
}

void writeIPConfig() {
	char ipconfig[255];
	
	if (!strncmp (IPconfig_path,"mc0:/SYS-CONF",13)){
		fioMkdir("mc0:/SYS-CONF");
	}else if (!strncmp (IPconfig_path,"mc1:/SYS-CONF",13)){
		fioMkdir("mc1:/SYS-CONF");
	}

	if (IPconfig_path[2] == '?')
		IPconfig_path[2] = '0';
	
	int fd=fioOpen(IPconfig_path, O_WRONLY | O_CREAT);
	if (fd<0) {
		if (IPconfig_path[2] == '0')
			IPconfig_path[2] = '1';
		else
			IPconfig_path[2] = '0';
		fd=fioOpen(IPconfig_path, O_WRONLY | O_CREAT);
		if (fd<0) {
			//DEBUG: printf("No config. Exiting...\n");
			IPconfig_path[2] = '?';
			return;
		}
	}
	
	sprintf(ipconfig, "%d.%d.%d.%d %d.%d.%d.%d %d.%d.%d.%d\r\n", ps2_ip[0], ps2_ip[1], ps2_ip[2], ps2_ip[3],
															ps2_netmask[0], ps2_netmask[1], ps2_netmask[2], ps2_netmask[3],
															ps2_gateway[0], ps2_gateway[1], ps2_gateway[2], ps2_gateway[3]);

	fioWrite(fd, ipconfig, strlen(ipconfig)+1);
	
	fioClose(fd);
	
	return;
}


int readConfig(struct TConfigSet* config, const char *fname) {
	int fd=fioOpen(fname, O_RDONLY);
	
	if (fd<0) {
		//DEBUG: printf("No config. Exiting...\n");
		return 0;
	}
	
	char line[2048];
	unsigned int lineno = 0;
	
	while (readline(fd, line, 2048)) {
		lineno++;
		
		char key[255], val[255];
		memset(key, 0, 255);
		memset(val, 0, 255);
		
		if (splitAssignment(line, key, val)) {
			// insert config value
			setConfigStr(config, key, val);
		} else {
			// DEBUG: ignoring...
			// printf("Malformed config file '%s' line %d: '%s'\n", fname, lineno, line);
		}
	}
	
	fioClose(fd);
	return 1;
}

int writeConfig(struct TConfigSet* config, const char *fname) {

	if (!strncmp (fname,"mc0:/SYS-CONF",13)){
		fioMkdir("mc0:/SYS-CONF");
	}else if (!strncmp (fname,"mc1:/SYS-CONF",13)){
		fioMkdir("mc1:/SYS-CONF");
	}

	int fd=fioOpen(fname, O_WRONLY | O_CREAT | O_TRUNC);
	
	if (fd<0)
		return 0;
	
	char line[2048];
	struct TConfigValue* cur = config->head;
	
	while (cur) {
		line[0] = '\0';
		
		configValueToText(cur, line, 2048);
		
		int len = strlen(line);

		fioWrite(fd, line, len);
		fioWrite(fd, "\r\n", 2);

		// and advance
		cur = cur->next;
	}
	
	fioClose(fd);
	return 1;
}

void clearConfig(struct TConfigSet* config) {
	while (config->head) {
		struct TConfigValue* cur = config->head;
		config->head = cur->next;
		
		free(cur);
	}
	
	config->head = NULL;
	config->tail = NULL;
}

void ListDir(char* directory) {
	int ret=-1;
	int n_dir=0;
	fio_dirent_t record;
	int i=0;
	char dir[255];
	
	// free the prev. list
	i = 0;
	while (theme_dir[i] && i < 256) {
		free(theme_dir[i]);
		theme_dir[i] = NULL;
		++i;
	}

	theme_dir[0]=malloc(7);
	sprintf(theme_dir[0],"<none>");
	n_dir=1;

	for(i=0;i<3;i++){
	
		switch(i){
			case 0:
				snprintf(dir, 255, "mc0:%s", directory);
			break;
			case 1:
				snprintf(dir, 255, "mc1:%s", directory);
			break;
			case 2:
				snprintf(dir, 255, "%s%s", USB_prefix, directory);
			break;
		}
	
		if ((ret = fioDopen(dir)) >= 0) {
			while (fioDread(ret, &record) > 0) {
				/*Keep track of number of files */
				if (FIO_SO_ISDIR(record.stat.mode)) {
					if(record.name[0]!='.' && (record.name[0]!='.' && record.name[1]!='.')){
						//printf("%s\n",record.name);
						theme_dir[n_dir]=malloc(sizeof(record.name));
						sprintf(theme_dir[n_dir],"%s",record.name);
						n_dir++;
					}
				}else{
					if(strstr(record.name,".zip") || strstr(record.name,".ZIP")){
						//printf("%s\n",record.name);
						theme_dir[n_dir]=malloc(sizeof(record.name));
						sprintf(theme_dir[n_dir],"%s",record.name);
						theme_dir[n_dir][strlen(theme_dir[n_dir])-4]='\0';
						n_dir++;
					}
				}
				
				if (n_dir >= 254)
					break; // ensure padding is there!
			}
				fioDclose(ret);
		}
	}
	
	max_theme_dir = n_dir;
	theme_dir[max_theme_dir] = NULL;
	
	fioDclose(ret);
}