在嵌入式系統中,我們通常會要求VxWorks文件儘量小,比如通過串口、軟盤或tffs加載VxWorks的時候,如果文件太大,可能無法存儲,或加載失敗。下面介紹一種利用Tornado和VxWorks自帶的deflate和inflate,對VxWorks文件進行壓縮和解壓縮的技術。
1. 使用Tornado創建bootable的project,包括應用程序。對VxWorks進行適當的裁減和配置。
2. 如果準備將VxWorks存儲於硬盤,軟盤或tffs上,應該在usrAppInit中使用usrNetEndDevStart和usrNetifConfig啓動網絡接口。如果存儲於tffs上,還要修改usrNetBoot.c中:
if ( (strncmp (sysBootParams.bootDev, "scsi", 4) == 0)
|| (strncmp (sysBootParams.bootDev, "ide", 3) == 0)
|| (strncmp (sysBootParams.bootDev, "ata", 3) == 0)
|| (strncmp (sysBootParams.bootDev, "fd", 2) == 0))
爲:
if ( (strncmp (sysBootParams.bootDev, "scsi", 4) == 0)
|| (strncmp (sysBootParams.bootDev, "ide", 3) == 0)
|| (strncmp (sysBootParams.bootDev, "ata", 3) == 0)
|| (strncmp (sysBootParams.bootDev, "tffs", 4) == 0)
|| (strncmp (sysBootParams.bootDev, "fd", 2) == 0))
3. 在dos下運行 tornado/host/x86-win32/bin/torvars。
4. 進入VxWorks所在的目錄,運行:
deflate VxWorks.z。這裏我們默認.z文件是壓縮文件。
5. 如果準備將VxWorks.z存儲於硬盤,軟盤或tffs上,需要首先創建相應的設備,並用dosFS初始化。如果是通過串口或網絡加載VxWorks.z,則需要初始化相應的接口。
6. 修改bootConfig.c文件:
a. 在LOCAL STATUS netLoad 函數的 tftpXfer和 ftpXfer這一部分代碼結束的地方添加:
if (strstr(fileName, ".z") || strstr(fileName, ".Z"))
{
printf("file % s is compressed, now begin uncompressing... ", fileName);
if (bootLoadModuleInflate(fd, pEntry) != OK)
goto readErr;
}
else if (bootLoadModule(fd, pEntry) != OK)
goto readErr;
b. 在 LOCAL STATUS tffsLoad 函數的 usrTffsConfig和open這一部分代碼結束的地方添加:
if (strstr(fileName, ".z") || strstr(fileName, ".Z"))
{
printf("file % s is compressed, now begin uncompressing... ", fileName);
if (bootLoadModuleInflate(fd, pEntry) != OK)
goto readErr;
}
else if (bootLoadModule(fd, pEntry) != OK)
goto readErr;
c. 在 LOCAL STATUS bootLoad 函數之前定義函數 bootLoadModuleInflate的原型:
#define DECOMP_BUF_SIZE (RAM_HIGH_ADRS -RAM_LOW_ADRS)
#define COMP_BUF_SIZE (DECOMP_BUF_SIZE / 3)
STATUS bootLoadModuleInflate(int zfd, FUNCPTR *pEntry)
{
char *imageBuf = NULL;
char *compBuf = NULL;
int fd = - 1;
int rv = ERROR;
int compSize, r;
extern STATUS inflate(char *src, char *dst, int src_size);
if ((compBuf = malloc(COMP_BUF_SIZE)) == NULL)
{
printErr("No enough memory for image buffer");
goto done;
}
compSize = 0;
while ((r = read(zfd, compBuf + compSize, COMP_BUF_SIZE - compSize)) > 0)
compSize += r;
if (r < 0)
{
printErr("Read failed: errno = %d", errnoGet());
goto done;
}
if (compSize == COMP_BUF_SIZE)
{
printErr("Compressed image too large");
goto done;
}
printErr("Uncompressing %d bytes... ", compSize);
if ((imageBuf = malloc(DECOMP_BUF_SIZE)) == NULL)
{
printErr("Not enough memory for decompression buffer");
goto done;
}
if ((r = inflate(compBuf, imageBuf, compSize)) < 0)
{
printErr("Uncompress failed ");
goto done;
}
printErr("Loading image... ");
memDrv();
memDevCreate("mem:", imageBuf, DECOMP_BUF_SIZE);
if ((fd = open("mem:0", O_RDONLY, 0)) < 0)
{
printErr("Cannot open memory device. ");
goto done;
}
if (bootLoadModule(fd, pEntry) != OK)
{
printErr("Error loading: errno = % d ", errnoGet());
goto done;
}
printErr("");
rv = OK;
done: if (fd >= 0)
close(fd);
if (imageBuf)
free(imageBuf);
if (compBuf)
free(compBuf);
return rv;
}
d. 如果加載不成功,應讀懂上一段代碼,調整 RAM_HIGH_ADRS 和 RAM_LOW_ADRS的大小。
7. 修改 config.h中的啓動參數,比如啓動設備爲tffs=0,0(0,0),文件名爲/tffs0/VxWorks.z等等,重新制作bootrom,並寫入flash。
8. 啓動時,修改啓動參數,使系統仍然從網絡加載VxWorks,這個VxWorks中應該實現了ftp或tftp功能。
通過這些功能,把VxWorks.z文件寫入存儲介質如tffs中。
9. 重新啓動從tffs或硬盤,軟盤加載VxWorks,即可成功。
10. 可以首先通過網絡啓動,把啓動文件名改爲 VxWorks.z來進行驗證壓縮和解壓縮。
11. 以上只是考慮了從網絡和tffs來加載VxWorks.z壓縮文件,如果從fd, ata等加載,只需在相應地方添加和6.a中相同的代碼即可。
12. 本方法在ppc850上,利用tffs和網絡加載進行了驗證,完全適用。