Writing GRUB Modules: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
m (→‎External Links: web link fixed (broken due to erroring pdf module on wordpress))
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(One intermediate revision by one other user not shown)
Line 15:
=== Special Sections ===
Each module is required to contain three special pieces of information: the module name, the dependencies of the module (i.e. which other modules need to be loaded prior to this one) and the licence of the module. For GRUB 2, the licence is required to be GPL otherwise it will not be loaded. These need to be placed in special sections within the module. The following piece of code (GCC specific) at module scope will achieve the required functionality:
<sourcesyntaxhighlight lang="C">
char modname[] __attribute__((section(".modname"))) = "mymodule";
 
Line 22:
 
GRUB_MOD_LICENSE("GPLv3+");
</syntaxhighlight>
</source>
 
=== Initialization ===
You need to provide initialization and finalization functions for your module. These are called when the module is loaded/unloaded, and should register the commands that the module provides along with any other resources it requires. A short example is:
<sourcesyntaxhighlight lang="C">
static grub_extcmd_t cmd;
 
Line 43:
grub_unregister_extcmd(cmd);
}
</syntaxhighlight>
</source>
 
See the GRUB source/example modules for more information about the other options here.
Line 50:
Some useful functions exposed by grub are given below. Note that there are implementations of typical libc/POSIX functions often prefixed by grub_ in the grub include directory. Some may require additional modules to be loaded first - GRUB will highlight this when you try and insmod a module that contains functionality that is not currently loaded.
 
<sourcesyntaxhighlight lang="C">
int EXPORT_FUNC() grub_printf(const char* func, ...); //Very similar to standard printf()
void *grub_malloc(grub_size_t size); // grub's malloc function - memory allocated by this will
Line 62:
// Attempt to set a video mode.
// e.g. grub_video_set_mode("1024x768x32,auto", 0, 0);
</syntaxhighlight>
</source>
 
=== Building ===
Line 81:
 
[[Category:GRUB]]
[[Category:Bootloaders]]