From 8bc36e9effaea0d7394ff0892c09b40d34dbddfa Mon Sep 17 00:00:00 2001
From: Vladimir Serbinenko <phcoder@gmail.com>
Date: Fri, 25 Oct 2013 16:07:01 +0200
Subject: [PATCH 06/14] * conf/Makefile.common (CPPFLAGS_KERNEL): Add
 -DGRUB_KERNEL=1. * include/grub/dl.h (GRUB_MOD_INIT), (GRUB_MOD_FINI): Define
 functions when compiling for kernel.

---
 ChangeLog                          |  6 ++++++
 conf/Makefile.common               |  2 +-
 grub-core/commands/boot.c          |  8 --------
 grub-core/commands/keylayouts.c    |  8 --------
 grub-core/term/at_keyboard.c       |  8 --------
 grub-core/term/gfxterm.c           |  8 --------
 grub-core/term/i386/pc/vga_text.c  |  8 --------
 grub-core/term/serial.c            |  8 --------
 grub-core/term/terminfo.c          |  8 --------
 grub-core/video/radeon_fuloong2e.c |  8 --------
 grub-core/video/sis315pro.c        |  8 --------
 grub-core/video/sm712.c            |  8 --------
 include/grub/dl.h                  | 28 ++++++++++++++++++++--------
 13 files changed, 27 insertions(+), 89 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index cad20c6..d098ce3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2013-10-25  Vladimir Serbinenko  <phcoder@gmail.com>
+
+	* conf/Makefile.common (CPPFLAGS_KERNEL): Add -DGRUB_KERNEL=1.
+	* include/grub/dl.h (GRUB_MOD_INIT), (GRUB_MOD_FINI): Define
+	functions when compiling for kernel.
+
 2013-03-22  Vladimir Serbinenko  <phcoder@gmail.com>
 
 	Add ability to generate newc additions on runtime.
diff --git a/conf/Makefile.common b/conf/Makefile.common
index b5615a1..71d03fe 100644
--- a/conf/Makefile.common
+++ b/conf/Makefile.common
@@ -69,7 +69,7 @@ endif
 
 CFLAGS_KERNEL = $(CFLAGS_CPU) $(CFLAGS_PLATFORM) -ffreestanding
 LDFLAGS_KERNEL = $(LDFLAGS_CPU) $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) -static-libgcc
-CPPFLAGS_KERNEL = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
+CPPFLAGS_KERNEL = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM) -DGRUB_KERNEL=1
 CCASFLAGS_KERNEL = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)
 if COND_CYGWIN
 STRIPFLAGS_KERNEL = -F elf32-i386 -R .rel.dyn -R .reginfo -R .note -R .comment -R .drectve -R .note.gnu.gold-version
