xref: /openbmc/u-boot/doc/README.standalone (revision 7784674852c66b0924bdc79062bd208aa51fd0a9)
127b207fdSwdenkDesign Notes on Exporting U-Boot Functions to Standalone Applications:
227b207fdSwdenk======================================================================
327b207fdSwdenk
4*77846748Swdenk1. The functions are exported by U-Boot via a jump table. The jump
5*77846748Swdenk   table is allocated and initialized in the jumptable_init() routine
6*77846748Swdenk   (common/exports.c). Other routines may also modify the jump table,
7*77846748Swdenk   however. The jump table can be accessed as the 'jt' field of the
8*77846748Swdenk   'global_data' structure. The slot numbers for the jump table are
9*77846748Swdenk   defined in the <include/exports.h> header. E.g., to substitute the
10*77846748Swdenk   malloc() and free() functions that will be available to standalone
11*77846748Swdenk   applications, one should do the following:
1227b207fdSwdenk
13*77846748Swdenk	DECLARE_GLOBAL_DATA_PTR;
1427b207fdSwdenk
15*77846748Swdenk	gd->jt[XF_malloc]	= my_malloc;
16*77846748Swdenk	gd->jt[XF_free]		= my_free;
1727b207fdSwdenk
18*77846748Swdenk   Note that the pointers to the functions all have 'void *' type and
19*77846748Swdenk   thus the compiler cannot perform type checks on these assignments.
2027b207fdSwdenk
21*77846748Swdenk2. The pointer to the jump table is passed to the application in a
22*77846748Swdenk   machine-dependent way. PowerPC, ARM and MIPS architectures use a
23*77846748Swdenk   dedicated register to hold the pointer to the 'global_data'
24*77846748Swdenk   structure: r29 on PowerPC, r8 on ARM and k0 on MIPS. The x86
25*77846748Swdenk   architecture does not use such a register; instead, the pointer to
26*77846748Swdenk   the 'global_data' structure is passed as 'argv[-1]' pointer.
2727b207fdSwdenk
28*77846748Swdenk   The application can access the 'global_data' structure in the same
29*77846748Swdenk   way as U-Boot does:
3027b207fdSwdenk
31*77846748Swdenk	DECLARE_GLOBAL_DATA_PTR;
3227b207fdSwdenk
33*77846748Swdenk	printf("U-Boot relocation offset: %x\n", gd->reloc_off);
3427b207fdSwdenk
35*77846748Swdenk3. The application should call the app_startup() function before any
36*77846748Swdenk   call to the exported functions. Also, implementor of the
37*77846748Swdenk   application may want to check the version of the ABI provided by
38*77846748Swdenk   U-Boot. To facilitate this, a get_version() function is exported
39*77846748Swdenk   that returns the ABI version of the running U-Boot. I.e., a
40*77846748Swdenk   typical application startup may look like this:
4127b207fdSwdenk
42*77846748Swdenk	int my_app (int argc, char *argv[])
43*77846748Swdenk	{
44*77846748Swdenk		app_startup (argv);
45*77846748Swdenk		if (get_version () != XF_VERSION)
46*77846748Swdenk			return 1;
47*77846748Swdenk	}
4827b207fdSwdenk
49*77846748Swdenk4. The default load and start addresses of the applications are as
50*77846748Swdenk   follows:
5127b207fdSwdenk
52*77846748Swdenk		Load address	Start address
53*77846748Swdenk	x86	0x00040000	0x00040000
54*77846748Swdenk	PowerPC	0x00040000	0x00040004
55*77846748Swdenk	ARM	0x0c100000	0x0c100000
56*77846748Swdenk	MIPS	0x80200000	0x80200000
5727b207fdSwdenk
58*77846748Swdenk   For example, the "hello world" application may be loaded and
59*77846748Swdenk   executed on a PowerPC board with the following commands:
6027b207fdSwdenk
61*77846748Swdenk   => tftp 0x40000 hello_world.bin
6227b207fdSwdenk   => go 0x40004
6327b207fdSwdenk
64*77846748Swdenk5. To export some additional function foobar(), the following steps
65*77846748Swdenk   should be undertaken:
6627b207fdSwdenk
67*77846748Swdenk   - Append the following line at the end of the include/_exports.h
68*77846748Swdenk     file:
6927b207fdSwdenk
70*77846748Swdenk	EXPORT_FUNC(foobar)
7127b207fdSwdenk
72*77846748Swdenk   - Add the prototype for this function to the include/exports.h
73*77846748Swdenk     file:
7427b207fdSwdenk
75*77846748Swdenk	void foobar(void);
7627b207fdSwdenk
77*77846748Swdenk   - Add the initialization of the jump table slot wherever
78*77846748Swdenk     appropriate (most likely, to the jumptable_init() function):
7927b207fdSwdenk
80*77846748Swdenk	gd->jt[XF_foobar] = foobar;
8127b207fdSwdenk
82*77846748Swdenk   - Increase the XF_VERSION value by one in the include/exports.h
83*77846748Swdenk     file
8427b207fdSwdenk
85*77846748Swdenk6. The code for exporting the U-Boot functions to applications is
86*77846748Swdenk   mostly machine-independent. The only places written in assembly
87*77846748Swdenk   language are stub functions that perform the jump through the jump
88*77846748Swdenk   table. That said, to port this code to a new architecture, the
89*77846748Swdenk   only thing to be provided is the code in the examples/stubs.c
90*77846748Swdenk   file. If this architecture, however, uses some uncommon method of
91*77846748Swdenk   passing the 'global_data' pointer (like x86 does), one should add
92*77846748Swdenk   the respective code to the app_startup() function in that file.
9327b207fdSwdenk
94*77846748Swdenk   Note that these functions may only use call-clobbered registers;
95*77846748Swdenk   those registers that are used to pass the function's arguments,
96*77846748Swdenk   the stack contents and the return address should be left intact.
97