usb gecko retries
[libogc.git] / libogc / conf.c
blob1cc6aee9c4edf9d922d081c4bd08fe06051bc042
1 /*-------------------------------------------------------------
3 conf.c -- SYSCONF support
5 Copyright (C) 2008
6 Hector Martin (marcan)
8 This software is provided 'as-is', without any express or implied
9 warranty. In no event will the authors be held liable for any
10 damages arising from the use of this software.
12 Permission is granted to anyone to use this software for any
13 purpose, including commercial applications, and to alter it and
14 redistribute it freely, subject to the following restrictions:
16 1. The origin of this software must not be misrepresented; you
17 must not claim that you wrote the original software. If you use
18 this software in a product, an acknowledgment in the product
19 documentation would be appreciated but is not required.
21 2. Altered source versions must be plainly marked as such, and
22 must not be misrepresented as being the original software.
24 3. This notice may not be removed or altered from any source
25 distribution.
27 -------------------------------------------------------------*/
29 #if defined(HW_RVL)
31 #include <stdio.h>
32 #include <string.h>
33 #include <malloc.h>
34 #include "ipc.h"
35 #include "asm.h"
36 #include "processor.h"
37 #include "conf.h"
40 static int __conf_inited = 0;
41 static u8 __conf_buffer[0x4000] ATTRIBUTE_ALIGN(32);
42 static char __conf_txt_buffer[0x101] ATTRIBUTE_ALIGN(32);
44 static const char __conf_file[] ATTRIBUTE_ALIGN(32) = "/shared2/sys/SYSCONF";
45 static const char __conf_txt_file[] ATTRIBUTE_ALIGN(32) = "/title/00000001/00000002/data/setting.txt";
47 void __CONF_DecryptTextBuffer(void)
49 u32 key = 0x73B5DBFA;
50 int i;
52 for(i=0; i<0x100; i++) {
53 __conf_txt_buffer[i] ^= key & 0xff;
54 key = (key<<1) | (key>>31);
58 s32 CONF_Init(void)
60 int fd;
61 int ret;
63 if(__conf_inited) return 0;
65 fd = IOS_Open(__conf_file,1);
66 if(fd < 0) return fd;
68 memset(__conf_buffer,0,0x4000);
69 memset(__conf_txt_buffer,0,0x101);
71 ret = IOS_Read(fd, __conf_buffer, 0x4000);
72 IOS_Close(fd);
73 if(ret != 0x4000) return CONF_EBADFILE;
75 fd = IOS_Open(__conf_txt_file,1);
76 if(fd < 0) return fd;
78 ret = IOS_Read(fd, __conf_txt_buffer, 0x100);
79 IOS_Close(fd);
80 if(ret != 0x100) return CONF_EBADFILE;
82 if(memcmp(__conf_buffer, "SCv0", 4)) return CONF_EBADFILE;
84 __CONF_DecryptTextBuffer();
86 __conf_inited = 1;
87 return 0;
90 int __CONF_GetTxt(const char *name, char *buf, int length)
92 char *line = __conf_txt_buffer;
93 char *delim, *end;
94 int slen;
95 int nlen = strlen(name);
97 if(!__conf_inited) return CONF_ENOTINIT;
99 while(line < (__conf_txt_buffer+0x100) ) {
100 delim = strchr(line, '=');
101 if(delim && ((delim - line) == nlen) && !memcmp(name, line, nlen)) {
102 delim++;
103 end = strchr(line, '\r');
104 if(end) {
105 slen = end - delim;
106 if(slen < length) {
107 memcpy(buf, delim, slen);
108 buf[slen] = 0;
109 return slen;
110 } else {
111 return CONF_ETOOBIG;
116 // skip to line end
117 while(line < (__conf_txt_buffer+0x100) && *line++ != '\n');
119 return CONF_ENOENT;
122 u8 *__CONF_Find(const char *name)
124 u16 count;
125 u16 *offset;
126 int nlen = strlen(name);
127 count = *((u16*)(&__conf_buffer[4]));
128 offset = (u16*)&__conf_buffer[6];
130 while(count--) {
131 if((nlen == ((__conf_buffer[*offset]&0x0F)+1)) && !memcmp(name, &__conf_buffer[*offset+1], nlen))
132 return &__conf_buffer[*offset];
133 offset++;
135 return NULL;
138 s32 CONF_GetLength(const char *name)
140 u8 *entry;
142 if(!__conf_inited) return CONF_ENOTINIT;
144 entry = __CONF_Find(name);
145 if(!entry) return CONF_ENOENT;
147 switch(*entry>>5) {
148 case 1:
149 return *((u16*)&entry[strlen(name)+1]) + 1;
150 case 2:
151 return entry[strlen(name)+1] + 1;
152 case 3:
153 return 1;
154 case 4:
155 return 2;
156 case 5:
157 return 4;
158 case 7:
159 return 1;
160 default:
161 return CONF_ENOTIMPL;
165 int CONF_GetType(const char *name)
167 u8 *entry;
168 if(!__conf_inited) return CONF_ENOTINIT;
170 entry = __CONF_Find(name);
171 if(!entry) return CONF_ENOENT;
173 return *entry>>5;
176 s32 CONF_Get(const char *name, void *buffer, u32 length)
178 u8 *entry;
179 s32 len;
180 if(!__conf_inited) return CONF_ENOTINIT;
182 entry = __CONF_Find(name);
183 if(!entry) return CONF_ENOENT;
185 len = CONF_GetLength(name);
186 if(len<0) return len;
187 if(len>length) return CONF_ETOOBIG;
189 switch(*entry>>5) {
190 case CONF_BIGARRAY:
191 memcpy(buffer, &entry[strlen(name)+3], len);
192 break;
193 case CONF_SMALLARRAY:
194 memcpy(buffer, &entry[strlen(name)+2], len);
195 break;
196 case CONF_BYTE:
197 case CONF_SHORT:
198 case CONF_LONG:
199 case CONF_BOOL:
200 memset(buffer, 0, length);
201 memcpy(buffer, &entry[strlen(name)+1], len);
202 break;
203 default:
204 return CONF_ENOTIMPL;
206 return len;
209 s32 CONF_GetShutdownMode(void)
211 u8 idleconf[2] = {0,0};
212 int res;
214 res = CONF_Get("IPL.IDL", idleconf, 2);
215 if(res<0) return res;
216 if(res!=2) return CONF_EBADVALUE;
217 return idleconf[0];
220 s32 CONF_GetIdleLedMode(void)
222 int res;
223 u8 idleconf[2] = {0,0};
224 res = CONF_Get("IPL.IDL", idleconf, 2);
225 if(res<0) return res;
226 if(res!=2) return CONF_EBADVALUE;
227 return idleconf[1];
230 s32 CONF_GetProgressiveScan(void)
232 int res;
233 u8 val = 0;
234 res = CONF_Get("IPL.PGS", &val, 1);
235 if(res<0) return res;
236 if(res!=1) return CONF_EBADVALUE;
237 return val;
240 s32 CONF_GetEuRGB60(void)
242 int res;
243 u8 val = 0;
244 res = CONF_Get("IPL.E60", &val, 1);
245 if(res<0) return res;
246 if(res!=1) return CONF_EBADVALUE;
247 return val;
250 s32 CONF_GetIRSensitivity(void)
252 int res;
253 u32 val = 0;
254 res = CONF_Get("BT.SENS", &val, 4);
255 if(res<0) return res;
256 if(res!=4) return CONF_EBADVALUE;
257 return val;
260 s32 CONF_GetSensorBarPosition(void)
262 int res;
263 u8 val = 0;
264 res = CONF_Get("BT.BAR", &val, 1);
265 if(res<0) return res;
266 if(res!=1) return CONF_EBADVALUE;
267 return val;
270 s32 CONF_GetPadSpeakerVolume(void)
272 int res;
273 u8 val = 0;
274 res = CONF_Get("BT.SPKV", &val, 1);
275 if(res<0) return res;
276 if(res!=1) return CONF_EBADVALUE;
277 return val;
280 s32 CONF_GetPadMotorMode(void)
282 int res;
283 u8 val = 0;
284 res = CONF_Get("BT.MOT", &val, 1);
285 if(res<0) return res;
286 if(res!=1) return CONF_EBADVALUE;
287 return val;
290 s32 CONF_GetSoundMode(void)
292 int res;
293 u8 val = 0;
294 res = CONF_Get("IPL.SND", &val, 1);
295 if(res<0) return res;
296 if(res!=1) return CONF_EBADVALUE;
297 return val;
300 s32 CONF_GetLanguage(void)
302 int res;
303 u8 val = 0;
304 res = CONF_Get("IPL.LNG", &val, 1);
305 if(res<0) return res;
306 if(res!=1) return CONF_EBADVALUE;
307 return val;
310 s32 CONF_GetCounterBias(u32 *bias)
312 int res;
313 res = CONF_Get("IPL.CB", bias, 4);
314 if(res<0) return res;
315 if(res!=4) return CONF_EBADVALUE;
316 return CONF_ERR_OK;
319 s32 CONF_GetScreenSaverMode(void)
321 int res;
322 u8 val = 0;
323 res = CONF_Get("IPL.SSV", &val, 1);
324 if(res<0) return res;
325 if(res!=1) return CONF_EBADVALUE;
326 return val;
329 s32 CONF_GetDisplayOffsetH(s8 *offset)
331 int res;
332 res = CONF_Get("IPL.DH", offset, 1);
333 if(res<0) return res;
334 if(res!=1) return CONF_EBADVALUE;
335 return 0;
338 s32 CONF_GetPadDevices(conf_pads *pads)
340 int res;
342 res = CONF_Get("BT.DINF", pads, sizeof(conf_pads));
343 if(res < 0) return res;
344 if(res < sizeof(conf_pads)) return CONF_EBADVALUE;
345 return 0;
348 s32 CONF_GetNickName(u8 *nickname)
350 int i, res;
351 u16 buf[11];
353 res = CONF_Get("IPL.NIK", buf, 0x16);
354 if(res < 0) return res;
355 if((res != 0x16) || (!buf[0])) return CONF_EBADVALUE;
357 for(i=0; i<10; i++)
358 nickname[i] = buf[i];
359 nickname[10] = 0;
361 return res;
364 s32 CONF_GetAspectRatio(void)
366 int res;
367 u8 val = 0;
369 res = CONF_Get("IPL.AR", &val, 1);
370 if(res < 0) return res;
371 if(res!=1) return CONF_EBADVALUE;
372 return val;
375 s32 CONF_GetEULA(void)
377 int res;
378 u8 val = 0;
380 res = CONF_Get("IPL.EULA", &val, 1);
381 if(res < 0) return res;
382 if(res!=1) return CONF_EBADVALUE;
383 return val;
386 s32 CONF_GetParentalPassword(s8 *password)
388 int res;
389 u8 buf[0x4A];
391 res = CONF_Get("IPL.PC", buf, 0x4A);
392 if(res < 0) return res;
393 if(res!=1) return CONF_EBADVALUE;
395 memcpy(password, buf+3, 4);
396 password[4] = 0;
398 return res;
401 s32 CONF_GetParentalAnswer(s8 *answer)
403 int res;
404 u8 buf[0x4A];
406 res = CONF_Get("IPL.PC", buf, 0x4A);
407 if(res < 0) return res;
408 if(res!=1) return CONF_EBADVALUE;
410 memcpy(answer, buf+8, 32);
411 answer[32] = 0;
413 return res;
416 s32 CONF_GetWiiConnect24(void)
418 int res;
419 u32 val = 0;
421 res = CONF_Get("NET.WCFG", &val, 4);
422 if(res < 0) return res;
423 if(res!=4) return CONF_EBADVALUE;
424 return val;
427 s32 CONF_GetRegion(void)
429 int res;
430 char buf[3];
432 res = __CONF_GetTxt("GAME", buf, 3);
433 if(res < 0) return res;
434 if(!strcmp(buf, "JP")) return CONF_REGION_JP;
435 if(!strcmp(buf, "US")) return CONF_REGION_US;
436 if(!strcmp(buf, "EU")) return CONF_REGION_EU;
437 if(!strcmp(buf, "KR")) return CONF_REGION_KR;
438 if(!strcmp(buf, "CN")) return CONF_REGION_CN;
439 return CONF_EBADVALUE;
442 s32 CONF_GetArea(void)
444 int res;
445 char buf[4];
447 res = __CONF_GetTxt("AREA", buf, 4);
448 if(res < 0) return res;
449 if(!strcmp(buf, "JPN")) return CONF_AREA_JPN;
450 if(!strcmp(buf, "USA")) return CONF_AREA_USA;
451 if(!strcmp(buf, "EUR")) return CONF_AREA_EUR;
452 if(!strcmp(buf, "AUS")) return CONF_AREA_AUS;
453 if(!strcmp(buf, "BRA")) return CONF_AREA_BRA;
454 if(!strcmp(buf, "TWN")) return CONF_AREA_TWN;
455 if(!strcmp(buf, "ROC")) return CONF_AREA_ROC;
456 if(!strcmp(buf, "KOR")) return CONF_AREA_KOR;
457 if(!strcmp(buf, "HKG")) return CONF_AREA_HKG;
458 if(!strcmp(buf, "ASI")) return CONF_AREA_ASI;
459 if(!strcmp(buf, "LTN")) return CONF_AREA_LTN;
460 if(!strcmp(buf, "SAF")) return CONF_AREA_SAF;
461 if(!strcmp(buf, "CHN")) return CONF_AREA_CHN;
462 return CONF_EBADVALUE;
465 s32 CONF_GetVideo(void)
467 int res;
468 char buf[5];
470 res = __CONF_GetTxt("VIDEO", buf, 5);
471 if(res < 0) return res;
472 if(!strcmp(buf, "NTSC")) return CONF_VIDEO_NTSC;
473 if(!strcmp(buf, "PAL")) return CONF_VIDEO_PAL;
474 if(!strcmp(buf, "MPAL")) return CONF_VIDEO_MPAL;
475 return CONF_EBADVALUE;
478 #endif