Skip to contentSkip to conversation

map command for grub2 draft

DISCUSSION adrian15 3 participants 7 messages Jun 7, 2007 – Jun 16, 2007

lore
Share & tools
lore

adrian15

author
#0 lore raw

I want to implement the map command for grub2 as long as it is an
interesting thing for stupid windows oses and because it's the clue for making an "usbshift" equivalent command (See Super Grub Disk usbshift command for more details).

1st) Where to save an array?
=============================

If I mimic the grub legacy map command I need to save an array with the
map definitions.

	static unsigned short bios_drive_map[DRIVE_MAP_SIZE + 1];
	
	So that each time I call something like:
	map (hd0) (hd1)
	it modifies this vector.

Any example on where should I define this vector so that I can use it from each command ?

2nd) boot_func and actual mapping
====================================

	If you look at grub legacy boot command


    case KERNEL_TYPE_CHAINLOADER:
      /* Chainloader */

      /* Check if we should set the int13 handler.  */
      if (bios_drive_map[0] != 0)
	{
	  int i;
	
	  /* Search for SAVED_DRIVE.  */
	  for (i = 0; i < DRIVE_MAP_SIZE; i++)
	    {
	      if (! bios_drive_map[i])
		break;
	      else if ((bios_drive_map[i] & 0xFF) == saved_drive)
		{
		  /* Exchage SAVED_DRIVE with the mapped drive.  */
		  saved_drive = (bios_drive_map[i] >> 8) & 0xFF;
		  break;
		}
	    }
	
	  /* Set the handler. This is somewhat dangerous.  */
	  set_int13_handler (bios_drive_map);
	}

      gateA20 (0);
      boot_drive = saved_drive;
      chain_stage1 (0, BOOTSEC_LOCATION, boot_part_addr);
      break;

you will see that among other things it detects if you want to do a chainloader boot. If you do a chainloader boot it checks the bios_drive_map array,... run some BIOS calls so that there's an actual map and then it finally boots.

When I told marco_g that I want to modificy grub2's boot command he
became scared because the boot command was arquitecture specific (or maybe not, I actually did not understood what he meant) and as long as I understood I was not meant to modify it.

how should I implement this then?

adrian15

author
#1 lore raw
Quoted 6 lines · adrian15 escribió:

1st) Where to save an array?
=============================

If I mimic the grub legacy map command I need to save an array with the
map definitions.

Quoted 8 lines

static unsigned short bios_drive_map[DRIVE_MAP_SIZE + 1];

So that each time I call something like:
map (hd0) (hd1)
it modifies this vector.

Any example on where should I define this vector so that I can use it from each command ?

I ask the question again.

I've begun to implement commands/i386/pc/map.c (I suppose the map.c is pc specific althought I am not sure).

The question is how do I get the bios number for a given hard disk? Thank you. You know hd0 is usually 0x80, that's the number that I want.

Here is the source code written so far which I have not compiled.

adrian15

static grub_err_t
grub_cmd_map (struct grub_arg_list *state __attribute__ ((unused)),
int argc, char **args)
{
grub_disk_t map_disk_1,map_disk_2;

  if (argc != 2)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Two devices required");

  map_disk_1 = grub_disk_open (args[0]);
  if (! map_disk_1)
    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown 1st disk");
  map_disk_2 = grub_disk_open (args[1]);
  if (! map_disk_2)
    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown 2nd disk");

/* How do I get the BIOS number for a given disk */

#2 lore raw
Quoted 12 lines · adrian15 <[email protected]> writes:
Quoted 11 lines · adrian15 escribió:

1st) Where to save an array?
=============================

If I mimic the grub legacy map command I need to save an array with the
map definitions.

static unsigned short bios_drive_map[DRIVE_MAP_SIZE + 1];
So that each time I call something like:
map (hd0) (hd1)
it modifies this vector.

Quoted 4 lines
Quoted 2 lines

Any example on where should I define this vector so that I can use it from each command ?

I ask the question again.

What is this vector supposed to do? Do you really need it outside map.c?

Can you describe the design of the map command?

--
Marco

#3 lore raw
Quoted 17 lines · Marco Gerards
Quoted 15 lines · adrian15 <[email protected]> writes:
Quoted 13 lines · adrian15 escribió:

1st) Where to save an array?
=============================

