summarylogtreecommitdiffstats
path: root/savedefault.patch
blob: e0749b9e8b72832931bb5e4bfc06745dfb30fecf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
Index: b/stage2/builtins.c
===================================================================
--- a/stage2/builtins.c
+++ b/stage2/builtins.c
@@ -86,6 +86,10 @@
    inside other functions.  */
 static int configfile_func (char *arg, int flags);
 
+static int savedefault_helper (char *arg, int flags);
+
+static int savedefault_shell (char *arg, int flags);
+
 /* Initialize the data for builtins.  */
 void
 init_builtins (void)
@@ -3512,7 +3516,109 @@
 static int
 savedefault_func (char *arg, int flags)
 {
-#if !defined(SUPPORT_DISKLESS) && !defined(GRUB_UTIL)
+#if !defined(SUPPORT_DISKLESS)
+  #if !defined(GRUB_UTIL)
+	return savedefault_helper(arg, flags);
+  #else
+	return savedefault_shell(arg, flags);
+  #endif
+#else /* !SUPPORT_DISKLESS */ 
+  errnum = ERR_UNRECOGNIZED;
+  return 1;
+#endif /* !SUPPORT_DISKLESS */
+}
+
+#if !defined(SUPPORT_DISKLESS) && defined(GRUB_UTIL)
+/* savedefault_shell */
+static int
+savedefault_shell(char *arg, int flags)
+ {
+  int once_only = 0;
+  int new_default;
+  int curr_default = -1;
+  int curr_prev_default = -1;
+  int new_prev_default = -1;
+  FILE *fp;
+  size_t bytes = 10;
+  char line[bytes];
+  char *default_file = (char *) DEFAULT_FILE_BUF;
+  char buf[bytes];
+  int i;
+  
+  while (1)
+    {
+      if (grub_memcmp ("--default=", arg, sizeof ("--default=") - 1) == 0)
+        {
+          char *p = arg + sizeof ("--default=") - 1;
+          if (! safe_parse_maxint (&p, &new_default))
+            return 1;
+          arg = skip_to (0, arg);
+        }
+      else if (grub_memcmp ("--once", arg, sizeof ("--once") - 1) == 0)
+        {
+         once_only = 1;
+         arg = skip_to (0, arg);
+	}
+      else
+        break;
+    }
+
+  *default_file = 0;
+  grub_strncat (default_file, config_file, DEFAULT_FILE_BUFLEN);
+  for (i = grub_strlen(default_file); i >= 0; i--)
+    if (default_file[i] == '/')
+    {
+      i++;
+      break;
+    }
+  default_file[i] = 0;
+  grub_strncat (default_file + i, "default", DEFAULT_FILE_BUFLEN - i);
+
+  if(!(fp = fopen(default_file,"r")))
+    {
+      errnum = ERR_READ;
+      goto fail;
+    }
+  
+  fgets(line, bytes, fp);
+  fclose(fp);
+ 
+  sscanf(line, "%d:%d", &curr_prev_default, &curr_default);
+     
+  if(curr_default != -1)
+    new_prev_default = curr_default;
+  else
+    {
+      if(curr_prev_default != -1)
+        new_prev_default = curr_prev_default;
+      else
+        new_prev_default = 0;
+    }
+     
+  if(once_only)
+    sprintf(buf, "%d:%d", new_prev_default, new_default);
+  else
+    sprintf(buf, "%d", new_default);
+
+  if(!(fp = fopen(default_file,"w")))
+    {
+      errnum = ERR_READ;
+      goto fail;
+    }
+     
+  fprintf(fp, buf);   
+     
+fail:
+  fclose(fp);
+  return errnum;
+}
+#endif
+
+/* savedefault_helper */
+static int
+savedefault_helper (char *arg, int flags)
+{
+#if !defined(SUPPORT_DISKLESS)
   unsigned long tmp_drive = saved_drive;
   unsigned long tmp_partition = saved_partition;
   char *default_file = (char *) DEFAULT_FILE_BUF;
@@ -3588,22 +3694,26 @@
       
       disk_read_hook = disk_read_savesect_func;
       len = grub_read (buf, sizeof (buf));
+      buf[9]='\0';/* Make sure grub_strstr() below terminates */
       disk_read_hook = 0;
       grub_close ();
       
-      if (len != sizeof (buf))
-	{
-	  /* This is too small. Do not modify the file manually, please!  */
-	  errnum = ERR_READ;
-	  goto fail;
-	}
-
       if (sector_count > 2)
 	{
 	  /* Is this possible?! Too fragmented!  */
 	  errnum = ERR_FSYS_CORRUPT;
 	  goto fail;
 	}
+
+      char *tmp;
+      if((tmp = grub_strstr(buf, ":")) != NULL)
+      {
+       int f_len = grub_strlen(buf) - grub_strlen(tmp);
+       char *def;
+       buf[f_len] = '\0';
+       def = buf;
+       safe_parse_maxint (&def, &entryno);
+      }
       
       /* Set up a string to be written.  */
       grub_memset (buf, '\n', sizeof (buf));
Index: b/stage2/stage2.c
===================================================================
--- a/stage2/stage2.c
+++ b/stage2/stage2.c
@@ -934,8 +934,16 @@
 	      len = grub_read (buf, sizeof (buf));
 	      if (len > 0)
 		{
-		  buf[sizeof (buf) - 1] = 0;
-		  safe_parse_maxint (&p, &saved_entryno);
+		  char *tmp;
+		  char *def;
+		  if((tmp = grub_strstr(p, ":")) != NULL)
+		  {
+		    *tmp++;
+		    grub_strcpy(&def, &tmp);
+		  }else
+		    grub_strcpy(&def, &p);
+		  
+		  safe_parse_maxint (&def, &saved_entryno);
 		}
 
 	      grub_close ();