01

How to run mod file that is loaded into HEAP


05-15-2008, 11:20 PM
Brewin
Registered User Join Date: Mar 2006
Posts: 152
Rep Power: 4

How to run file that is loaded in the

——————————————————————————–

Hi everyone,
How can I run a MOD file that is loaded into the HEAP???? Any idea’s…

__________________
Thanks everyone for the info they share here..
Ramki@TTSL
VALUE HAS A VALUE ONLY IF ITS VALUE IS VALUED

——————————————————————————–
Last edited by Brewin : 05-15-2008 at 11:27 PM.

Brewin
View Public Profile
Send a private message to Brewin
Send email to Brewin
Find all posts by Brewin
Add Brewin to Your Buddy List

#2 06-04-2008, 07:36 PM
ArdyFalls
Registered User Join Date: Apr 2007
Posts: 23
Rep Power: 0

Steps for loading a mod into the heap

——————————————————————————–

1) malloc memory on the heap that is the size of the mode file + 4 in bytes.
2) Set the initial 4 btyes to point to the AEE struct thing that has all the AEEStdlib funcitons.
3) Set the processors program counter (PC register) to address 0
Please note, I could be kinda off, look at the code for AEE_ModLoad, it is always at address zero. It will tell you the arguments that you need to pass to it before setting the pc register to it. Remember, the first four arguments of any function are passed in registers r0 -r4.

I hope this helps

__________________
Ardavon Falls
Senior Software Engineer
MobiTV Inc

ArdyFalls
View Public Profile
Send a private message to ArdyFalls
Find all posts by ArdyFalls
Add ArdyFalls to Your Buddy List

#3 07-28-2008, 10:45 PM
Brewin
Registered User Join Date: Mar 2006
Posts: 152
Rep Power: 4

Loading and running a compressed mod file…

——————————————————————————–

Hi All,
To run a compressed mod file in you need to create a mod loader(Approxmately 700+ bytes, atleast mine).

For 2.x brew devices, your mod loader file name should be same as of your app name.
Ex: myapp.mif, myapp.mod(your mod loader), filename.gz(your compressed myapp.mod file, you can name this file as filename.bar so that you don’t have any problems when user disables your app).

For 3.x brew devices on which we can read a mod file by setting the required permissions in MIF can add this mod loader file content at the start of your compressed mod file or you can use the same as that used for 2.x which don’t required any permissions or settings in MIF.
Ex: myapp.mif, myapp.mod(your mod loader + filename.gz).

To create a mod loader, you have every information you need in 2 threads.

First read only the first post by user “ajiva” in the following thread. Then return to this thread for additional info you need.

Quote:

http://brewforums.qualcomm.com/showthread.php?t=11637

A call to AEEMod_Load that you have seen in the first thread will set the params in the registers.

Quote:
Note 1: Don’t release the buffer that contains the uncompressed content of your actual mod file in your mod loader. Once the actual app is closed, it will released by BREW system.
Note2: Release all other allocated buffers and interfaces you created in mod loader before making a call to AEEMod_Load.

Thanks for ajiva and Ardy Falls for the valuable info they provided.

__________________
Thanks everyone for the info they share here..
Ramki@TTSL
VALUE HAS A VALUE ONLY IF ITS VALUE IS VALUED

——————————————————————————–
Last edited by Brewin : 08-06-2008 at 12:59 AM.

Brewin
View Public Profile
Send a private message to Brewin
Send email to Brewin
Find all posts by Brewin
Add Brewin to Your Buddy List

#4 07-29-2008, 12:36 AM
Rajni
Registered User Join Date: Apr 2005
Posts: 32
Rep Power: 0

Really usefull

——————————————————————————–

Hi Ramki,
Really very useful information.

Rajni
View Public Profile
Send a private message to Rajni
Find all posts by Rajni
Add Rajni to Your Buddy List

#5 08-06-2008, 12:57 AM
Brewin
Registered User Join Date: Mar 2006
Posts: 152
Rep Power: 4

Loading and running a compressed extension mod file…

——————————————————————————–

Hi all,
The above procedure is not working in case if the compressed mod file is an extension. Applet compressed mod file is running fine. The decompressed file of the extension mod is Identical with the original extension mod file, but device is getting crashed when AEEMod_Load of the extension mod is executed.