diff --git a/grub-core/commands/boot.c b/grub-core/commands/boot.c
index 91ec87d..e0148c7 100644
--- a/grub-core/commands/boot.c
+++ b/grub-core/commands/boot.c
@@ -183,22 +183,14 @@ grub_cmd_boot (struct grub_command *cmd __attribute__ ((unused)),
 
 static grub_command_t cmd_boot;
 
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
-void grub_boot_init (void)
-#else
 GRUB_MOD_INIT(boot)
-#endif
 {
   cmd_boot =
     grub_register_command ("boot", grub_cmd_boot,
 			   0, N_("Boot an operating system."));
 }
 
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
-void grub_boot_fini (void)
-#else
 GRUB_MOD_FINI(boot)
-#endif
 {
   grub_unregister_command (cmd_boot);
 }
diff --git a/grub-core/commands/keylayouts.c b/grub-core/commands/keylayouts.c
index 6b5141c..10eeae3 100644
--- a/grub-core/commands/keylayouts.c
+++ b/grub-core/commands/keylayouts.c
@@ -290,21 +290,13 @@ grub_cmd_keymap (struct grub_command *cmd __attribute__ ((unused)),
 
 static grub_command_t cmd;
 
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
-void grub_keylayouts_init (void)
-#else
 GRUB_MOD_INIT(keylayouts)
-#endif
 {
   cmd = grub_register_command ("keymap", grub_cmd_keymap,
 			       0, N_("Load a keyboard layout."));
 }
 
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
-void grub_keylayouts_fini (void)
-#else
 GRUB_MOD_FINI(keylayouts)
-#endif
 {
   grub_unregister_command (cmd);
 }
diff --git a/grub-core/term/at_keyboard.c b/grub-core/term/at_keyboard.c
index 2afcceb..bcd20d3 100644
--- a/grub-core/term/at_keyboard.c
+++ b/grub-core/term/at_keyboard.c
@@ -625,22 +625,14 @@ static struct grub_term_input grub_at_keyboard_term =
     .getkey = grub_at_keyboard_getkey
   };
 
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
-void grub_at_keyboard_init (void)
-#else
 GRUB_MOD_INIT(at_keyboard)
-#endif
 {
   grub_term_register_input ("at_keyboard", &grub_at_keyboard_term);
   grub_loader_register_preboot_hook (grub_at_fini_hw, grub_at_restore_hw,
 				     GRUB_LOADER_PREBOOT_HOOK_PRIO_CONSOLE);
 }
 
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
-void grub_at_keyboard_fini (void)
-#else
 GRUB_MOD_FINI(at_keyboard)
-#endif
 {
   grub_keyboard_controller_fini (NULL);
   grub_term_unregister_input (&grub_at_keyboard_term);
diff --git a/grub-core/term/gfxterm.c b/grub-core/term/gfxterm.c
index c995b84..39a0034 100644
--- a/grub-core/term/gfxterm.c
+++ b/grub-core/term/gfxterm.c
@@ -1252,11 +1252,7 @@ static struct grub_term_output grub_video_term =
 static grub_extcmd_t background_image_cmd_handle;
 static grub_command_t background_color_cmd_handle;
 
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
-void grub_gfxterm_init (void)
-#else
 GRUB_MOD_INIT(gfxterm)
-#endif
 {
   grub_term_register_output ("gfxterm", &grub_video_term);
   background_image_cmd_handle =
@@ -1272,11 +1268,7 @@ GRUB_MOD_INIT(gfxterm)
                            N_("Set background color for active terminal."));
 }
 
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
-void grub_gfxterm_fini (void)
-#else
 GRUB_MOD_FINI(gfxterm)
-#endif
 {
   grub_unregister_command (background_color_cmd_handle);
   grub_unregister_extcmd (background_image_cmd_handle);
diff --git a/grub-core/term/i386/pc/vga_text.c b/grub-core/term/i386/pc/vga_text.c
index c934c68..638092d 100644
--- a/grub-core/term/i386/pc/vga_text.c
+++ b/grub-core/term/i386/pc/vga_text.c
@@ -172,20 +172,12 @@ static struct grub_term_output grub_vga_text_term =
     .highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR,
   };
 
-#if defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_MULTIBOOT)
-void grub_vga_text_init (void)
-#else
 GRUB_MOD_INIT(vga_text)
-#endif
 {
   grub_term_register_output ("vga_text", &grub_vga_text_term);
 }
 
-#if defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_MULTIBOOT)
-void grub_vga_text_fini (void)
-#else
 GRUB_MOD_FINI(vga_text)
-#endif
 {
   grub_term_unregister_output (&grub_vga_text_term);
 }
diff --git a/grub-core/term/serial.c b/grub-core/term/serial.c
index e1469e5..612566f 100644
--- a/grub-core/term/serial.c
+++ b/grub-core/term/serial.c
@@ -365,11 +365,7 @@ grub_serial_unregister_driver (struct grub_serial_driver *driver)
 
 static grub_extcmd_t cmd;
 
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
-void grub_serial_init (void)
-#else
 GRUB_MOD_INIT(serial)
-#endif
 {
   cmd = grub_register_extcmd ("serial", grub_cmd_serial, 0,
 			      N_("[OPTIONS...]"),
@@ -393,11 +389,7 @@ GRUB_MOD_INIT(serial)
 #endif
 }
 
-#if defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS)
-void grub_serial_fini (void)
-#else
 GRUB_MOD_FINI(serial)
-#endif
 {
   while (grub_serial_ports)
     grub_serial_unregister (grub_serial_ports);
diff --git a/grub-core/term/terminfo.c b/grub-core/term/terminfo.c
index e35563f..03eadc5 100644
--- a/grub-core/term/terminfo.c
+++ b/grub-core/term/terminfo.c
@@ -737,11 +737,7 @@ grub_cmd_terminfo (grub_extcmd_context_t ctxt, int argc, char **args)
 
 static grub_extcmd_t cmd;
 
-#if defined (GRUB_MACHINE_IEEE1275) || defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_ARC)
-void grub_terminfo_init (void)
-#else
 GRUB_MOD_INIT(terminfo)
-#endif
 {
   cmd = grub_register_extcmd ("terminfo", grub_cmd_terminfo, 0,
 			      N_("[[-a|-u|-v] [-g WxH] TERM [TYPE]]"),
@@ -749,11 +745,7 @@ GRUB_MOD_INIT(terminfo)
 			      options);
 }
 
-#if defined (GRUB_MACHINE_IEEE1275) || defined (GRUB_MACHINE_MIPS_LOONGSON) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_ARC)
-void grub_terminfo_fini (void)
-#else
 GRUB_MOD_FINI(terminfo)
-#endif
 {
   grub_unregister_extcmd (cmd);
 }
diff --git a/grub-core/video/radeon_fuloong2e.c b/grub-core/video/radeon_fuloong2e.c
index 32f66c7..0d78267 100644
--- a/grub-core/video/radeon_fuloong2e.c
+++ b/grub-core/video/radeon_fuloong2e.c
@@ -220,20 +220,12 @@ static struct grub_video_adapter grub_video_radeon_fuloong2e_adapter =
     .next = 0
   };
 
-#ifdef GRUB_MACHINE_MIPS_LOONGSON
-void grub_video_radeon_fuloong2e_init (void)
-#else
 GRUB_MOD_INIT(video_radeon_fuloong2e)
-#endif
 {
   grub_video_register (&grub_video_radeon_fuloong2e_adapter);
 }
 
-#ifdef GRUB_MACHINE_MIPS_LOONGSON
-void grub_video_radeon_fuloong2e_fini (void)
-#else
 GRUB_MOD_FINI(video_radeon_fuloong2e)
-#endif
 {
   grub_video_unregister (&grub_video_radeon_fuloong2e_adapter);
 }
diff --git a/grub-core/video/sis315pro.c b/grub-core/video/sis315pro.c
index 5d06daa..0b46c95 100644
--- a/grub-core/video/sis315pro.c
+++ b/grub-core/video/sis315pro.c
@@ -433,20 +433,12 @@ static struct grub_video_adapter grub_video_sis315pro_adapter =
     .next = 0
   };
 
-#ifdef GRUB_MACHINE_MIPS_LOONGSON
-void grub_video_sis315pro_init (void)
-#else
 GRUB_MOD_INIT(video_sis315pro)
-#endif
 {
   grub_video_register (&grub_video_sis315pro_adapter);
 }
 
-#ifdef GRUB_MACHINE_MIPS_LOONGSON
-void grub_video_sis315pro_fini (void)
-#else
 GRUB_MOD_FINI(video_sis315pro)
-#endif
 {
   grub_video_unregister (&grub_video_sis315pro_adapter);
 }
diff --git a/grub-core/video/sm712.c b/grub-core/video/sm712.c
index 5f22c40..82160d3 100644
--- a/grub-core/video/sm712.c
+++ b/grub-core/video/sm712.c
@@ -792,20 +792,12 @@ static struct grub_video_adapter grub_video_sm712_adapter =
     .next = 0
   };
 
