+#include "stdafx.h"\r
+#include "tester1.h"\r
+#include <commctrl.h>\r
+//#include <aygshell.h>\r
+#include <sipapi.h>\r
+#include "setup.h"\r
+\r
+\r
+#define BOOT_LOGO_PATH "\\My Documents\\booting.bmp"\r
+#define BOOT_LOGO_PATH_CF "\\CF Card\\booting.bmp"\r
+#define BOOT_LOGO_DONE_PATH "\\My Documents\\done.bmp"\r
+#define BOOT_LOGO_DONE_PATH_CF "\\CF Card\\done.bmp"\r
+#define DONE1_X 100\r
+#define DONE1_Y 100\r
+#define DONE2_X 100\r
+#define DONE2_Y 130\r
+\r
+\r
+void setup_linux_params(long bootimg_dest, UINT32 initrd,UINT32 initrdl, long dram_size, const char *cmdline, char*base)\r
+{\r
+ int rootdev = 0x00ff;\r
+ struct tag *tag;\r
+ int newcmdlinelen = 0;\r
+ char *newcmdline = NULL;\r
+\r
+\r
+ tag = (struct tag *)(base+0x100);\r
+\r
+ tag->hdr.tag = ATAG_CORE;\r
+ tag->hdr.size = tag_size(tag_core);\r
+ tag->u.core.flags =0;\r
+ tag->u.core.pagesize = 0x00001000;\r
+ tag->u.core.rootdev = rootdev;\r
+ tag = tag_next(tag);\r
+\r
+ // now the cmdline tag\r
+ tag->hdr.tag = ATAG_CMDLINE;\r
+ // must be at least +3!! 1 for the null and 2 for the ???\r
+ tag->hdr.size = (strlen(cmdline) + 3 + sizeof(struct tag_header)) >> 2;\r
+ //tag->hdr.size = (strlen(cmdline) + 10 + sizeof(struct tag_header)) >> 2;\r
+ strcpy(tag->u.cmdline.cmdline,cmdline);\r
+ tag = tag_next(tag);\r
+\r
+\r
+ // now the mem32 tag\r
+ tag->hdr.tag = ATAG_MEM;\r
+ tag->hdr.size = tag_size(tag_mem32);\r
+ tag->u.mem.size = dram_size;\r
+ tag->u.mem.start = MEM_START;\r
+ tag = tag_next(tag);\r
+ \r
+\r
+ /* and now the initrd tag */\r
+ if (initrdl) {\r
+ tag->hdr.tag = INITRD_TAG;\r
+ tag->hdr.size = tag_size(tag_initrd);\r
+ tag->u.initrd.start = initrd;\r
+ tag->u.initrd.size = initrdl;\r
+ tag = tag_next(tag);\r
+ }\r
+ \r
+ tag->hdr.tag = ATAG_VIDEOTEXT;\r
+ tag->hdr.size = tag_size(tag_videotext);\r
+ tag->u.videotext.video_lines = 40;\r
+ tag->u.videotext.video_cols = 30;\r
+ tag = tag_next(tag);\r
+\r
+ // now the NULL tag\r
+ tag->hdr.tag = ATAG_NONE;\r
+ tag->hdr.size = 0;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+/* loading process:\r
+function do_it is loaded onto address KERNELCOPY along with parameters(offset=0x100) and\r
+kernel image(offset=0x8000). Afterwards DRAMloader is called; it disables MMU and\r
+jumps onto KERNELCOPY. Function do_it then copies kernel image to its proper address(0xA0008000) \r
+and calls it.\r
+Initrd is loaded onto address INITRD and the address is passed to kernel via ATAG\r
+*/\r
+\r
+\r
+// This resets some devices\r
+void ResetDevices()\r
+{\r
+#ifndef STRONGARM\r
+ WritePhysical(0x4050000C,0); // Reset AC97\r
+ WritePhysical(0x48000014,0); // Reset PCMCIA\r
+ for(int i=0;i<0x3C;i+=4)\r
+ WritePhysical(0x40000000,8); // Set DMAs to Stop state\r
+ WritePhysical(0x400000F0,0); // DMA do not gen interrupt\r
+ SetGPIOio(28,0); // AC97\r
+ SetGPIOio(29,0); // AC97/I2S\r
+ SetGPIOio(30,0); // I2S/AC97\r
+ SetGPIOio(31,0); // I2S/AC97\r
+ SetGPIOio(32,0); // AC97/I2S\r
+ SetGPIOalt(28,0);\r
+ SetGPIOalt(29,0);\r
+ SetGPIOalt(30,0);\r
+ SetGPIOalt(31,0);\r
+ SetGPIOalt(32,0);\r
+#endif\r
+}\r
+\r
+\r
+\r
+\r
+void mymemcpy(char* a, char* b, int size);\r
+\r
+void boot_linux(char *filename,char* initrd,char *param)\r
+{\r
+ FILE *fd=fopen(filename,"rb");\r
+ int ret;\r
+\r
+ FILE* fd1;\r
+\r
+ long initrdl;\r
+ long len;\r
+\r
+#ifndef STRONGARM\r
+ Image image;\r
+ Image image_done;\r
+#endif\r
+\r
+\r
+\r
+ if(!fd)\r
+ {\r
+ FILE *logfd=fopen("\\bootlog.txt","a");\r
+ fprintf(logfd, "Booting: ***FAILED TO OPEN %s***\n",filename);\r
+ fclose(logfd);\r
+ return;\r
+ }\r
+\r
+ fseek(fd,0,SEEK_END);\r
+ len=ftell(fd);\r
+ fseek(fd,0,SEEK_SET);\r
+\r
+ fd1=fopen(initrd,"rb");\r
+ initrdl=0;\r
+ if(fd1) \r
+ {\r
+ fseek(fd1,0,SEEK_END);\r
+ initrdl=ftell(fd1);\r
+ fseek(fd1,0,SEEK_SET);\r
+ }\r
+ FILE *logfd=fopen("\\bootlog.txt","a");\r
+ fprintf(logfd, "Booting: Images.");\r
+ fclose(logfd);\r
+\r
+ \r
+#ifndef STRONGARM\r
+ /* i haven't ported this to strongarm, hope this is not important to\r
+ * anyone */\r
+ init_fb();\r
+ try_fb();\r
+\r
+ image=ReadBMP(BOOT_LOGO_PATH);\r
+ if (!image.p) image=ReadBMP(BOOT_LOGO_PATH_CF);\r
+ image_done=ReadBMP(BOOT_LOGO_DONE_PATH);\r
+ if (!image_done.p) image_done = ReadBMP(BOOT_LOGO_DONE_PATH_CF);\r
+ if (image.p) ShowImage(image.p,image.x,image.y);\r
+#endif\r
+ \r
+ logfd=fopen("\\bootlog.txt","a");\r
+ fprintf(logfd, "Booting: entering supervisor mode.");\r
+ fclose(logfd);\r
+\r
+ /* now becoming supervisor. */\r
+ SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);\r
+// CeSetThreadQuantum(GetCurrentThread(),0);\r
+ SetKMode(1);\r
+ SetProcPermissions(0xffffffff);\r
+ /* <ibot> rooooooooot has landed! */\r
+\r
+ logfd=fopen("\\bootlog.txt","a");\r
+ fprintf(logfd, "Booting: supervisor mode.");\r
+ fclose(logfd);\r
+\r
+ void *mmu=(void*)read_mmu();\r
+ UINT32 *data=NULL,*lcd=NULL,*intr=NULL,*_mmu=NULL;\r
+ char *watch=NULL,*krnl=NULL;\r
+\r
+\r
+ IntOff();\r
+\r
+\r
+ char *kernel_copy2=(char*)VirtualAlloc((void*)0x0,0x8000+len, MEM_RESERVE|MEM_TOP_DOWN,PAGE_READWRITE);\r
+ ret=VirtualCopy((void*)kernel_copy2,(void *) (KERNELCOPY/256), 0x8000+len, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);\r
+\r
+ char *initrd_copy2;\r
+\r
+\r
+ if(fd1)\r
+ {\r
+ initrd_copy2=(char*)VirtualAlloc((void*)0x0,initrdl, MEM_RESERVE|MEM_TOP_DOWN,PAGE_READWRITE);\r
+ ret=VirtualCopy((void*)initrd_copy2,(void *) (INITRD/256), initrdl, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);\r
+ }\r
+\r
+ void(*relmemcpy)(char*,char*,int);\r
+ relmemcpy=(void (__cdecl *)(char *,char *,int))VirtualAlloc((void*)0x0, 1024, MEM_RESERVE|MEM_TOP_DOWN,PAGE_READWRITE);\r
+\r
+ /* ask joshua */\r
+#ifndef STRONGARM\r
+ ret=VirtualCopy((void*)relmemcpy,(void *) (0xa0001000/256), 1024, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);\r
+#else\r
+ ret=VirtualCopy((void*)relmemcpy,(void *) (0xc0001000/256), 1024, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);\r
+#endif\r
+\r
+ if(!kernel_copy2) return;\r
+\r
+\r
+ UINT32 phys_addr;\r
+ phys_addr=KERNELCOPY;\r
+\r
+\r
+ char *data1,*data2;\r
+\r
+ data1=(char*)malloc(len);\r
+\r
+ char *initrd1=NULL;\r
+\r
+ if(fd1) initrd1=(char*)malloc(initrdl);\r
+\r
+ if(!data1) return;\r
+\r
+ if(!ret) return;\r
+\r
+ data2= (char*)do_it;\r
+\r
+\r
+ fread(data1,len,1,fd);\r
+ fclose(fd);\r
+\r
+\r
+ if(fd1)\r
+ {\r
+ fread(initrd1,initrdl,1,fd1);\r
+ fclose(fd1);\r
+ }\r
+\r
+ // Do not block interrupts before they are needed anymore\r
+ // Like reading the SD card.\r
+ intr=(UINT32*)VirtualAlloc((void*)0x0,0x100, MEM_RESERVE,PAGE_READWRITE);\r
+\r
+ // Interrupt control registers\r
+ ret=VirtualCopy((void*)intr,(void *) (ICIP/256), 0x100, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);\r
+\r
+ intr[1]=0;\r
+\r
+// ResetDevices();\r
+\r
+ UART_puts("LinExec: Passing the point of no return.. Now.\r\n");\r
+\r
+ UINT32 crc=0;\r
+\r
+ setup_linux_params(BOOTIMG, INITRD,initrdl, MEM_SIZE*1024*1024 , param,kernel_copy2);\r
+\r
+ memcpy(relmemcpy,mymemcpy,1024);\r
+ relmemcpy(kernel_copy2,data2,0x100);\r
+\r
+ if(fd1)\r
+ relmemcpy(initrd_copy2,initrd1,initrdl);\r
+\r
+ relmemcpy(kernel_copy2+0x8000,data1,len);\r
+\r
+ UART_puts("LinExec: Entering DRAMloader...\r\n");\r
+ \r
+ DRAMloader(phys_addr, MACH_TYPE);\r
+}\r
+\r
+void mymemcpy(char* a, char* b, int size)\r
+{\r
+ while (size)\r
+ {\r
+ *a=*b;\r
+ size--;\r
+ a++; b++;\r
+ };\r
+};\r
+\r
+/*\r
+ Loads parameters from file given.\r
+ The file has to be:\r
+ kernel image\r
+ initrd\r
+ kernel cmdline\r
+*/\r
+\r
+void load_boot(char *ParamFile)\r
+{\r
+ FILE *stream;\r
+\r
+ UART_setup();\r
+\r
+ stream=fopen(ParamFile,"r");\r
+ if(!stream) {\r
+ FILE *logfd=fopen("\\bootlog.txt","a");\r
+ fprintf(logfd, "Booting: ***FAILED TO OPEN %s***\n",ParamFile);\r
+ fclose(logfd);\r
+ return;\r
+ }\r
+ char cmd[200],image[50],initrd[50];\r
+\r
+ fgets(image,50,stream);\r
+ image[strlen(image)-1]=0; // remove \n from the end\r
+ \r
+ fgets(initrd,50,stream);\r
+ initrd[strlen(initrd)-1]=0;\r
+ \r
+ fgets(cmd,200,stream);\r
+ cmd[strlen(cmd)-1]=0;\r
+\r
+ fclose(stream);\r
+\r
+ UART_puts("LinExec: Beginning boot_linux.\r\n");\r
+ boot_linux(image,initrd,cmd);\r
+} \r