if both an applet and extensions loads the same way then
why is this difference..

If I use normal extension mod everything goes well.

Do anyone has any idea about this.

Quote:
The extension I used is xmlparser, I will write my own extension and check does it make any difference using debug info.

__________________
Thanks everyone for the info they share here..
Ramki@TTSL
VALUE HAS A VALUE ONLY IF ITS VALUE IS VALUED

——————————————————————————–
Last edited by Brewin : 08-06-2008 at 01:02 AM.

Brewin
View Public Profile
Send a private message to Brewin
Send email to Brewin
Find all posts by Brewin
Add Brewin to Your Buddy List

#6 08-06-2008, 06:15 AM
Brewin
Registered User Join Date: Mar 2006
Posts: 152
Rep Power: 4

Loading a compressed module(Application/Extension)

——————————————————————————–

I hope I got the resolution,
Quote:
Originally Posted by Brewin
Hi all,
The above procedure is not working in case if the compressed mod file is an extension. Applet compressed mod file is running fine. The decompressed file of the extension mod is Identical with the original extension mod file, but device is getting crashed when AEEMod_Load of the extension mod is executed.

if both an applet and extensions loads the same way then
why is this difference..

If I use normal extension mod everything goes well.

Do anyone has any idea about this.

If the module irrespective of Application or Extension, is complied with standard AEEModGen.C then the MOD compression will work else the behavior is unknown.

Hope this helps, Please correct me if I am wrong.
But since all module will have the same entry point why this happens…

__________________
Thanks everyone for the info they share here..
Ramki@TTSL
VALUE HAS A VALUE ONLY IF ITS VALUE IS VALUED

——————————————————————————–
Last edited by Brewin : 08-06-2008 at 06:17 AM.

Brewin
View Public Profile
Send a private message to Brewin
Send email to Brewin
Find all posts by Brewin
Add Brewin to Your Buddy List

#7 08-07-2008, 12:32 PM
TG1e
Registered User Join Date: May 2008
Posts: 3
Rep Power: 0

Reference code please [if possible]

——————————————————————————–

Addressed to all who are working (or have worked) on this topic of loading a mod from heap mem…
Will it be possible for you to post some reference code?

Also, for clarity…

Do we only have to call AEEMod_Load?
Or do we have to call the other functions as well….How does the AEE come to know that we have loaded the compressed (and now decompressed) mod? How will the AEE send events to this newly loaded mod?

Awaiting your response(s)…

——————————————————————————–
Last edited by TG1e : 08-07-2008 at 12:34 PM.

TG1e
View Public Profile
Send a private message to TG1e
Find all posts by TG1e
Add TG1e to Your Buddy List

#8 08-23-2008, 06:51 AM
Brewin
Registered User Join Date: Mar 2006
Posts: 152
Rep Power: 4

Loading compressed mod file..Step by Step

——————————————————————————–

Quote:
Originally Posted by TG1e
Addressed to all who are working (or have worked) on this topic of loading a mod from heap mem…
Will it be possible for you to post some reference code?

What code you need, first compress you MOD file with gzip(if 7zip is used then you need to supply you own decompressor code in the mod loader or bootloader)
Example: You have an application with myapp.mod, myapp.bar, myapp.mif
After compressing MOD, your files are myapp.zip, myapp.bar,myapp.mif.
Now write a bootloader which will load myapp.zip into memory and uncompress..

1. Create a project with name ex: .

2. copy AEEModGen.C and AEEModGen.H into your project and add the following header files
AEE.h, AEEShell.h, AEEUnzipStream.h, AEEFile.h, AEEHeap.h, AEEStdLib.h.

3. Delete everything not related to AEEMod_Load and AEEStaticMod_New in both files and add the below line

Quote:
For AEEMod_Load typecast…..
typedef int (*RunLoadMod)(IShell *ps, void * ph, IModule ** pMod);

2. Delete every thing inside the AEEStaticMod_New function.

3. Now inside the AEEStaticMod_New function declare pointers to IUnzipAStream, IFileMgr and IFile interfaces.

4. Create an Instance of IFileMgr and IUnzipStream(if compressed with gzip). else
provide your own decompress function here.

5.Now allocate some memory and copy the “ph” variable which is the pointer to AEEHelper Functions into the first four bytes of allocated memory.