If I mimic the grub legacy map command I need to save an array with the
map definitions.

static unsigned short bios_drive_map[DRIVE_MAP_SIZE + 1];
So that each time I call something like:
map (hd0) (hd1)
it modifies this vector.

Any example on where should I define this vector so that I can use it from each command ?

Quoted 6 lines
Quoted 1 line

I ask the question again.

What is this vector supposed to do? Do you really need it outside map.c?

Can you describe the design of the map command?

I happen to know something about the map command in GRUB Legacy, it works by hooking INT 13.

It first reserves a small range of memory from the top of convensional memory by decreasing value at 0x413, then copy its INT 13 interrupt handler and the drive map to this area. Finally, it modifies the IVT table so that INT 13 points to the new handler.

Inside the handler, it uses the drive map to translate the current drive stored in %dl to the real drive, then jump to the original handler.

Signature · 1 line
Bean <[email protected]>
#4 lore raw
Quoted 28 lines · Bean
Quoted 17 lines · Marco Gerards
Quoted 15 lines · adrian15 <[email protected]> writes:
Quoted 13 lines · adrian15 escribió:

1st) Where to save an array?
=============================

If I mimic the grub legacy map command I need to save an array with the
map definitions.

static unsigned short bios_drive_map[DRIVE_MAP_SIZE + 1];
So that each time I call something like:
map (hd0) (hd1)
it modifies this vector.

Any example on where should I define this vector so that I can use it from each command ?

Quoted 6 lines
Quoted 1 line

I ask the question again.

What is this vector supposed to do? Do you really need it outside map.c?

Can you describe the design of the map command?

I happen to know something about the map command in GRUB Legacy, it works by hooking INT 13.

Quoted 7 lines

It first reserves a small range of memory from the top of convensional memory by decreasing value at 0x413, then copy its INT 13 interrupt handler and the drive map to this area. Finally, it modifies the IVT table so that INT 13 points to the new handler.

Inside the handler, it uses the drive map to translate the current drive stored in %dl to the real drive, then jump to the original handler.

Right, but the problem Adrian brought up was that he though he needs to add a vector which describes the mapping as a global. I do not see why this is required. So I wonder what he had in mind.

I think the main issue here is how to hook this into GRUB 2 so that the hook becomes effective just before booting.

--
Marco

#5 lore raw
Quoted 35 lines · Marco Gerards
Quoted 33 lines · Bean
Quoted 17 lines · Marco Gerards
Quoted 15 lines · adrian15 <[email protected]> writes:
Quoted 13 lines · adrian15 escribió:

1st) Where to save an array?
=============================

If I mimic the grub legacy map command I need to save an array with the
map definitions.

static unsigned short bios_drive_map[DRIVE_MAP_SIZE + 1];
So that each time I call something like:
map (hd0) (hd1)
it modifies this vector.

Any example on where should I define this vector so that I can use it from each command ?

Quoted 6 lines
Quoted 1 line

I ask the question again.

What is this vector supposed to do? Do you really need it outside map.c?

Can you describe the design of the map command?

I happen to know something about the map command in GRUB Legacy, it works by hooking INT 13.

It first reserves a small range of memory from the top of convensional memory by decreasing value at 0x413, then copy its INT 13 interrupt handler and the drive map to this area. Finally, it modifies the IVT table so that INT 13 points to the new handler.

Quoted 6 lines
Quoted 2 lines

Inside the handler, it uses the drive map to translate the current drive stored in %dl to the real drive, then jump to the original handler.

Right, but the problem Adrian brought up was that he though he needs to add a vector which describes the mapping as a global. I do not see why this is required. So I wonder what he had in mind.

Yes, it doesn't need to be a global variable.

Quoted 3 lines

I think the main issue here is how to hook this into GRUB 2 so that the hook becomes effective just before booting.

It can be done in a grub_map_real_boot function in kern/i386/pc/startup.S

Signature · 1 line
Bean <[email protected]>
author
#6 lore raw
In reply to adrian15

Here I am again with another question...

How do I check that a given string: (hd0), (hd1,0), (hd1) is an
existent drive?

I supposed that I had to use grub_disk_open because I did not want to
use grub_device_open can open (hd1,0) and (hd1,0) is not a drive but a partition.

Here there is my map.c source code (which does compile) and some more
doubts that I have:

I attach a patch that contains an unfinished and untested pause command too.

