参考:doc/README.standalone
The functions are exported by U-Boot via a jump table.
// u-boot/include/asm-generic/global_data.h
typedef struct global_data {
bd_t *bd;
...
struct jt_funcs *jt; /* jump table */
// u-boot/include/exports.h
struct jt_funcs {
#define EXPORT_FUNC(impl, res, func, ...) res(*func)(__VA_ARGS__); // 先宏定义
#include <_exports.h>
#undef EXPORT_FUNC // 去宏定义
};
// u-boot/include/_exports.h
#ifndef EXPORT_FUNC // 宏定义检查,规避直接引用导致异常
#define EXPORT_FUNC(a, b, c, ...)
#endif
...
EXPORT_FUNC(malloc, void *, malloc, size_t)
...
EXPORT_FUNC(free, void, free, void *)
此时,standlone程序就可以使用malloc与free。
此外可以使用修改gd->jt中默认函数,如:
DECLARE_GLOBAL_DATA_PTR; // 定义变量gd;
gd->jt->malloc = my_malloc; // 修改为自定义的my_malloc
gd->jt->free = my_free;
此时,standalone使用就是替换后的malloc与free。
The pointer to the jump table is passed to the application in a machine-dependent way.
standalone程序可以直接访问全局变量gd,如:
DECLARE_GLOBAL_DATA_PTR;
printf("U-Boot relocation offset: %x\n", gd->reloc_off);
The application should call the app_startup() function before any call to the exported functions.
standalone程序入口先使用app_startup()函数启动应用,再进行后续操作,如:
int my_app (int argc, char * const argv[])
{
app_startup (argv);
if (get_version () != XF_VERSION)
return 1;
}
The default load and start addresses of the applications are as follows:
standalone程序默认运行地址如下:
Load address Start address
x86 0x00040000 0x00040000
PowerPC 0x00040000 0x00040004
ARM 0x0c100000 0x0c100000
MIPS 0x80200000 0x80200000
Blackfin 0x00001000 0x00001000
NDS32 0x00300000 0x00300000
Nios II 0x02000000 0x02000000
进而,加载运行时可以使用命令:
=> tftp 0x40000 hello_world.bin
=> go 0x40004
To export some additional function long foobar(int i,char c), the following steps should be undertaken:
导出自定义的函数实现给standalone程序需要遵循如下步骤(要求):
在include/_exports.h中添加函数属性,如:
#ifdef CONFIG_FOOBAR
EXPORT_FUNC(foobar, long, foobar, int, char)
#else
EXPORT_FUNC(dummy, void, foobar, void)
#endif
在gd->tj新装成员foobar后,可以使用赋值的方式直接修改指向的函数实现,如:
gd->jt->foobar = another_foobar;
standalone版本号递增更新
#define XF_VERSION 9
The code for exporting the U-Boot functions to applications is mostly machine-independent.
导出给standalone程序使用函数多是设备无关的代码实现,所以standalone在移植到一个新设备时,唯一需要关注的是examples/stubs.c中的代码。