6. Open myapp.zip and Set it for uncompressing using
” IUNZIPASTREAM_SetStream “.

7. Now read the uncompressed stream using ” IUNZIPASTREAM_Read ” and copy the contents from 5th byte onwards into the same buffer using MEMCPY, REALLOC as per requirements.
( Now your buffer contains pointer to AEEHelper functions + uncompressed myapp.zip, now buffer size will be a minimum of 4+sizeof(myapp.mod) ).

8. Once your buffer is ready with the above you are done. Release the IFileMgr, Ifile and IUnzipStream interface pointers.

9. Now type cast the 5th byte of the buffer to AEEMod_Load with a return statement like

Quote:
return (RunLoadMod)(buffer+4)(pIShell,ph,ppMod);
to execute the actual mod file….

Note: Don’t release the buffer, once the app exits this memory will be released by AEE.

You modloader is ready now.
Compile and create the mod file and rename the mod file as myapp.mod.

Now you app contains, myapp.mif, myapp.mod, myapp.zip, myapp.bar.

Quote:
Also, for clarity…
Do we only have to call AEEMod_Load?
Or do we have to call the other functions as well….How does the AEE come to know that we have loaded the compressed (and now decompressed) mod? How will the AEE send events to this newly loaded mod?

When you click on your app, AEE loads the mod file associated with the app name in this case it is myapp.mod(mod loader) and executes the first address i.e AEEMod_load which will feed the actual mod to the AEE, it’s not the AEE that load your actual MOD file.

There is no applet related to modloader, when AEE encounter the last statement in the AEEStaticMod_New of the modloader AEE just jumps to the address where we have kept the actual MOD file and exectues from there in the process whatever is created and registered(IModuleVtbl, Applet, Applet_HandleEvent) are all belongs to the actual application mod file and AEE will send events to the registered handleEvent of the top visible applet which is nothing but your application handle Event.

__________________
Thanks everyone for the info they share here..
Ramki@TTSL
VALUE HAS A VALUE ONLY IF ITS VALUE IS VALUED

——————————————————————————–
Last edited by Brewin : 08-23-2008 at 06:59 AM.

Brewin
View Public Profile
Send a private message to Brewin
Send email to Brewin
Find all posts by Brewin
Add Brewin to Your Buddy List

#9 01-13-2009, 07:55 AM
ed_est
Registered User Join Date: Jan 2009
Posts: 3
Rep Power: 0

Hi Brewin,
I am interested in the functionality that you have described in this topic and I have tried to create a modloader by myself according the steps you have provided, but unfortunately my device just reboots when I am trying to execute the loaded into memory .mod file. I have tried my code on different devices with different BREW version and on all of them I had device reboot.
Here is my code in AEEStaticMod_New function ( I have simplified it because I don’t use compression for .MOD file, also I have removed all checks):

ISHELL_CreateInstance(pIShell, AEECLSID_FILEMGR, (void**) &piFileMgr);
piFile = IFILEMGR_OpenFile(piFileMgr, “realmod.bin”, _OFM_READ);
IFILE_GetInfo(piFile, &iFileInfo);
filesize = iFileInfo.dwSize;
pBuf = (byte*) MALLOC (filesize + 4);
MEMCPY(pBuf, ph, 4);
pBuf2 = pBuf + 4;
pFunc = (RunLoadMod)pBuf2;
bytesread = IFILE_Read(piFile, pBuf2, filesize);
IFILE_Release(piFile);
IFILEMGR_Release(piFileMgr);
return pFunc(pIShell,ph,ppMod);

Can you help me to understand why I can’t start .MOD file I have loaded by modloader? I am doing all needed steps that you provided in your post and everything seems correct for me, but maybe you will find that I am doing something wrong.

Thanks in advance.

ed_est
View Public Profile
Send a private message to ed_est
Send email to ed_est
Find all posts by ed_est
Add ed_est to Your Buddy List

#10 01-13-2009, 08:44 PM
Brewin
Registered User Join Date: Mar 2006
Posts: 152
Rep Power: 4

Helper functions Entry point problem…..

——————————————————————————–

Quote:
Originally Posted by ed_est
MEMCPY(pBuf, ph, 4); *********
pBuf2 = pBuf + 4;
((RunLoadMod)pBuf2)(pIShell,ph,ppMod);

