/[pcsx2_0.9.7]/trunk/fps2bios/kernel/iopload/iopboot/iopboot.c
ViewVC logotype

Contents of /trunk/fps2bios/kernel/iopload/iopboot/iopboot.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations) (download)
Mon Sep 6 11:40:06 2010 UTC (9 years, 10 months ago) by william
File MIME type: text/plain
File size: 8067 byte(s)
exported r3113 from ./upstream/trunk
1 //
2 // iopboot.c
3 //
4 // this is the c code for the iopboot file in the ps2 rom.
5 // this file is located at 0xBFC4A000 in the ps2 bios rom0.
6 // modload.irx also from the ps2 bios executes this direct from the rom
7 // (no loading to ram first)
8 //
9 // this is based on florin's disasm and converted to c code by xorloser and zerofrog
10 //
11
12 #include <tamtypes.h>
13 #include <stdio.h>
14
15 #include "iopload.h"
16 #include "iopdebug.h"
17 #include "iopelf.h"
18 #include "romdir.h"
19 #include "kloadcore.h"
20
21 static void kstrcpy(char* dst, const char* src);
22 static int kstrlen(const char* src);
23
24 //BOOT_PARAMS boot_params;
25 //u32* next_free_address[0x40]; // up to 64 modules
26
27
28 // this is the start point of execution at 0xBFC4A000
29 //
30 // it loads the IOPBTCONF module list from rom0 and compiles a
31 // list of modules and their addresses.
32 //
33 // this list is then passed to loadcore as it is executed in order
34 // to then load the rest of the modules
35 //
36 // args: total size of IOP ram in MegaBytes
37 // bootinfo flags
38 // string containing the reboot image filepath
39 // ? doesnt seem to be used
40 void _start(int ramMBSize, int bootInfo, char* udnlString, int unk)
41 {
42 ROMFS ri;
43 void *(*sysmem_entry)(u32 iopmemsize);
44 void (*loadcore_entry)(BOOT_PARAMS *init);
45 int i;
46 ROMDIR_INFO romdir_info;
47 ROMFILE_INFO romfile_info;
48 char conf_filename[10];
49 int ram_byte_size, num_lines;
50 u32 module_load_addr;
51 u32** modules_ptr;
52 char* file_data_ptr, *file_data_end;
53 void* psysmemstart;
54 BOOT_PARAMS* boot_params;
55
56 if( ramMBSize <= 2 )
57 ram_byte_size = 2;
58 else
59 ram_byte_size = ramMBSize;
60 ram_byte_size <<= 20;
61
62 // compile module list to send to loadcore
63 boot_params = (BOOT_PARAMS*)0x30000; // random address, has to be clear before loadcore call
64 boot_params->ramMBSize = ramMBSize;
65 boot_params->bootInfo = bootInfo;
66 boot_params->udnlString = NULL;
67 boot_params->moduleAddrs = (u32**)((u32)boot_params + sizeof(BOOT_PARAMS)); // right after
68
69 // if a undl string is specified, get a copy of it and store a pointer to it
70 if(udnlString)
71 {
72 boot_params->udnlString = (char*)boot_params->moduleAddrs;
73 kstrcpy(boot_params->udnlString, udnlString);
74 boot_params->moduleAddrs = (u32**)((u32)boot_params->udnlString + ROUND_UP(kstrlen(udnlString) + 8, 4));
75 }
76
77 // find the romdir table in the rom
78 if( searchRomDir((u32*)0xBFC00000, (u32*)0xBFC10000, &romdir_info) == NULL )
79 {
80 __printf("IOPBOOT: failed to find start of rom!\n");
81 // error - cant find romdir!
82 while(1) *(u8*)0x80000000 = 0;
83 }
84
85 // find the bootconf file in the romdir table
86 kstrcpy(conf_filename, "IOPBTCONF");
87 conf_filename[8] = '0' + bootInfo;
88 if( !searchFileInRom(&romdir_info, conf_filename, &romfile_info) )
89 {
90 kstrcpy(conf_filename, "IOPBTCONF");
91 if( !searchFileInRom(&romdir_info, conf_filename, &romfile_info) )
92 {
93 __printf("IOPBTCONF file not found!\n");
94 // error - cant find conf file!
95 while(1) *(u8*)0x80000000 = 1;
96 }
97 }
98
99 // count the number of lines in conf file
100 file_data_ptr = (char*)romfile_info.fileData;
101 file_data_end = (char*)romfile_info.fileData + romfile_info.entry->fileSize;
102 {
103 num_lines = 0;
104 while( file_data_ptr < file_data_end ) {
105 // loop until a "newline" charcter is found
106 while(file_data_ptr < file_data_end) {
107 if(*file_data_ptr++ < ' ')
108 break;
109 }
110
111 // loop until a "non-newline" charcter is found
112 while(file_data_ptr < file_data_end) {
113 if(*file_data_ptr++ >= ' ')
114 break;
115 }
116
117 num_lines++;
118 }
119 num_lines++;
120 }
121
122 // get the addresses of each module
123 {
124 module_load_addr = 0;
125 boot_params->numConfLines = num_lines-1;
126 modules_ptr = boot_params->moduleAddrs;
127 char* file_data_ptr = (char*)romfile_info.fileData;
128 while( file_data_ptr < file_data_end ) {
129 if(*file_data_ptr == '@') {
130 file_data_ptr++;
131 module_load_addr = getHexNumber(&file_data_ptr);
132 }
133 else if(*file_data_ptr == '!') {
134 if( file_data_ptr[1] == 'a' &&
135 file_data_ptr[2] == 'd' &&
136 file_data_ptr[3] == 'd' &&
137 file_data_ptr[4] == 'r' &&
138 file_data_ptr[5] == ' ' ) {
139 file_data_ptr += 6;
140 *modules_ptr++ = (u32*)(getHexNumber(&file_data_ptr) * 4 + 1);
141 *modules_ptr++ = 0;
142 }
143 }
144 else if(*file_data_ptr != '#') {
145 // 'file_data_ptr' should be pointing to a filename
146 // this finds the address of that file in the rom
147 ROMFILE_INFO module_fileinfo;
148 char strmodule[16];
149 for(i = 0; i < 16; ++i) {
150 if( file_data_ptr[i] < ' ' )
151 break;
152 strmodule[i] = file_data_ptr[i];
153 }
154 strmodule[i] = 0;
155
156 if( searchFileInRom(&romdir_info, strmodule, &module_fileinfo) == NULL ) {
157 __printf("IOPBOOT: failed to find %s module\n", strmodule);
158 return;
159 }
160
161 //__printf("mod: %s:%x\n", strmodule, module_fileinfo.fileData);
162
163 *modules_ptr++ = (u32*)module_fileinfo.fileData;
164 *modules_ptr = 0; // don't increment
165 }
166
167 // loop until a "newline" charcter is found
168 while(file_data_ptr < file_data_end) {
169 if(*file_data_ptr++ < ' ')
170 break;
171 }
172
173 // loop until a "non-newline" charcter is found
174 while(file_data_ptr < file_data_end) {
175 if(*file_data_ptr >= ' ')
176 break;
177 file_data_ptr++;
178 }
179 }
180 }
181
182 if( searchFileInRom(&romdir_info, "IOPBOOT", &romfile_info) == NULL ) {
183 __printf("loadElfFile: failed to find IOPBOOT module\n");
184 return;
185 }
186
187 // load sysmem module to memory and execute it
188 if( searchFileInRom(&romdir_info, "SYSMEM", &romfile_info) == NULL ) {
189 __printf("loadElfFile: failed to find SYSMEM module\n");
190 return;
191 }
192 sysmem_entry = (void *(*)(u32))loadElfFile(&romfile_info, module_load_addr);
193 if( sysmem_entry == 0 )
194 return;
195
196 psysmemstart = sysmem_entry(ram_byte_size);
197 //FlushIcache();
198 if( psysmemstart == 0 ) {
199 __printf("IOPBOOT: sysmem failed\n");
200 return;
201 }
202
203 __printf("SYSMEM success, start addr: %x, alloc start: %x\n", module_load_addr, psysmemstart);
204
205 if( searchFileInRom(&romdir_info, "LOADCORE", &romfile_info) == NULL ) {
206 __printf("loadElfFile: failed to find SYSMEM module\n");
207 return;
208 }
209 loadcore_entry = (void (*)())loadElfFile(&romfile_info, (u32)psysmemstart);
210 if( loadcore_entry == 0 )
211 return;
212
213 boot_params->firstModuleAddr = (u32)module_load_addr + 0x30; // skip elf?
214 if(0x1FC10000 < ram_byte_size) {
215 boot_params->pos = 0x1FC00000;
216 boot_params->size = 0x10100;
217 }
218 else {
219 boot_params->pos = 0;
220 boot_params->size = 0;
221 }
222
223 __printf("executing LOADCORE entry at %p\n", loadcore_entry);
224 loadcore_entry(boot_params);
225
226 __printf("iopboot error\n");
227
228 // error - loadcore shouldnt ever return
229 while(1) *(u8*)0x80000000 = 2;
230 }
231
232 void Kmemcpy(void *dest, const void *src, int n) {
233 const u8 *s = (u8*)src;
234 u8 *d = (u8*)dest;
235
236 while (n) {
237 *d++ = *s++; n--;
238 }
239 }
240
241 static void kstrcpy(char* dst, const char* src)
242 {
243 while(*src) *dst++ = *src++;
244 *dst = 0;
245 }
246
247 static int kstrlen(const char* src)
248 {
249 int len = 0;
250 while(*src++) len++;
251 return len;
252 }

  ViewVC Help
Powered by ViewVC 1.1.22