fix kernel commandline "parsing" -> do not always cut off the last
[linexec-j720] / boot.cpp
CommitLineData
c5f1f439 1#include "stdafx.h"
2#include "tester1.h"
3#include <commctrl.h>
4//#include <aygshell.h>
5#include <sipapi.h>
6#include "setup.h"
7
8
9#define BOOT_LOGO_PATH "\\My Documents\\booting.bmp"
10#define BOOT_LOGO_PATH_CF "\\CF Card\\booting.bmp"
11#define BOOT_LOGO_DONE_PATH "\\My Documents\\done.bmp"
12#define BOOT_LOGO_DONE_PATH_CF "\\CF Card\\done.bmp"
13#define DONE1_X 100
14#define DONE1_Y 100
15#define DONE2_X 100
16#define DONE2_Y 130
17
18
19void setup_linux_params(long bootimg_dest, UINT32 initrd,UINT32 initrdl, long dram_size, const char *cmdline, char*base)
20{
21 int rootdev = 0x00ff;
22 struct tag *tag;
23 int newcmdlinelen = 0;
24 char *newcmdline = NULL;
25
26
27 tag = (struct tag *)(base+0x100);
28
29 tag->hdr.tag = ATAG_CORE;
30 tag->hdr.size = tag_size(tag_core);
31 tag->u.core.flags =0;
32 tag->u.core.pagesize = 0x00001000;
33 tag->u.core.rootdev = rootdev;
34 tag = tag_next(tag);
35
36 // now the cmdline tag
37 tag->hdr.tag = ATAG_CMDLINE;
d4a1fe7d 38 // tag header, zero-terminated string and round size to 32-bit words
39 tag->hdr.size = (sizeof (struct tag_header) + strlen (cmdline) + 1 + 3) >> 2;
c5f1f439 40 strcpy(tag->u.cmdline.cmdline,cmdline);
41 tag = tag_next(tag);
42
43
44 // now the mem32 tag
45 tag->hdr.tag = ATAG_MEM;
46 tag->hdr.size = tag_size(tag_mem32);
47 tag->u.mem.size = dram_size;
48 tag->u.mem.start = MEM_START;
49 tag = tag_next(tag);
50
51
52 /* and now the initrd tag */
53 if (initrdl) {
54 tag->hdr.tag = INITRD_TAG;
55 tag->hdr.size = tag_size(tag_initrd);
2f5dc0a0 56 tag->u.initrd.start = INITRD;
c5f1f439 57 tag->u.initrd.size = initrdl;
58 tag = tag_next(tag);
59 }
60
61 tag->hdr.tag = ATAG_VIDEOTEXT;
62 tag->hdr.size = tag_size(tag_videotext);
63 tag->u.videotext.video_lines = 40;
64 tag->u.videotext.video_cols = 30;
65 tag = tag_next(tag);
66
67 // now the NULL tag
68 tag->hdr.tag = ATAG_NONE;
69 tag->hdr.size = 0;
70}
71
72
73
74
75
76/* loading process:
77function do_it is loaded onto address KERNELCOPY along with parameters(offset=0x100) and
78kernel image(offset=0x8000). Afterwards DRAMloader is called; it disables MMU and
79jumps onto KERNELCOPY. Function do_it then copies kernel image to its proper address(0xA0008000)
80and calls it.
81Initrd is loaded onto address INITRD and the address is passed to kernel via ATAG
82*/
83
84
85// This resets some devices
86void ResetDevices()
87{
88#ifndef STRONGARM
89 WritePhysical(0x4050000C,0); // Reset AC97
90 WritePhysical(0x48000014,0); // Reset PCMCIA
91 for(int i=0;i<0x3C;i+=4)
92 WritePhysical(0x40000000,8); // Set DMAs to Stop state
93 WritePhysical(0x400000F0,0); // DMA do not gen interrupt
94 SetGPIOio(28,0); // AC97
95 SetGPIOio(29,0); // AC97/I2S
96 SetGPIOio(30,0); // I2S/AC97
97 SetGPIOio(31,0); // I2S/AC97
98 SetGPIOio(32,0); // AC97/I2S
99 SetGPIOalt(28,0);
100 SetGPIOalt(29,0);
101 SetGPIOalt(30,0);
102 SetGPIOalt(31,0);
103 SetGPIOalt(32,0);
104#endif
105}
106
107
108
109
110void mymemcpy(char* a, char* b, int size);
111
112void boot_linux(char *filename,char* initrd,char *param)
113{
114 FILE *fd=fopen(filename,"rb");
115 int ret;
116
117 FILE* fd1;
118
119 long initrdl;
120 long len;
121
122#ifndef STRONGARM
123 Image image;
124 Image image_done;
125#endif
126
127
128
129 if(!fd)
130 {
131 FILE *logfd=fopen("\\bootlog.txt","a");
132 fprintf(logfd, "Booting: ***FAILED TO OPEN %s***\n",filename);
133 fclose(logfd);
134 return;
135 }
136
137 fseek(fd,0,SEEK_END);
138 len=ftell(fd);
139 fseek(fd,0,SEEK_SET);
140
141 fd1=fopen(initrd,"rb");
142 initrdl=0;
143 if(fd1)
144 {
145 fseek(fd1,0,SEEK_END);
146 initrdl=ftell(fd1);
147 fseek(fd1,0,SEEK_SET);
148 }
149 FILE *logfd=fopen("\\bootlog.txt","a");
150 fprintf(logfd, "Booting: Images.");
151 fclose(logfd);
152
153
154#ifndef STRONGARM
155 /* i haven't ported this to strongarm, hope this is not important to
156 * anyone */
157 init_fb();
158 try_fb();
159
160 image=ReadBMP(BOOT_LOGO_PATH);
161 if (!image.p) image=ReadBMP(BOOT_LOGO_PATH_CF);
162 image_done=ReadBMP(BOOT_LOGO_DONE_PATH);
163 if (!image_done.p) image_done = ReadBMP(BOOT_LOGO_DONE_PATH_CF);
164 if (image.p) ShowImage(image.p,image.x,image.y);
165#endif
166
167 logfd=fopen("\\bootlog.txt","a");
168 fprintf(logfd, "Booting: entering supervisor mode.");
169 fclose(logfd);
170
171 /* now becoming supervisor. */
172 SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);
173// CeSetThreadQuantum(GetCurrentThread(),0);
174 SetKMode(1);
175 SetProcPermissions(0xffffffff);
176 /* <ibot> rooooooooot has landed! */
177
178 logfd=fopen("\\bootlog.txt","a");
179 fprintf(logfd, "Booting: supervisor mode.");
180 fclose(logfd);
181
182 void *mmu=(void*)read_mmu();
183 UINT32 *data=NULL,*lcd=NULL,*intr=NULL,*_mmu=NULL;
184 char *watch=NULL,*krnl=NULL;
185
186
187 IntOff();
188
189
190 char *kernel_copy2=(char*)VirtualAlloc((void*)0x0,0x8000+len, MEM_RESERVE|MEM_TOP_DOWN,PAGE_READWRITE);
191 ret=VirtualCopy((void*)kernel_copy2,(void *) (KERNELCOPY/256), 0x8000+len, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);
192
193 char *initrd_copy2;
194
195
196 if(fd1)
197 {
198 initrd_copy2=(char*)VirtualAlloc((void*)0x0,initrdl, MEM_RESERVE|MEM_TOP_DOWN,PAGE_READWRITE);
199 ret=VirtualCopy((void*)initrd_copy2,(void *) (INITRD/256), initrdl, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);
200 }
201
202 void(*relmemcpy)(char*,char*,int);
203 relmemcpy=(void (__cdecl *)(char *,char *,int))VirtualAlloc((void*)0x0, 1024, MEM_RESERVE|MEM_TOP_DOWN,PAGE_READWRITE);
204
205 /* ask joshua */
206#ifndef STRONGARM
207 ret=VirtualCopy((void*)relmemcpy,(void *) (0xa0001000/256), 1024, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);
208#else
209 ret=VirtualCopy((void*)relmemcpy,(void *) (0xc0001000/256), 1024, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);
210#endif
211
212 if(!kernel_copy2) return;
213
214
215 UINT32 phys_addr;
216 phys_addr=KERNELCOPY;
217
218
219 char *data1,*data2;
220
221 data1=(char*)malloc(len);
222
223 char *initrd1=NULL;
224
225 if(fd1) initrd1=(char*)malloc(initrdl);
226
227 if(!data1) return;
228
229 if(!ret) return;
230
231 data2= (char*)do_it;
232
233
234 fread(data1,len,1,fd);
235 fclose(fd);
236
237
238 if(fd1)
239 {
240 fread(initrd1,initrdl,1,fd1);
241 fclose(fd1);
242 }
243
244 // Do not block interrupts before they are needed anymore
245 // Like reading the SD card.
246 intr=(UINT32*)VirtualAlloc((void*)0x0,0x100, MEM_RESERVE,PAGE_READWRITE);
247
248 // Interrupt control registers
249 ret=VirtualCopy((void*)intr,(void *) (ICIP/256), 0x100, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);
250
251 intr[1]=0;
252
253// ResetDevices();
254
255 UART_puts("LinExec: Passing the point of no return.. Now.\r\n");
256
257 UINT32 crc=0;
258
259 setup_linux_params(BOOTIMG, INITRD,initrdl, MEM_SIZE*1024*1024 , param,kernel_copy2);
260
261 memcpy(relmemcpy,mymemcpy,1024);
262 relmemcpy(kernel_copy2,data2,0x100);
263
264 if(fd1)
265 relmemcpy(initrd_copy2,initrd1,initrdl);
266
267 relmemcpy(kernel_copy2+0x8000,data1,len);
268
269 UART_puts("LinExec: Entering DRAMloader...\r\n");
270
271 DRAMloader(phys_addr, MACH_TYPE);
272}
273
274void mymemcpy(char* a, char* b, int size)
275{
276 while (size)
277 {
278 *a=*b;
279 size--;
280 a++; b++;
281 };
282};
283
284/*
285 Loads parameters from file given.
286 The file has to be:
287 kernel image
288 initrd
289 kernel cmdline
290*/
291
292void load_boot(char *ParamFile)
293{
294 FILE *stream;
295
296 UART_setup();
297
298 stream=fopen(ParamFile,"r");
299 if(!stream) {
300 FILE *logfd=fopen("\\bootlog.txt","a");
301 fprintf(logfd, "Booting: ***FAILED TO OPEN %s***\n",ParamFile);
302 fclose(logfd);
303 return;
304 }
305 char cmd[200],image[50],initrd[50];
306
307 fgets(image,50,stream);
308 image[strlen(image)-1]=0; // remove \n from the end
309
310 fgets(initrd,50,stream);
311 initrd[strlen(initrd)-1]=0;
312
313 fgets(cmd,200,stream);
d4a1fe7d 314 if (cmd[strlen(cmd)-1] == 0x0a)
315 {
316 cmd[strlen(cmd)-1]=0;
317 if (cmd[strlen(cmd)-2] == 0x0d)
318 cmd[strlen(cmd)-2]=0;
319 }
c5f1f439 320
321 fclose(stream);
322
323 UART_puts("LinExec: Beginning boot_linux.\r\n");
324 boot_linux(image,initrd,cmd);
325}
Impressum, Datenschutz