You problem is here. It not properly copying the ph pointer(Helper functions Entry point).
MEMCPY(pBuf, ph, 4);

__________________
Thanks everyone for the info they share here..
Ramki@TTSL
VALUE HAS A VALUE ONLY IF ITS VALUE IS VALUED

Brewin
View Public Profile
Send a private message to Brewin
Send email to Brewin
Find all posts by Brewin
Add Brewin to Your Buddy List

#11 01-14-2009, 06:17 AM
ed_est
Registered User Join Date: Jan 2009
Posts: 3
Rep Power: 0

Brewin,
Thank you very match for your help.
I have fixed the this part of code and now it works.

0

在BREW程序中调用另一个mod的分析


近日读到这样一段程序。可以在程序运行时调用另一个mod文件并执行。这样可以做到应用自升级,压缩程序文件等实用的功能。

 

 要想理解这点,先看一下正常的BREW程序加载过程。

 BREW程序的入口,即相当于c程序的main,是位于AEEModGen.c中的AEEMod_Load函数。这可以从mak文件的连接选项中看出来。

 

LINK_ORDER = -first AEEMod_Load

 

 这个选项使得链接程序将该函数放在程序文件的0地址处。函数的定义如下:

 

int AEEMod_Load(IShell *pIShell, void *ph, IModule **ppMod)

{

   // Invoke helper function to do the actual loading.

   return AEEStaticMod_New(sizeof(AEEMod),pIShell,ph,ppMod,NULL,NULL);

}

  

  BREW底层调用这个函数,需要提供三个参数。pIShell是ISHELL接口的指针,有了它就可以创建和访问其它接口。ph是helper function类函数的函数列表指针,有了它就可以调用MALLOC等函数。ppMod是用于返回给BREW底层的地址,存储生成的module的信息。

  AEEMod_Load函数调用了AEEStaticMod_New函数。在这个函数中初始化了module数据结构,ppMod就是指向它的。函数结束后返回BREW底层。

  BREW底层稍后通过存储在module数据结构中的函数指针,调用同样位于AEEModGen.c中的AEEMod_CreateInstance函数,生成该Module的某一个Applet实例。在这个函数中会调用到我们所编写的AEEClsCreateInstance函数。在我们这个函数中注册了HandleEvent函数和FreeAppDate函数。这些信息同样通过一个applet数据结构指针返回给BREW底层。这之后,BREW会通过给HandleEvent发各种Event,来驱动程序运行。

 

  通过这整个过程,可以总结出BREW程序运行的必要条件。

  程序必须得到IShell指针,Helper Function指针,这样程序中对BREW底层各种函数的调用才能进行。BREW底层必须知道两个地址,通过它们,可以将Event传递给程序,调用FreeAppDate函数。

  因此,想在程序中调用另外一个mod,只需要想办法把程序里的IShell指针,Helper Function指针传递给mod,同时得到mod中的HandleEvent函数,FreeAppDate函数的地址就可以了。

  剩下的问题就是,程序怎样和mod文件交互呢,mod文件的格式是怎样的?

 

  生成mod的mak文件最后一般有这样两句话,

 

ld *.o a.elf

fromelf –bin a.elf a.

 

  arm链接器ld生成ELF格式,之后用格式转换工具fromelf将ELF文件转换成mod文件。

  ELF文件是带格式的可执行文件,对它的执行要靠操作系统的解析来进行。而通过–bin选项生成的mod文件,格式却是plain binary,即赤裸裸的二进制机器指令,其实是无格式的。只要将mod文件载入内存,跳转到它的0地址处,就可以一条条指令的执行下来。和mod文件交互,也就是要安排好mod文件0地址处的内容,知道mod文件的调用者会传入什么东西,mod文件会返回什么东西。

 

  

  下面来看一种实际的调用方法吧。代码如下:

 

typedef int (*RunLoadMod)(IShell *pIShell, void *ph, IModule **ppMod);

 

pData = MyLoadZip(pIShell);

if( !pData )

return EFAILED;

DBGPRINTF(“to RunLoadMod”);

if( SUCCESS != ((RunLoadMod)pData)(pIShell,ph,&pOrgMod) )