Quoted 7 lines

#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>

Is there any include that I do not need at all?

Quoted 5 lines

/* 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];

I've finally decided to put this here. I will put the other commands:
mapboot (the bios hook) and usbshift (A complex map command that maps many drives) in this same file.

Quoted 26 lines

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);
Quoted 4 lines

return 0;

}

Copy-Pasted from grub legacy. :)
Should I improve it, removing the break or not ?

Quoted 8 lines

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;

I want to check disks not devices this is why I use grub_disk_t.

Quoted 6 lines
  
  if (argc != 2)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Two devices required");

map_disk_to = grub_disk_open (args[0]);
/* TODO: Check map arguments from grub2 engine */

This means that in a future the grub2 engine should be able to read an string like: <DISK1,DISK2> and check itself if the strings are disks or not thus I would not have to write the checks inside this file.

Quoted 7 lines
  if (! map_disk_to)
    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown TO disk");
  map_disk_from = grub_disk_open (args[1]);
  if (! map_disk_from)
    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown FROM disk");

  return (real_map_func(map_disk_to->id,map_disk_from->id));

Do you want me to merge the two functions in one maybe?

Quoted 8 lines

}


GRUB_MOD_INIT(map)
{
(void)mod; /* To stop warning. */
grub_register_command ("map", grub_cmd_map, GRUB_COMMAND_FLAG_BOTH,

Quoted 7 lines
			 "map TODEVICE FROMDEVICE", "Map a drive to another", 0);
}

GRUB_MOD_FINI(map)
{
grub_unregister_command ("map");
}

One more question marco_g told me that I had to check the field "name" from grub_disk_t struct. If it read: "biosdisk" then it was ok for the map command. If it did not read this then it was an error.

I am sorry about the noob question but how should I compare two strings on grub2?

And why isn't there an enum type for grub_disk_t name field (as the grub2 errors)? Do we have a new name for disk everyday ?

adrian15

commands/i386/pc/map.c +98 -0
diff -urN grub2_2007_05_31_original/commands/i386/pc/map.c grub2_2007_05_31_map/commands/i386/pc/map.c
@@ -0,0 +1,98 @@
+/* map.c - Define map vector  */
+/*
+ *  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>
+
+/* 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;
+  
+  if (argc != 2)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Two devices required");
+
+  map_disk_to = grub_disk_open (args[0]);
+/* TODO: Check map arguments from grub2 engine */
+  if (! map_disk_to)
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown TO disk");
+  map_disk_from = grub_disk_open (args[1]);
+  if (! map_disk_from)
+    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown FROM 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");
+}
commands/pause.c +52 -0

52 lines not on this page · raw message

conf/i386-pc.mk +55 -1

73 lines not on this page · raw message

conf/i386-pc.rmk +8 -1
diff -urN grub2_2007_05_31_original/conf/i386-pc.rmk grub2_2007_05_31_map/conf/i386-pc.rmk
@@ -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)
gendistlist +38 -0
diff -urN grub2_2007_05_31_original/gendistlist grub2_2007_05_31_map/gendistlist
@@ -0,0 +1,38 @@
+#! /bin/sh
+#
+# Copyright (C) 2005  Free Software Foundation, Inc.
+#
+# This gendistlist.sh is free software; the author
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE.
+
+# Generate a list of distributed files.
+
+EXTRA_DISTFILES="AUTHORS COPYING ChangeLog DISTLIST INSTALL NEWS README \
+	THANKS TODO Makefile.in aclocal.m4 autogen.sh config.guess \
+	config.h.in config.sub configure configure.ac gencmdlist.sh \
+	gendistlist.sh genfslist.sh genkernsyms.sh genmk.rb \
+	genmodsrc.sh gensymlist.sh install-sh mkinstalldirs stamp-h.in"
+
+DISTDIRS="boot commands conf disk font fs hello include io kern loader \
+	normal partmap term util video"
+
+for f in $EXTRA_DISTFILES; do
+    echo $f
+done
+
+dir=`dirname $0`
+cd $dir
+
+for dir in $DISTDIRS; do
+  for d in `find $dir -type d | sort`; do
+    find $d -maxdepth 1 -name '*.[chS]' -o -name '*.mk' -o -name '*.rmk' \
+      -o -name '*.rb' -o -name '*.in' \
+      | sort
+  done
+done