map command draft #2
I attach the patch that makes grub2 to have a map command. The map command only modifies a vector. If we want true map in grub2 we need to hook some functions so that some bios calls ocurr.
Meanwhile I think how to do it... here there is my patch and some questions and suggestions.
Quoted 4 lines
diff -urN
grub2_2007_05_31_original/commands/i386/pc/map.cgrub2_2007_05_31_map/commands/i386/pc/map.c
commands/i386/pc/map.c line 10
Quoted 10 lines
commands/i386/pc/map.c +10
-0@@ -0,0 +1,108 @@ +/* map.c - Define map array */ + +#include <grub/normal.h> +#include <grub/dl.h> +#include <grub/arg.h> +#include <grub/disk.h> +#include <grub/term.h> +#include <grub/misc.h> +#include <grub/device.h> +#include <grub/file.h>
I am including file.h because it includes the function:grub_file_get_device_name
I think an equivalent function such as:grub_disk_get_device_name
should be defined in disk.h and implemented where it corresponds to be implemented.
commands/i386/pc/map.c line 13
Quoted 3 lines
commands/i386/pc/map.c +3
-0+ +/* The size of the drive map. */ +#define DRIVE_MAP_SIZE 16
Is there any bios that supports more than 16 hard disks?
Quoted 22 lines
commands/i386/pc/map.c +22
-0+/* The BIOS drive map. */ +static unsigned short bios_drive_map[DRIVE_MAP_SIZE +1 ]; + +int real_map_func (unsigned long to,unsigned long from) { +int i; +/* Search for an empty slot in BIOS_DRIVE_MAP. */ +for (i =0 ; i <DRIVE_MAP_SIZE ; i++) + { +/* Perhaps the user wants to override the map. */ +if ((bios_drive_map[i] &0xff ) == from) +break ; + +if (! bios_drive_map[i]) +break ; + } + +if (i ==DRIVE_MAP_SIZE ) + { +return GRUB_ERR_OUT_OF_RANGE ; + } + +if (to == from)
commands/i386/pc/map.c line 45
Quoted 10 lines
commands/i386/pc/map.c +10
-0+/* If TO is equal to FROM, delete the entry. */ +grub_memmove ((char *) &bios_drive_map[i], (char *) &bios_drive_map[i +1 ], +sizeof (unsigned short ) * (DRIVE_MAP_SIZE - i)); + else + bios_drive_map[i] = from | (to <<8 ); + +return 0 ; + + +}
The above code was copy-pasted from grub legacy. Do you think it can be improved?
Quoted 9 lines
commands/i386/pc/map.c +9
-0+ + + +static grub_err_t +grub_cmd_map (struct grub_arg_list *state __attribute__ ((unused)), +int argc,char **args) +{ +grub_disk_t map_disk_to,map_disk_from; +
commands/i386/pc/map.c line 64
Quoted 10 lines
commands/i386/pc/map.c +10
-0+/* TODO: Check map arguments from grub2 engine */ +if (argc !=2 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Two devices required" ); + map_disk_to =grub_disk_open (grub_file_get_device_name (args[0 ])); +if (! map_disk_to) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"Unknown TO disk" ); +/* This was a marco_g suggestion but it does not make sense to me. +if (grub_strcmp(map_disk_to->name,"biosdisk")) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "TO disk is not a bios disk."); +*/
marco_g ... if I try to grub_printf the disk_to-> name it says hd0. It does not say "biosdisk" as you have told me.
I am bit confused.
commands/i386/pc/map.c line 71
Quoted 7 lines
commands/i386/pc/map.c +7
-0+ map_disk_from =grub_disk_open (grub_file_get_device_name (args[1 ])); +if (! map_disk_from) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"Unknown FROM disk" ); +/* This was a marco_g suggestion but it does not make sense to me. +if (grub_strcmp(map_disk_from->name,"biosdisk")) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "FROM disk is not a bios disk."); +*/
Idem for marco_g.
Should I add a function that checks the validity of a hard disk and asignts it to a grub_disk_t variable and call it twice in order to make the code smaller ?
Quoted 40 lines
commands/i386/pc/map.c +18
-0+ + +return (real_map_func (map_disk_to->id,map_disk_from->id)); + +} + + +GRUB_MOD_INIT (map) +{ + (void )mod;/* To stop warning. */ +grub_register_command ("map" , grub_cmd_map,GRUB_COMMAND_FLAG_BOTH , +"map TODEVICE FROMDEVICE" ,"Map a drive to another" ,0 ); +} + +GRUB_MOD_FINI (map) +{ +grub_unregister_command ("map" ); +}diff -urN grub2_2007_05_31_original/conf/i386-pc.rmk grub2_2007_05_31_map/conf/i386-pc.rmk
conf/i386-pc.rmk +3
-1@@ -1,5 +1,7 @@ # -*- makefile -*- + + COMMON_ASFLAGS = -nostdinc -fno-builtin -m32 COMMON_CFLAGS = -fno-builtin -mrtd -mregparm=3 -m32 COMMON_LDFLAGS = -m32 -nostdlib@@ -121,7 +123,7 @@ pkgdata_MODULES = _chain.mod _linux.mod linux.mod normal.mod \ _multiboot.mod chain.mod multiboot.mod reboot.mod halt.mod \ vbe.mod vbetest.mod vbeinfo.mod video.mod gfxterm.mod \ - videotest.mod play.mod bitmap.mod tga.mod cpuid.mod + videotest.mod map.mod play.mod bitmap.mod tga.mod cpuid.mod # For _chain.mod. _chain_mod_SOURCES = loader/i386/pc/chainloader.c@@ -195,6 +197,11 @@ vbetest_mod_CFLAGS = $(COMMON_CFLAGS)
conf/i386-pc.rmk line 207
Quoted 10 lines
conf/i386-pc.rmk +5
-0vbetest_mod_LDFLAGS = $(COMMON_LDFLAGS) +# For map.mod. +map_mod_SOURCES = commands/i386/pc/map.c +map_mod_CFLAGS = $(COMMON_CFLAGS) +map_mod_LDFLAGS = $(COMMON_LDFLAGS) + # For play.mod. play_mod_SOURCES = commands/i386/pc/play.c play_mod_CFLAGS = $(COMMON_CFLAGS)
Is it important that I also copy-paste the rmk files modifications or not?
adrian15
commands/i386/pc/map.c +107 -0
@@ -0,0 +1,107 @@ +/* map.c - Define drives map array */ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2005 Free Software Foundation, Inc. +* +* GRUB is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include <grub/normal.h> +#include <grub/dl.h> +#include <grub/arg.h> +#include <grub/disk.h> +#include <grub/term.h> +#include <grub/misc.h> +#include <grub/device.h> +#include <grub/file.h> + +/* The size of the drive map. */ +#define DRIVE_MAP_SIZE 16 +/* The BIOS drive map. */ +static unsigned short bios_drive_map[DRIVE_MAP_SIZE +1 ]; + +int real_map_func (unsigned long to,unsigned long from) { +int i; +/* Search for an empty slot in BIOS_DRIVE_MAP. */ +for (i =0 ; i <DRIVE_MAP_SIZE ; i++) + { +/* Perhaps the user wants to override the map. */ +if ((bios_drive_map[i] &0xff ) == from) +break ; + +if (! bios_drive_map[i]) +break ; + } + +if (i ==DRIVE_MAP_SIZE ) + { +return GRUB_ERR_OUT_OF_RANGE ; + } + +if (to == from) +/* If TO is equal to FROM, delete the entry. */ +grub_memmove ((char *) &bios_drive_map[i], (char *) &bios_drive_map[i +1 ], +sizeof (unsigned short ) * (DRIVE_MAP_SIZE - i)); +else + bios_drive_map[i] = from | (to <<8 ); + +return 0 ; + + +} + + + +static grub_err_t +grub_cmd_map (struct grub_arg_list *state __attribute__ ((unused)), +int argc,char **args) +{ +grub_disk_t map_disk_to,map_disk_from; + +/* TODO: Check map arguments from grub2 engine */ +if (argc !=2 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Two devices required" ); + map_disk_to =grub_disk_open (grub_file_get_device_name (args[0 ])); +if (! map_disk_to) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"Unknown TO disk" ); +/* This was a marco_g suggestion but it does not make sense to me. +if (grub_strcmp(map_disk_to->name,"biosdisk")) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "TO disk is not a bios disk."); +*/ + map_disk_from =grub_disk_open (grub_file_get_device_name (args[1 ])); +if (! map_disk_from) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"Unknown FROM disk" ); +/* This was a marco_g suggestion but it does not make sense to me. +if (grub_strcmp(map_disk_from->name,"biosdisk")) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "FROM disk is not a bios disk."); +*/ + + +return (real_map_func (map_disk_to->id,map_disk_from->id)); + +} + + +GRUB_MOD_INIT (map) +{ + (void )mod;/* To stop warning. */ +grub_register_command ("map" , grub_cmd_map,GRUB_COMMAND_FLAG_BOTH , +"map TODEVICE FROMDEVICE" ,"Map a drive to another" ,0 ); +} + +GRUB_MOD_FINI (map) +{ +grub_unregister_command ("map" ); +}
conf/i386-pc.rmk +8 -1
@@ -1,5 +1,7 @@ # -*- makefile -*- + + COMMON_ASFLAGS = -nostdinc -fno-builtin -m32 COMMON_CFLAGS = -fno-builtin -mrtd -mregparm=3 -m32 COMMON_LDFLAGS = -m32 -nostdlib
@@ -121,7 +123,7 @@ pkgdata_MODULES = _chain.mod _linux.mod linux.mod normal.mod \ _multiboot.mod chain.mod multiboot.mod reboot.mod halt.mod \ vbe.mod vbetest.mod vbeinfo.mod video.mod gfxterm.mod \ - videotest.mod play.mod bitmap.mod tga.mod cpuid.mod + videotest.mod map.mod play.mod bitmap.mod tga.mod cpuid.mod # For _chain.mod. _chain_mod_SOURCES = loader/i386/pc/chainloader.c
@@ -195,6 +197,11 @@ vbetest_mod_CFLAGS = $(COMMON_CFLAGS) vbetest_mod_LDFLAGS = $(COMMON_LDFLAGS) +# For map.mod. +map_mod_SOURCES = commands/i386/pc/map.c +map_mod_CFLAGS = $(COMMON_CFLAGS) +map_mod_LDFLAGS = $(COMMON_LDFLAGS) + # For play.mod. play_mod_SOURCES = commands/i386/pc/play.c play_mod_CFLAGS = $(COMMON_CFLAGS)