Skip to contentSkip to conversation

romfs support for Grub2

PATCH In review y.volta 3 participants 3 messages Aug 17, 2008 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

y.volta

author
#1 lore raw

PATCH 1/1 romfs support for Grub2

Hi,

I made a small patch for grub2 using a romfs file ( zip compressing support ). there are 3 files added:

-/commands/i386/pc/romfst.c command to mount a romfs image file.
-/fs/i386/pc/romfs.c fs and disk layer for romfs.
-/include/grub/i386/pc/romfs.h header file.

usage:
grub>romfst /romfs.gz
grub>cat (rd)/readme.txt

info
------------------------------------------------------------
i make this patch have two resources: the grub legancy romfs patch and the bean's pxe patch.
i get to know the pxe mod merged the disk&fs into a fs source file. this will make mod more simple to implement. the romfst command will load the specify file to a new memory block. when user access (rd), the new files are ready for him.

limitations
------------------------------------------------------------
* this patch is for file only: if you want to use it on a disk/partition, we should improve it.
* grub_romfs_data *data, has not set to file->data - i used several global varibles to store releated stuffs.

well, this is just a work-able patch, not too professional. ;-)

btw, i think the grub_dprintf should output to non-display device, this will make debuging more easier and comfort . i think the low-cost device is a com-port. for, now time virtual machine software has this legancy device full supported. the freeldr coming with ReactOS have this kind of function (only display a string, but can do kernal debug also )

y.volta

commands/i386/pc/romfst.c +111 -0
diff -ruNa grub2/commands/i386/pc/romfst.c grub2_romfs/commands/i386/pc/romfst.c
@@ -0,0 +1,111 @@
+/* romfst.c -- init a romfs from a disk file.  */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2008  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 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/normal.h>
+#include <grub/dl.h>
+#include <grub/arg.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/env.h>
+#include <grub/gzio.h>
+#include <grub/file.h>
+#include <grub/disk.h>
+#include <grub/machine/romfs.h>
+
+
+static grub_err_t
+grub_cmd_romfst (struct grub_arg_list *state __attribute__ ((unused)), int argc,
+	       char **args)
+{
+  grub_file_t file;
+  char chHead[8];
+
+  DBG_MSG ("\n\n ------- romfst get in. ----------\n");
+
+  /* check for existence of this file */
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
+
+  file = grub_gzfile_open (args[0], 1);
+  if (! file)
+      return 0;
+  
+  /* check for "-romfs-" */
+  if (grub_file_read (file, chHead, 8) != 8)
+  {
+      grub_file_close ( file );
+      return grub_error(GRUB_ERR_FILE_READ_ERROR, "read file header failed");
+  }
+
+  DBG_MSG ("\t seek to begin.\n");
+  grub_file_seek (file, 0);
+  
+  if(grub_memcmp(chHead, "-rom1fs-", 8) !=0)
+  {
+      grub_file_close( file );
+      return grub_error(GRUB_ERR_UNKNOWN_FS, "not a valid romfs");
+  }
+
+  DBG_MSG ("\t file header verified.\n");
+  
+  /* reset the basic romfs information */
+  if (romdisk_addr)
+    grub_free (romdisk_addr);
+
+  romdisk_size = 0;
+
+  DBG_MSG ("\t malloc the romdisk_addr.\n");
+
+  /* prepare the memory */
+  romdisk_addr = grub_malloc (grub_file_size(file));
+  if (!romdisk_addr)
+  {
+      grub_file_close(file);
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY, "romfs base memory alloc failed");
+  }
+
+  romdisk_size = grub_file_size(file);
+
+  /* read this all */
+  grub_file_read (file, romdisk_addr, romdisk_size);
+  grub_file_close(file);
+
+  DBG_MSG ("\t load this file whole.\n");
+
+  /* O.K. reinit this disk */
+  re_init_romdisk ();
+
+  DBG_MSG ("\t romfs re-inited..\n");
+
+  return grub_errno;
+}
+
+
+
+GRUB_MOD_INIT(romfst)
+{
+  (void)mod;			/* To stop warning. */
+  grub_register_command ("romfst", grub_cmd_romfst, GRUB_COMMAND_FLAG_BOTH,
+			 "romfst FILE", "Init romfs from a file", 0);
+}
+
+GRUB_MOD_FINI(romfst)
+{
+  grub_unregister_command ("romfst");
+}
conf/i386-pc.rmk +11 -1
diff -ruNa grub2/conf/i386-pc.rmk grub2_romfs/conf/i386-pc.rmk
@@ -164,7 +164,7 @@
 	videotest.mod play.mod bitmap.mod tga.mod cpuid.mod serial.mod	\
 	ata.mod vga.mod memdisk.mod jpeg.mod png.mod pci.mod lspci.mod \
 	aout.mod _bsd.mod bsd.mod pxe.mod pxecmd.mod datetime.mod date.mod \