{

DBGPRINTF(“RunLoadMod_Err”);

goto Crt_Err;

}

  

  这段程序将mod文件读入内存,放在pData缓冲区内,用((RunLoadMod)pData)(pIShell,ph,&pOrgMod)一句执行之。RunLoadMod只不过是用typedef定义的一种函数指针类型。这句话的意思相当于是为待调用的mod文件准备好pIShell,ph,pOrgMod三个数据后,直接跳到内存中的mod文件的第一个字节处,将它当做普通机器指令一样执行。这种方法调用的mod文件,应是一个用完整的BREW框架编译后生成的。0地址处放下的是AEEMod_Load函数。Mod文件直接将Applet信息传递给BREW底层,注册HandleEvent函数。实际上是Mod文件取代了调用者的位置。

  

  但是这段程序很可能会运行失败。因为BREW底层除了传参外,还做了其它事情。这在AEEStdlib.h中可以看出。

 

#define GET_HELPER()      (*(((AEEHelperFuncs **)AEEMod_Load) – 1))

#define GET_HELPER_VER()  (*((uint32 *)(((byte *)AEEMod_Load) – sizeof(AEEHelperFuncs *) – sizeof(uint32))))

 

  这两个宏定义可以看成是两个全局常量。第一个是BREW Helper Function的函数指针表,第二个是函数指针表的版本号。上面提到的通过参数传递Helper Function函数指针,是只在模拟器上成立的。手机上其实是通过全局常量形式传递的。为保证mod可以正常运行,也应在缓冲区前保存这两个常量。

 

  这种“冒名顶替”的方式,对于只想压缩一下mod的应用来说是完全可以的。但有时,我们希望新调用的mod只是整个应用的一小部分,调用者继续存在。这就需要调用者充当BREW底层的一部分角色,记住mod返回的各种信息,在适当的时候调用mod中的相应函数。新调用的mod如果想要取得主mod的信息,类似于上面提到的HelperFuncs全局常量指针的方法是个可行的选择。

 

  其实知道了BREW程序的加载过程,就可以比较灵活的达到调用mod的目的,甚至BREW程序架构都是可以改动的。但是改动之后,如何在模拟器上进行调试就又成了一个问题。

 

  程序中调用mod的技巧,在国外几年前就已得到了应用。甚至有家公司专门开发了利用这个技巧压缩mod的工具。

 

http://www.s-cradle.com/english/products/sophiacompress_/index.html

 

参考文章:

 

1.How to build A mod to load B mod?

 

http://brewforums.qualcomm.com/showthread.php?t=11637

 

2.How to run mod file that is loaded in the

 

http://brewforums.qualcomm.com/showthread.php?t=18413&highlight=

 

3.深入BREW模块加载机制

 

http://nicefuture.ycool.com/post.722858.html

 

4.深度剖析BREW实现原理收藏

 

http://blog.csdn.net/Gemsea/archive/2006/09/07/1190206.aspx

Random Posts Recent Comments

  • 女友糖尿病害我蛀牙 Says:

    汗一个…...

  • Htj06 Says:

    zhenyouchuangyi...

  • 电商圈 Says:

    试图该怎么建立啊,,怎在程序中是吸纳...

  • edward Says:

    看得人心旷神怡,好文,情不自禁的顶一下...

  • Daniel Says:

    我也在处理这个问题,没有找到好的方法。我用了楼上兄弟的方法,还是可以的。不知道您找到好的方法了吗、我暂时楼上兄弟的方法。...

  • 卡,卡 Says:

    弱弱问一句:博主,你博客的模板这样设计pv高吗?...

  • 站长工具 Says:

    博主,兔年快乐!...

  • health Says:

    great post!!I hope I can read more in your website....

  • pdu Says:

    好博文,支持分享...

  • 站长工具 Says:

    博主的文章很不错,我是站长工具-站长精灵的作者,一款专业的SEO工具软件(可以帮您提高博客的流量),想跟您交换个链接,不知可否...

Tag Cloud

arm audio blog brew cache class debug flash google html j2me java javascript Joke linux lua mobile mtk php python ror ruby server shell stream unix web windows 优化 动态加载 女人 女生 平台 开发 手机 技术 流媒体 测试 漫画 生活 男人 男生 缓存 芯片