-#ifdef GRUB_MACHINE_MIPS_LOONGSON
-void grub_video_sm712_init (void)
-#else
 GRUB_MOD_INIT(video_sm712)
-#endif
 {
   grub_video_register (&grub_video_sm712_adapter);
 }
 
-#ifdef GRUB_MACHINE_MIPS_LOONGSON
-void grub_video_sm712_fini (void)
-#else
 GRUB_MOD_FINI(video_sm712)
-#endif
 {
   grub_video_unregister (&grub_video_sm712_adapter);
 }
diff --git a/include/grub/dl.h b/include/grub/dl.h
index 3119978..f6e3e12 100644
--- a/include/grub/dl.h
+++ b/include/grub/dl.h
@@ -34,25 +34,38 @@
  */
 #ifndef ASM_FILE
 
-#if !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU)
-
 #ifndef GRUB_MOD_INIT
+
+#if !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_KERNEL)
+
 #define GRUB_MOD_INIT(name)	\
 static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
 static void \
 grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
-#endif
 
-#ifndef GRUB_MOD_FINI
 #define GRUB_MOD_FINI(name)	\
 static void grub_mod_fini (void) __attribute__ ((used)); \
 static void \
 grub_mod_fini (void)
-#endif
+
+#elif defined (GRUB_KERNEL)
+
+#define GRUB_MOD_INIT(name)	\
+static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
+void \
+grub_##name##_init (void) { grub_mod_init (0); } \
+static void \
+grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
+
+#define GRUB_MOD_FINI(name)	\
+static void grub_mod_fini (void) __attribute__ ((used)); \
+void \
+grub_##name##_fini (void) { grub_mod_fini (); } \
+static void \
+grub_mod_fini (void)
 
 #else
 
-#ifndef GRUB_MOD_INIT
 #define GRUB_MOD_INIT(name)	\
 static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
 void grub_##name##_init (void); \
@@ -60,9 +73,7 @@ void \
 grub_##name##_init (void) { grub_mod_init (0); } \
 static void \
 grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
-#endif
 
-#ifndef GRUB_MOD_FINI
 #define GRUB_MOD_FINI(name)	\
 static void grub_mod_fini (void) __attribute__ ((used)); \
 void grub_##name##_fini (void); \
@@ -70,6 +81,7 @@ void \
 grub_##name##_fini (void) { grub_mod_fini (); } \
 static void \
 grub_mod_fini (void)
+
 #endif
 
 #endif
-- 
1.8.1.4