-	datehook.mod
+	datehook.mod romfst.mod romfs.mod
 
 # For biosdisk.mod.
 biosdisk_mod_SOURCES = disk/i386/pc/biosdisk.c
@@ -356,4 +356,14 @@
 datehook_mod_CFLAGS = $(COMMON_CFLAGS)
 datehook_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
+# For romfs.mod.
+romfs_mod_SOURCES = fs/i386/pc/romfs.c
+romfs_mod_CFLAGS = $(COMMON_CFLAGS)
+romfs_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
+# For romfst.mod.
+romfst_mod_SOURCES = commands/i386/pc/romfst.c
+romfst_mod_CFLAGS = $(COMMON_CFLAGS)
+romfst_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
 include $(srcdir)/conf/common.mk
fs/i386/pc/romfs.c +653 -0

653 lines not on this page · raw message

include/grub/disk.h +1 -0
diff -ruNa grub2/include/grub/disk.h grub2_romfs/include/grub/disk.h
@@ -40,6 +40,7 @@
     GRUB_DISK_DEVICE_NAND_ID,
     GRUB_DISK_DEVICE_UUID_ID,
     GRUB_DISK_DEVICE_PXE_ID,
+	GRUB_DISK_DEVICE_ROMDISK_ID,
   };
 
 struct grub_disk;
include/grub/i386/pc/romfs.h +72 -0

72 lines not on this page · raw message

#2 lore raw

Hi,

Quoted 12 lines · y.volta

Hi,

I made a small patch for grub2 using a romfs file ( zip compressing support ). there are 3 files added:

(quoted diff) +0 -3
-/commands/i386/pc/romfst.c                                 command to mount a romfs image file.
-/fs/i386/pc/romfs.c                                                 fs and disk layer for romfs.
-/include/grub/i386/pc/romfs.h                                 header file.

usage:
grub>romfst /romfs.gz
grub>cat (rd)/readme.txt

Quoted 5 lines

info
------------------------------------------------------------
i make this patch have two resources: the grub legancy romfs patch and the bean's pxe patch.
i get to know the pxe mod merged the disk&fs into a fs source file. this will make mod more simple to implement. the romfst command will load the specify file to a new memory block. when user access (rd), the new files are ready for him.

I think romfs support is not in grub legacy, do you mean some local patch of distro ? Also, code from grub legacy is not always compatible with grub2. To avoid legal issue, just don't import code from other source, write them from scratch.

There is no need to add a command to mount the image, you can use loopback. Also, don't merge disk&fs, romfs should be a fs module, just like cpio.

BTW, I notice that you define le32, but there is already a function to do the job: grub_le_to_cpu32.

Also, you use global buffer blk_buf, this is very grub-legacy. In grub2, you should avoid it as much as possible, because it would have problem when accessing multiple file, try to place the buffer in local structure like grub_romfs_data.

Quoted 10 lines

limitations
------------------------------------------------------------
* this patch is for file only: if you want to use it on a disk/partition, we should improve it.
* grub_romfs_data *data, has not set to file->data - i used several global varibles to store releated stuffs.

well, this is just a work-able patch, not too professional. ;-)

btw, i think the grub_dprintf should output to non-display device, this will make debuging more easier and comfort . i think the low-cost device is a com-port. for, now time virtual machine software has this legancy device full supported. the freeldr coming with ReactOS have this kind of function (only display a string, but can do kernal debug also )

Please don't define new debug mechanism. grub_dprintf may not be the best, but it's already used through grub2, just keep it that way.

Also, I notice that indentation is not right, grub2 use gnu style. If you're too lazy to reformat the code, you can run it through indent.

Signature · 1 line
Bean
#3 lore raw
Quoted 6 lines · Bean
Quoted 2 lines · y.volta

btw, i think the grub_dprintf should output to non-display device, this will make debuging more easier and comfort . i think the low-cost device is a com-port. for, now time virtual machine software has this legancy device full supported. the freeldr coming with ReactOS have this kind of function (only display a string, but can do kernal debug also )

Please don't define new debug mechanism. grub_dprintf may not be the best, but it's already used through grub2, just keep it that way.

Hmm.. How about if we define new targets for dprintf?

Like you load module debug_vmware/qemu/serial/or something common. Then it would provide new environment variable where you can set target of dprintf. Though dprintf would then need to provide hook to allow redirecting of those, but that should not be big deal.