bug-grub
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

GRUB: extending savedefault


From: Martin von Gagern
Subject: GRUB: extending savedefault
Date: Sat, 19 Jun 2004 00:50:20 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040319

Hello!

I changed the savedefault function to accept the number of the new
deafult entry as a parameter. I use this for one-time booting a
different OS.

---SNIP---
default=saved

title Linux
kernel /boot/vmlinuz

title Linux (old)
kernel /boot/vmlinuz.old

title Windows
savedefault 0
rootnoverify (hd0,0)
---SNAP---

I then use the simple application grub-savedefault to boot windows one
single time automatically, and after that my default is back to Linux.

Other option would be some one time entry field in the stage2. This
would allow grub to skip the menu completely in this case, and a
previousely saved default would not need to be changed for this. But as
this affects the data layout, I'd like someones opinion on this. An
argument to savedefault is a nice feature anyway, so I'd say keep it
anyway, even if there are better ways to do a single shot boot of a
different OS.

Greetings,
 Martin
diff -ruN grub-0.94/ChangeLog grub-0.94-savedefault/ChangeLog
--- grub-0.94/ChangeLog 2004-01-25 16:41:08.000000000 +0100
+++ grub-0.94-savedefault/ChangeLog     2004-06-19 00:37:34.185049624 +0200
@@ -1,3 +1,10 @@
+2004-06-19  Martin von Gagern  <address@hidden>
+
+       * stage2/builtins.c: Extended savedefault to accept parameter.
+       * docs/grub.texi: Changed accordingly
+       * util/grub-savedefault.c: Application to modify saved default.
+       Someone add this to some Makefile please.
+
 2004-01-25  Yoshinori K. Okuji  <address@hidden>
 
        * docs/grub.texi (Obtaining and Building GRUB): Instead of
diff -ruN grub-0.94/docs/grub.texi grub-0.94-savedefault/docs/grub.texi
--- grub-0.94/docs/grub.texi    2004-01-25 16:39:14.000000000 +0100
+++ grub-0.94-savedefault/docs/grub.texi        2004-06-18 22:12:11.139543696 
+0200
@@ -2894,8 +2894,11 @@
 @node savedefault
 @subsection savedefault
 
address@hidden Command savedefault
-Save the current menu entry as a default entry. Here is an example:
address@hidden Command savedefault [num]
+Save a new entry as the default boot entry.
+If @var{num} is specified, it is the number of the new default entry,
+otherwise when booting from a menu entry, this entry is used.
+Here is an example:
 
 @example
 @group
diff -ruN grub-0.94/stage2/builtins.c grub-0.94-savedefault/stage2/builtins.c
--- grub-0.94/stage2/builtins.c 2004-06-18 22:13:50.315466664 +0200
+++ grub-0.94-savedefault/stage2/builtins.c     2004-06-18 22:05:32.000000000 
+0200
@@ -3321,12 +3321,18 @@
   int *entryno_ptr;
   
   /* This command is only useful when you boot an entry from the menu
-     interface.  */
-  if (! (flags & BUILTIN_SCRIPT))
+     interface or an the entry is passed as an argument.  */
+  if (! (*arg && (flags & BUILTIN_CMDLINE)) && ! (flags & BUILTIN_SCRIPT))
     {
       errnum = ERR_UNRECOGNIZED;
       return 1;
     }
+
+  /* The number of the new default entry may be specified as a parameter.  */
+  if (*arg && ! safe_parse_maxint (&arg, &current_entryno))
+    {
+      return 1;
+    }
   
   /* Get the geometry of the boot drive (i.e. the disk which contains
      this stage2).  */
@@ -3378,8 +3384,10 @@
   "savedefault",
   savedefault_func,
   BUILTIN_CMDLINE,
-  "savedefault",
-  "Save the current entry as the default boot entry."
+  "savedefault [NUM]",
+  "Save a new entry as the default boot entry."
+  " If NUM is specified, it is the number of the new default entry,"
+  " otherwise when booting from a menu entry, this entry is used."
 };
 
 
diff -ruN grub-0.94/util/grub-savedefault.c 
grub-0.94-savedefault/util/grub-savedefault.c
--- grub-0.94/util/grub-savedefault.c   1970-01-01 01:00:00.000000000 +0100
+++ grub-0.94-savedefault/util/grub-savedefault.c       2004-06-19 
00:33:01.871447584 +0200
@@ -0,0 +1,67 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 1999,2000,2001,2002,2003,2004  Free Software Foundation, Inc.
+ *
+ *  This program 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 this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define SAVED_ENTRY_POS 0x20c
+#define DEFAULT_STAGE2_LOCATION "/boot/grub/stage2"
+
+int main(int argc, char** argv) {
+       const char *filename=DEFAULT_STAGE2_LOCATION;
+       char* ptr;
+       int entry;
+       FILE* file;
+       
+       if (argc>=2) entry=strtoul(argv[1], &ptr, 0);
+       if (argc<2 || argc>3 || *ptr || entry > 0xff) {
+               ptr=strrchr(argv[0], '/');
+               if (ptr==NULL) ptr=strrchr(argv[0], '\\');
+               if (ptr==NULL) ptr=argv[0];
+               else ++ptr;
+               fprintf(stderr,
+                       "Usage: %s NUM [STAGE2]\n\t"
+                       "Modify grub stage2 file STAGE2 (default "
+                       DEFAULT_STAGE2_LOCATION
+                       ")\n\tto set default entry to NUM\n",
+                       ptr);
+               return argc==1 ? 0 : 1;
+       }
+       file=fopen(filename, "r+b");
+       if (file==NULL) {
+               perror(filename);
+               return 1;
+       }
+       if (fseek(file, SAVED_ENTRY_POS, SEEK_SET)) {
+               perror("seek to entry number location (20C)");
+               fclose(file);
+               return 2;
+       }
+       if (fputc(entry, file) != entry) {
+               perror("write number to stage2 file");
+               fclose(file);
+               return 2;
+       }
+       if (fclose(file)) {
+               perror("close stage2 file");
+               return 2;
+       }
+       return 0;
+}

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]