Magellan Linux

Diff of /tags/grubby-8_40_20170706/grubby.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1722 by niro, Sat Feb 18 00:53:14 2012 UTC revision 3029 by niro, Tue Jun 27 14:46:56 2017 UTC
# Line 36  Line 36 
36  #include <signal.h>  #include <signal.h>
37  #include <blkid/blkid.h>  #include <blkid/blkid.h>
38    
39    #include "log.h"
40    
41  #ifndef DEBUG  #ifndef DEBUG
42  #define DEBUG 0  #define DEBUG 0
43  #endif  #endif
# Line 46  Line 48 
48  #define dbgPrintf(format, args...)  #define dbgPrintf(format, args...)
49  #endif  #endif
50    
51    int debug = 0; /* Currently just for template debugging */
52    
53  #define _(A) (A)  #define _(A) (A)
54    
55  #define MAX_EXTRA_INITRDS  16 /* code segment checked by --bootloader-probe */  #define MAX_EXTRA_INITRDS  16 /* code segment checked by --bootloader-probe */
# Line 54  Line 58 
58  #define NOOP_OPCODE 0x90  #define NOOP_OPCODE 0x90
59  #define JMP_SHORT_OPCODE 0xeb  #define JMP_SHORT_OPCODE 0xeb
60    
61    int isEfi = 0;
62    
63    #if defined(__aarch64__)
64    #define isEfiOnly 1
65    #else
66    #define isEfiOnly 0
67    #endif
68    
69    char *saved_command_line = NULL;
70    
71  /* comments get lumped in with indention */  /* comments get lumped in with indention */
72  struct lineElement {  struct lineElement {
73      char * item;   char *item;
74      char * indent;   char *indent;
75  };  };
76    
77  enum lineType_e {  enum lineType_e {
78      LT_WHITESPACE   = 1 << 0,   LT_UNIDENTIFIED = 0,
79      LT_TITLE        = 1 << 1,   LT_WHITESPACE = 1 << 0,
80      LT_KERNEL       = 1 << 2,   LT_TITLE = 1 << 1,
81      LT_INITRD       = 1 << 3,   LT_KERNEL = 1 << 2,
82      LT_HYPER        = 1 << 4,   LT_INITRD = 1 << 3,
83      LT_DEFAULT      = 1 << 5,   LT_HYPER = 1 << 4,
84      LT_MBMODULE     = 1 << 6,   LT_DEFAULT = 1 << 5,
85      LT_ROOT         = 1 << 7,   LT_MBMODULE = 1 << 6,
86      LT_FALLBACK     = 1 << 8,   LT_ROOT = 1 << 7,
87      LT_KERNELARGS   = 1 << 9,   LT_FALLBACK = 1 << 8,
88      LT_BOOT         = 1 << 10,   LT_KERNELARGS = 1 << 9,
89      LT_BOOTROOT     = 1 << 11,   LT_BOOT = 1 << 10,
90      LT_LBA          = 1 << 12,   LT_BOOTROOT = 1 << 11,
91      LT_OTHER        = 1 << 13,   LT_LBA = 1 << 12,
92      LT_GENERIC      = 1 << 14,   LT_OTHER = 1 << 13,
93      LT_ECHO    = 1 << 16,   LT_GENERIC = 1 << 14,
94      LT_MENUENTRY    = 1 << 17,   LT_ECHO = 1 << 16,
95      LT_ENTRY_END    = 1 << 18,   LT_MENUENTRY = 1 << 17,
96      LT_SET_VARIABLE = 1 << 19,   LT_ENTRY_END = 1 << 18,
97      LT_UNKNOWN      = 1 << 20,   LT_SET_VARIABLE = 1 << 19,
98     LT_KERNEL_EFI = 1 << 20,
99     LT_INITRD_EFI = 1 << 21,
100     LT_KERNEL_16 = 1 << 22,
101     LT_INITRD_16 = 1 << 23,
102     LT_DEVTREE = 1 << 24,
103     LT_UNKNOWN = 1 << 25,
104  };  };
105    
106  struct singleLine {  struct singleLine {
107      char * indent;   char *indent;
108      int numElements;   int numElements;
109      struct lineElement * elements;   struct lineElement *elements;
110      struct singleLine * next;   struct singleLine *next;
111      enum lineType_e type;   enum lineType_e type;
112  };  };
113    
114  struct singleEntry {  struct singleEntry {
115      struct singleLine * lines;   struct singleLine *lines;
116      int skip;   int skip;
117      int multiboot;   int multiboot;
118      struct singleEntry * next;   struct singleEntry *next;
119  };  };
120    
121  #define GRUBBY_BADIMAGE_OKAY (1 << 0)  #define GRUBBY_BADIMAGE_OKAY (1 << 0)
# Line 109  struct singleEntry { Line 129  struct singleEntry {
129  #define NEED_ARGS    (1 << 3)  #define NEED_ARGS    (1 << 3)
130  #define NEED_MB      (1 << 4)  #define NEED_MB      (1 << 4)
131  #define NEED_END     (1 << 5)  #define NEED_END     (1 << 5)
132    #define NEED_DEVTREE (1 << 6)
133    
134  #define MAIN_DEFAULT    (1 << 0)  #define MAIN_DEFAULT    (1 << 0)
135    #define FIRST_ENTRY_INDEX    0 /* boot entry index value begin and increment
136       from this initial value */
137    #define NO_DEFAULT_ENTRY    -1 /* indicates that no specific default boot
138       entry was set or currently exists */
139  #define DEFAULT_SAVED       -2  #define DEFAULT_SAVED       -2
140    #define DEFAULT_SAVED_GRUB2 -3
141    
142  struct keywordTypes {  struct keywordTypes {
143      char * key;   char *key;
144      enum lineType_e type;   enum lineType_e type;
145      char nextChar;   char nextChar;
146      char separatorChar;   char separatorChar;
147  };  };
148    
149  struct configFileInfo;  struct configFileInfo;
150    
151  typedef const char *(*findConfigFunc)(struct configFileInfo *);  typedef const char *(*findConfigFunc) (struct configFileInfo *);
152  typedef const int (*writeLineFunc)(struct configFileInfo *,  typedef const int (*writeLineFunc) (struct configFileInfo *,
153   struct singleLine *line);      struct singleLine * line);
154    typedef char *(*getEnvFunc) (struct configFileInfo *, char *name);
155    typedef int (*setEnvFunc) (struct configFileInfo *, char *name, char *value);
156    
157  struct configFileInfo {  struct configFileInfo {
158      char * defaultConfig;   char *defaultConfig;
159      findConfigFunc findConfig;   findConfigFunc findConfig;
160      writeLineFunc writeLine;   writeLineFunc writeLine;
161      struct keywordTypes * keywords;   getEnvFunc getEnv;
162      int defaultIsIndex;   setEnvFunc setEnv;
163      int defaultIsVariable;   struct keywordTypes *keywords;
164      int defaultSupportSaved;   int caseInsensitive;
165      enum lineType_e entryStart;   int defaultIsIndex;
166      enum lineType_e entryEnd;   int defaultIsVariable;
167      int needsBootPrefix;   int defaultSupportSaved;
168      int argsInQuotes;   int defaultIsSaved;
169      int maxTitleLength;   int defaultIsUnquoted;
170      int titleBracketed;   enum lineType_e entryStart;
171      int titlePosition;   enum lineType_e entryEnd;
172      int mbHyperFirst;   int needsBootPrefix;
173      int mbInitRdIsModule;   int argsInQuotes;
174      int mbConcatArgs;   int maxTitleLength;
175      int mbAllowExtraInitRds;   int titleBracketed;
176     int titlePosition;
177     int mbHyperFirst;
178     int mbInitRdIsModule;
179     int mbConcatArgs;
180     int mbAllowExtraInitRds;
181     char *envFile;
182  };  };
183    
184  struct keywordTypes grubKeywords[] = {  struct keywordTypes grubKeywords[] = {
185      { "title",    LT_TITLE,    ' ' },   {"title", LT_TITLE, ' '},
186      { "root",    LT_BOOTROOT,    ' ' },   {"root", LT_BOOTROOT, ' '},
187      { "default",    LT_DEFAULT,    ' ' },   {"default", LT_DEFAULT, ' '},
188      { "fallback",   LT_FALLBACK,    ' ' },   {"fallback", LT_FALLBACK, ' '},
189      { "kernel",    LT_KERNEL,    ' ' },   {"kernel", LT_KERNEL, ' '},
190      { "initrd",    LT_INITRD,    ' ', ' ' },   {"initrd", LT_INITRD, ' ', ' '},
191      { "module",     LT_MBMODULE,    ' ' },   {"module", LT_MBMODULE, ' '},
192      { "kernel",     LT_HYPER,       ' ' },   {"kernel", LT_HYPER, ' '},
193      { NULL,    0, 0 },   {NULL, 0, 0},
194  };  };
195    
196  const char *grubFindConfig(struct configFileInfo *cfi) {  const char *grubFindConfig(struct configFileInfo *cfi)
197      static const char *configFiles[] = {  {
198   "/etc/grub.conf",   static const char *configFiles[] = {
199   "/boot/grub/grub.conf",   "/boot/grub/grub.conf",
200   "/boot/grub/menu.lst",   "/boot/grub/menu.lst",
201   NULL   "/etc/grub.conf",
202      };   NULL
203      static int i = -1;   };
204     static int i = -1;
205      if (i == -1) {  
206   for (i = 0; configFiles[i] != NULL; i++) {   if (i == -1) {
207      dbgPrintf("Checking \"%s\": ", configFiles[i]);   for (i = 0; configFiles[i] != NULL; i++) {
208      if (!access(configFiles[i], R_OK)) {   dbgPrintf("Checking \"%s\": ", configFiles[i]);
209   dbgPrintf("found\n");   if (!access(configFiles[i], R_OK)) {
210   return configFiles[i];   dbgPrintf("found\n");
211      }   return configFiles[i];
212      dbgPrintf("not found\n");   }
213     dbgPrintf("not found\n");
214     }
215   }   }
216      }   return configFiles[i];
     return configFiles[i];  
217  }  }
218    
219  struct configFileInfo grubConfigType = {  struct configFileInfo grubConfigType = {
220      .findConfig = grubFindConfig,   .findConfig = grubFindConfig,
221      .keywords = grubKeywords,   .keywords = grubKeywords,
222      .defaultIsIndex = 1,   .defaultIsIndex = 1,
223      .defaultSupportSaved = 1,   .defaultSupportSaved = 1,
224      .entryStart = LT_TITLE,   .entryStart = LT_TITLE,
225      .needsBootPrefix = 1,   .needsBootPrefix = 1,
226      .mbHyperFirst = 1,   .mbHyperFirst = 1,
227      .mbInitRdIsModule = 1,   .mbInitRdIsModule = 1,
228      .mbAllowExtraInitRds = 1,   .mbAllowExtraInitRds = 1,
229     .titlePosition = 1,
230  };  };
231    
232  struct keywordTypes grub2Keywords[] = {  struct keywordTypes grub2Keywords[] = {
233      { "menuentry",  LT_MENUENTRY,   ' ' },   {"menuentry", LT_MENUENTRY, ' '},
234      { "}",          LT_ENTRY_END,   ' ' },   {"}", LT_ENTRY_END, ' '},
235      { "echo",       LT_ECHO,        ' ' },   {"echo", LT_ECHO, ' '},
236      { "set",        LT_SET_VARIABLE,' ', '=' },   {"set", LT_SET_VARIABLE, ' ', '='},
237      { "root",       LT_BOOTROOT,    ' ' },   {"root", LT_BOOTROOT, ' '},
238      { "default",    LT_DEFAULT,     ' ' },   {"default", LT_DEFAULT, ' '},
239      { "fallback",   LT_FALLBACK,    ' ' },   {"fallback", LT_FALLBACK, ' '},
240      { "linux",      LT_KERNEL,      ' ' },   {"linux", LT_KERNEL, ' '},
241      { "initrd",     LT_INITRD,      ' ', ' ' },   {"linuxefi", LT_KERNEL_EFI, ' '},
242      { "module",     LT_MBMODULE,    ' ' },   {"linux16", LT_KERNEL_16, ' '},
243      { "kernel",     LT_HYPER,       ' ' },   {"initrd", LT_INITRD, ' ', ' '},
244      { NULL, 0, 0 },   {"initrdefi", LT_INITRD_EFI, ' ', ' '},
245  };   {"initrd16", LT_INITRD_16, ' ', ' '},
246     {"module", LT_MBMODULE, ' '},
247  const char *grub2FindConfig(struct configFileInfo *cfi) {   {"kernel", LT_HYPER, ' '},
248      static const char *configFiles[] = {   {"devicetree", LT_DEVTREE, ' '},
249   "/boot/grub/grub-efi.cfg",   {NULL, 0, 0},
250   "/boot/grub/grub.cfg",  };
251   NULL  
252      };  const char *grub2FindConfig(struct configFileInfo *cfi)
253      static int i = -1;  {
254      static const char *grub_cfg = "/boot/grub/grub.cfg";   static const char *configFiles[] = {
255     "/boot/grub/grub-efi.cfg",
256      if (i == -1) {   "/boot/grub/grub.cfg",
257   for (i = 0; configFiles[i] != NULL; i++) {   "/etc/grub2-efi.cfg",
258      dbgPrintf("Checking \"%s\": ", configFiles[i]);   "/etc/grub2.cfg",
259      if (!access(configFiles[i], R_OK)) {   "/boot/grub2/grub.cfg",
260     "/boot/grub2-efi/grub.cfg",
261     NULL
262     };
263     static int i = -1;
264     static const char *grub_cfg = "/boot/grub/grub.cfg";
265     int rc = -1;
266    
267     if (i == -1) {
268     for (i = 0; configFiles[i] != NULL; i++) {
269     dbgPrintf("Checking \"%s\": ", configFiles[i]);
270     if ((rc = access(configFiles[i], R_OK))) {
271     if (errno == EACCES) {
272     printf
273        ("Unable to access bootloader configuration file "
274         "\"%s\": %m\n", configFiles[i]);
275     exit(1);
276     }
277     continue;
278     } else {
279     dbgPrintf("found\n");
280     return configFiles[i];
281     }
282     }
283     }
284    
285     /* Ubuntu renames grub2 to grub, so check for the grub.d directory
286     * that isn't in grub1, and if it exists, return the config file path
287     * that they use. */
288     if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) {
289   dbgPrintf("found\n");   dbgPrintf("found\n");
290   return configFiles[i];   return grub_cfg;
     }  
291   }   }
     }  
292    
293      /* Ubuntu renames grub2 to grub, so check for the grub.d directory   dbgPrintf("not found\n");
294       * that isn't in grub1, and if it exists, return the config file path   return configFiles[i];
295       * that they use. */  }
296      if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) {  
297   dbgPrintf("found\n");  /* kind of hacky.  It'll give the first 1024 bytes, ish. */
298   return grub_cfg;  static char *grub2GetEnv(struct configFileInfo *info, char *name)
299      }  {
300     static char buf[1025];
301     char *s = NULL;
302     char *ret = NULL;
303     char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv";
304     int rc =
305        asprintf(&s, "grub-editenv %s list | grep '^%s='", envFile, name);
306    
307     if (rc < 0)
308     return NULL;
309    
310     FILE *f = popen(s, "r");
311     if (!f)
312     goto out;
313    
314     memset(buf, '\0', sizeof(buf));
315     ret = fgets(buf, 1024, f);
316     pclose(f);
317    
318     if (ret) {
319     ret += strlen(name) + 1;
320     ret[strlen(ret) - 1] = '\0';
321     }
322     dbgPrintf("grub2GetEnv(%s): %s\n", name, ret);
323    out:
324     free(s);
325     return ret;
326    }
327    
328    static int sPopCount(const char *s, const char *c)
329    {
330     int ret = 0;
331     if (!s)
332     return -1;
333     for (int i = 0; s[i] != '\0'; i++)
334     for (int j = 0; c[j] != '\0'; j++)
335     if (s[i] == c[j])
336     ret++;
337     return ret;
338    }
339    
340    static char *shellEscape(const char *s)
341    {
342     int l = strlen(s) + sPopCount(s, "'") * 2;
343    
344     char *ret = calloc(l + 1, sizeof(*ret));
345     if (!ret)
346     return NULL;
347     for (int i = 0, j = 0; s[i] != '\0'; i++, j++) {
348     if (s[i] == '\'')
349     ret[j++] = '\\';
350     ret[j] = s[i];
351     }
352     return ret;
353    }
354    
355    static void unquote(char *s)
356    {
357     int l = strlen(s);
358    
359     if ((s[l - 1] == '\'' && s[0] == '\'')
360        || (s[l - 1] == '"' && s[0] == '"')) {
361     memmove(s, s + 1, l - 2);
362     s[l - 2] = '\0';
363     }
364    }
365    
366    static int grub2SetEnv(struct configFileInfo *info, char *name, char *value)
367    {
368     char *s = NULL;
369     int rc = 0;
370     char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv";
371    
372     unquote(value);
373     value = shellEscape(value);
374     if (!value)
375     return -1;
376    
377     rc = asprintf(&s, "grub-editenv %s set '%s=%s'", envFile, name, value);
378     free(value);
379     if (rc < 0)
380     return -1;
381    
382     dbgPrintf("grub2SetEnv(%s): %s\n", name, s);
383     rc = system(s);
384     free(s);
385     return rc;
386    }
387    
388    /* this is a gigantic hack to avoid clobbering grub2 variables... */
389    static int is_special_grub2_variable(const char *name)
390    {
391     if (!strcmp(name, "\"${next_entry}\""))
392     return 1;
393     if (!strcmp(name, "\"${prev_saved_entry}\""))
394     return 1;
395     return 0;
396    }
397    
398    int sizeOfSingleLine(struct singleLine *line)
399    {
400     int count = 0;
401    
402     for (int i = 0; i < line->numElements; i++) {
403     int indentSize = 0;
404    
405     count = count + strlen(line->elements[i].item);
406    
407     indentSize = strlen(line->elements[i].indent);
408     if (indentSize > 0)
409     count = count + indentSize;
410     else
411     /* be extra safe and add room for whitespaces */
412     count = count + 1;
413     }
414    
415     /* room for trailing terminator */
416     count = count + 1;
417    
418     return count;
419    }
420    
421    static int isquote(char q)
422    {
423     if (q == '\'' || q == '\"')
424     return 1;
425     return 0;
426    }
427    
428    static int iskernel(enum lineType_e type)
429    {
430     return (type == LT_KERNEL || type == LT_KERNEL_EFI
431     || type == LT_KERNEL_16);
432    }
433    
434    static int isinitrd(enum lineType_e type)
435    {
436     return (type == LT_INITRD || type == LT_INITRD_EFI
437     || type == LT_INITRD_16);
438    }
439    
440    char *grub2ExtractTitle(struct singleLine *line)
441    {
442     char *current;
443     char *current_indent;
444     int current_len;
445     int current_indent_len;
446     int i;
447    
448     /* bail out if line does not start with menuentry */
449     if (strcmp(line->elements[0].item, "menuentry"))
450     return NULL;
451    
452     i = 1;
453     current = line->elements[i].item;
454     current_len = strlen(current);
455    
456     /* if second word is quoted, strip the quotes and return single word */
457     if (isquote(*current) && isquote(current[current_len - 1])) {
458     char *tmp;
459    
460     tmp = strdup(current + 1);
461     if (!tmp)
462     return NULL;
463     tmp[strlen(tmp) - 1] = '\0';
464     return tmp;
465     }
466    
467     /* if no quotes, return second word verbatim */
468     if (!isquote(*current))
469     return current;
470    
471     /* second element start with a quote, so we have to find the element
472     * whose last character is also quote (assuming it's the closing one) */
473     int resultMaxSize;
474     char *result;
475     /* need to ensure that ' does not match " as we search */
476     char quote_char = *current;
477    
478     resultMaxSize = sizeOfSingleLine(line);
479     result = malloc(resultMaxSize);
480     snprintf(result, resultMaxSize, "%s", ++current);
481    
482      dbgPrintf("not found\n");   i++;
483      return configFiles[i];   for (; i < line->numElements; ++i) {
484     current = line->elements[i].item;
485     current_len = strlen(current);
486     current_indent = line->elements[i].indent;
487     current_indent_len = strlen(current_indent);
488    
489     strncat(result, current_indent, current_indent_len);
490     if (current[current_len - 1] != quote_char) {
491     strncat(result, current, current_len);
492     } else {
493     strncat(result, current, current_len - 1);
494     break;
495     }
496     }
497     return result;
498  }  }
499    
500  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
501      .findConfig = grub2FindConfig,   .findConfig = grub2FindConfig,
502      .keywords = grub2Keywords,   .getEnv = grub2GetEnv,
503      .defaultIsIndex = 1,   .setEnv = grub2SetEnv,
504      .defaultSupportSaved = 0,   .keywords = grub2Keywords,
505      .defaultIsVariable = 1,   .defaultIsIndex = 1,
506      .entryStart = LT_MENUENTRY,   .defaultSupportSaved = 1,
507      .entryEnd = LT_ENTRY_END,   .defaultIsVariable = 1,
508      .titlePosition = 1,   .entryStart = LT_MENUENTRY,
509      .needsBootPrefix = 1,   .entryEnd = LT_ENTRY_END,
510      .mbHyperFirst = 1,   .titlePosition = 1,
511      .mbInitRdIsModule = 1,   .needsBootPrefix = 1,
512      .mbAllowExtraInitRds = 1,   .mbHyperFirst = 1,
513     .mbInitRdIsModule = 1,
514     .mbAllowExtraInitRds = 1,
515  };  };
516    
517  struct keywordTypes yabootKeywords[] = {  struct keywordTypes yabootKeywords[] = {
518      { "label",    LT_TITLE,    '=' },   {"label", LT_TITLE, '='},
519      { "root",    LT_ROOT,    '=' },   {"root", LT_ROOT, '='},
520      { "default",    LT_DEFAULT,    '=' },   {"default", LT_DEFAULT, '='},
521      { "image",    LT_KERNEL,    '=' },   {"image", LT_KERNEL, '='},
522      { "bsd",    LT_GENERIC,    '=' },   {"bsd", LT_GENERIC, '='},
523      { "macos",    LT_GENERIC,    '=' },   {"macos", LT_GENERIC, '='},
524      { "macosx",    LT_GENERIC,    '=' },   {"macosx", LT_GENERIC, '='},
525      { "magicboot",  LT_GENERIC,    '=' },   {"magicboot", LT_GENERIC, '='},
526      { "darwin",    LT_GENERIC,    '=' },   {"darwin", LT_GENERIC, '='},
527      { "timeout",    LT_GENERIC,    '=' },   {"timeout", LT_GENERIC, '='},
528      { "install",    LT_GENERIC,    '=' },   {"install", LT_GENERIC, '='},
529      { "fstype",    LT_GENERIC,    '=' },   {"fstype", LT_GENERIC, '='},
530      { "hfstype",    LT_GENERIC,    '=' },   {"hfstype", LT_GENERIC, '='},
531      { "delay",    LT_GENERIC,    '=' },   {"delay", LT_GENERIC, '='},
532      { "defaultos",  LT_GENERIC,     '=' },   {"defaultos", LT_GENERIC, '='},
533      { "init-message", LT_GENERIC,   '=' },   {"init-message", LT_GENERIC, '='},
534      { "enablecdboot", LT_GENERIC,   ' ' },   {"enablecdboot", LT_GENERIC, ' '},
535      { "enableofboot", LT_GENERIC,   ' ' },   {"enableofboot", LT_GENERIC, ' '},
536      { "enablenetboot", LT_GENERIC,  ' ' },   {"enablenetboot", LT_GENERIC, ' '},
537      { "nonvram",    LT_GENERIC,    ' ' },   {"nonvram", LT_GENERIC, ' '},
538      { "hide",    LT_GENERIC,    ' ' },   {"hide", LT_GENERIC, ' '},
539      { "protect",    LT_GENERIC,    ' ' },   {"protect", LT_GENERIC, ' '},
540      { "nobless",    LT_GENERIC,    ' ' },   {"nobless", LT_GENERIC, ' '},
541      { "nonvram",    LT_GENERIC,    ' ' },   {"nonvram", LT_GENERIC, ' '},
542      { "brokenosx",  LT_GENERIC,    ' ' },   {"brokenosx", LT_GENERIC, ' '},
543      { "usemount",   LT_GENERIC,    ' ' },   {"usemount", LT_GENERIC, ' '},
544      { "mntpoint",   LT_GENERIC,    '=' },   {"mntpoint", LT_GENERIC, '='},
545      { "partition",  LT_GENERIC,    '=' },   {"partition", LT_GENERIC, '='},
546      { "device",    LT_GENERIC,    '=' },   {"device", LT_GENERIC, '='},
547      { "fstype",    LT_GENERIC,    '=' },   {"fstype", LT_GENERIC, '='},
548      { "initrd",    LT_INITRD,    '=', ';' },   {"initrd", LT_INITRD, '=', ';'},
549      { "append",    LT_KERNELARGS,  '=' },   {"append", LT_KERNELARGS, '='},
550      { "boot",    LT_BOOT,    '=' },   {"boot", LT_BOOT, '='},
551      { "lba",    LT_LBA,    ' ' },   {"lba", LT_LBA, ' '},
552      { NULL,    0, 0 },   {NULL, 0, 0},
553  };  };
554    
555  struct keywordTypes liloKeywords[] = {  struct keywordTypes liloKeywords[] = {
556      { "label",    LT_TITLE,    '=' },   {"label", LT_TITLE, '='},
557      { "root",    LT_ROOT,    '=' },   {"root", LT_ROOT, '='},
558      { "default",    LT_DEFAULT,    '=' },   {"default", LT_DEFAULT, '='},
559      { "image",    LT_KERNEL,    '=' },   {"image", LT_KERNEL, '='},
560      { "other",    LT_OTHER,    '=' },   {"other", LT_OTHER, '='},
561      { "initrd",    LT_INITRD,    '=' },   {"initrd", LT_INITRD, '='},
562      { "append",    LT_KERNELARGS,  '=' },   {"append", LT_KERNELARGS, '='},
563      { "boot",    LT_BOOT,    '=' },   {"boot", LT_BOOT, '='},
564      { "lba",    LT_LBA,    ' ' },   {"lba", LT_LBA, ' '},
565      { NULL,    0, 0 },   {NULL, 0, 0},
566  };  };
567    
568  struct keywordTypes eliloKeywords[] = {  struct keywordTypes eliloKeywords[] = {
569      { "label",    LT_TITLE,    '=' },   {"label", LT_TITLE, '='},
570      { "root",    LT_ROOT,    '=' },   {"root", LT_ROOT, '='},
571      { "default",    LT_DEFAULT,    '=' },   {"default", LT_DEFAULT, '='},
572      { "image",    LT_KERNEL,    '=' },   {"image", LT_KERNEL, '='},
573      { "initrd",    LT_INITRD,    '=' },   {"initrd", LT_INITRD, '='},
574      { "append",    LT_KERNELARGS,  '=' },   {"append", LT_KERNELARGS, '='},
575      { "vmm",    LT_HYPER,       '=' },   {"vmm", LT_HYPER, '='},
576      { NULL,    0, 0 },   {NULL, 0, 0},
577  };  };
578    
579  struct keywordTypes siloKeywords[] = {  struct keywordTypes siloKeywords[] = {
580      { "label",    LT_TITLE,    '=' },   {"label", LT_TITLE, '='},
581      { "root",    LT_ROOT,    '=' },   {"root", LT_ROOT, '='},
582      { "default",    LT_DEFAULT,    '=' },   {"default", LT_DEFAULT, '='},
583      { "image",    LT_KERNEL,    '=' },   {"image", LT_KERNEL, '='},
584      { "other",    LT_OTHER,    '=' },   {"other", LT_OTHER, '='},
585      { "initrd",    LT_INITRD,    '=' },   {"initrd", LT_INITRD, '='},
586      { "append",    LT_KERNELARGS,  '=' },   {"append", LT_KERNELARGS, '='},
587      { "boot",    LT_BOOT,    '=' },   {"boot", LT_BOOT, '='},
588      { NULL,    0, 0 },   {NULL, 0, 0},
589  };  };
590    
591  struct keywordTypes ziplKeywords[] = {  struct keywordTypes ziplKeywords[] = {
592      { "target",     LT_BOOTROOT,    '=' },   {"target", LT_BOOTROOT, '='},
593      { "image",      LT_KERNEL,      '=' },   {"image", LT_KERNEL, '='},
594      { "ramdisk",    LT_INITRD,      '=' },   {"ramdisk", LT_INITRD, '='},
595      { "parameters", LT_KERNELARGS,  '=' },   {"parameters", LT_KERNELARGS, '='},
596      { "default",    LT_DEFAULT,     '=' },   {"default", LT_DEFAULT, '='},
597      { NULL,         0, 0 },   {NULL, 0, 0},
598  };  };
599    
600  struct keywordTypes extlinuxKeywords[] = {  struct keywordTypes extlinuxKeywords[] = {
601      { "label",    LT_TITLE,    ' ' },   {"label", LT_TITLE, ' '},
602      { "root",    LT_ROOT,    ' ' },   {"root", LT_ROOT, ' '},
603      { "default",    LT_DEFAULT,    ' ' },   {"default", LT_DEFAULT, ' '},
604      { "kernel",    LT_KERNEL,    ' ' },   {"kernel", LT_KERNEL, ' '},
605      { "initrd",    LT_INITRD,      ' ', ',' },   {"initrd", LT_INITRD, ' ', ','},
606      { "append",    LT_KERNELARGS,  ' ' },   {"append", LT_KERNELARGS, ' '},
607      { "prompt",     LT_UNKNOWN,     ' ' },   {"prompt", LT_UNKNOWN, ' '},
608      { NULL,    0, 0 },   {"fdt", LT_DEVTREE, ' '},
609     {"fdtdir", LT_DEVTREE, ' '},
610     {NULL, 0, 0},
611  };  };
612    
613  int useextlinuxmenu;  int useextlinuxmenu;
614  struct configFileInfo eliloConfigType = {  struct configFileInfo eliloConfigType = {
615      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",   .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",
616      .keywords = eliloKeywords,   .keywords = eliloKeywords,
617      .entryStart = LT_KERNEL,   .entryStart = LT_KERNEL,
618      .needsBootPrefix = 1,   .needsBootPrefix = 1,
619      .argsInQuotes = 1,   .argsInQuotes = 1,
620      .mbConcatArgs = 1,   .mbConcatArgs = 1,
621     .titlePosition = 1,
622  };  };
623    
624  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
625      .defaultConfig = "/etc/lilo.conf",   .defaultConfig = "/etc/lilo.conf",
626      .keywords = liloKeywords,   .keywords = liloKeywords,
627      .entryStart = LT_KERNEL,   .entryStart = LT_KERNEL,
628      .argsInQuotes = 1,   .argsInQuotes = 1,
629      .maxTitleLength = 15,   .maxTitleLength = 15,
630     .titlePosition = 1,
631  };  };
632    
633  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
634      .defaultConfig = "/etc/yaboot.conf",   .defaultConfig = "/etc/yaboot.conf",
635      .keywords = yabootKeywords,   .keywords = yabootKeywords,
636      .entryStart = LT_KERNEL,   .entryStart = LT_KERNEL,
637      .needsBootPrefix = 1,   .needsBootPrefix = 1,
638      .argsInQuotes = 1,   .argsInQuotes = 1,
639      .maxTitleLength = 15,   .maxTitleLength = 15,
640      .mbAllowExtraInitRds = 1,   .mbAllowExtraInitRds = 1,
641     .titlePosition = 1,
642  };  };
643    
644  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
645      .defaultConfig = "/etc/silo.conf",   .defaultConfig = "/etc/silo.conf",
646      .keywords = siloKeywords,   .keywords = siloKeywords,
647      .entryStart = LT_KERNEL,   .entryStart = LT_KERNEL,
648      .needsBootPrefix = 1,   .needsBootPrefix = 1,
649      .argsInQuotes = 1,   .argsInQuotes = 1,
650      .maxTitleLength = 15,   .maxTitleLength = 15,
651     .titlePosition = 1,
652  };  };
653    
654  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
655      .defaultConfig = "/etc/zipl.conf",   .defaultConfig = "/etc/zipl.conf",
656      .keywords = ziplKeywords,   .keywords = ziplKeywords,
657      .entryStart = LT_TITLE,   .entryStart = LT_TITLE,
658      .argsInQuotes = 1,   .argsInQuotes = 1,
659      .titleBracketed = 1,   .titleBracketed = 1,
660  };  };
661    
662  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
663      .defaultConfig = "/boot/extlinux/extlinux.conf",   .defaultConfig = "/boot/extlinux/extlinux.conf",
664      .keywords = extlinuxKeywords,   .keywords = extlinuxKeywords,
665      .entryStart = LT_TITLE,   .caseInsensitive = 1,
666      .needsBootPrefix = 1,   .entryStart = LT_TITLE,
667      .maxTitleLength = 255,   .needsBootPrefix = 1,
668      .mbAllowExtraInitRds = 1,   .maxTitleLength = 255,
669     .mbAllowExtraInitRds = 1,
670     .defaultIsUnquoted = 1,
671     .titlePosition = 1,
672  };  };
673    
674  struct grubConfig {  struct grubConfig {
675      struct singleLine * theLines;   struct singleLine *theLines;
676      struct singleEntry * entries;   struct singleEntry *entries;
677      char * primaryIndent;   char *primaryIndent;
678      char * secondaryIndent;   char *secondaryIndent;
679      int defaultImage;    /* -1 if none specified -- this value is   int defaultImage; /* -1 if none specified -- this value is
680       * written out, overriding original */   * written out, overriding original */
681      int fallbackImage;    /* just like defaultImage */   int fallbackImage; /* just like defaultImage */
682      int flags;   int flags;
683      struct configFileInfo * cfi;   struct configFileInfo *cfi;
684     int isModified; /* assumes only one entry added
685       per invocation of grubby */
686  };  };
687    
688  blkid_cache blkid;  blkid_cache blkid;
689    
690  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);  struct singleEntry *findEntryByIndex(struct grubConfig *cfg, int index);
691  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry *findEntryByPath(struct grubConfig *cfg,
692       const char * path, const char * prefix,      const char *path, const char *prefix,
693       int * index);      int *index);
694  static int readFile(int fd, char ** bufPtr);  struct singleEntry *findEntryByTitle(struct grubConfig *cfg, char *title,
695  static void lineInit(struct singleLine * line);       int *index);
696  struct singleLine * lineDup(struct singleLine * line);  static int readFile(int fd, char **bufPtr);
697  static void lineFree(struct singleLine * line);  static void lineInit(struct singleLine *line);
698  static int lineWrite(FILE * out, struct singleLine * line,  struct singleLine *lineDup(struct singleLine *line);
699       struct configFileInfo * cfi);  static void lineFree(struct singleLine *line);
700  static int getNextLine(char ** bufPtr, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine *line,
701         struct configFileInfo * cfi);       struct configFileInfo *cfi);
702  static char * getRootSpecifier(char * str);  static int getNextLine(char **bufPtr, struct singleLine *line,
703  static void requote(struct singleLine *line, struct configFileInfo * cfi);         struct configFileInfo *cfi);
704  static void insertElement(struct singleLine * line,  static char *getRootSpecifier(char *str);
705    const char * item, int insertHere,  static void requote(struct singleLine *line, struct configFileInfo *cfi);
706    struct configFileInfo * cfi);  static void insertElement(struct singleLine *line,
707  static void removeElement(struct singleLine * line, int removeHere);    const char *item, int insertHere,
708  static struct keywordTypes * getKeywordByType(enum lineType_e type,    struct configFileInfo *cfi);
709        struct configFileInfo * cfi);  static void removeElement(struct singleLine *line, int removeHere);
710  static enum lineType_e getTypeByKeyword(char * keyword,  static struct keywordTypes *getKeywordByType(enum lineType_e type,
711   struct configFileInfo * cfi);       struct configFileInfo *cfi);
712  static struct singleLine * getLineByType(enum lineType_e type,  static enum lineType_e getTypeByKeyword(char *keyword,
713   struct singleLine * line);   struct configFileInfo *cfi);
714  static int checkForExtLinux(struct grubConfig * config);  static struct singleLine *getLineByType(enum lineType_e type,
715  struct singleLine * addLineTmpl(struct singleEntry * entry,   struct singleLine *line);
716                                  struct singleLine * tmplLine,  static int checkForExtLinux(struct grubConfig *config);
717                                  struct singleLine * prevLine,  struct singleLine *addLineTmpl(struct singleEntry *entry,
718                                  const char * val,         struct singleLine *tmplLine,
719   struct configFileInfo * cfi);         struct singleLine *prevLine,
720  struct singleLine *  addLine(struct singleEntry * entry,         const char *val, struct configFileInfo *cfi);
721                               struct configFileInfo * cfi,  struct singleLine *addLine(struct singleEntry *entry,
722                               enum lineType_e type, char * defaultIndent,     struct configFileInfo *cfi,
723                               const char * val);     enum lineType_e type, char *defaultIndent,
724       const char *val);
725    
726  static char * sdupprintf(const char *format, ...)  static char *sdupprintf(const char *format, ...)
727  #ifdef __GNUC__  #ifdef __GNUC__
728          __attribute__ ((format (printf, 1, 2)));      __attribute__ ((format(printf, 1, 2)));
729  #else  #else
730          ;  ;
731  #endif  #endif
732    
733  static char * sdupprintf(const char *format, ...) {  static char *sdupprintf(const char *format, ...)
734      char *buf = NULL;  {
735      char c;   char *buf = NULL;
736      va_list args;   char c;
737      size_t size = 0;   va_list args;
738      va_start(args, format);   size_t size = 0;
739         va_start(args, format);
740      /* XXX requires C99 vsnprintf behavior */  
741      size = vsnprintf(&c, 1, format, args) + 1;   /* XXX requires C99 vsnprintf behavior */
742      if (size == -1) {   size = vsnprintf(&c, 1, format, args) + 1;
743   printf("ERROR: vsnprintf behavior is not C99\n");   if (size == -1) {
744   abort();   printf("ERROR: vsnprintf behavior is not C99\n");
745      }   abort();
746     }
747    
748      va_end(args);   va_end(args);
749      va_start(args, format);   va_start(args, format);
750    
751      buf = malloc(size);   buf = malloc(size);
752      if (buf == NULL)   if (buf == NULL)
753   return NULL;   return NULL;
754      vsnprintf(buf, size, format, args);   vsnprintf(buf, size, format, args);
755      va_end (args);   va_end(args);
756    
757      return buf;   return buf;
758  }  }
759    
760  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static inline int
761        struct configFileInfo * cfi) {  kwcmp(struct keywordTypes *kw, const char * label, int case_insensitive)
762      struct keywordTypes * kw;  {
763      for (kw = cfi->keywords; kw->key; kw++) {      int kwl = strlen(kw->key);
764   if (kw->type == type)      int ll = strlen(label);
765      return kw;      int rc;
766        int (*snc)(const char *s1, const char *s2, size_t n) =
767               case_insensitive ? strncasecmp : strncmp;
768        int (*sc)(const char *s1, const char *s2) =
769               case_insensitive ? strcasecmp : strcmp;
770    
771        rc = snc(kw->key, label, kwl);
772        if (rc)
773           return rc;
774    
775        for (int i = kwl; i < ll; i++) {
776           if (isspace(label[i]))
777               return 0;
778           if (kw->separatorChar && label[i] == kw->separatorChar)
779               return 0;
780           else if (kw->nextChar && label[i] == kw->nextChar)
781               return 0;
782           return sc(kw->key+kwl, label+kwl);
783      }      }
784      return NULL;      return 0;
785    }
786    
787    static enum lineType_e preferredLineType(enum lineType_e type,
788     struct configFileInfo *cfi)
789    {
790     if (isEfi && cfi == &grub2ConfigType) {
791     switch (type) {
792     case LT_KERNEL:
793     return isEfiOnly ? LT_KERNEL : LT_KERNEL_EFI;
794     case LT_INITRD:
795     return isEfiOnly ? LT_INITRD : LT_INITRD_EFI;
796     default:
797     return type;
798     }
799    #if defined(__i386__) || defined(__x86_64__)
800     } else if (cfi == &grub2ConfigType) {
801     switch (type) {
802     case LT_KERNEL:
803     return LT_KERNEL_16;
804     case LT_INITRD:
805     return LT_INITRD_16;
806     default:
807     return type;
808     }
809    #endif
810     }
811     return type;
812    }
813    
814    static struct keywordTypes *getKeywordByType(enum lineType_e type,
815         struct configFileInfo *cfi)
816    {
817     for (struct keywordTypes * kw = cfi->keywords; kw->key; kw++) {
818     if (kw->type == type)
819     return kw;
820     }
821     return NULL;
822  }  }
823    
824  static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) {  static char *getKeyByType(enum lineType_e type, struct configFileInfo *cfi)
825      struct keywordTypes *kt = getKeywordByType(type, cfi);  {
826      if (kt)   struct keywordTypes *kt = getKeywordByType(type, cfi);
827   return kt->key;   if (kt)
828      return "unknown";   return kt->key;
829     return "unknown";
830  }  }
831    
832  static char * getpathbyspec(char *device) {  static char *getpathbyspec(char *device)
833      if (!blkid)  {
834          blkid_get_cache(&blkid, NULL);   if (!blkid)
835     blkid_get_cache(&blkid, NULL);
836    
837      return blkid_get_devname(blkid, device, NULL);   return blkid_get_devname(blkid, device, NULL);
838  }  }
839    
840  static char * getuuidbydev(char *device) {  static char *getuuidbydev(char *device)
841      if (!blkid)  {
842   blkid_get_cache(&blkid, NULL);   if (!blkid)
843     blkid_get_cache(&blkid, NULL);
844    
845      return blkid_get_tag_value(blkid, "UUID", device);   return blkid_get_tag_value(blkid, "UUID", device);
846  }  }
847    
848  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char *keyword,
849   struct configFileInfo * cfi) {   struct configFileInfo *cfi)
850      struct keywordTypes * kw;  {
851      for (kw = cfi->keywords; kw->key; kw++) {   for (struct keywordTypes * kw = cfi->keywords; kw->key; kw++) {
852   if (!strcmp(keyword, kw->key))   if (!kwcmp(kw, keyword, cfi->caseInsensitive))
853      return kw->type;   return kw->type;
854      }   }
855      return LT_UNKNOWN;   return LT_UNKNOWN;
856  }  }
857    
858  static struct singleLine * getLineByType(enum lineType_e type,  static struct singleLine *getLineByType(enum lineType_e type,
859   struct singleLine * line) {   struct singleLine *line)
860      dbgPrintf("getLineByType(%d): ", type);  {
861      for (; line; line = line->next) {   dbgPrintf("getLineByType(%d): ", type);
862   dbgPrintf("%d:%s ", line->type,   for (; line; line = line->next) {
863    line->numElements ? line->elements[0].item : "(empty)");   dbgPrintf("%d:%s ", line->type,
864   if (line->type & type) break;    line->numElements ? line->elements[0].
865      }    item : "(empty)");
866      dbgPrintf(line ? "\n" : " (failed)\n");   if (line->type & type)
867      return line;   break;
868     }
869     dbgPrintf(line ? "\n" : " (failed)\n");
870     return line;
871  }  }
872    
873  static int isBracketedTitle(struct singleLine * line) {  static int isBracketedTitle(struct singleLine *line)
874      if (line->numElements == 1 && *line->elements[0].item == '[') {  {
875          int len = strlen(line->elements[0].item);   if (line->numElements == 1 && *line->elements[0].item == '[') {
876          if (*(line->elements[0].item + len - 1) == ']') {   int len = strlen(line->elements[0].item);
877              /* FIXME: this is a hack... */   if (*(line->elements[0].item + len - 1) == ']') {
878              if (strcmp(line->elements[0].item, "[defaultboot]")) {   /* FIXME: this is a hack... */
879                  return 1;   if (strcmp(line->elements[0].item, "[defaultboot]")) {
880              }   return 1;
881          }   }
882      }   }
883      return 0;   }
884     return 0;
885  }  }
886    
887  static int isEntryStart(struct singleLine * line,  static int isEntryStart(struct singleLine *line, struct configFileInfo *cfi)
888                              struct configFileInfo * cfi) {  {
889      return line->type == cfi->entryStart || line->type == LT_OTHER ||   return line->type == cfi->entryStart || line->type == LT_OTHER ||
890   (cfi->titleBracketed && isBracketedTitle(line));      (cfi->titleBracketed && isBracketedTitle(line));
891  }  }
892    
893  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
894  static char * extractTitle(struct singleLine * line) {  static char *extractTitle(struct grubConfig *cfg, struct singleLine *line)
895      /* bracketed title... let's extract it (leaks a byte) */  {
896      char * title;   /* bracketed title... let's extract it */
897      title = strdup(line->elements[0].item);   char *title = NULL;
898      title++;   if (line->type == LT_TITLE) {
899      *(title + strlen(title) - 1) = '\0';   char *tmp = line->elements[cfg->cfi->titlePosition].item;
900      return title;   if (cfg->cfi->titleBracketed) {
901  }   tmp++;
902     title = strdup(tmp);
903  static int readFile(int fd, char ** bufPtr) {   *(title + strlen(title) - 1) = '\0';
904      int alloced = 0, size = 0, i = 0;   } else {
905      char * buf = NULL;   title = strdup(tmp);
906     }
907      do {   } else if (line->type == LT_MENUENTRY)
908   size += i;   title = strdup(line->elements[1].item);
909   if ((size + 1024) > alloced) {   else
910      alloced += 4096;   return NULL;
911      buf = realloc(buf, alloced + 1);   return title;
  }  
     } while ((i = read(fd, buf + size, 1024)) > 0);  
   
     if (i < 0) {  
  fprintf(stderr, _("error reading input: %s\n"), strerror(errno));  
         free(buf);  
  return 1;  
     }  
   
     buf = realloc(buf, size + 2);  
     if (size == 0)  
         buf[size++] = '\n';  
     else  
         if (buf[size - 1] != '\n')  
             buf[size++] = '\n';  
     buf[size] = '\0';  
   
     *bufPtr = buf;  
   
     return 0;  
912  }  }
913    
914  static void lineInit(struct singleLine * line) {  static int readFile(int fd, char **bufPtr)
915      line->indent = NULL;  {
916      line->elements = NULL;   int alloced = 0, size = 0, i = 0;
917      line->numElements = 0;   char *buf = NULL;
     line->next = NULL;  
 }  
   
 struct singleLine * lineDup(struct singleLine * line) {  
     int i;  
     struct singleLine * newLine = malloc(sizeof(*newLine));  
   
     newLine->indent = strdup(line->indent);  
     newLine->next = NULL;  
     newLine->type = line->type;  
     newLine->numElements = line->numElements;  
     newLine->elements = malloc(sizeof(*newLine->elements) *  
        newLine->numElements);  
   
     for (i = 0; i < newLine->numElements; i++) {  
  newLine->elements[i].indent = strdup(line->elements[i].indent);  
  newLine->elements[i].item = strdup(line->elements[i].item);  
     }  
918    
919      return newLine;   do {
920  }   size += i;
921     if ((size + 1024) > alloced) {
922     alloced += 4096;
923     buf = realloc(buf, alloced + 1);
924     }
925     } while ((i = read(fd, buf + size, 1024)) > 0);
926    
927  static void lineFree(struct singleLine * line) {   if (i < 0) {
928      int i;   fprintf(stderr, _("error reading input: %s\n"),
929     strerror(errno));
930     free(buf);
931     return 1;
932     }
933    
934      if (line->indent) free(line->indent);   buf = realloc(buf, size + 2);
935     if (size == 0)
936     buf[size++] = '\n';
937     else if (buf[size - 1] != '\n')
938     buf[size++] = '\n';
939     buf[size] = '\0';
940    
941      for (i = 0; i < line->numElements; i++) {   *bufPtr = buf;
942   free(line->elements[i].item);  
943   free(line->elements[i].indent);   return 0;
944      }  }
945    
946      if (line->elements) free(line->elements);  static void lineInit(struct singleLine *line)
947      lineInit(line);  {
948     line->type = LT_UNIDENTIFIED;
949     line->indent = NULL;
950     line->elements = NULL;
951     line->numElements = 0;
952     line->next = NULL;
953  }  }
954    
955  static int lineWrite(FILE * out, struct singleLine * line,  struct singleLine *lineDup(struct singleLine *line)
956       struct configFileInfo * cfi) {  {
957      int i;   struct singleLine *newLine = malloc(sizeof(*newLine));
958    
959      if (fprintf(out, "%s", line->indent) == -1) return -1;   newLine->indent = strdup(line->indent);
960     newLine->next = NULL;
961     newLine->type = line->type;
962     newLine->numElements = line->numElements;
963     newLine->elements = malloc(sizeof(*newLine->elements) *
964       newLine->numElements);
965    
966      for (i = 0; i < line->numElements; i++) {   for (int i = 0; i < newLine->numElements; i++) {
967   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)   newLine->elements[i].indent = strdup(line->elements[i].indent);
968      if (fputc('"', out) == EOF) return -1;   newLine->elements[i].item = strdup(line->elements[i].item);
969     }
970    
971   if (fprintf(out, "%s", line->elements[i].item) == -1) return -1;   return newLine;
972   if (i < line->numElements - 1)  }
     if (fprintf(out, "%s", line->elements[i].indent) == -1) return -1;  
     }  
973    
974      if (line->type == LT_KERNELARGS && cfi->argsInQuotes)  static void lineFree(struct singleLine *line)
975   if (fputc('"', out) == EOF) return -1;  {
976     if (line->indent)
977     free(line->indent);
978    
979      if (fprintf(out, "\n") == -1) return -1;   for (int i = 0; i < line->numElements; i++) {
980     free(line->elements[i].item);
981     free(line->elements[i].indent);
982     }
983    
984      return 0;   if (line->elements)
985     free(line->elements);
986     lineInit(line);
987  }  }
988    
989  /* we've guaranteed that the buffer ends w/ \n\0 */  static int lineWrite(FILE * out, struct singleLine *line,
990  static int getNextLine(char ** bufPtr, struct singleLine * line,       struct configFileInfo *cfi)
991                         struct configFileInfo * cfi) {  {
992      char * end;   if (fprintf(out, "%s", line->indent) == -1)
993      char * start = *bufPtr;   return -1;
     char * chptr;  
     int elementsAlloced = 0;  
     struct lineElement * element;  
     int first = 1;  
994    
995      lineFree(line);   for (int i = 0; i < line->numElements; i++) {
996     /* Need to handle this, because we strip the quotes from
997     * menuentry when read it. */
998     if (line->type == LT_MENUENTRY && i == 1) {
999     if (!isquote(*line->elements[i].item)) {
1000     int substring = 0;
1001     /* If the line contains nested quotes, we did
1002     * not strip the "interna" quotes and we must
1003     * use the right quotes again when writing
1004     * the updated file. */
1005     for (int j = i; j < line->numElements; j++) {
1006     if (strchr(line->elements[i].item, '\'')
1007        != NULL) {
1008     substring = 1;
1009     fprintf(out, "\"%s\"",
1010     line->elements[i].item);
1011     break;
1012     }
1013     }
1014     if (!substring)
1015     fprintf(out, "\'%s\'",
1016     line->elements[i].item);
1017     } else {
1018     fprintf(out, "%s", line->elements[i].item);
1019     }
1020     fprintf(out, "%s", line->elements[i].indent);
1021    
1022      end = strchr(start, '\n');   continue;
1023      *end = '\0';   }
     *bufPtr = end + 1;  
1024    
1025      for (chptr = start; *chptr && isspace(*chptr); chptr++) ;   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)
1026     if (fputc('"', out) == EOF)
1027     return -1;
1028    
1029     if (fprintf(out, "%s", line->elements[i].item) == -1)
1030     return -1;
1031     if (i < line->numElements - 1 || line->type == LT_SET_VARIABLE)
1032     if (fprintf(out, "%s", line->elements[i].indent) == -1)
1033     return -1;
1034     }
1035    
1036     if (line->type == LT_KERNELARGS && cfi->argsInQuotes)
1037     if (fputc('"', out) == EOF)
1038     return -1;
1039    
1040      line->indent = strndup(start, chptr - start);   if (fprintf(out, "\n") == -1)
1041      start = chptr;   return -1;
1042    
1043      while (start < end) {   return 0;
1044   /* we know !isspace(*start) */  }
1045    
1046   if (elementsAlloced == line->numElements) {  /* we've guaranteed that the buffer ends w/ \n\0 */
1047      elementsAlloced += 5;  static int getNextLine(char **bufPtr, struct singleLine *line,
1048      line->elements = realloc(line->elements,         struct configFileInfo *cfi)
1049   sizeof(*line->elements) * elementsAlloced);  {
1050   }   char *end;
1051     char *start = *bufPtr;
1052     char *chptr;
1053     int elementsAlloced = 0;
1054     struct lineElement *element;
1055     int first = 1;
1056    
1057   element = line->elements + line->numElements;   lineFree(line);
1058    
1059   chptr = start;   end = strchr(start, '\n');
1060   while (*chptr && !isspace(*chptr)) {   *end = '\0';
1061      if (first && *chptr == '=') break;   *bufPtr = end + 1;
     chptr++;  
  }  
  element->item = strndup(start, chptr - start);  
  start = chptr;  
1062    
1063          /* lilo actually accepts the pathological case of append = " foo " */   for (chptr = start; *chptr && isspace(*chptr); chptr++) ;
         if (*start == '=')  
             chptr = start + 1;  
         else  
             chptr = start;  
   
         do {  
             for (; *chptr && isspace(*chptr); chptr++);  
             if (*chptr == '=')  
                 chptr = chptr + 1;  
         } while (isspace(*chptr));  
1064    
1065   element->indent = strndup(start, chptr - start);   line->indent = strndup(start, chptr - start);
1066   start = chptr;   start = chptr;
1067    
1068   line->numElements++;   while (start < end) {
1069   first = 0;   /* we know !isspace(*start) */
     }  
1070    
1071      if (!line->numElements)   if (elementsAlloced == line->numElements) {
1072   line->type = LT_WHITESPACE;   elementsAlloced += 5;
1073      else {   line->elements = realloc(line->elements,
1074   line->type = getTypeByKeyword(line->elements[0].item, cfi);   sizeof(*line->elements) *
1075   if (line->type == LT_UNKNOWN) {   elementsAlloced);
1076              /* zipl does [title] instead of something reasonable like all   }
              * the other boot loaders.  kind of ugly */  
             if (cfi->titleBracketed && isBracketedTitle(line)) {  
                 line->type = LT_TITLE;  
             }  
   
     /* this is awkward, but we need to be able to handle keywords  
        that begin with a # (specifically for #boot in grub.conf),  
        but still make comments lines with no elements (everything  
        stored in the indent */  
     if (*line->elements[0].item == '#') {  
  char * fullLine;  
  int len;  
  int i;  
   
  len = strlen(line->indent);  
  for (i = 0; i < line->numElements; i++)  
     len += strlen(line->elements[i].item) +  
    strlen(line->elements[i].indent);  
1077    
1078   fullLine = malloc(len + 1);   element = line->elements + line->numElements;
  strcpy(fullLine, line->indent);  
  free(line->indent);  
  line->indent = fullLine;  
1079    
1080   for (i = 0; i < line->numElements; i++) {   chptr = start;
1081      strcat(fullLine, line->elements[i].item);   while (*chptr && !isspace(*chptr)) {
1082      strcat(fullLine, line->elements[i].indent);   if (first && *chptr == '=')
1083      free(line->elements[i].item);   break;
1084      free(line->elements[i].indent);   chptr++;
1085   }   }
1086     if (line->type == LT_UNIDENTIFIED)
1087     line->type = getTypeByKeyword(start, cfi);
1088     element->item = strndup(start, chptr - start);
1089     start = chptr;
1090    
1091     /* lilo actually accepts the pathological case of
1092     * append = " foo " */
1093     if (*start == '=')
1094     chptr = start + 1;
1095     else
1096     chptr = start;
1097    
1098     do {
1099     for (; *chptr && isspace(*chptr); chptr++) ;
1100     if (*chptr == '=')
1101     chptr = chptr + 1;
1102     } while (isspace(*chptr));
1103    
1104     element->indent = strndup(start, chptr - start);
1105     start = chptr;
1106    
1107     line->numElements++;
1108     first = 0;
1109     }
1110    
1111     if (!line->numElements)
1112   line->type = LT_WHITESPACE;   line->type = LT_WHITESPACE;
1113   line->numElements = 0;   else {
1114      }   line->type = getTypeByKeyword(line->elements[0].item, cfi);
1115   } else {   if (line->type == LT_UNKNOWN) {
1116   struct keywordTypes *kw;   /* zipl does [title] instead of something reasonable
1117     * like all the other boot loaders.  kind of ugly */
1118     if (cfi->titleBracketed && isBracketedTitle(line)) {
1119     line->type = LT_TITLE;
1120     }
1121    
1122   kw = getKeywordByType(line->type, cfi);   /* this is awkward, but we need to be able to handle
1123     * keywords that begin with a # (specifically for
1124     * #boot in grub.conf), but still make comments lines
1125     * with no elements (everything stored in the indent
1126     */
1127     if (*line->elements[0].item == '#') {
1128     char *fullLine;
1129     int len;
1130    
1131     len = strlen(line->indent);
1132     for (int i = 0; i < line->numElements; i++)
1133     len += strlen(line->elements[i].item) +
1134        strlen(line->elements[i].indent);
1135    
1136     fullLine = malloc(len + 1);
1137     strcpy(fullLine, line->indent);
1138     free(line->indent);
1139     line->indent = fullLine;
1140    
1141     for (int i = 0; i < line->numElements; i++) {
1142     strcat(fullLine,
1143           line->elements[i].item);
1144     strcat(fullLine,
1145           line->elements[i].indent);
1146     free(line->elements[i].item);
1147     free(line->elements[i].indent);
1148     }
1149    
1150   /* space isn't the only separator, we need to split   line->type = LT_WHITESPACE;
1151   * elements up more   line->numElements = 0;
  */  
  if (!isspace(kw->separatorChar)) {  
     int i;  
     char indent[2] = "";  
     indent[0] = kw->separatorChar;  
     for (i = 1; i < line->numElements; i++) {  
  char *p;  
  int j;  
  int numNewElements;  
   
  numNewElements = 0;  
  p = line->elements[i].item;  
  while (*p != '\0') {  
  if (*p == kw->separatorChar)  
  numNewElements++;  
  p++;  
  }  
  if (line->numElements + numNewElements >= elementsAlloced) {  
  elementsAlloced += numNewElements + 5;  
  line->elements = realloc(line->elements,  
     sizeof(*line->elements) * elementsAlloced);  
  }  
   
  for (j = line->numElements; j > i; j--) {  
  line->elements[j + numNewElements] = line->elements[j];  
1152   }   }
1153   line->numElements += numNewElements;   } else if (line->type == LT_INITRD) {
1154     struct keywordTypes *kw;
1155    
1156   p = line->elements[i].item;   kw = getKeywordByType(line->type, cfi);
  while (*p != '\0') {  
1157    
1158   while (*p != kw->separatorChar && *p != '\0') p++;   /* space isn't the only separator, we need to split
1159   if (*p == '\0') {   * elements up more
1160   break;   */
1161   }   if (!isspace(kw->separatorChar)) {
1162     char indent[2] = "";
1163     indent[0] = kw->separatorChar;
1164     for (int i = 1; i < line->numElements; i++) {
1165     char *p;
1166     int numNewElements;
1167    
1168     numNewElements = 0;
1169     p = line->elements[i].item;
1170     while (*p != '\0') {
1171     if (*p == kw->separatorChar)
1172     numNewElements++;
1173     p++;
1174     }
1175     if (line->numElements +
1176        numNewElements >= elementsAlloced) {
1177     elementsAlloced +=
1178        numNewElements + 5;
1179     line->elements =
1180        realloc(line->elements,
1181        sizeof(*line->
1182       elements) *
1183        elementsAlloced);
1184     }
1185    
1186   free(line->elements[i].indent);   for (int j = line->numElements; j > i;
1187   line->elements[i].indent = strdup(indent);       j--) {
1188   *p++ = '\0';   line->elements[j +
1189   i++;         numNewElements] =
1190   line->elements[i].item = strdup(p);      line->elements[j];
1191   line->elements[i].indent = strdup("");   }
1192   p = line->elements[i].item;   line->numElements += numNewElements;
1193    
1194     p = line->elements[i].item;
1195     while (*p != '\0') {
1196    
1197     while (*p != kw->separatorChar
1198           && *p != '\0')
1199     p++;
1200     if (*p == '\0') {
1201     break;
1202     }
1203    
1204     line->elements[i + 1].indent =
1205        line->elements[i].indent;
1206     line->elements[i].indent =
1207        strdup(indent);
1208     *p++ = '\0';
1209     i++;
1210     line->elements[i].item =
1211        strdup(p);
1212     }
1213     }
1214     }
1215     } else if (line->type == LT_SET_VARIABLE) {
1216     /* and if it's a "set blah=" we need to split it
1217     * yet a third way to avoid rhbz# XXX FIXME :/
1218     */
1219     char *eq;
1220     int l;
1221     int numElements = line->numElements;
1222     struct lineElement *newElements;
1223     eq = strchr(line->elements[1].item, '=');
1224     if (!eq)
1225     return 0;
1226     l = eq - line->elements[1].item;
1227     if (eq[1] != 0)
1228     numElements++;
1229     newElements = calloc(numElements,sizeof (*newElements));
1230     memcpy(&newElements[0], &line->elements[0],
1231           sizeof (newElements[0]));
1232     newElements[1].item =
1233     strndup(line->elements[1].item, l);
1234     newElements[1].indent = "=";
1235     *(eq++) = '\0';
1236     newElements[2].item = strdup(eq);
1237     free(line->elements[1].item);
1238     if (line->elements[1].indent)
1239     newElements[2].indent = line->elements[1].indent;
1240     for (int i = 2; i < line->numElements; i++) {
1241     newElements[i+1].item = line->elements[i].item;
1242     newElements[i+1].indent =
1243     line->elements[i].indent;
1244   }   }
1245      }   free(line->elements);
1246     line->elements = newElements;
1247     line->numElements = numElements;
1248   }   }
1249   }   }
     }  
1250    
1251      return 0;   return 0;
1252  }  }
1253    
1254  static struct grubConfig * readConfig(const char * inName,  static int isnumber(const char *s)
1255        struct configFileInfo * cfi) {  {
1256      int in;   int i;
1257      char * incoming = NULL, * head;   for (i = 0; s[i] != '\0'; i++)
1258      int rc;   if (s[i] < '0' || s[i] > '9')
1259      int sawEntry = 0;   return 0;
1260      int movedLine = 0;   return i;
1261      struct grubConfig * cfg;  }
1262      struct singleLine * last = NULL, * line, * defaultLine = NULL;  
1263      char * end;  static struct grubConfig *readConfig(const char *inName,
1264      struct singleEntry * entry = NULL;       struct configFileInfo *cfi)
1265      int i, len;  {
1266      char * buf;   int in;
1267     char *incoming = NULL, *head;
1268      if (!strcmp(inName, "-")) {   int rc;
1269   in = 0;   int sawEntry = 0;
1270      } else {   int movedLine = 0;
1271   if ((in = open(inName, O_RDONLY)) < 0) {   struct grubConfig *cfg;
1272      fprintf(stderr, _("error opening %s for read: %s\n"),   struct singleLine *last = NULL, *line, *defaultLine = NULL;
1273      inName, strerror(errno));   char *end;
1274      return NULL;   struct singleEntry *entry = NULL;
1275     int len;
1276     char *buf;
1277    
1278     if (inName == NULL) {
1279     printf("Could not find bootloader configuration\n");
1280     exit(1);
1281     } else if (!strcmp(inName, "-")) {
1282     in = 0;
1283     } else {
1284     if ((in = open(inName, O_RDONLY)) < 0) {
1285     fprintf(stderr, _("error opening %s for read: %s\n"),
1286     inName, strerror(errno));
1287     return NULL;
1288     }
1289   }   }
     }  
1290    
1291      rc = readFile(in, &incoming);   rc = readFile(in, &incoming);
1292      close(in);   close(in);
1293      if (rc) return NULL;   if (rc)
1294     return NULL;
1295      head = incoming;  
1296      cfg = malloc(sizeof(*cfg));   head = incoming;
1297      cfg->primaryIndent = strdup("");   cfg = malloc(sizeof(*cfg));
1298      cfg->secondaryIndent = strdup("\t");   cfg->primaryIndent = strdup("");
1299      cfg->flags = GRUB_CONFIG_NO_DEFAULT;   cfg->secondaryIndent = strdup("\t");
1300      cfg->cfi = cfi;   cfg->flags = GRUB_CONFIG_NO_DEFAULT;
1301      cfg->theLines = NULL;   cfg->cfi = cfi;
1302      cfg->entries = NULL;   cfg->theLines = NULL;
1303      cfg->fallbackImage = 0;   cfg->entries = NULL;
1304     cfg->fallbackImage = 0;
1305      /* copy everything we have */   cfg->isModified = 0;
1306      while (*head) {  
1307   line = malloc(sizeof(*line));   /* copy everything we have */
1308   lineInit(line);   while (*head) {
1309     line = malloc(sizeof(*line));
1310     lineInit(line);
1311    
1312     if (getNextLine(&head, line, cfi)) {
1313     free(line);
1314     /* XXX memory leak of everything in cfg */
1315     return NULL;
1316     }
1317    
1318   if (getNextLine(&head, line, cfi)) {   if (!sawEntry && line->numElements) {
1319      free(line);   free(cfg->primaryIndent);
1320      /* XXX memory leak of everything in cfg */   cfg->primaryIndent = strdup(line->indent);
1321      return NULL;   } else if (line->numElements) {
1322   }   free(cfg->secondaryIndent);
1323     cfg->secondaryIndent = strdup(line->indent);
1324   if (!sawEntry && line->numElements) {   }
     free(cfg->primaryIndent);  
     cfg->primaryIndent = strdup(line->indent);  
  } else if (line->numElements) {  
     free(cfg->secondaryIndent);  
     cfg->secondaryIndent = strdup(line->indent);  
  }  
   
  if (isEntryStart(line, cfi)) {  
     sawEntry = 1;  
     if (!entry) {  
  cfg->entries = malloc(sizeof(*entry));  
  entry = cfg->entries;  
     } else {  
  entry->next = malloc(sizeof(*entry));  
  entry = entry->next;  
     }  
1325    
1326      entry->skip = 0;   if (isEntryStart(line, cfi) || (cfg->entries && !sawEntry)) {
1327              entry->multiboot = 0;   sawEntry = 1;
1328      entry->lines = NULL;   if (!entry) {
1329      entry->next = NULL;   cfg->entries = malloc(sizeof(*entry));
1330   }   entry = cfg->entries;
1331     } else {
1332   if (line->type == LT_SET_VARIABLE) {   entry->next = malloc(sizeof(*entry));
1333      int i;   entry = entry->next;
1334      dbgPrintf("found 'set' command (%d elements): ", line->numElements);   }
     dbgPrintf("%s", line->indent);  
     for (i = 0; i < line->numElements; i++)  
  dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item);  
     dbgPrintf("\n");  
     struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);  
     if (kwType && line->numElements == 3 &&  
     !strcmp(line->elements[1].item, kwType->key)) {  
  dbgPrintf("Line sets default config\n");  
  cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;  
  defaultLine = line;  
     }  
  } else if (line->type == LT_DEFAULT && line->numElements == 2) {  
     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;  
     defaultLine = line;  
   
         } else if (line->type == LT_KERNEL) {  
     /* if by some freak chance this is multiboot and the "module"  
      * lines came earlier in the template, make sure to use LT_HYPER  
      * instead of LT_KERNEL now  
      */  
     if (entry->multiboot)  
  line->type = LT_HYPER;  
   
         } else if (line->type == LT_MBMODULE) {  
     /* go back and fix the LT_KERNEL line to indicate LT_HYPER  
      * instead, now that we know this is a multiboot entry.  
      * This only applies to grub, but that's the only place we  
      * should find LT_MBMODULE lines anyway.  
      */  
     struct singleLine * l;  
     for (l = entry->lines; l; l = l->next) {  
  if (l->type == LT_HYPER)  
     break;  
  else if (l->type == LT_KERNEL) {  
     l->type = LT_HYPER;  
     break;  
  }  
     }  
             entry->multiboot = 1;  
   
  } else if (line->type == LT_HYPER) {  
     entry->multiboot = 1;  
   
  } else if (line->type == LT_FALLBACK && line->numElements == 2) {  
     cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);  
     if (*end) cfg->fallbackImage = -1;  
   
  } else if (line->type == LT_TITLE && line->numElements > 1) {  
     /* make the title a single argument (undoing our parsing) */  
     len = 0;  
     for (i = 1; i < line->numElements; i++) {  
  len += strlen(line->elements[i].item);  
  len += strlen(line->elements[i].indent);  
     }  
     buf = malloc(len + 1);  
     *buf = '\0';  
1335    
1336      for (i = 1; i < line->numElements; i++) {   entry->skip = 0;
1337   strcat(buf, line->elements[i].item);   entry->multiboot = 0;
1338   free(line->elements[i].item);   entry->lines = NULL;
1339     entry->next = NULL;
1340     }
1341    
1342   if ((i + 1) != line->numElements) {   if (line->type == LT_SET_VARIABLE) {
1343      strcat(buf, line->elements[i].indent);   dbgPrintf("found 'set' command (%d elements): ",
1344      free(line->elements[i].indent);    line->numElements);
1345   }   dbgPrintf("%s", line->indent);
1346      }   for (int i = 0; i < line->numElements; i++)
1347     dbgPrintf("\"%s\"%s", line->elements[i].item,
1348      line->elements[1].indent =    line->elements[i].indent);
1349      line->elements[line->numElements - 1].indent;   dbgPrintf("\n");
1350      line->elements[1].item = buf;   struct keywordTypes *kwType =
1351      line->numElements = 2;      getKeywordByType(LT_DEFAULT, cfi);
1352     if (kwType && line->numElements == 3
1353   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {      && !strcmp(line->elements[1].item, kwType->key)
1354      /* Strip off any " which may be present; they'll be put back      && !is_special_grub2_variable(
1355         on write. This is one of the few (the only?) places that grubby   line->elements[2].item)) {
1356         canonicalizes the output */   dbgPrintf("Line sets default config\n");
1357     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1358      if (line->numElements >= 2) {   defaultLine = line;
1359   int last, len;   }
1360     } else if (iskernel(line->type)) {
1361   if (*line->elements[1].item == '"')   /* if by some freak chance this is multiboot and the
1362      memmove(line->elements[1].item, line->elements[1].item + 1,   * "module" lines came earlier in the template, make
1363      strlen(line->elements[1].item + 1) + 1);   * sure to use LT_HYPER instead of LT_KERNEL now
1364     */
1365   last = line->numElements - 1;   if (entry && entry->multiboot)
1366   len = strlen(line->elements[last].item) - 1;   line->type = LT_HYPER;
  if (line->elements[last].item[len] == '"')  
     line->elements[last].item[len] = '\0';  
     }  
  }  
   
  /* If we find a generic config option which should live at the  
    top of the file, move it there. Old versions of grubby were  
    probably responsible for putting new images in the wrong  
    place in front of it anyway. */  
  if (sawEntry && line->type == LT_GENERIC) {  
  struct singleLine **l = &cfg->theLines;  
  struct singleLine **last_nonws = &cfg->theLines;  
  while (*l) {  
  if ((*l)->type != LT_WHITESPACE)  
  last_nonws = &((*l)->next);  
  l = &((*l)->next);  
  }  
  line->next = *last_nonws;  
  *last_nonws = line;  
  movedLine = 1;  
  continue; /* without setting 'last' */  
  }  
   
  /* If a second line of whitespace happens after a generic option  
    which was moved, drop it. */  
  if (movedLine && line->type == LT_WHITESPACE && last->type == LT_WHITESPACE) {  
  lineFree(line);  
  free(line);  
  movedLine = 0;  
  continue;  
  }  
  movedLine = 0;  
1367    
1368   if (sawEntry) {   } else if (line->type == LT_MBMODULE) {
1369      if (!entry->lines)   /* go back and fix the LT_KERNEL line to indicate
1370   entry->lines = line;   * LT_HYPER instead, now that we know this is a
1371      else   * multiboot entry.  This only applies to grub, but
1372   last->next = line;   * that's the only place we should find LT_MBMODULE
1373      dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry);   * lines anyway.
1374     */
1375      /* we could have seen this outside of an entry... if so, we   for (struct singleLine * l = entry->lines; l;
1376       * ignore it like any other line we don't grok */       l = l->next) {
1377      if (line->type == LT_ENTRY_END && sawEntry)   if (l->type == LT_HYPER)
1378   sawEntry = 0;   break;
1379   } else {   else if (iskernel(l->type)) {
1380      if (!cfg->theLines)   l->type = LT_HYPER;
1381   cfg->theLines = line;   break;
1382      else   }
1383   last->next = line;   }
1384      dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi));   entry->multiboot = 1;
  }  
1385    
1386   last = line;   } else if (line->type == LT_HYPER) {
1387      }   entry->multiboot = 1;
1388    
1389      free(incoming);   } else if (line->type == LT_FALLBACK && line->numElements == 2) {
1390     cfg->fallbackImage =
1391        strtol(line->elements[1].item, &end, 10);
1392     if (*end)
1393     cfg->fallbackImage = -1;
1394    
1395     } else if ((line->type == LT_DEFAULT && cfi->defaultIsUnquoted)
1396       || (line->type == LT_TITLE
1397           && line->numElements > 1)) {
1398     /* make the title/default a single argument (undoing
1399     * our parsing) */
1400     len = 0;
1401     for (int i = 1; i < line->numElements; i++) {
1402     len += strlen(line->elements[i].item);
1403     len += strlen(line->elements[i].indent);
1404     }
1405     buf = malloc(len + 1);
1406     *buf = '\0';
1407    
1408      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");   for (int i = 1; i < line->numElements; i++) {
1409      if (defaultLine) {   strcat(buf, line->elements[i].item);
1410   if (cfi->defaultIsVariable) {   free(line->elements[i].item);
1411      char *value = defaultLine->elements[2].item;  
1412      while (*value && (*value == '"' || *value == '\'' ||   if ((i + 1) != line->numElements) {
1413      *value == ' ' || *value == '\t'))   strcat(buf, line->elements[i].indent);
1414   value++;   free(line->elements[i].indent);
1415      cfg->defaultImage = strtol(value, &end, 10);   }
1416      while (*end && (*end == '"' || *end == '\'' ||   }
     *end == ' ' || *end == '\t'))  
  end++;  
     if (*end) cfg->defaultImage = -1;  
  } else if (cfi->defaultSupportSaved &&  
  !strncmp(defaultLine->elements[1].item, "saved", 5)) {  
     cfg->defaultImage = DEFAULT_SAVED;  
  } else if (cfi->defaultIsIndex) {  
     cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);  
     if (*end) cfg->defaultImage = -1;  
  } else if (defaultLine->numElements >= 2) {  
     i = 0;  
     while ((entry = findEntryByIndex(cfg, i))) {  
  for (line = entry->lines; line; line = line->next)  
     if (line->type == LT_TITLE) break;  
   
                 if (!cfi->titleBracketed) {  
                     if (line && (line->numElements >= 2) &&  
                         !strcmp(defaultLine->elements[1].item,  
                                 line->elements[1].item)) break;  
                 } else if (line) {  
                     if (!strcmp(defaultLine->elements[1].item,  
                                 extractTitle(line))) break;  
                 }  
  i++;  
  entry = NULL;  
     }  
1417    
1418      if (entry){   line->elements[1].indent =
1419          cfg->defaultImage = i;      line->elements[line->numElements - 1].indent;
1420      }else{   line->elements[1].item = buf;
1421          cfg->defaultImage = -1;   line->numElements = 2;
1422      }   } else if (line->type == LT_MENUENTRY && line->numElements > 3) {
1423   }   /* let --remove-kernel="TITLE=what" work */
1424      } else {   len = 0;
1425          cfg->defaultImage = 0;   char *extras;
1426      }   char *title;
1427     /* initially unseen value */
1428     char quote_char = '\0';
1429    
1430     for (int i = 1; i < line->numElements; i++) {
1431     len += strlen(line->elements[i].item);
1432     len += strlen(line->elements[i].indent);
1433     }
1434     buf = malloc(len + 1);
1435     *buf = '\0';
1436    
1437      return cfg;   /* allocate mem for extra flags. */
1438  }   extras = malloc(len + 1);
1439     *extras = '\0';
1440    
1441     /* get title. */
1442     for (int i = 0; i < line->numElements; i++) {
1443     if (!strcmp
1444        (line->elements[i].item, "menuentry"))
1445     continue;
1446     if (isquote(*line->elements[i].item)
1447        && quote_char == '\0') {
1448     /* ensure we properly pair off quotes */
1449     quote_char = *line->elements[i].item;
1450     title = line->elements[i].item + 1;
1451     } else {
1452     title = line->elements[i].item;
1453     }
1454    
1455  static void writeDefault(FILE * out, char * indent,   len = strlen(title);
1456   char * separator, struct grubConfig * cfg) {   if (title[len - 1] == quote_char) {
1457      struct singleEntry * entry;   strncat(buf, title, len - 1);
1458      struct singleLine * line;   break;
1459      int i;   } else {
1460     strcat(buf, title);
1461      if (!cfg->defaultImage && cfg->flags == GRUB_CONFIG_NO_DEFAULT) return;   strcat(buf, line->elements[i].indent);
1462     }
1463      if (cfg->defaultImage == DEFAULT_SAVED)   }
  fprintf(out, "%sdefault%ssaved\n", indent, separator);  
     else if (cfg->defaultImage > -1) {  
  if (cfg->cfi->defaultIsIndex) {  
     if (cfg->cfi->defaultIsVariable) {  
         fprintf(out, "%sset default=\"%d\"\n", indent,  
  cfg->defaultImage);  
     } else {  
  fprintf(out, "%sdefault%s%d\n", indent, separator,  
  cfg->defaultImage);  
     }  
  } else {  
     int image = cfg->defaultImage;  
1464    
1465      entry = cfg->entries;   /* get extras */
1466      while (entry && entry->skip) entry = entry->next;   int count = 0;
1467     quote_char = '\0';
1468     for (int i = 0; i < line->numElements; i++) {
1469     if (count >= 2) {
1470     strcat(extras, line->elements[i].item);
1471     strcat(extras,
1472           line->elements[i].indent);
1473     }
1474    
1475      i = 0;   if (!strcmp
1476      while (entry && i < image) {      (line->elements[i].item, "menuentry"))
1477   entry = entry->next;   continue;
1478    
1479     /* count ' or ", there should be two in menuentry line. */
1480     if (isquote(*line->elements[i].item)
1481        && quote_char == '\0') {
1482     /* ensure we properly pair off quotes */
1483     quote_char = *line->elements[i].item;
1484     count++;
1485     }
1486    
1487   while (entry && entry->skip) entry = entry->next;   len = strlen(line->elements[i].item);
  i++;  
     }  
1488    
1489      if (!entry) return;   if (line->elements[i].item[len - 1] ==
1490        quote_char)
1491     count++;
1492    
1493      line = getLineByType(LT_TITLE, entry->lines);   /* ok, we get the final ' or ", others are extras. */
1494     }
1495     line->elements[1].indent =
1496        line->elements[line->numElements - 2].indent;
1497     line->elements[1].item = buf;
1498     line->elements[2].indent =
1499        line->elements[line->numElements - 2].indent;
1500     line->elements[2].item = extras;
1501     line->numElements = 3;
1502     } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {
1503     /* Strip off any " which may be present; they'll be
1504     * put back on write. This is one of the few (the
1505     * only?) places that grubby canonicalizes the output
1506     */
1507     if (line->numElements >= 2) {
1508     int last, len;
1509    
1510      if (line && line->numElements >= 2)   if (isquote(*line->elements[1].item))
1511   fprintf(out, "%sdefault%s%s\n", indent, separator,   memmove(line->elements[1].item,
1512   line->elements[1].item);   line->elements[1].item + 1,
1513              else if (line && (line->numElements == 1) &&   strlen(line->elements[1].item +
1514                       cfg->cfi->titleBracketed) {         1) + 1);
1515   fprintf(out, "%sdefault%s%s\n", indent, separator,  
1516                          extractTitle(line));   last = line->numElements - 1;
1517              }   len = strlen(line->elements[last].item) - 1;
1518   }   if (isquote(line->elements[last].item[len]))
1519      }   line->elements[last].item[len] = '\0';
1520  }   }
1521     }
1522    
1523  static int writeConfig(struct grubConfig * cfg, char * outName,   if (line->type == LT_DEFAULT && line->numElements == 2) {
1524         const char * prefix) {   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1525      FILE * out;   defaultLine = line;
1526      struct singleLine * line;   }
     struct singleEntry * entry;  
     char * tmpOutName;  
     int needs = MAIN_DEFAULT;  
     struct stat sb;  
     int i;  
   
     if (!strcmp(outName, "-")) {  
  out = stdout;  
  tmpOutName = NULL;  
     } else {  
  if (!lstat(outName, &sb) && S_ISLNK(sb.st_mode)) {  
     char * buf;  
     int len = 256;  
     int rc;  
   
     /* most likely the symlink is relative, so change our  
        directory to the dir of the symlink */  
             rc = chdir(dirname(strdupa(outName)));  
     do {  
  buf = alloca(len + 1);  
  rc = readlink(basename(outName), buf, len);  
  if (rc == len) len += 256;  
     } while (rc == len);  
       
     if (rc < 0) {  
  fprintf(stderr, _("grubby: error readlink link %s: %s\n"),  
  outName, strerror(errno));  
  return 1;  
     }  
   
     outName = buf;  
     outName[rc] = '\0';  
  }  
   
  tmpOutName = alloca(strlen(outName) + 2);  
  sprintf(tmpOutName, "%s-", outName);  
  out = fopen(tmpOutName, "w");  
  if (!out) {  
     fprintf(stderr, _("grubby: error creating %s: %s\n"), tmpOutName,  
     strerror(errno));  
     return 1;  
  }  
   
  if (!stat(outName, &sb)) {  
     if (chmod(tmpOutName, sb.st_mode & ~(S_IFMT))) {  
  fprintf(stderr, _("grubby: error setting perms on %s: %s\n"),  
         tmpOutName, strerror(errno));  
  fclose(out);  
  unlink(tmpOutName);  
                 return 1;  
     }  
  }  
     }  
1527    
1528      line = cfg->theLines;   /* If we find a generic config option which should live at the
1529      struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);     top of the file, move it there. Old versions of grubby were
1530      while (line) {     probably responsible for putting new images in the wrong
1531          if (line->type == LT_SET_VARIABLE && defaultKw &&     place in front of it anyway. */
1532   line->numElements == 3 &&   if (sawEntry && line->type == LT_GENERIC) {
1533   !strcmp(line->elements[1].item, defaultKw->key)) {   struct singleLine **l = &cfg->theLines;
1534      writeDefault(out, line->indent, line->elements[0].indent, cfg);   struct singleLine **last_nonws = &cfg->theLines;
1535      needs &= ~MAIN_DEFAULT;   while (*l) {
1536   } else if (line->type == LT_DEFAULT) {   if ((*l)->type != LT_WHITESPACE)
1537      writeDefault(out, line->indent, line->elements[0].indent, cfg);   last_nonws = &((*l)->next);
1538      needs &= ~MAIN_DEFAULT;   l = &((*l)->next);
1539   } else if (line->type == LT_FALLBACK) {   }
1540      if (cfg->fallbackImage > -1)   line->next = *last_nonws;
1541   fprintf(out, "%s%s%s%d\n", line->indent,   *last_nonws = line;
1542   line->elements[0].item, line->elements[0].indent,   movedLine = 1;
1543   cfg->fallbackImage);   continue; /* without setting 'last' */
1544   } else {   }
     if (lineWrite(out, line, cfg->cfi) == -1) {  
                 fprintf(stderr, _("grubby: error writing %s: %s\n"),  
                         tmpOutName, strerror(errno));  
                 fclose(out);  
                 unlink(tmpOutName);  
                 return 1;  
             }  
  }  
1545    
1546   line = line->next;   /* If a second line of whitespace happens after a generic
1547      }   * option which was moved, drop it. */
1548     if (movedLine && line->type == LT_WHITESPACE
1549        && last->type == LT_WHITESPACE) {
1550     lineFree(line);
1551     free(line);
1552     movedLine = 0;
1553     continue;
1554     }
1555     movedLine = 0;
1556    
1557      if (needs & MAIN_DEFAULT) {   if (sawEntry) {
1558   writeDefault(out, cfg->primaryIndent, "=", cfg);   if (!entry->lines)
1559   needs &= ~MAIN_DEFAULT;   entry->lines = line;
1560      }   else
1561     last->next = line;
1562     dbgPrintf("readConfig added %s to %p\n",
1563      getKeyByType(line->type, cfi), entry);
1564    
1565      i = 0;   /* we could have seen this outside of an entry... if
1566      while ((entry = findEntryByIndex(cfg, i++))) {   * so, we ignore it like any other line we don't grok
1567   if (entry->skip) continue;   */
1568     if (line->type == LT_ENTRY_END && sawEntry)
1569     sawEntry = 0;
1570     } else {
1571     if (!cfg->theLines)
1572     cfg->theLines = line;
1573     else
1574     last->next = line;
1575     dbgPrintf("readConfig added %s to cfg\n",
1576      getKeyByType(line->type, cfi));
1577     }
1578    
1579   line = entry->lines;   last = line;
  while (line) {  
     if (lineWrite(out, line, cfg->cfi) == -1) {  
                 fprintf(stderr, _("grubby: error writing %s: %s\n"),  
                         tmpOutName, strerror(errno));  
                 fclose(out);  
                 unlink(tmpOutName);  
                 return 1;  
             }  
     line = line->next;  
1580   }   }
     }  
1581    
1582      if (tmpOutName) {   free(incoming);
  if (rename(tmpOutName, outName)) {  
     fprintf(stderr, _("grubby: error moving %s to %s: %s\n"),  
     tmpOutName, outName, strerror(errno));  
     unlink(outName);  
             return 1;  
  }  
     }  
1583    
1584      return 0;   dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1585  }   if (defaultLine) {
1586     if (defaultLine->numElements > 2 &&
1587        cfi->defaultSupportSaved &&
1588        !strncmp(defaultLine->elements[2].item,
1589         "\"${saved_entry}\"", 16)) {
1590     cfg->cfi->defaultIsSaved = 1;
1591     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1592     if (cfg->cfi->getEnv) {
1593     char *defTitle =
1594        cfi->getEnv(cfg->cfi, "saved_entry");
1595     if (defTitle) {
1596     int index = 0;
1597     if (isnumber(defTitle)) {
1598     index = atoi(defTitle);
1599     entry =
1600        findEntryByIndex(cfg,
1601         index);
1602     } else {
1603     entry =
1604        findEntryByTitle(cfg,
1605         defTitle,
1606         &index);
1607     }
1608     if (entry)
1609     cfg->defaultImage = index;
1610     }
1611     }
1612     } else if (cfi->defaultIsVariable) {
1613     if (defaultLine->numElements == 2) {
1614     char *value = defaultLine->elements[1].item + 8;
1615     while (*value && (*value == '"' ||
1616      *value == '\'' ||
1617      *value == ' ' ||
1618      *value == '\t'))
1619     value++;
1620     cfg->defaultImage = strtol(value, &end, 10);
1621     while (*end && (*end == '"' || *end == '\'' ||
1622     *end == ' ' || *end == '\t'))
1623     end++;
1624     if (*end)
1625     cfg->defaultImage = NO_DEFAULT_ENTRY;
1626     } else if (defaultLine->numElements == 3) {
1627     char *value = defaultLine->elements[2].item;
1628     while (*value && (*value == '"' ||
1629      *value == '\'' ||
1630      *value == ' ' ||
1631      *value == '\t'))
1632     value++;
1633     cfg->defaultImage = strtol(value, &end, 10);
1634     while (*end && (*end == '"' || *end == '\'' ||
1635     *end == ' ' || *end == '\t'))
1636     end++;
1637     if (*end)
1638     cfg->defaultImage = NO_DEFAULT_ENTRY;
1639     }
1640     } else if (cfi->defaultSupportSaved &&
1641       !strncmp(defaultLine->elements[1].item, "saved",
1642        5)) {
1643     cfg->defaultImage = DEFAULT_SAVED;
1644     } else if (cfi->defaultIsIndex) {
1645     cfg->defaultImage =
1646        strtol(defaultLine->elements[1].item, &end, 10);
1647     if (*end)
1648     cfg->defaultImage = NO_DEFAULT_ENTRY;
1649     } else if (defaultLine->numElements >= 2) {
1650     int i = 0;
1651     while ((entry = findEntryByIndex(cfg, i))) {
1652     for (line = entry->lines; line;
1653         line = line->next)
1654     if (line->type == LT_TITLE)
1655     break;
1656    
1657     if (!cfi->titleBracketed) {
1658     if (line && (line->numElements >= 2) &&
1659        !strcmp(defaultLine->elements[1].
1660        item,
1661        line->elements[1].item))
1662     break;
1663     } else if (line) {
1664     if (!strcmp
1665        (defaultLine->elements[1].item,
1666         extractTitle(cfg, line)))
1667     break;
1668     }
1669     i++;
1670     entry = NULL;
1671     }
1672    
1673  static int numEntries(struct grubConfig *cfg) {   if (entry) {
1674      int i = 0;   cfg->defaultImage = i;
1675      struct singleEntry * entry;   } else {
1676     cfg->defaultImage = NO_DEFAULT_ENTRY;
1677      entry = cfg->entries;   }
1678      while (entry) {   }
1679          if (!entry->skip)   } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1680              i++;   char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1681          entry = entry->next;   if (defTitle) {
1682      }   int index = 0;
1683      return i;   if (isnumber(defTitle)) {
1684     index = atoi(defTitle);
1685     entry = findEntryByIndex(cfg, index);
1686     } else {
1687     entry = findEntryByTitle(cfg, defTitle, &index);
1688     }
1689     if (entry)
1690     cfg->defaultImage = index;
1691     }
1692     } else {
1693     cfg->defaultImage = FIRST_ENTRY_INDEX;
1694     }
1695    
1696     return cfg;
1697  }  }
1698    
1699  static char *findDiskForRoot()  static void writeDefault(FILE * out, char *indent,
1700     char *separator, struct grubConfig *cfg)
1701  {  {
1702      int fd;   struct singleEntry *entry;
1703      char buf[65536];   struct singleLine *line;
1704      char *devname;   int i;
1705      char *chptr;  
1706      int rc;   if (!cfg->defaultImage && cfg->flags == GRUB_CONFIG_NO_DEFAULT)
1707     return;
1708    
1709     if (cfg->defaultImage == DEFAULT_SAVED)
1710     fprintf(out, "%sdefault%ssaved\n", indent, separator);
1711     else if (cfg->cfi->defaultIsSaved) {
1712     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1713     if (cfg->defaultImage >= FIRST_ENTRY_INDEX && cfg->cfi->setEnv) {
1714     char *title;
1715     int trueIndex, currentIndex;
1716    
1717     trueIndex = 0;
1718     currentIndex = 0;
1719    
1720     while ((entry = findEntryByIndex(cfg, currentIndex))) {
1721     if (!entry->skip) {
1722     if (trueIndex == cfg->defaultImage) {
1723     break;
1724     }
1725     trueIndex++;
1726     }
1727     currentIndex++;
1728     }
1729     line = getLineByType(LT_MENUENTRY, entry->lines);
1730     if (!line)
1731     line = getLineByType(LT_TITLE, entry->lines);
1732     if (line) {
1733     title = extractTitle(cfg, line);
1734     if (title)
1735     cfg->cfi->setEnv(cfg->cfi,
1736     "saved_entry", title);
1737     }
1738     }
1739     } else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) {
1740     if (cfg->cfi->defaultIsIndex) {
1741     if (cfg->cfi->defaultIsVariable) {
1742     fprintf(out, "%sset default=\"%d\"\n", indent,
1743     cfg->defaultImage);
1744     } else {
1745     fprintf(out, "%sdefault%s%d\n", indent,
1746     separator, cfg->defaultImage);
1747     }
1748     } else {
1749     int image = cfg->defaultImage;
1750    
1751      if ((fd = open(_PATH_MOUNTED, O_RDONLY)) < 0) {   entry = cfg->entries;
1752          fprintf(stderr, "grubby: failed to open %s: %s\n",   while (entry && entry->skip)
1753                  _PATH_MOUNTED, strerror(errno));   entry = entry->next;
1754          return NULL;  
1755      }   i = 0;
1756     while (entry && i < image) {
1757     entry = entry->next;
1758    
1759      rc = read(fd, buf, sizeof(buf) - 1);   while (entry && entry->skip)
1760      if (rc <= 0) {   entry = entry->next;
1761          fprintf(stderr, "grubby: failed to read %s: %s\n",   i++;
1762                  _PATH_MOUNTED, strerror(errno));   }
         close(fd);  
         return NULL;  
     }  
     close(fd);  
     buf[rc] = '\0';  
     chptr = buf;  
   
     while (chptr && chptr != buf+rc) {  
         devname = chptr;  
   
         /*  
          * The first column of a mtab entry is the device, but if the entry is a  
          * special device it won't start with /, so move on to the next line.  
          */  
         if (*devname != '/') {  
             chptr = strchr(chptr, '\n');  
             if (chptr)  
                 chptr++;  
             continue;  
         }  
   
         /* Seek to the next space */  
         chptr = strchr(chptr, ' ');  
         if (!chptr) {  
             fprintf(stderr, "grubby: error parsing %s: %s\n",  
                     _PATH_MOUNTED, strerror(errno));  
             return NULL;  
         }  
   
         /*  
          * The second column of a mtab entry is the mount point, we are looking  
          * for '/' obviously.  
          */  
         if (*(++chptr) == '/' && *(++chptr) == ' ') {  
             /*  
              * Move back 2, which is the first space after the device name, set  
              * it to \0 so strdup will just get the devicename.  
              */  
             chptr -= 2;  
             *chptr = '\0';  
             return strdup(devname);  
         }  
   
         /* Next line */  
         chptr = strchr(chptr, '\n');  
         if (chptr)  
             chptr++;  
     }  
1763    
1764      return NULL;   if (!entry)
1765  }   return;
1766    
1767  int suitableImage(struct singleEntry * entry, const char * bootPrefix,   line = getLineByType(LT_TITLE, entry->lines);
   int skipRemoved, int flags) {  
     struct singleLine * line;  
     char * fullName;  
     int i;  
     char * dev;  
     char * rootspec;  
     char * rootdev;  
   
     if (skipRemoved && entry->skip) return 0;  
   
     line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);  
     if (!line || line->numElements < 2) return 0;  
   
     if (flags & GRUBBY_BADIMAGE_OKAY) return 1;  
   
     fullName = alloca(strlen(bootPrefix) +  
       strlen(line->elements[1].item) + 1);  
     rootspec = getRootSpecifier(line->elements[1].item);  
     sprintf(fullName, "%s%s", bootPrefix,  
             line->elements[1].item + (rootspec ? strlen(rootspec) : 0));  
     if (access(fullName, R_OK)) return 0;  
   
     for (i = 2; i < line->numElements; i++)  
  if (!strncasecmp(line->elements[i].item, "root=", 5)) break;  
     if (i < line->numElements) {  
  dev = line->elements[i].item + 5;  
     } else {  
  /* look for a lilo style LT_ROOT line */  
  line = getLineByType(LT_ROOT, entry->lines);  
1768    
1769   if (line && line->numElements >= 2) {   if (line && line->numElements >= 2)
1770      dev = line->elements[1].item;   fprintf(out, "%sdefault%s%s\n", indent,
1771   } else {   separator, line->elements[1].item);
1772      /* didn't succeed in finding a LT_ROOT, let's try LT_KERNELARGS.   else if (line && (line->numElements == 1)
1773       * grub+multiboot uses LT_MBMODULE for the args, so check that too.   && cfg->cfi->titleBracketed) {
1774       */   char *title = extractTitle(cfg, line);
1775      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);   if (title) {
1776     fprintf(out, "%sdefault%s%s\n", indent,
1777              /* failed to find one */   separator, title);
1778              if (!line) return 0;   free(title);
1779     }
1780      for (i = 1; i < line->numElements; i++)   }
1781          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;   }
     if (i < line->numElements)  
         dev = line->elements[i].item + 5;  
     else {  
  /* it failed too...  can't find root= */  
         return 0;  
             }  
1782   }   }
1783      }  }
   
     dev = getpathbyspec(dev);  
     if (!dev)  
         return 0;  
   
     rootdev = findDiskForRoot();  
     if (!rootdev)  
  return 0;  
   
     if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {  
         free(rootdev);  
         return 0;  
     }  
1784    
1785      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {  static int writeConfig(struct grubConfig *cfg, char *outName,
1786   free(rootdev);         const char *prefix)
1787          return 0;  {
1788      }   FILE *out;
1789     struct singleLine *line;
1790     struct singleEntry *entry;
1791     char *tmpOutName;
1792     int needs = MAIN_DEFAULT;
1793     struct stat sb;
1794     int i;
1795    
1796     if (!strcmp(outName, "-")) {
1797     out = stdout;
1798     tmpOutName = NULL;
1799     } else {
1800     if (!lstat(outName, &sb) && S_ISLNK(sb.st_mode)) {
1801     char *buf;
1802     int len = 256;
1803     int rc;
1804    
1805     /* most likely the symlink is relative, so change our
1806       directory to the dir of the symlink */
1807     char *dir = strdupa(outName);
1808     rc = chdir(dirname(dir));
1809     do {
1810     buf = alloca(len + 1);
1811     rc = readlink(basename(outName), buf, len);
1812     if (rc == len)
1813     len += 256;
1814     } while (rc == len);
1815    
1816     if (rc < 0) {
1817     fprintf(stderr,
1818     _
1819     ("grubby: error readlink link %s: %s\n"),
1820     outName, strerror(errno));
1821     return 1;
1822     }
1823    
1824      free(rootdev);   outName = buf;
1825     outName[rc] = '\0';
1826     }
1827    
1828      return 1;   tmpOutName = alloca(strlen(outName) + 2);
1829  }   sprintf(tmpOutName, "%s-", outName);
1830     out = fopen(tmpOutName, "w");
1831     if (!out) {
1832     fprintf(stderr, _("grubby: error creating %s: %s\n"),
1833     tmpOutName, strerror(errno));
1834     return 1;
1835     }
1836    
1837  /* returns the first match on or after the one pointed to by index (if index   if (!stat(outName, &sb)) {
1838     is not NULL) which is not marked as skip */   if (chmod(tmpOutName, sb.st_mode & ~(S_IFMT))) {
1839  struct singleEntry * findEntryByPath(struct grubConfig * config,   fprintf(stderr,
1840       const char * kernel, const char * prefix,   _
1841       int * index) {   ("grubby: error setting perms on %s: %s\n"),
1842      struct singleEntry * entry = NULL;   tmpOutName, strerror(errno));
1843      struct singleLine * line;   fclose(out);
1844      int i;   unlink(tmpOutName);
1845      char * chptr;   return 1;
1846      char * rootspec = NULL;   }
1847      enum lineType_e checkType = LT_KERNEL;   }
1848     }
1849    
1850      if (isdigit(*kernel)) {   line = cfg->theLines;
1851   int * indexVars = alloca(sizeof(*indexVars) * strlen(kernel));   struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);
1852     while (line) {
1853     if (line->type == LT_SET_VARIABLE && defaultKw &&
1854        line->numElements == 3 &&
1855        !strcmp(line->elements[1].item, defaultKw->key) &&
1856        !is_special_grub2_variable(line->elements[2].item)) {
1857     writeDefault(out, line->indent,
1858         line->elements[0].indent, cfg);
1859     needs &= ~MAIN_DEFAULT;
1860     } else if (line->type == LT_DEFAULT) {
1861     writeDefault(out, line->indent,
1862         line->elements[0].indent, cfg);
1863     needs &= ~MAIN_DEFAULT;
1864     } else if (line->type == LT_FALLBACK) {
1865     if (cfg->fallbackImage > -1)
1866     fprintf(out, "%s%s%s%d\n", line->indent,
1867     line->elements[0].item,
1868     line->elements[0].indent,
1869     cfg->fallbackImage);
1870     } else {
1871     if (lineWrite(out, line, cfg->cfi) == -1) {
1872     fprintf(stderr,
1873     _("grubby: error writing %s: %s\n"),
1874     tmpOutName, strerror(errno));
1875     fclose(out);
1876     unlink(tmpOutName);
1877     return 1;
1878     }
1879     }
1880    
1881   i = 0;   line = line->next;
  indexVars[i] = strtol(kernel, &chptr, 10);  
  while (*chptr == ',') {  
     i++;  
     kernel = chptr + 1;  
     indexVars[i] = strtol(kernel, &chptr, 10);  
1882   }   }
1883    
1884   if (*chptr) {   if (needs & MAIN_DEFAULT) {
1885      /* can't parse it, bail */   writeDefault(out, cfg->primaryIndent, "=", cfg);
1886      return NULL;   needs &= ~MAIN_DEFAULT;
1887   }   }
1888    
  indexVars[i + 1] = -1;  
   
1889   i = 0;   i = 0;
1890   if (index) {   while ((entry = findEntryByIndex(cfg, i++))) {
1891      while (i < *index) i++;   if (entry->skip)
1892      if (indexVars[i] == -1) return NULL;   continue;
1893    
1894     line = entry->lines;
1895     while (line) {
1896     if (lineWrite(out, line, cfg->cfi) == -1) {
1897     fprintf(stderr,
1898     _("grubby: error writing %s: %s\n"),
1899     tmpOutName, strerror(errno));
1900     fclose(out);
1901     unlink(tmpOutName);
1902     return 1;
1903     }
1904     line = line->next;
1905     }
1906   }   }
1907    
1908   entry = findEntryByIndex(config, indexVars[i]);   if (tmpOutName) {
1909   if (!entry) return NULL;   if (rename(tmpOutName, outName)) {
1910     fprintf(stderr,
1911     _("grubby: error moving %s to %s: %s\n"),
1912     tmpOutName, outName, strerror(errno));
1913     unlink(outName);
1914     return 1;
1915     }
1916     }
1917    
1918   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   return 0;
1919   if (!line) return NULL;  }
1920    
1921   if (index) *index = indexVars[i];  static int numEntries(struct grubConfig *cfg)
1922   return entry;  {
1923      }   int i = 0;
1924         struct singleEntry *entry;
     if (!strcmp(kernel, "DEFAULT")) {  
  if (index && *index > config->defaultImage) {  
     entry = NULL;  
  } else {  
     entry = findEntryByIndex(config, config->defaultImage);  
     if (entry && entry->skip)  
  entry = NULL;  
     else if (index)  
  *index = config->defaultImage;  
  }  
     } else if (!strcmp(kernel, "ALL")) {  
  if (index)  
     i = *index;  
  else  
     i = 0;  
1925    
1926   while ((entry = findEntryByIndex(config, i))) {   entry = cfg->entries;
1927      if (!entry->skip) break;   while (entry) {
1928      i++;   if (!entry->skip)
1929     i++;
1930     entry = entry->next;
1931   }   }
1932     return i;
1933    }
1934    
1935   if (entry && index)  static char *findDiskForRoot()
1936      *index = i;  {
1937      } else {   int fd;
1938   if (index)   char buf[65536];
1939      i = *index;   char *devname;
1940   else   char *chptr;
1941      i = 0;   int rc;
1942    
1943   if (!strncmp(kernel, "TITLE=", 6)) {   if ((fd = open(_PATH_MOUNTED, O_RDONLY)) < 0) {
1944      prefix = "";   fprintf(stderr, "grubby: failed to open %s: %s\n",
1945      checkType = LT_TITLE;   _PATH_MOUNTED, strerror(errno));
1946      kernel += 6;   return NULL;
1947     }
1948    
1949     rc = read(fd, buf, sizeof(buf) - 1);
1950     if (rc <= 0) {
1951     fprintf(stderr, "grubby: failed to read %s: %s\n",
1952     _PATH_MOUNTED, strerror(errno));
1953     close(fd);
1954     return NULL;
1955   }   }
1956     close(fd);
1957     buf[rc] = '\0';
1958     chptr = buf;
1959    
1960   for (entry = findEntryByIndex(config, i); entry; entry = entry->next, i++) {   char *foundanswer = NULL;
     if (entry->skip) continue;  
1961    
1962      dbgPrintf("findEntryByPath looking for %d %s in %p\n", checkType, kernel, entry);   while (chptr && chptr != buf + rc) {
1963     devname = chptr;
1964    
1965     /*
1966     * The first column of a mtab entry is the device, but if the
1967     * entry is a special device it won't start with /, so move
1968     * on to the next line.
1969     */
1970     if (*devname != '/') {
1971     chptr = strchr(chptr, '\n');
1972     if (chptr)
1973     chptr++;
1974     continue;
1975     }
1976    
1977      /* check all the lines matching checkType */   /* Seek to the next space */
1978      for (line = entry->lines; line; line = line->next) {   chptr = strchr(chptr, ' ');
1979   line = getLineByType(entry->multiboot && checkType == LT_KERNEL ?   if (!chptr) {
1980       LT_KERNEL|LT_MBMODULE|LT_HYPER :   fprintf(stderr, "grubby: error parsing %s: %s\n",
1981       checkType, line);   _PATH_MOUNTED, strerror(errno));
1982   if (!line) break;  /* not found in this entry */   return NULL;
1983     }
1984    
1985   if (line && line->numElements >= 2) {   /*
1986      rootspec = getRootSpecifier(line->elements[1].item);   * The second column of a mtab entry is the mount point, we
1987      if (!strcmp(line->elements[1].item +   * are looking for '/' obviously.
1988   ((rootspec != NULL) ? strlen(rootspec) : 0),   */
1989   kernel + strlen(prefix)))   if (*(++chptr) == '/' && *(++chptr) == ' ') {
1990   break;   /* remember the last / entry in mtab */
1991     foundanswer = devname;
1992   }   }
     }  
1993    
1994      /* make sure this entry has a kernel identifier; this skips   /* Next line */
1995       * non-Linux boot entries (could find netbsd etc, though, which is   chptr = strchr(chptr, '\n');
1996       * unfortunate)   if (chptr)
1997       */   chptr++;
     if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines))  
  break; /* found 'im! */  
1998   }   }
1999    
2000   if (index) *index = i;   /* Return the last / entry found */
2001      }   if (foundanswer) {
2002     chptr = strchr(foundanswer, ' ');
2003     *chptr = '\0';
2004     return strdup(foundanswer);
2005     }
2006    
2007      return entry;   return NULL;
2008  }  }
2009    
2010  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  void printEntry(struct singleEntry *entry, FILE * f)
2011      struct singleEntry * entry;  {
2012     int i;
2013     struct singleLine *line;
2014    
2015      entry = cfg->entries;   for (line = entry->lines; line; line = line->next) {
2016      while (index && entry) {   log_message(f, "DBG: %s", line->indent);
2017   entry = entry->next;   for (i = 0; i < line->numElements; i++) {
2018   index--;   /* Need to handle this, because we strip the quotes from
2019      }   * menuentry when read it. */
2020     if (line->type == LT_MENUENTRY && i == 1) {
2021     if (!isquote(*line->elements[i].item))
2022     log_message(f, "\'%s\'",
2023        line->elements[i].item);
2024     else
2025     log_message(f, "%s",
2026        line->elements[i].item);
2027     log_message(f, "%s", line->elements[i].indent);
2028    
2029      return entry;   continue;
2030  }   }
2031    
2032  /* Find a good template to use for the new kernel. An entry is   log_message(f, "%s%s",
2033   * good if the kernel and mkinitrd exist (even if the entry      line->elements[i].item,
2034   * is going to be removed). Try and use the default entry, but      line->elements[i].indent);
2035   * if that doesn't work just take the first. If we can't find one,   }
2036   * bail. */   log_message(f, "\n");
 struct singleEntry * findTemplate(struct grubConfig * cfg, const char * prefix,  
  int * indexPtr, int skipRemoved, int flags) {  
     struct singleEntry * entry, * entry2;  
     int index;  
   
     if (cfg->defaultImage > -1) {  
  entry = findEntryByIndex(cfg, cfg->defaultImage);  
  if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {  
     if (indexPtr) *indexPtr = cfg->defaultImage;  
     return entry;  
2037   }   }
2038      }  }
2039    
2040    void notSuitablePrintf(struct singleEntry *entry, int okay, const char *fmt,
2041           ...)
2042    {
2043     static int once;
2044     va_list argp, argq;
2045    
2046      index = 0;   va_start(argp, fmt);
     while ((entry = findEntryByIndex(cfg, index))) {  
  if (suitableImage(entry, prefix, skipRemoved, flags)) {  
             int j;  
             for (j = 0; j < index; j++) {  
                 entry2 = findEntryByIndex(cfg, j);  
                 if (entry2->skip) index--;  
             }  
     if (indexPtr) *indexPtr = index;  
2047    
2048      return entry;   va_copy(argq, argp);
2049     if (!once) {
2050     log_time(NULL);
2051     log_message(NULL, "command line: %s\n", saved_command_line);
2052   }   }
2053     log_message(NULL, "DBG: Image entry %s: ",
2054        okay ? "succeeded" : "failed");
2055     log_vmessage(NULL, fmt, argq);
2056    
2057   index++;   printEntry(entry, NULL);
2058      }   va_end(argq);
2059    
2060      fprintf(stderr, _("grubby fatal error: unable to find a suitable template\n"));   if (!debug) {
2061     once = 1;
2062     va_end(argp);
2063     return;
2064     }
2065    
2066     if (okay) {
2067     va_end(argp);
2068     return;
2069     }
2070    
2071      return NULL;   if (!once)
2072     log_message(stderr, "DBG: command line: %s\n",
2073        saved_command_line);
2074     once = 1;
2075     fprintf(stderr, "DBG: Image entry failed: ");
2076     vfprintf(stderr, fmt, argp);
2077     printEntry(entry, stderr);
2078     va_end(argp);
2079  }  }
2080    
2081  char * findBootPrefix(void) {  #define beginswith(s, c) ((s) && (s)[0] == (c))
     struct stat sb, sb2;  
2082    
2083      stat("/", &sb);  static int endswith(const char *s, char c)
2084  #ifdef __ia64__  {
2085      stat("/boot/efi/EFI/redhat/", &sb2);   int slen;
 #else  
     stat("/boot", &sb2);  
 #endif  
2086    
2087      if (sb.st_dev == sb2.st_dev)   if (!s || !s[0])
2088   return strdup("");   return 0;
2089     slen = strlen(s) - 1;
2090    
2091  #ifdef __ia64__   return s[slen] == c;
     return strdup("/boot/efi/EFI/redhat/");  
 #else  
     return strdup("/boot");  
 #endif  
2092  }  }
2093    
2094  void markRemovedImage(struct grubConfig * cfg, const char * image,  int suitableImage(struct singleEntry *entry, const char *bootPrefix,
2095        const char * prefix) {    int skipRemoved, int flags)
2096      struct singleEntry * entry;  {
2097     struct singleLine *line;
2098      if (!image) return;   char *fullName;
2099     int i;
2100      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))   char *dev;
2101   entry->skip = 1;   char *rootspec;
2102  }   char *rootdev;
2103    
2104  void setDefaultImage(struct grubConfig * config, int hasNew,   if (skipRemoved && entry->skip) {
2105       const char * defaultKernelPath, int newIsDefault,   notSuitablePrintf(entry, 0, "marked to skip\n");
2106       const char * prefix, int flags) {   return 0;
2107      struct singleEntry * entry, * entry2, * newDefault;   }
2108      int i, j;  
2109     line =
2110      if (newIsDefault) {      getLineByType(LT_KERNEL | LT_HYPER | LT_KERNEL_EFI | LT_KERNEL_16,
2111   config->defaultImage = 0;    entry->lines);
2112   return;   if (!line) {
2113      } else if (defaultKernelPath) {   notSuitablePrintf(entry, 0, "no line found\n");
2114   i = 0;   return 0;
2115   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   }
2116      config->defaultImage = i;   if (line->numElements < 2) {
2117   } else {   notSuitablePrintf(entry, 0, "line has only %d elements\n",
2118      config->defaultImage = -1;    line->numElements);
2119      return;   return 0;
2120   }   }
     }  
2121    
2122      /* defaultImage now points to what we'd like to use, but before any order   if (flags & GRUBBY_BADIMAGE_OKAY) {
2123         changes */   notSuitablePrintf(entry, 1, "\n");
2124      if (config->defaultImage == DEFAULT_SAVED)   return 1;
2125        /* default is set to saved, we don't want to change it */   }
       return;  
   
     if (config->defaultImage > -1)  
  entry = findEntryByIndex(config, config->defaultImage);  
     else  
  entry = NULL;  
2126    
2127      if (entry && !entry->skip) {   fullName = alloca(strlen(bootPrefix) +
2128   /* we can preserve the default */    strlen(line->elements[1].item) + 1);
2129   if (hasNew)   rootspec = getRootSpecifier(line->elements[1].item);
2130      config->defaultImage++;   int rootspec_offset = rootspec ? strlen(rootspec) : 0;
2131     int hasslash = endswith(bootPrefix, '/') ||
2132   /* count the number of entries erased before this one */      beginswith(line->elements[1].item + rootspec_offset, '/');
2133   for (j = 0; j < config->defaultImage; j++) {   sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
2134      entry2 = findEntryByIndex(config, j);   line->elements[1].item + rootspec_offset);
2135      if (entry2->skip) config->defaultImage--;   if (access(fullName, R_OK)) {
2136   }   notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
2137      } else if (hasNew) {   return 0;
2138   config->defaultImage = 0;   }
2139      } else {   for (i = 2; i < line->numElements; i++)
2140   /* Either we just erased the default (or the default line was bad   if (!strncasecmp(line->elements[i].item, "root=", 5))
2141   * to begin with) and didn't put a new one in. We'll use the first   break;
2142   * valid image. */   if (i < line->numElements) {
2143   newDefault = findTemplate(config, prefix, &config->defaultImage, 1,   dev = line->elements[i].item + 5;
2144    flags);   } else {
2145   if (!newDefault)   /* look for a lilo style LT_ROOT line */
2146      config->defaultImage = -1;   line = getLineByType(LT_ROOT, entry->lines);
     }  
 }  
2147    
2148  void setFallbackImage(struct grubConfig * config, int hasNew) {   if (line && line->numElements >= 2) {
2149      struct singleEntry * entry, * entry2;   dev = line->elements[1].item;
2150      int j;   } else {
2151     /* didn't succeed in finding a LT_ROOT, let's try
2152      if (config->fallbackImage == -1) return;   * LT_KERNELARGS.  grub+multiboot uses LT_MBMODULE
2153     * for the args, so check that too.
2154      entry = findEntryByIndex(config, config->fallbackImage);   */
2155      if (!entry || entry->skip) {   line =
2156   config->fallbackImage = -1;      getLineByType(LT_KERNELARGS | LT_MBMODULE,
2157   return;    entry->lines);
2158      }  
2159     /* failed to find one */
2160     if (!line) {
2161     notSuitablePrintf(entry, 0, "no line found\n");
2162     return 0;
2163     }
2164    
2165      if (hasNew)   for (i = 1; i < line->numElements; i++)
2166   config->fallbackImage++;   if (!strncasecmp
2167            (line->elements[i].item, "root=", 5))
2168      /* count the number of entries erased before this one */   break;
2169      for (j = 0; j < config->fallbackImage; j++) {   if (i < line->numElements)
2170   entry2 = findEntryByIndex(config, j);   dev = line->elements[i].item + 5;
2171   if (entry2->skip) config->fallbackImage--;   else {
2172      }   notSuitablePrintf(entry, 0,
2173      "no root= entry found\n");
2174     /* it failed too...  can't find root= */
2175     return 0;
2176     }
2177     }
2178     }
2179    
2180     dev = getpathbyspec(dev);
2181     if (!getpathbyspec(dev)) {
2182     notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n",
2183      dev);
2184     return 0;
2185     } else
2186     dev = getpathbyspec(dev);
2187    
2188     rootdev = findDiskForRoot();
2189     if (!rootdev) {
2190     notSuitablePrintf(entry, 0, "can't find root device\n");
2191     return 0;
2192     }
2193    
2194     if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
2195     notSuitablePrintf(entry, 0,
2196      "uuid missing: rootdev %s, dev %s\n",
2197      getuuidbydev(rootdev), getuuidbydev(dev));
2198     free(rootdev);
2199     return 0;
2200     }
2201    
2202     if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
2203     notSuitablePrintf(entry, 0,
2204      "uuid mismatch: rootdev %s, dev %s\n",
2205      getuuidbydev(rootdev), getuuidbydev(dev));
2206     free(rootdev);
2207     return 0;
2208     }
2209    
2210     free(rootdev);
2211     notSuitablePrintf(entry, 1, "\n");
2212    
2213     return 1;
2214  }  }
2215    
2216  void displayEntry(struct singleEntry * entry, const char * prefix, int index) {  /* returns the first match on or after the one pointed to by index (if index
2217      struct singleLine * line;     is not NULL) which is not marked as skip */
2218      char * root = NULL;  struct singleEntry *findEntryByPath(struct grubConfig *config,
2219      int i;      const char *kernel, const char *prefix,
2220        int *index)
2221      printf("index=%d\n", index);  {
2222     struct singleEntry *entry = NULL;
2223      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   struct singleLine *line;
2224      if (!line) {   int i;
2225          printf("non linux entry\n");   char *chptr;
2226          return;   char *rootspec = NULL;
2227      }   enum lineType_e checkType = LT_KERNEL;
2228    
2229     if (isdigit(*kernel)) {
2230     int *indexVars = alloca(sizeof(*indexVars) * strlen(kernel));
2231    
2232     i = 0;
2233     indexVars[i] = strtol(kernel, &chptr, 10);
2234     while (*chptr == ',') {
2235     i++;
2236     kernel = chptr + 1;
2237     indexVars[i] = strtol(kernel, &chptr, 10);
2238     }
2239    
2240      printf("kernel=%s\n", line->elements[1].item);   if (*chptr) {
2241     /* can't parse it, bail */
2242     return NULL;
2243     }
2244    
2245      if (line->numElements >= 3) {   indexVars[i + 1] = -1;
  printf("args=\"");  
  i = 2;  
  while (i < line->numElements) {  
     if (!strncmp(line->elements[i].item, "root=", 5)) {  
  root = line->elements[i].item + 5;  
     } else {  
  printf("%s%s", line->elements[i].item,  
        line->elements[i].indent);  
     }  
2246    
2247      i++;   i = 0;
2248   }   if (index) {
2249   printf("\"\n");   while (i < *index) {
2250      } else {   i++;
2251   line = getLineByType(LT_KERNELARGS, entry->lines);   if (indexVars[i] == -1)
2252   if (line) {   return NULL;
2253      char * s;   }
2254     }
2255    
2256      printf("args=\"");   entry = findEntryByIndex(config, indexVars[i]);
2257      i = 1;   if (!entry)
2258      while (i < line->numElements) {   return NULL;
2259   if (!strncmp(line->elements[i].item, "root=", 5)) {  
2260      root = line->elements[i].item + 5;   line =
2261        getLineByType(LT_KERNEL | LT_HYPER | LT_KERNEL_EFI |
2262      LT_KERNEL_16, entry->lines);
2263     if (!line)
2264     return NULL;
2265    
2266     if (index)
2267     *index = indexVars[i];
2268     return entry;
2269     }
2270    
2271     if (!strcmp(kernel, "DEFAULT")) {
2272     if (index && *index > config->defaultImage) {
2273     entry = NULL;
2274   } else {   } else {
2275      s = line->elements[i].item;   entry = findEntryByIndex(config, config->defaultImage);
2276     if (entry && entry->skip)
2277     entry = NULL;
2278     else if (index)
2279     *index = config->defaultImage;
2280     }
2281     } else if (!strcmp(kernel, "ALL")) {
2282     if (index)
2283     i = *index;
2284     else
2285     i = 0;
2286    
2287     while ((entry = findEntryByIndex(config, i))) {
2288     if (!entry->skip)
2289     break;
2290     i++;
2291     }
2292    
2293      printf("%s%s", s, line->elements[i].indent);   if (entry && index)
2294     *index = i;
2295     } else {
2296     if (index)
2297     i = *index;
2298     else
2299     i = 0;
2300    
2301     if (!strncmp(kernel, "TITLE=", 6)) {
2302     prefix = "";
2303     checkType = LT_TITLE | LT_MENUENTRY;
2304     kernel += 6;
2305   }   }
2306    
2307   i++;   for (entry = findEntryByIndex(config, i); entry;
2308      }       entry = entry->next, i++) {
2309     if (entry->skip)
2310     continue;
2311    
2312     dbgPrintf("findEntryByPath looking for %d %s in %p\n",
2313      checkType, kernel, entry);
2314    
2315     /* check all the lines matching checkType */
2316     for (line = entry->lines; line; line = line->next) {
2317     enum lineType_e ct = checkType;
2318     if (entry->multiboot && checkType == LT_KERNEL)
2319     ct = LT_KERNEL | LT_KERNEL_EFI |
2320        LT_MBMODULE | LT_HYPER |
2321        LT_KERNEL_16;
2322     else if (checkType & LT_KERNEL)
2323     ct = checkType | LT_KERNEL_EFI |
2324        LT_KERNEL_16;
2325     line = getLineByType(ct, line);
2326     if (!line)
2327     break; /* not found in this entry */
2328    
2329     if (line && line->type != LT_MENUENTRY &&
2330        line->numElements >= 2) {
2331     rootspec =
2332        getRootSpecifier(line->elements[1].
2333         item);
2334     if (!strcmp
2335        (line->elements[1].item +
2336         ((rootspec !=
2337           NULL) ? strlen(rootspec) : 0),
2338         kernel + strlen(prefix)))
2339     break;
2340     }
2341     if (line->type == LT_MENUENTRY &&
2342        !strcmp(line->elements[1].item, kernel))
2343     break;
2344     }
2345    
2346     /* make sure this entry has a kernel identifier; this skips
2347     * non-Linux boot entries (could find netbsd etc, though, which is
2348     * unfortunate)
2349     */
2350     if (line
2351        && getLineByType(LT_KERNEL | LT_HYPER |
2352         LT_KERNEL_EFI | LT_KERNEL_16,
2353         entry->lines))
2354     break; /* found 'im! */
2355     }
2356    
2357      s = line->elements[i - 1].indent;   if (index)
2358      printf("\"\n");   *index = i;
2359   }   }
     }  
2360    
2361      if (!root) {   return entry;
2362   line = getLineByType(LT_ROOT, entry->lines);  }
  if (line && line->numElements >= 2)  
     root=line->elements[1].item;  
     }  
2363    
2364      if (root) {  struct singleEntry *findEntryByTitle(struct grubConfig *cfg, char *title,
2365   char * s = alloca(strlen(root) + 1);       int *index)
2366    {
2367   strcpy(s, root);   struct singleEntry *entry;
2368   if (s[strlen(s) - 1] == '"')   struct singleLine *line;
2369      s[strlen(s) - 1] = '\0';   int i;
2370   /* make sure the root doesn't have a trailing " */   char *newtitle;
2371   printf("root=%s\n", s);  
2372      }   for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2373     if (index && i < *index)
2374     continue;
2375     line = getLineByType(LT_TITLE, entry->lines);
2376     if (!line)
2377     line = getLineByType(LT_MENUENTRY, entry->lines);
2378     if (!line)
2379     continue;
2380     newtitle = grub2ExtractTitle(line);
2381     if (!newtitle)
2382     continue;
2383     if (!strcmp(title, newtitle))
2384     break;
2385     }
2386    
2387      line = getLineByType(LT_INITRD, entry->lines);   if (!entry)
2388     return NULL;
2389    
2390      if (line && line->numElements >= 2) {   if (index)
2391   printf("initrd=%s", prefix);   *index = i;
2392   for (i = 1; i < line->numElements; i++)   return entry;
     printf("%s%s", line->elements[i].item, line->elements[i].indent);  
  printf("\n");  
     }  
2393  }  }
2394    
2395  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  struct singleEntry *findEntryByIndex(struct grubConfig *cfg, int index)
2396      FILE * in;  {
2397      char buf[1024];   struct singleEntry *entry;
     char * chptr;  
     char * start;  
     char * param;  
   
     in = fopen("/etc/conf.d/grub", "r");  
     if (!in) return 1;  
   
     if (lbaPtr) *lbaPtr = 0;  
     if (bootPtr) *bootPtr = NULL;  
   
     while (fgets(buf, sizeof(buf), in)) {  
  start = buf;  
  while (isspace(*start)) start++;  
  if (*start == '#') continue;  
   
  chptr = strchr(start, '=');  
  if (!chptr) continue;  
  chptr--;  
  while (*chptr && isspace(*chptr)) chptr--;  
  chptr++;  
  *chptr = '\0';  
   
  param = chptr + 1;  
  while (*param && isspace(*param)) param++;  
  if (*param == '=') {  
     param++;  
     while (*param && isspace(*param)) param++;  
  }  
   
  chptr = param;  
  while (*chptr && !isspace(*chptr)) chptr++;  
  *chptr = '\0';  
   
  if (!strcmp(start, "forcelba") && !strcmp(param, "1") && lbaPtr)  
     *lbaPtr = 1;  
  else if (!strcmp(start, "boot") && bootPtr)  
     *bootPtr = strdup(param);  
     }  
   
     fclose(in);  
2398    
2399      return 0;   entry = cfg->entries;
2400  }   while (index && entry) {
2401     entry = entry->next;
2402     index--;
2403     }
2404    
2405  void dumpSysconfigGrub(void) {   return entry;
     char * boot;  
     int lba;  
   
     if (!parseSysconfigGrub(&lba, &boot)) {  
  if (lba) printf("lba\n");  
  if (boot) printf("boot=%s\n", boot);  
     }  
2406  }  }
2407    
2408  int displayInfo(struct grubConfig * config, char * kernel,  /* Find a good template to use for the new kernel. An entry is
2409   const char * prefix) {   * good if the kernel and mkinitrd exist (even if the entry
2410      int i = 0;   * is going to be removed). Try and use the default entry, but
2411      struct singleEntry * entry;   * if that doesn't work just take the first. If we can't find one,
2412      struct singleLine * line;   * bail. */
2413    struct singleEntry *findTemplate(struct grubConfig *cfg, const char *prefix,
2414      entry = findEntryByPath(config, kernel, prefix, &i);   int *indexPtr, int skipRemoved, int flags)
2415      if (!entry) {  {
2416   fprintf(stderr, _("grubby: kernel not found\n"));   struct singleEntry *entry, *entry2;
2417   return 1;   int index;
     }  
2418    
2419      /* this is a horrible hack to support /etc/conf.d/grub; there must   if (cfg->cfi->defaultIsSaved) {
2420         be a better way */   if (cfg->cfi->getEnv) {
2421      if (config->cfi == &grubConfigType) {   char *defTitle =
2422   dumpSysconfigGrub();      cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2423      } else {   if (defTitle) {
2424   line = getLineByType(LT_BOOT, config->theLines);   int index = 0;
2425   if (line && line->numElements >= 1) {   if (isnumber(defTitle)) {
2426      printf("boot=%s\n", line->elements[1].item);   index = atoi(defTitle);
2427     entry = findEntryByIndex(cfg, index);
2428     } else {
2429     entry =
2430        findEntryByTitle(cfg, defTitle,
2431         &index);
2432     }
2433     if (entry
2434        && suitableImage(entry, prefix, skipRemoved,
2435         flags)) {
2436     cfg->defaultImage = index;
2437     if (indexPtr)
2438     *indexPtr = index;
2439     return entry;
2440     }
2441     }
2442     }
2443     } else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) {
2444     entry = findEntryByIndex(cfg, cfg->defaultImage);
2445     if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2446     if (indexPtr)
2447     *indexPtr = cfg->defaultImage;
2448     return entry;
2449     }
2450   }   }
2451    
2452   line = getLineByType(LT_LBA, config->theLines);   index = 0;
2453   if (line) printf("lba\n");   while ((entry = findEntryByIndex(cfg, index))) {
2454      }   if (suitableImage(entry, prefix, skipRemoved, flags)) {
2455     int j, unmodifiedIndex;
2456      displayEntry(entry, prefix, i);  
2457     unmodifiedIndex = index;
2458      i++;  
2459      while ((entry = findEntryByPath(config, kernel, prefix, &i))) {   for (j = 0; j < unmodifiedIndex; j++) {
2460   displayEntry(entry, prefix, i);   entry2 = findEntryByIndex(cfg, j);
2461   i++;   if (entry2->skip)
2462      }   index--;
2463     }
2464     if (indexPtr)
2465     *indexPtr = index;
2466    
2467      return 0;   return entry;
2468  }   }
2469    
2470  struct singleLine * addLineTmpl(struct singleEntry * entry,   index++;
  struct singleLine * tmplLine,  
  struct singleLine * prevLine,  
  const char * val,  
  struct configFileInfo * cfi)  
 {  
     struct singleLine * newLine = lineDup(tmplLine);  
   
     if (val) {  
  /* override the inherited value with our own.  
  * This is a little weak because it only applies to elements[1]  
  */  
  if (newLine->numElements > 1)  
     removeElement(newLine, 1);  
  insertElement(newLine, val, 1, cfi);  
   
  /* but try to keep the rootspec from the template... sigh */  
  if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD)) {  
     char * rootspec = getRootSpecifier(tmplLine->elements[1].item);  
     if (rootspec != NULL) {  
  free(newLine->elements[1].item);  
  newLine->elements[1].item =  
     sdupprintf("%s%s", rootspec, val);  
     }  
2471   }   }
     }  
2472    
2473      dbgPrintf("addLineTmpl(%s)\n", newLine->numElements ?   fprintf(stderr,
2474        newLine->elements[0].item : "");   _("grubby fatal error: unable to find a suitable template\n"));
2475    
2476      if (!entry->lines) {   return NULL;
  /* first one on the list */  
  entry->lines = newLine;  
     } else if (prevLine) {  
  /* add after prevLine */  
  newLine->next = prevLine->next;  
  prevLine->next = newLine;  
     }  
   
     return newLine;  
2477  }  }
2478    
2479  /* val may be NULL */  char *findBootPrefix(void)
2480  struct singleLine *  addLine(struct singleEntry * entry,  {
2481       struct configFileInfo * cfi,   struct stat sb, sb2;
      enum lineType_e type, char * defaultIndent,  
      const char * val) {  
     struct singleLine * line, * prev;  
     struct keywordTypes * kw;  
     struct singleLine tmpl;  
   
     /* NB: This function shouldn't allocate items on the heap, rather on the  
      * stack since it calls addLineTmpl which will make copies.  
      */  
   
     if (type == LT_TITLE && cfi->titleBracketed) {  
  /* we're doing a bracketed title (zipl) */  
  tmpl.type = type;  
  tmpl.numElements = 1;  
  tmpl.elements = alloca(sizeof(*tmpl.elements));  
  tmpl.elements[0].item = alloca(strlen(val)+3);  
  sprintf(tmpl.elements[0].item, "[%s]", val);  
  tmpl.elements[0].indent = "";  
  val = NULL;  
     } else if (type == LT_MENUENTRY) {  
  char *lineend = "--class gnu-linux --class gnu --class os {";  
  if (!val) {  
     fprintf(stderr, "Line type LT_MENUENTRY requires a value\n");  
     abort();  
  }  
  kw = getKeywordByType(type, cfi);  
  if (!kw) {  
     fprintf(stderr, "Looking up keyword for unknown type %d\n", type);  
     abort();  
  }  
  tmpl.indent = "";  
  tmpl.type = type;  
  tmpl.numElements = 3;  
  tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);  
  tmpl.elements[0].item = kw->key;  
  tmpl.elements[0].indent = alloca(2);  
  sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);  
  tmpl.elements[1].item = (char *)val;  
  tmpl.elements[1].indent = alloca(2);  
  sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);  
  tmpl.elements[2].item = alloca(strlen(lineend)+1);  
  strcpy(tmpl.elements[2].item, lineend);  
  tmpl.elements[2].indent = "";  
     } else {  
  kw = getKeywordByType(type, cfi);  
  if (!kw) {  
     fprintf(stderr, "Looking up keyword for unknown type %d\n", type);  
     abort();  
  }  
  tmpl.type = type;  
  tmpl.numElements = val ? 2 : 1;  
  tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);  
  tmpl.elements[0].item = kw->key;  
  tmpl.elements[0].indent = alloca(2);  
  sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);  
  if (val) {  
     tmpl.elements[1].item = (char *)val;  
     tmpl.elements[1].indent = "";  
  }  
     }  
2482    
2483      /* The last non-empty line gives us the indention to us and the line   stat("/", &sb);
2484         to insert after. Note that comments are considered empty lines, which  #ifdef __ia64__
2485         may not be ideal? If there are no lines or we are looking at the   stat("/boot/efi/EFI/redhat/", &sb2);
2486         first line, we use defaultIndent (the first line is normally indented  #else
2487         differently from the rest) */   stat("/boot", &sb2);
2488      for (line = entry->lines, prev = NULL; line; line = line->next) {  #endif
  if (line->numElements) prev = line;  
  /* fall back on the last line if prev isn't otherwise set */  
  if (!line->next && !prev) prev = line;  
     }  
2489    
2490      struct singleLine *menuEntry;   if (sb.st_dev == sb2.st_dev)
2491      menuEntry = getLineByType(LT_MENUENTRY, entry->lines);   return strdup("");
     if (tmpl.type == LT_ENTRY_END) {  
  if (menuEntry)  
     tmpl.indent = menuEntry->indent;  
  else  
     tmpl.indent = defaultIndent ?: "";  
     } else if (tmpl.type != LT_MENUENTRY) {  
  if (menuEntry)  
     tmpl.indent = "\t";  
  else if (prev == entry->lines)  
     tmpl.indent = defaultIndent ?: "";  
  else  
     tmpl.indent = prev->indent;  
     }  
2492    
2493      return addLineTmpl(entry, &tmpl, prev, val, cfi);  #ifdef __ia64__
2494     return strdup("/boot/efi/EFI/redhat/");
2495    #else
2496     return strdup("/boot");
2497    #endif
2498  }  }
2499    
2500  void removeLine(struct singleEntry * entry, struct singleLine * line) {  void markRemovedImage(struct grubConfig *cfg, const char *image,
2501      struct singleLine * prev;        const char *prefix)
2502      int i;  {
2503     struct singleEntry *entry;
2504    
2505      for (i = 0; i < line->numElements; i++) {   if (!image)
2506   free(line->elements[i].item);   return;
  free(line->elements[i].indent);  
     }  
     free(line->elements);  
     free(line->indent);  
2507    
2508      if (line == entry->lines) {   /* check and see if we're removing the default image */
2509   entry->lines = line->next;   if (isdigit(*image)) {
2510      } else {   entry = findEntryByPath(cfg, image, prefix, NULL);
2511   prev = entry->lines;   if (entry)
2512   while (prev->next != line) prev = prev->next;   entry->skip = 1;
2513   prev->next = line->next;   return;
2514      }   }
2515    
2516      free(line);   while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
2517     entry->skip = 1;
2518  }  }
2519    
2520  static int isquote(char q)  void setDefaultImage(struct grubConfig *config, int isAddingBootEntry,
2521         const char *defaultKernelPath, int newBootEntryIsDefault,
2522         const char *prefix, int flags,
2523         int newDefaultBootEntryIndex, int newBootEntryIndex)
2524  {  {
2525      if (q == '\'' || q == '\"')   struct singleEntry *bootEntry, *newDefault;
2526   return 1;   int indexToVerify, firstKernelEntryIndex, currentLookupIndex;
     return 0;  
 }  
2527    
2528  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)          /* initialize */
2529  {          currentLookupIndex = FIRST_ENTRY_INDEX;
     struct singleLine newLine = {  
  .indent = tmplLine->indent,  
  .type = tmplLine->type,  
  .next = tmplLine->next,  
     };  
     int firstQuotedItem = -1;  
     int quoteLen = 0;  
     int j;  
     int element = 0;  
     char *c;  
   
     c = malloc(strlen(tmplLine->elements[0].item) + 1);  
     strcpy(c, tmplLine->elements[0].item);  
     insertElement(&newLine, c, element++, cfi);  
     free(c);  
     c = NULL;  
   
     for (j = 1; j < tmplLine->numElements; j++) {  
  if (firstQuotedItem == -1) {  
     quoteLen += strlen(tmplLine->elements[j].item);  
       
     if (isquote(tmplLine->elements[j].item[0])) {  
  firstQuotedItem = j;  
         quoteLen += strlen(tmplLine->elements[j].indent);  
     } else {  
  c = malloc(quoteLen + 1);  
  strcpy(c, tmplLine->elements[j].item);  
  insertElement(&newLine, c, element++, cfi);  
  free(c);  
  quoteLen = 0;  
     }  
  } else {  
     int itemlen = strlen(tmplLine->elements[j].item);  
     quoteLen += itemlen;  
     quoteLen += strlen(tmplLine->elements[j].indent);  
       
     if (isquote(tmplLine->elements[j].item[itemlen - 1])) {  
  c = malloc(quoteLen + 1);  
  c[0] = '\0';  
  for (int i = firstQuotedItem; i < j+1; i++) {  
     strcat(c, tmplLine->elements[i].item);  
     strcat(c, tmplLine->elements[i].indent);  
  }  
  insertElement(&newLine, c, element++, cfi);  
  free(c);  
   
  firstQuotedItem = -1;  
  quoteLen = 0;  
     }  
  }  
     }  
     while (tmplLine->numElements)  
  removeElement(tmplLine, 0);  
     if (tmplLine->elements)  
  free(tmplLine->elements);  
   
     tmplLine->numElements = newLine.numElements;  
     tmplLine->elements = newLine.elements;  
 }  
   
 static void insertElement(struct singleLine * line,  
   const char * item, int insertHere,  
   struct configFileInfo * cfi)  
 {  
     struct keywordTypes * kw;  
     char indent[2] = "";  
   
     /* sanity check */  
     if (insertHere > line->numElements) {  
  dbgPrintf("insertElement() adjusting insertHere from %d to %d\n",  
   insertHere, line->numElements);  
  insertHere = line->numElements;  
     }  
2530    
2531      line->elements = realloc(line->elements, (line->numElements + 1) *   /* handle the two cases where the user explictly picks the default
2532       sizeof(*line->elements));   * boot entry index as it would exist post-modification */
     memmove(&line->elements[insertHere+1],  
     &line->elements[insertHere],  
     (line->numElements - insertHere) *  
     sizeof(*line->elements));  
     line->elements[insertHere].item = strdup(item);  
   
     kw = getKeywordByType(line->type, cfi);  
   
     if (line->numElements == 0) {  
  indent[0] = '\0';  
     } else if (insertHere == 0) {  
  indent[0] = kw->nextChar;  
     } else if (kw->separatorChar != '\0') {  
  indent[0] = kw->separatorChar;  
     } else {  
  indent[0] = ' ';  
     }  
2533    
2534      if (insertHere > 0 && line->elements[insertHere-1].indent[0] == '\0') {   /* Case 1: user chose to make the latest boot entry the default */
2535   /* move the end-of-line forward */   if (newBootEntryIsDefault) {
2536   line->elements[insertHere].indent =   config->defaultImage = newBootEntryIndex;
2537      line->elements[insertHere-1].indent;   return;
2538   line->elements[insertHere-1].indent = strdup(indent);   }
2539      } else {  
2540   line->elements[insertHere].indent = strdup(indent);   /* Case 2: user picked an arbitrary index as the default boot entry */
2541      }   if (newDefaultBootEntryIndex >= FIRST_ENTRY_INDEX
2542        && config->cfi->defaultIsIndex) {
2543     indexToVerify = newDefaultBootEntryIndex;
2544    
2545     /* user chose to make latest boot entry the default */
2546     if (newDefaultBootEntryIndex == newBootEntryIndex) {
2547     config->defaultImage = newBootEntryIndex;
2548     return;
2549     }
2550    
2551      line->numElements++;   /* the user picks the default index based on the
2552     * order of the bootloader configuration after
2553     * modification; ensure we are checking for the
2554     * existence of the correct entry */
2555     if (newBootEntryIndex < newDefaultBootEntryIndex) {
2556     if (!config->isModified)
2557     indexToVerify--;
2558     }
2559    
2560      dbgPrintf("insertElement(%s, '%s%s', %d)\n",   /* verify the user selected index will exist */
2561        line->elements[0].item,   if (findEntryByIndex(config, indexToVerify)) {
2562        line->elements[insertHere].item,   config->defaultImage = newDefaultBootEntryIndex;
2563        line->elements[insertHere].indent,   } else {
2564        insertHere);   config->defaultImage = NO_DEFAULT_ENTRY;
2565  }   }
2566    
2567  static void removeElement(struct singleLine * line, int removeHere) {   return;
2568      int i;   }
2569    
2570      /* sanity check */   /* handle cases where the index value may shift */
     if (removeHere >= line->numElements) return;  
2571    
2572      dbgPrintf("removeElement(%s, %d:%s)\n", line->elements[0].item,   /* check validity of existing default or first-entry-found
2573        removeHere, line->elements[removeHere].item);     selection */
2574     if (defaultKernelPath) {
2575                    /* we must initialize this */
2576                    firstKernelEntryIndex = 0;
2577     /* user requested first-entry-found */
2578     if (!findEntryByPath(config, defaultKernelPath,
2579         prefix, &firstKernelEntryIndex)) {
2580     /* don't change default if can't find match */
2581     config->defaultImage = NO_DEFAULT_ENTRY;
2582     return;
2583     }
2584    
2585      free(line->elements[removeHere].item);   config->defaultImage = firstKernelEntryIndex;
2586    
2587      if (removeHere > 1) {   /* this is where we start looking for decrement later */
2588   /* previous argument gets this argument's post-indentation */   currentLookupIndex = config->defaultImage;
  free(line->elements[removeHere-1].indent);  
  line->elements[removeHere-1].indent =  
     line->elements[removeHere].indent;  
     } else {  
  free(line->elements[removeHere].indent);  
     }  
2589    
2590      /* now collapse the array, but don't bother to realloc smaller */   if (isAddingBootEntry && !config->isModified &&
2591      for (i = removeHere; i < line->numElements - 1; i++)      (newBootEntryIndex < config->defaultImage)) {
2592   line->elements[i] = line->elements[i + 1];   /* increment because new entry added before default */
2593     config->defaultImage++;
2594      line->numElements--;   }
2595  }   } else {
2596                    /* check to see if the default is stored in the environment */
2597  int argMatch(const char * one, const char * two) {                  if (config->defaultImage < FIRST_ENTRY_INDEX) {
2598      char * first, * second;                      if (config->defaultImage == DEFAULT_SAVED || config->defaultImage == DEFAULT_SAVED_GRUB2)
2599      char * chptr;                      {
2600                            if (config->cfi->defaultIsSaved) {
2601      first = strcpy(alloca(strlen(one) + 1), one);                              if (config->cfi->getEnv) {
2602      second = strcpy(alloca(strlen(two) + 1), two);                                  char *defaultTitle = config->cfi->getEnv(config->cfi, "saved_entry");
2603    
2604      chptr = strchr(first, '=');                                  if (defaultTitle) {
2605      if (chptr) *chptr = '\0';                                      if (isnumber(defaultTitle)) {
2606                                            currentLookupIndex = atoi(defaultTitle);
2607      chptr = strchr(second, '=');                                      } else {
2608      if (chptr) *chptr = '\0';                                          findEntryByTitle(config, defaultTitle, &currentLookupIndex);
2609                                        }
2610      return strcmp(first, second);                                      /* set the default Image to an actual index */
2611  }                                      config->defaultImage = currentLookupIndex;
2612                                    }
2613  int updateActualImage(struct grubConfig * cfg, const char * image,                              }
2614                        const char * prefix, const char * addArgs,                           }
2615                        const char * removeArgs, int multibootArgs) {                      }
2616      struct singleEntry * entry;                  } else {
2617      struct singleLine * line, * rootLine;                          /* use pre-existing default entry from the file*/
2618      int index = 0;                          currentLookupIndex = config->defaultImage;
2619      int i, k;                  }
     const char ** newArgs, ** oldArgs;  
     const char ** arg;  
     int useKernelArgs, useRoot;  
     int firstElement;  
     int *usedElements;  
     int doreplace;  
   
     if (!image) return 0;  
   
     if (!addArgs) {  
  newArgs = malloc(sizeof(*newArgs));  
  *newArgs = NULL;  
     } else {  
  if (poptParseArgvString(addArgs, NULL, &newArgs)) {  
     fprintf(stderr,  
     _("grubby: error separating arguments '%s'\n"), addArgs);  
     return 1;  
  }  
     }  
2620    
2621      if (!removeArgs) {   if (isAddingBootEntry
2622   oldArgs = malloc(sizeof(*oldArgs));      && (newBootEntryIndex <= config->defaultImage)) {
2623   *oldArgs = NULL;   config->defaultImage++;
     } else {  
  if (poptParseArgvString(removeArgs, NULL, &oldArgs)) {  
     fprintf(stderr,  
     _("grubby: error separating arguments '%s'\n"), removeArgs);  
             free(newArgs);  
     return 1;  
  }  
     }  
2624    
2625     if (config->isModified) {
2626     currentLookupIndex++;
2627     }
2628     }
2629     }
2630    
2631      useKernelArgs = (getKeywordByType(LT_KERNELARGS, cfg->cfi)   /* sanity check - is this entry index valid? */
2632       && (!multibootArgs || cfg->cfi->mbConcatArgs));   bootEntry = findEntryByIndex(config, currentLookupIndex);
2633    
2634      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)   if ((bootEntry && bootEntry->skip) || !bootEntry) {
2635         && !multibootArgs);   /* entry is to be skipped or is invalid */
2636     if (isAddingBootEntry) {
2637     config->defaultImage = newBootEntryIndex;
2638     return;
2639     }
2640     newDefault =
2641        findTemplate(config, prefix, &config->defaultImage, 1,
2642     flags);
2643     if (!newDefault) {
2644     config->defaultImage = NO_DEFAULT_ENTRY;
2645     }
2646    
2647      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {   return;
2648     }
2649    
2650   if (multibootArgs && !entry->multiboot)   currentLookupIndex--;
     continue;  
2651    
2652   /* Determine where to put the args.  If this config supports   /* decrement index by the total number of entries deleted */
  * LT_KERNELARGS, use that.  Otherwise use  
  * LT_HYPER/LT_KERNEL/LT_MBMODULE lines.  
  */  
  if (useKernelArgs) {  
     line = getLineByType(LT_KERNELARGS, entry->lines);  
     if (!line) {  
  /* no LT_KERNELARGS, need to add it */  
  line = addLine(entry, cfg->cfi, LT_KERNELARGS,  
        cfg->secondaryIndent, NULL);  
     }  
     firstElement = 1;  
   
  } else if (multibootArgs) {  
     line = getLineByType(LT_HYPER, entry->lines);  
     if (!line) {  
  /* a multiboot entry without LT_HYPER? */  
  continue;  
     }  
     firstElement = 2;  
   
  } else {  
     line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines);  
     if (!line) {  
  /* no LT_KERNEL or LT_MBMODULE in this entry? */  
  continue;  
     }  
     firstElement = 2;  
  }  
2653    
2654   /* handle the elilo case which does:   for (indexToVerify = currentLookupIndex;
2655   *   append="hypervisor args -- kernel args"       indexToVerify >= FIRST_ENTRY_INDEX; indexToVerify--) {
  */  
  if (entry->multiboot && cfg->cfi->mbConcatArgs) {  
     /* this is a multiboot entry, make sure there's  
      * -- on the args line  
      */  
     for (i = firstElement; i < line->numElements; i++) {  
  if (!strcmp(line->elements[i].item, "--"))  
     break;  
     }  
     if (i == line->numElements) {  
  /* assume all existing args are kernel args,  
  * prepend -- to make it official  
  */  
  insertElement(line, "--", firstElement, cfg->cfi);  
  i = firstElement;  
     }  
     if (!multibootArgs) {  
  /* kernel args start after the -- */  
  firstElement = i + 1;  
     }  
  } else if (cfg->cfi->mbConcatArgs) {  
     /* this is a non-multiboot entry, remove hyper args */  
     for (i = firstElement; i < line->numElements; i++) {  
  if (!strcmp(line->elements[i].item, "--"))  
     break;  
     }  
     if (i < line->numElements) {  
  /* remove args up to -- */  
  while (strcmp(line->elements[firstElement].item, "--"))  
     removeElement(line, firstElement);  
  /* remove -- */  
  removeElement(line, firstElement);  
     }  
  }  
   
         usedElements = calloc(line->numElements, sizeof(*usedElements));  
   
  for (k = 0, arg = newArgs; *arg; arg++, k++) {  
   
     doreplace = 1;  
     for (i = firstElement; i < line->numElements; i++) {  
  if (multibootArgs && cfg->cfi->mbConcatArgs &&  
     !strcmp(line->elements[i].item, "--"))  
  {  
     /* reached the end of hyper args, insert here */  
     doreplace = 0;  
     break;    
  }  
                 if (usedElements[i])  
                     continue;  
  if (!argMatch(line->elements[i].item, *arg)) {  
                     usedElements[i]=1;  
     break;  
                 }  
             }  
2656    
2657      if (i < line->numElements && doreplace) {   bootEntry = findEntryByIndex(config, indexToVerify);
  /* direct replacement */  
  free(line->elements[i].item);  
  line->elements[i].item = strdup(*arg);  
2658    
2659      } else if (useRoot && !strncmp(*arg, "root=/dev/", 10)) {   if (bootEntry && bootEntry->skip) {
2660   /* root= replacement */   config->defaultImage--;
  rootLine = getLineByType(LT_ROOT, entry->lines);  
  if (rootLine) {  
     free(rootLine->elements[1].item);  
     rootLine->elements[1].item = strdup(*arg + 5);  
  } else {  
     rootLine = addLine(entry, cfg->cfi, LT_ROOT,  
        cfg->secondaryIndent, *arg + 5);  
2661   }   }
     }  
   
     else {  
  /* insert/append */  
  insertElement(line, *arg, i, cfg->cfi);  
  usedElements = realloc(usedElements, line->numElements *  
        sizeof(*usedElements));  
  memmove(&usedElements[i + 1], &usedElements[i],  
  line->numElements - i - 1);  
  usedElements[i] = 1;  
   
  /* if we updated a root= here even though there is a  
    LT_ROOT available we need to remove the LT_ROOT entry  
    (this will happen if we switch from a device to a label) */  
  if (useRoot && !strncmp(*arg, "root=", 5)) {  
     rootLine = getLineByType(LT_ROOT, entry->lines);  
     if (rootLine)  
  removeLine(entry, rootLine);  
  }  
     }  
  }  
   
         free(usedElements);  
   
  for (arg = oldArgs; *arg; arg++) {  
     for (i = firstElement; i < line->numElements; i++) {  
  if (multibootArgs && cfg->cfi->mbConcatArgs &&  
     !strcmp(line->elements[i].item, "--"))  
     /* reached the end of hyper args, stop here */  
     break;  
  if (!argMatch(line->elements[i].item, *arg)) {  
     removeElement(line, i);  
     break;  
  }  
     }  
     /* handle removing LT_ROOT line too */  
     if (useRoot && !strncmp(*arg, "root=", 5)) {  
  rootLine = getLineByType(LT_ROOT, entry->lines);  
  if (rootLine)  
     removeLine(entry, rootLine);  
     }  
  }  
   
  if (line->numElements == 1) {  
     /* don't need the line at all (note it has to be a  
        LT_KERNELARGS for this to happen */  
     removeLine(entry, line);  
2662   }   }
2663      }  }
2664    
2665      free(newArgs);  void setFallbackImage(struct grubConfig *config, int hasNew)
2666      free(oldArgs);  {
2667     struct singleEntry *entry, *entry2;
2668     int j;
2669    
2670      return 0;   if (config->fallbackImage == -1)
2671  }   return;
2672    
2673  int updateImage(struct grubConfig * cfg, const char * image,   entry = findEntryByIndex(config, config->fallbackImage);
2674                  const char * prefix, const char * addArgs,   if (!entry || entry->skip) {
2675                  const char * removeArgs,   config->fallbackImage = -1;
2676                  const char * addMBArgs, const char * removeMBArgs) {   return;
     int rc = 0;  
   
     if (!image) return rc;  
   
     /* update the main args first... */  
     if (addArgs || removeArgs)  
         rc = updateActualImage(cfg, image, prefix, addArgs, removeArgs, 0);  
     if (rc) return rc;  
   
     /* and now any multiboot args */  
     if (addMBArgs || removeMBArgs)  
         rc = updateActualImage(cfg, image, prefix, addMBArgs, removeMBArgs, 1);  
     return rc;  
 }  
   
 int updateInitrd(struct grubConfig * cfg, const char * image,  
                  const char * prefix, const char * initrd) {  
     struct singleEntry * entry;  
     struct singleLine * line, * kernelLine, *endLine = NULL;  
     int index = 0;  
   
     if (!image) return 0;  
   
     for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {  
         kernelLine = getLineByType(LT_KERNEL, entry->lines);  
         if (!kernelLine) continue;  
   
         line = getLineByType(LT_INITRD, entry->lines);  
         if (line)  
             removeLine(entry, line);  
         if (prefix) {  
             int prefixLen = strlen(prefix);  
             if (!strncmp(initrd, prefix, prefixLen))  
                 initrd += prefixLen;  
         }  
  endLine = getLineByType(LT_ENTRY_END, entry->lines);  
  if (endLine)  
     removeLine(entry, endLine);  
         line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);  
         if (!line)  
     return 1;  
  if (endLine) {  
     line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);  
             if (!line)  
  return 1;  
2677   }   }
2678    
2679          break;   if (hasNew)
2680      }   config->fallbackImage++;
2681    
2682      return 0;   /* count the number of entries erased before this one */
2683     for (j = 0; j < config->fallbackImage; j++) {
2684     entry2 = findEntryByIndex(config, j);
2685     if (entry2->skip)
2686     config->fallbackImage--;
2687     }
2688  }  }
2689    
2690  int checkDeviceBootloader(const char * device, const unsigned char * boot) {  void displayEntry(struct grubConfig *config, struct singleEntry *entry, const char *prefix, int index)
2691      int fd;  {
2692      unsigned char bootSect[512];   struct singleLine *line;
2693      int offset;   char *root = NULL;
2694     int i;
2695      fd = open(device, O_RDONLY);   int j;
     if (fd < 0) {  
  fprintf(stderr, _("grubby: unable to open %s: %s\n"),  
  device, strerror(errno));  
  return 1;  
     }  
2696    
2697      if (read(fd, bootSect, 512) != 512) {   printf("index=%d\n", index);
  fprintf(stderr, _("grubby: unable to read %s: %s\n"),  
  device, strerror(errno));  
  return 1;  
     }  
     close(fd);  
2698    
2699      /* first three bytes should match, a jmp short should be in there */   line =
2700      if (memcmp(boot, bootSect, 3))      getLineByType(LT_KERNEL | LT_HYPER | LT_KERNEL_EFI | LT_KERNEL_16,
2701   return 0;    entry->lines);
2702     if (!line) {
2703     printf("non linux entry\n");
2704     return;
2705     }
2706    
2707      if (boot[1] == JMP_SHORT_OPCODE) {   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2708   offset = boot[2] + 2;   printf("kernel=%s\n", line->elements[1].item);
2709      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {   else
2710   offset = (boot[3] << 8) + boot[2] + 2;   printf("kernel=%s%s\n", prefix, line->elements[1].item);
     } else if (boot[0] == JMP_SHORT_OPCODE) {  
       offset = boot[1] + 2;  
         /*  
  * it looks like grub, when copying stage1 into the mbr, patches stage1  
  * right after the JMP location, replacing other instructions such as  
  * JMPs for NOOPs. So, relax the check a little bit by skipping those  
  * different bytes.  
  */  
       if ((bootSect[offset + 1] == NOOP_OPCODE)  
   && (bootSect[offset + 2] == NOOP_OPCODE)) {  
  offset = offset + 3;  
       }  
     } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {  
  offset = (boot[2] << 8) + boot[1] + 2;  
     } else {  
  return 0;  
     }  
2711    
2712      if (memcmp(boot + offset, bootSect + offset, CODE_SEG_SIZE))   if (line->numElements >= 3) {
2713   return 0;   printf("args=\"");
2714     i = 2;
2715     while (i < line->numElements) {
2716     if (!strncmp(line->elements[i].item, "root=", 5)) {
2717     root = line->elements[i].item + 5;
2718     } else {
2719     printf("%s%s", line->elements[i].item,
2720           line->elements[i].indent);
2721     }
2722    
2723      return 2;   i++;
2724  }   }
2725     printf("\"\n");
2726     } else {
2727     line = getLineByType(LT_KERNELARGS, entry->lines);
2728     if (line) {
2729     char *s;
2730    
2731     printf("args=\"");
2732     i = 1;
2733     while (i < line->numElements) {
2734     if (!strncmp
2735        (line->elements[i].item, "root=", 5)) {
2736     root = line->elements[i].item + 5;
2737     } else {
2738     s = line->elements[i].item;
2739    
2740  int checkLiloOnRaid(char * mdDev, const unsigned char * boot) {   printf("%s%s", s,
2741      int fd;         line->elements[i].indent);
2742      char buf[65536];   }
     char * end;  
     char * chptr;  
     char * chptr2;  
     int rc;  
2743    
2744      /* it's on raid; we need to parse /proc/mdstat and check all of the   i++;
2745         *raw* devices listed in there */   }
2746    
2747      if (!strncmp(mdDev, "/dev/", 5))   s = line->elements[i - 1].indent;
2748   mdDev += 5;   printf("\"\n");
2749     }
2750     }
2751    
2752      if ((fd = open("/proc/mdstat", O_RDONLY)) < 0) {   if (!root) {
2753   fprintf(stderr, _("grubby: failed to open /proc/mdstat: %s\n"),   line = getLineByType(LT_ROOT, entry->lines);
2754   strerror(errno));   if (line && line->numElements >= 2)
2755   return 2;   root = line->elements[1].item;
2756      }   }
2757    
2758      rc = read(fd, buf, sizeof(buf) - 1);   if (root) {
2759      if (rc < 0 || rc == (sizeof(buf) - 1)) {   char *s = alloca(strlen(root) + 1);
  fprintf(stderr, _("grubby: failed to read /proc/mdstat: %s\n"),  
  strerror(errno));  
  close(fd);  
  return 2;  
     }  
     close(fd);  
     buf[rc] = '\0';  
2760    
2761      chptr = buf;   strcpy(s, root);
2762      while (*chptr) {   if (s[strlen(s) - 1] == '"')
2763   end = strchr(chptr, '\n');   s[strlen(s) - 1] = '\0';
2764   if (!end) break;   /* make sure the root doesn't have a trailing " */
2765   *end = '\0';   printf("root=%s\n", s);
2766     }
2767    
2768   if (!strncmp(chptr, mdDev, strlen(mdDev)) &&   line =
2769      chptr[strlen(mdDev)] == ' ') {      getLineByType(LT_INITRD | LT_INITRD_EFI | LT_INITRD_16,
2770      entry->lines);
2771    
2772      /* found the device */   if (line && line->numElements >= 2) {
2773      while (*chptr && *chptr != ':') chptr++;   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2774      chptr++;   printf("initrd=");
2775      while (*chptr && isspace(*chptr)) chptr++;   else
2776     printf("initrd=%s", prefix);
     /* skip the "active" bit */  
     while (*chptr && !isspace(*chptr)) chptr++;  
     while (*chptr && isspace(*chptr)) chptr++;  
   
     /* skip the raid level */  
     while (*chptr && !isspace(*chptr)) chptr++;  
     while (*chptr && isspace(*chptr)) chptr++;  
   
     /* everything else is partition stuff */  
     while (*chptr) {  
  chptr2 = chptr;  
  while (*chptr2 && *chptr2 != '[') chptr2++;  
  if (!*chptr2) break;  
   
  /* yank off the numbers at the end */  
  chptr2--;  
  while (isdigit(*chptr2) && chptr2 > chptr) chptr2--;  
  chptr2++;  
  *chptr2 = '\0';  
   
  /* Better, now we need the /dev/ back. We're done with  
  * everything before this point, so we can just put  
  * the /dev/ part there. There will always be room. */  
  memcpy(chptr - 5, "/dev/", 5);  
  rc = checkDeviceBootloader(chptr - 5, boot);  
  if (rc != 2) {  
     return rc;  
  }  
   
  chptr = chptr2 + 1;  
  /* skip the [11] bit */  
  while (*chptr && !isspace(*chptr)) chptr++;  
  /* and move to the next one */  
  while (*chptr && isspace(*chptr)) chptr++;  
     }  
2777    
2778      /*  we're good to go */   for (i = 1; i < line->numElements; i++)
2779      return 2;   printf("%s%s", line->elements[i].item,
2780           line->elements[i].indent);
2781     printf("\n");
2782   }   }
2783    
2784   chptr = end + 1;   line = getLineByType(LT_TITLE, entry->lines);
2785      }   if (line) {
2786                    char *entryTitle;
2787                    /* if we can extractTitle, then it's a zipl config and
2788                     * if not then we go ahead with what's existed prior */
2789                    entryTitle = extractTitle(config, line);
2790                    if (!entryTitle) {
2791                        entryTitle=line->elements[1].item;
2792                    }
2793     printf("title=%s\n", entryTitle);
2794     } else {
2795     char *title;
2796     line = getLineByType(LT_MENUENTRY, entry->lines);
2797     if (line) {
2798     title = grub2ExtractTitle(line);
2799     if (title)
2800     printf("title=%s\n", title);
2801     }
2802     }
2803    
2804      fprintf(stderr,   for (j = 0, line = entry->lines; line; line = line->next) {
2805      _("grubby: raid device /dev/%s not found in /proc/mdstat\n"),   if ((line->type & LT_MBMODULE) && line->numElements >= 2) {
2806      mdDev);   if (!strncmp
2807      return 0;      (prefix, line->elements[1].item, strlen(prefix)))
2808     printf("mbmodule%d=", j);
2809     else
2810     printf("mbmodule%d=%s", j, prefix);
2811    
2812     for (i = 1; i < line->numElements; i++)
2813     printf("%s%s", line->elements[i].item,
2814           line->elements[i].indent);
2815     printf("\n");
2816     j++;
2817     }
2818     }
2819  }  }
2820    
2821  int checkForLilo(struct grubConfig * config) {  int isSuseSystem(void)
2822      int fd;  {
2823      unsigned char boot[512];   const char *path;
2824      struct singleLine * line;   const static char default_path[] = "/etc/SuSE-release";
   
     for (line = config->theLines; line; line = line->next)  
  if (line->type == LT_BOOT) break;  
   
     if (!line) {  
  fprintf(stderr,  
  _("grubby: no boot line found in lilo configuration\n"));  
  return 1;  
     }  
2825    
2826      if (line->numElements != 2) return 1;   if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL)
2827     path = default_path;
2828    
2829      fd = open("/boot/boot.b", O_RDONLY);   if (!access(path, R_OK))
2830      if (fd < 0) {   return 1;
2831   fprintf(stderr, _("grubby: unable to open %s: %s\n"),   return 0;
2832   "/boot/boot.b", strerror(errno));  }
  return 1;  
     }  
2833    
2834      if (read(fd, boot, 512) != 512) {  int isSuseGrubConf(const char *path)
2835   fprintf(stderr, _("grubby: unable to read %s: %s\n"),  {
2836   "/boot/boot.b", strerror(errno));   FILE *grubConf;
2837   return 1;   char *line = NULL;
2838      }   size_t len = 0, res = 0;
2839      close(fd);  
2840     grubConf = fopen(path, "r");
2841     if (!grubConf) {
2842     dbgPrintf("Could not open SuSE configuration file '%s'\n",
2843      path);
2844     return 0;
2845     }
2846    
2847     while ((res = getline(&line, &len, grubConf)) != -1) {
2848     if (!strncmp(line, "setup", 5)) {
2849     fclose(grubConf);
2850     free(line);
2851     return 1;
2852     }
2853     }
2854    
2855      if (!strncmp("/dev/md", line->elements[1].item, 7))   dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n",
2856   return checkLiloOnRaid(line->elements[1].item, boot);    path);
2857    
2858      return checkDeviceBootloader(line->elements[1].item, boot);   fclose(grubConf);
2859     free(line);
2860     return 0;
2861  }  }
2862    
2863  int checkForGrub2(struct grubConfig * config) {  int suseGrubConfGetLba(const char *path, int *lbaPtr)
2864      if (!access("/etc/grub.d/", R_OK))  {
2865   return 2;   FILE *grubConf;
2866     char *line = NULL;
2867     size_t res = 0, len = 0;
2868    
2869      return 1;   if (!path)
2870  }   return 1;
2871     if (!lbaPtr)
2872     return 1;
2873    
2874  int checkForGrub(struct grubConfig * config) {   grubConf = fopen(path, "r");
2875      int fd;   if (!grubConf)
2876      unsigned char bootSect[512];   return 1;
     char * boot;  
2877    
2878      if (parseSysconfigGrub(NULL, &boot))   while ((res = getline(&line, &len, grubConf)) != -1) {
2879   return 0;   if (line[res - 1] == '\n')
2880     line[res - 1] = '\0';
2881     else if (len > res)
2882     line[res] = '\0';
2883     else {
2884     line = realloc(line, res + 1);
2885     line[res] = '\0';
2886     }
2887    
2888      /* assume grub is not installed -- not an error condition */   if (!strncmp(line, "setup", 5)) {
2889      if (!boot)   if (strstr(line, "--force-lba")) {
2890   return 0;   *lbaPtr = 1;
2891     } else {
2892     *lbaPtr = 0;
2893     }
2894     dbgPrintf("lba: %i\n", *lbaPtr);
2895     break;
2896     }
2897     }
2898    
2899      fd = open("/boot/grub/stage1", O_RDONLY);   free(line);
2900      if (fd < 0)   fclose(grubConf);
  /* this doesn't exist if grub hasn't been installed */  
2901   return 0;   return 0;
2902    }
2903    
2904      if (read(fd, bootSect, 512) != 512) {  int suseGrubConfGetInstallDevice(const char *path, char **devicePtr)
2905   fprintf(stderr, _("grubby: unable to read %s: %s\n"),  {
2906   "/boot/grub/stage1", strerror(errno));   FILE *grubConf;
2907   close(fd);   char *line = NULL;
2908   return 1;   size_t res = 0, len = 0;
2909      }   char *lastParamPtr = NULL;
2910      close(fd);   char *secLastParamPtr = NULL;
2911     char installDeviceNumber = '\0';
2912     char *bounds = NULL;
2913    
2914      return checkDeviceBootloader(boot, bootSect);   if (!path)
2915  }   return 1;
2916     if (!devicePtr)
2917     return 1;
2918    
2919  int checkForExtLinux(struct grubConfig * config) {   grubConf = fopen(path, "r");
2920      int fd;   if (!grubConf)
2921      unsigned char bootSect[512];   return 1;
     char * boot;  
     char executable[] = "/boot/extlinux/extlinux";  
2922    
2923      printf("entered: checkForExtLinux()\n");   while ((res = getline(&line, &len, grubConf)) != -1) {
2924     if (strncmp(line, "setup", 5))
2925     continue;
2926    
2927     if (line[res - 1] == '\n')
2928     line[res - 1] = '\0';
2929     else if (len > res)
2930     line[res] = '\0';
2931     else {
2932     line = realloc(line, res + 1);
2933     line[res] = '\0';
2934     }
2935    
2936      if (parseSysconfigGrub(NULL, &boot))   lastParamPtr = bounds = line + res;
  return 0;  
2937    
2938      /* assume grub is not installed -- not an error condition */   /* Last parameter in grub may be an optional IMAGE_DEVICE */
2939      if (!boot)   while (!isspace(*lastParamPtr))
2940   return 0;   lastParamPtr--;
2941     lastParamPtr++;
2942    
2943     secLastParamPtr = lastParamPtr - 2;
2944     dbgPrintf("lastParamPtr: %s\n", lastParamPtr);
2945    
2946     if (lastParamPtr + 3 > bounds) {
2947     dbgPrintf("lastParamPtr going over boundary");
2948     fclose(grubConf);
2949     free(line);
2950     return 1;
2951     }
2952     if (!strncmp(lastParamPtr, "(hd", 3))
2953     lastParamPtr += 3;
2954     dbgPrintf("lastParamPtr: %c\n", *lastParamPtr);
2955    
2956     /*
2957     * Second last parameter will decide wether last parameter is
2958     * an IMAGE_DEVICE or INSTALL_DEVICE
2959     */
2960     while (!isspace(*secLastParamPtr))
2961     secLastParamPtr--;
2962     secLastParamPtr++;
2963    
2964     if (secLastParamPtr + 3 > bounds) {
2965     dbgPrintf("secLastParamPtr going over boundary");
2966     fclose(grubConf);
2967     free(line);
2968     return 1;
2969     }
2970     dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr);
2971     if (!strncmp(secLastParamPtr, "(hd", 3)) {
2972     secLastParamPtr += 3;
2973     dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr);
2974     installDeviceNumber = *secLastParamPtr;
2975     } else {
2976     installDeviceNumber = *lastParamPtr;
2977     }
2978    
2979      fd = open(executable, O_RDONLY);   *devicePtr = malloc(6);
2980      if (fd < 0)   snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber);
2981   /* this doesn't exist if grub hasn't been installed */   dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber);
2982   return 0;   fclose(grubConf);
2983     free(line);
2984     return 0;
2985     }
2986    
2987      if (read(fd, bootSect, 512) != 512) {   free(line);
2988   fprintf(stderr, _("grubby: unable to read %s: %s\n"),   fclose(grubConf);
  executable, strerror(errno));  
2989   return 1;   return 1;
     }  
     close(fd);  
   
     return checkDeviceBootloader(boot, bootSect);  
2990  }  }
2991    
2992  static char * getRootSpecifier(char * str) {  int grubGetBootFromDeviceMap(const char *device, char **bootPtr)
2993      char * idx, * rootspec = NULL;  {
2994     FILE *deviceMap;
2995     char *line = NULL;
2996     size_t res = 0, len = 0;
2997     char *devicePtr;
2998     char *bounds = NULL;
2999     const char *path;
3000     const static char default_path[] = "/boot/grub/device.map";
3001    
3002      if (*str == '(') {   if (!device)
3003          idx = rootspec = strdup(str);   return 1;
3004          while(*idx && (*idx != ')') && (!isspace(*idx))) idx++;   if (!bootPtr)
3005          *(++idx) = '\0';   return 1;
     }  
     return rootspec;  
 }  
3006    
3007  static char * getInitrdVal(struct grubConfig * config,   if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL)
3008     const char * prefix, struct singleLine *tmplLine,   path = default_path;
    const char * newKernelInitrd,  
    char ** extraInitrds, int extraInitrdCount)  
 {  
     char *initrdVal, *end;  
     int i;  
     size_t totalSize;  
     size_t prefixLen;  
     char separatorChar;  
   
     prefixLen = strlen(prefix);  
     totalSize = strlen(newKernelInitrd) - prefixLen + 1 /* \0 */;  
   
     for (i = 0; i < extraInitrdCount; i++) {  
  totalSize += sizeof(separatorChar);  
  totalSize += strlen(extraInitrds[i]) - prefixLen;  
     }  
3009    
3010      initrdVal = end = malloc(totalSize);   dbgPrintf("opening grub device.map file from: %s\n", path);
3011     deviceMap = fopen(path, "r");
3012     if (!deviceMap)
3013     return 1;
3014    
3015      end = stpcpy (end, newKernelInitrd + prefixLen);   while ((res = getline(&line, &len, deviceMap)) != -1) {
3016     if (!strncmp(line, "#", 1))
3017     continue;
3018    
3019     if (line[res - 1] == '\n')
3020     line[res - 1] = '\0';
3021     else if (len > res)
3022     line[res] = '\0';
3023     else {
3024     line = realloc(line, res + 1);
3025     line[res] = '\0';
3026     }
3027    
3028      separatorChar = getKeywordByType(LT_INITRD, config->cfi)->separatorChar;   devicePtr = line;
3029      for (i = 0; i < extraInitrdCount; i++) {   bounds = line + res;
  const char *extraInitrd;  
  int j;  
3030    
3031   extraInitrd = extraInitrds[i] + prefixLen;   while ((isspace(*line) && ((devicePtr + 1) <= bounds)))
3032   /* Don't add entries that are already there */   devicePtr++;
3033   if (tmplLine != NULL) {   dbgPrintf("device: %s\n", devicePtr);
3034      for (j = 2; j < tmplLine->numElements; j++)  
3035   if (strcmp(extraInitrd, tmplLine->elements[j].item) == 0)   if (!strncmp(devicePtr, device, strlen(device))) {
3036      break;   devicePtr += strlen(device);
3037     while (isspace(*devicePtr)
3038           && ((devicePtr + 1) <= bounds))
3039     devicePtr++;
3040    
3041      if (j != tmplLine->numElements)   *bootPtr = strdup(devicePtr);
3042   continue;   break;
3043     }
3044   }   }
3045    
3046   *end++ = separatorChar;   free(line);
3047   end = stpcpy(end, extraInitrd);   fclose(deviceMap);
3048      }   return 0;
   
     return initrdVal;  
3049  }  }
3050    
3051  int addNewKernel(struct grubConfig * config, struct singleEntry * template,  int suseGrubConfGetBoot(const char *path, char **bootPtr)
3052           const char * prefix,  {
3053   char * newKernelPath, char * newKernelTitle,   char *grubDevice;
  char * newKernelArgs, char * newKernelInitrd,  
  char ** extraInitrds, int extraInitrdCount,  
                  char * newMBKernel, char * newMBKernelArgs) {  
     struct singleEntry * new;  
     struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;  
     int needs;  
     char * chptr;  
   
     if (!newKernelPath) return 0;  
   
     /* if the newKernelTitle is too long silently munge it into something  
      * we can live with. truncating is first check, then we'll just mess with  
      * it until it looks better */  
     if (config->cfi->maxTitleLength &&  
     (strlen(newKernelTitle) > config->cfi->maxTitleLength)) {  
  char * buf = alloca(config->cfi->maxTitleLength + 7);  
  char * numBuf = alloca(config->cfi->maxTitleLength + 1);  
  int i = 1;  
   
  sprintf(buf, "TITLE=%.*s", config->cfi->maxTitleLength, newKernelTitle);  
  while (findEntryByPath(config, buf, NULL, NULL)) {  
     sprintf(numBuf, "%d", i++);  
     strcpy(buf + strlen(buf) - strlen(numBuf), numBuf);  
  }  
3054    
3055   newKernelTitle = buf + 6;   if (suseGrubConfGetInstallDevice(path, &grubDevice))
3056      }   dbgPrintf("error looking for grub installation device\n");
3057     else
3058     dbgPrintf("grubby installation device: %s\n", grubDevice);
3059    
3060      new = malloc(sizeof(*new));   if (grubGetBootFromDeviceMap(grubDevice, bootPtr))
3061      new->skip = 0;   dbgPrintf("error looking for grub boot device\n");
3062      new->multiboot = 0;   else
3063      new->next = config->entries;   dbgPrintf("grubby boot device: %s\n", *bootPtr);
     new->lines = NULL;  
     config->entries = new;  
   
     /* copy/update from the template */  
     needs = NEED_KERNEL | NEED_TITLE;  
     if (newKernelInitrd)  
  needs |= NEED_INITRD;  
     if (newMBKernel) {  
         needs |= NEED_MB;  
         new->multiboot = 1;  
     }  
3064    
3065      if (template) {   free(grubDevice);
3066   for (masterLine = template->lines;   return 0;
3067       masterLine && (tmplLine = lineDup(masterLine));  }
      lineFree(tmplLine), masterLine = masterLine->next)  
  {  
     dbgPrintf("addNewKernel processing %d\n", tmplLine->type);  
   
     /* skip comments */  
     chptr = tmplLine->indent;  
     while (*chptr && isspace(*chptr)) chptr++;  
     if (*chptr == '#') continue;  
   
     if (tmplLine->type == LT_KERNEL &&  
     tmplLine->numElements >= 2) {  
  if (!template->multiboot && (needs & NEED_MB)) {  
     /* it's not a multiboot template and this is the kernel  
      * line.  Try to be intelligent about inserting the  
      * hypervisor at the same time.  
      */  
     if (config->cfi->mbHyperFirst) {  
  /* insert the hypervisor first */  
  newLine = addLine(new, config->cfi, LT_HYPER,  
   tmplLine->indent,  
   newMBKernel + strlen(prefix));  
  /* set up for adding the kernel line */  
  free(tmplLine->indent);  
  tmplLine->indent = strdup(config->secondaryIndent);  
  needs &= ~NEED_MB;  
     }  
     if (needs & NEED_KERNEL) {  
  /* use addLineTmpl to preserve line elements,  
  * otherwise we could just call addLine.  Unfortunately  
  * this means making some changes to the template  
  * such as the indent change above and the type  
  * change below.  
  */  
  struct keywordTypes * mbm_kw =  
     getKeywordByType(LT_MBMODULE, config->cfi);  
  if (mbm_kw) {  
     tmplLine->type = LT_MBMODULE;  
     free(tmplLine->elements[0].item);  
     tmplLine->elements[0].item = strdup(mbm_kw->key);  
  }  
  newLine = addLineTmpl(new, tmplLine, newLine,  
       newKernelPath + strlen(prefix), config->cfi);  
  needs &= ~NEED_KERNEL;  
     }  
     if (needs & NEED_MB) { /* !mbHyperFirst */  
  newLine = addLine(new, config->cfi, LT_HYPER,  
   config->secondaryIndent,  
   newMBKernel + strlen(prefix));  
  needs &= ~NEED_MB;  
     }  
  } else if (needs & NEED_KERNEL) {  
     newLine = addLineTmpl(new, tmplLine, newLine,  
   newKernelPath + strlen(prefix), config->cfi);  
     needs &= ~NEED_KERNEL;  
  }  
   
     } else if (tmplLine->type == LT_HYPER &&  
        tmplLine->numElements >= 2) {  
  if (needs & NEED_MB) {  
     newLine = addLineTmpl(new, tmplLine, newLine,  
   newMBKernel + strlen(prefix), config->cfi);  
     needs &= ~NEED_MB;  
  }  
   
     } else if (tmplLine->type == LT_MBMODULE &&  
        tmplLine->numElements >= 2) {  
  if (new->multiboot) {  
     if (needs & NEED_KERNEL) {  
  newLine = addLineTmpl(new, tmplLine, newLine,  
       newKernelPath +  
       strlen(prefix), config->cfi);  
  needs &= ~NEED_KERNEL;  
     } else if (config->cfi->mbInitRdIsModule &&  
        (needs & NEED_INITRD)) {  
  char *initrdVal;  
  initrdVal = getInitrdVal(config, prefix, tmplLine,  
  newKernelInitrd, extraInitrds,  
  extraInitrdCount);  
  newLine = addLineTmpl(new, tmplLine, newLine,  
       initrdVal, config->cfi);  
  free(initrdVal);  
  needs &= ~NEED_INITRD;  
     }  
  } else if (needs & NEED_KERNEL) {  
     /* template is multi but new is not,  
      * insert the kernel in the first module slot  
      */  
     tmplLine->type = LT_KERNEL;  
     free(tmplLine->elements[0].item);  
     tmplLine->elements[0].item =  
  strdup(getKeywordByType(LT_KERNEL, config->cfi)->key);  
     newLine = addLineTmpl(new, tmplLine, newLine,  
   newKernelPath + strlen(prefix), config->cfi);  
     needs &= ~NEED_KERNEL;  
  } else if (needs & NEED_INITRD) {  
     char *initrdVal;  
     /* template is multi but new is not,  
      * insert the initrd in the second module slot  
      */  
     tmplLine->type = LT_INITRD;  
     free(tmplLine->elements[0].item);  
     tmplLine->elements[0].item =  
  strdup(getKeywordByType(LT_INITRD, config->cfi)->key);  
     initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);  
     newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);  
     free(initrdVal);  
     needs &= ~NEED_INITRD;  
  }  
   
     } else if (tmplLine->type == LT_INITRD &&  
        tmplLine->numElements >= 2) {  
  if (needs & NEED_INITRD &&  
     new->multiboot && !template->multiboot &&  
     config->cfi->mbInitRdIsModule) {  
     /* make sure we don't insert the module initrd  
      * before the module kernel... if we don't do it here,  
      * it will be inserted following the template.  
      */  
     if (!needs & NEED_KERNEL) {  
  char *initrdVal;  
   
  initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);  
  newLine = addLine(new, config->cfi, LT_MBMODULE,  
   config->secondaryIndent,  
   initrdVal);  
  free(initrdVal);  
  needs &= ~NEED_INITRD;  
     }  
  } else if (needs & NEED_INITRD) {  
     char *initrdVal;  
     initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);  
     newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);  
     free(initrdVal);  
     needs &= ~NEED_INITRD;  
  }  
   
     } else if (tmplLine->type == LT_MENUENTRY &&  
        (needs & NEED_TITLE)) {  
  requote(tmplLine, config->cfi);  
  char *nkt = malloc(strlen(newKernelTitle)+3);  
  strcpy(nkt, "'");  
  strcat(nkt, newKernelTitle);  
  strcat(nkt, "'");  
  newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi);  
  free(nkt);  
  needs &= ~NEED_TITLE;  
     } else if (tmplLine->type == LT_TITLE &&  
        (needs & NEED_TITLE)) {  
  if (tmplLine->numElements >= 2) {  
     newLine = addLineTmpl(new, tmplLine, newLine,  
   newKernelTitle, config->cfi);  
     needs &= ~NEED_TITLE;  
  } else if (tmplLine->numElements == 1 &&  
    config->cfi->titleBracketed) {  
     /* addLineTmpl doesn't handle titleBracketed */  
     newLine = addLine(new, config->cfi, LT_TITLE,  
       tmplLine->indent, newKernelTitle);  
     needs &= ~NEED_TITLE;  
  }  
     } else if (tmplLine->type == LT_ECHO) {  
     requote(tmplLine, config->cfi);  
     static const char *prefix = "'Loading ";  
     if (tmplLine->numElements > 1 &&  
     strstr(tmplLine->elements[1].item, prefix) &&  
     masterLine->next && masterLine->next->type == LT_KERNEL) {  
  char *newTitle = malloc(strlen(prefix) +  
  strlen(newKernelTitle) + 2);  
   
  strcpy(newTitle, prefix);  
  strcat(newTitle, newKernelTitle);  
  strcat(newTitle, "'");  
  newLine = addLine(new, config->cfi, LT_ECHO,  
  tmplLine->indent, newTitle);  
  free(newTitle);  
     } else {  
  /* pass through other lines from the template */  
  newLine = addLineTmpl(new, tmplLine, newLine, NULL,  
  config->cfi);  
     }  
     } else {  
  /* pass through other lines from the template */  
  newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);  
     }  
  }  
3068    
3069      } else {  int parseSuseGrubConf(int *lbaPtr, char **bootPtr)
3070   /* don't have a template, so start the entry with the  {
3071   * appropriate starting line   /*
3072     * This SuSE grub configuration file at this location is not your
3073     * average grub configuration file, but instead the grub commands
3074     * used to setup grub on that system.
3075   */   */
3076   switch (config->cfi->entryStart) {   const char *path;
3077      case LT_KERNEL:   const static char default_path[] = "/etc/grub.conf";
  if (new->multiboot && config->cfi->mbHyperFirst) {  
     /* fall through to LT_HYPER */  
  } else {  
     newLine = addLine(new, config->cfi, LT_KERNEL,  
       config->primaryIndent,  
       newKernelPath + strlen(prefix));  
     needs &= ~NEED_KERNEL;  
     break;  
  }  
3078    
3079      case LT_HYPER:   if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL)
3080   newLine = addLine(new, config->cfi, LT_HYPER,   path = default_path;
   config->primaryIndent,  
   newMBKernel + strlen(prefix));  
  needs &= ~NEED_MB;  
  break;  
3081    
3082      case LT_MENUENTRY: {   if (!isSuseGrubConf(path))
3083   char *nkt = malloc(strlen(newKernelTitle)+3);   return 1;
  strcpy(nkt, "'");  
  strcat(nkt, newKernelTitle);  
  strcat(nkt, "'");  
         newLine = addLine(new, config->cfi, LT_MENUENTRY,  
   config->primaryIndent, nkt);  
  free(nkt);  
  needs &= ~NEED_TITLE;  
  needs |= NEED_END;  
  break;  
     }  
     case LT_TITLE:  
  if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above)  
  char * templabel;  
  int x = 0, y = 0;  
   
  templabel = strdup(newKernelTitle);  
  while( templabel[x]){  
  if( templabel[x] == ' ' ){  
  y = x;  
  while( templabel[y] ){  
  templabel[y] = templabel[y+1];  
  y++;  
  }  
  }  
  x++;  
  }  
  newLine = addLine(new, config->cfi, LT_TITLE,  
   config->primaryIndent, templabel);  
  free(templabel);  
  }else{  
  newLine = addLine(new, config->cfi, LT_TITLE,  
   config->primaryIndent, newKernelTitle);  
  }  
  needs &= ~NEED_TITLE;  
  break;  
3084    
3085      default:   if (lbaPtr) {
3086   abort();   *lbaPtr = 0;
3087     if (suseGrubConfGetLba(path, lbaPtr))
3088     return 1;
3089   }   }
     }  
   
     /* add the remainder of the lines, i.e. those that either  
      * weren't present in the template, or in the case of no template,  
      * all the lines following the entryStart.  
      */  
     if (needs & NEED_TITLE) {  
  newLine = addLine(new, config->cfi, LT_TITLE,  
   config->secondaryIndent,  
   newKernelTitle);  
  needs &= ~NEED_TITLE;  
     }  
     if ((needs & NEED_MB) && config->cfi->mbHyperFirst) {  
  newLine = addLine(new, config->cfi, LT_HYPER,  
   config->secondaryIndent,  
   newMBKernel + strlen(prefix));  
  needs &= ~NEED_MB;  
     }  
     if (needs & NEED_KERNEL) {  
  newLine = addLine(new, config->cfi,  
   (new->multiboot && getKeywordByType(LT_MBMODULE,  
       config->cfi)) ?  
   LT_MBMODULE : LT_KERNEL,  
   config->secondaryIndent,  
   newKernelPath + strlen(prefix));  
  needs &= ~NEED_KERNEL;  
     }  
     if (needs & NEED_MB) {  
  newLine = addLine(new, config->cfi, LT_HYPER,  
   config->secondaryIndent,  
   newMBKernel + strlen(prefix));  
  needs &= ~NEED_MB;  
     }  
     if (needs & NEED_INITRD) {  
  char *initrdVal;  
  initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);  
  newLine = addLine(new, config->cfi,  
   (new->multiboot && getKeywordByType(LT_MBMODULE,  
       config->cfi)) ?  
   LT_MBMODULE : LT_INITRD,  
   config->secondaryIndent,  
   initrdVal);  
  free(initrdVal);  
  needs &= ~NEED_INITRD;  
     }  
     if (needs & NEED_END) {  
  newLine = addLine(new, config->cfi, LT_ENTRY_END,  
  config->secondaryIndent, NULL);  
  needs &= ~NEED_END;  
     }  
   
     if (needs) {  
  printf(_("grubby: needs=%d, aborting\n"), needs);  
  abort();  
     }  
3090    
3091      if (updateImage(config, "0", prefix, newKernelArgs, NULL,   if (bootPtr) {
3092                      newMBKernelArgs, NULL)) return 1;   *bootPtr = NULL;
3093     suseGrubConfGetBoot(path, bootPtr);
3094     }
3095    
3096      return 0;   return 0;
3097  }  }
3098    
3099  static void traceback(int signum)  int parseSysconfigGrub(int *lbaPtr, char **bootPtr)
3100  {  {
3101      void *array[40];   FILE *in;
3102      size_t size;   char buf[1024];
3103     char *chptr;
3104     char *start;
3105     char *param;
3106    
3107      signal(SIGSEGV, SIG_DFL);   in = fopen("/etc/sysconfig/grub", "r");
3108      memset(array, '\0', sizeof (array));   if (!in)
     size = backtrace(array, 40);  
   
     fprintf(stderr, "grubby recieved SIGSEGV!  Backtrace (%ld):\n",  
             (unsigned long)size);  
     backtrace_symbols_fd(array, size, STDERR_FILENO);  
     exit(1);  
 }  
   
 int main(int argc, const char ** argv) {  
     poptContext optCon;  
     const char * grubConfig = NULL;  
     char * outputFile = NULL;  
     int arg = 0;  
     int flags = 0;  
     int badImageOkay = 0;  
     int configureGrub2 = 0;  
     int configureLilo = 0, configureELilo = 0, configureGrub = 0;  
     int configureYaboot = 0, configureSilo = 0, configureZipl = 0;  
     int configureExtLinux = 0;  
     int bootloaderProbe = 0;  
     int extraInitrdCount = 0;  
     char * updateKernelPath = NULL;  
     char * newKernelPath = NULL;  
     char * removeKernelPath = NULL;  
     char * newKernelArgs = NULL;  
     char * newKernelInitrd = NULL;  
     char * newKernelTitle = NULL;  
     char * newKernelVersion = NULL;  
     char * newMBKernel = NULL;  
     char * newMBKernelArgs = NULL;  
     char * removeMBKernelArgs = NULL;  
     char * removeMBKernel = NULL;  
     char * bootPrefix = NULL;  
     char * defaultKernel = NULL;  
     char * removeArgs = NULL;  
     char * kernelInfo = NULL;  
     char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };  
     const char * chptr = NULL;  
     struct configFileInfo * cfi = NULL;  
     struct grubConfig * config;  
     struct singleEntry * template = NULL;  
     int copyDefault = 0, makeDefault = 0;  
     int displayDefault = 0;  
     int displayDefaultIndex = 0;  
     int displayDefaultTitle = 0;  
     struct poptOption options[] = {  
  { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,  
     _("add an entry for the specified kernel"), _("kernel-path") },  
  { "add-multiboot", 0, POPT_ARG_STRING, &newMBKernel, 0,  
     _("add an entry for the specified multiboot kernel"), NULL },  
  { "args", 0, POPT_ARG_STRING, &newKernelArgs, 0,  
     _("default arguments for the new kernel or new arguments for "  
       "kernel being updated"), _("args") },  
  { "mbargs", 0, POPT_ARG_STRING, &newMBKernelArgs, 0,  
     _("default arguments for the new multiboot kernel or "  
               "new arguments for multiboot kernel being updated"), NULL },  
  { "bad-image-okay", 0, 0, &badImageOkay, 0,  
     _("don't sanity check images in boot entries (for testing only)"),  
     NULL },  
  { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,  
     _("filestystem which contains /boot directory (for testing only)"),  
     _("bootfs") },  
 #if defined(__i386__) || defined(__x86_64__)  
  { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,  
     _("check if lilo is installed on lilo.conf boot sector") },  
 #endif  
  { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,  
     _("path to grub config file to update (\"-\" for stdin)"),  
     _("path") },  
  { "copy-default", 0, 0, &copyDefault, 0,  
     _("use the default boot entry as a template for the new entry "  
       "being added; if the default is not a linux image, or if "  
       "the kernel referenced by the default image does not exist, "  
       "the first linux entry whose kernel does exist is used as the "  
       "template"), NULL },  
  { "default-kernel", 0, 0, &displayDefault, 0,  
     _("display the path of the default kernel") },  
  { "default-index", 0, 0, &displayDefaultIndex, 0,  
     _("display the index of the default kernel") },  
  { "default-title", 0, 0, &displayDefaultTitle, 0,  
     _("display the title of the default kernel") },  
  { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,  
     _("configure elilo bootloader") },  
  { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,  
     _("configure extlinux bootloader (from syslinux)") },  
  { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,  
     _("configure grub bootloader") },  
  { "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,  
     _("configure grub2 bootloader") },  
  { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,  
     _("display boot information for specified kernel"),  
     _("kernel-path") },  
  { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,  
     _("initrd image for the new kernel"), _("initrd-path") },  
  { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',  
     _("auxilliary initrd image for things other than the new kernel"), _("initrd-path") },  
  { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,  
     _("configure lilo bootloader") },  
  { "make-default", 0, 0, &makeDefault, 0,  
     _("make the newly added entry the default boot entry"), NULL },  
  { "output-file", 'o', POPT_ARG_STRING, &outputFile, 0,  
     _("path to output updated config file (\"-\" for stdout)"),  
     _("path") },  
  { "remove-args", 0, POPT_ARG_STRING, &removeArgs, 0,  
             _("remove kernel arguments"), NULL },  
         { "remove-mbargs", 0, POPT_ARG_STRING, &removeMBKernelArgs, 0,  
     _("remove multiboot kernel arguments"), NULL },  
  { "remove-kernel", 0, POPT_ARG_STRING, &removeKernelPath, 0,  
     _("remove all entries for the specified kernel"),  
     _("kernel-path") },  
  { "remove-multiboot", 0, POPT_ARG_STRING, &removeMBKernel, 0,  
             _("remove all entries for the specified multiboot kernel"), NULL },  
  { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,  
     _("make the first entry referencing the specified kernel "  
       "the default"), _("kernel-path") },  
  { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,  
     _("configure silo bootloader") },  
  { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,  
     _("title to use for the new kernel entry"), _("entry-title") },  
  { "update-kernel", 0, POPT_ARG_STRING, &updateKernelPath, 0,  
     _("updated information for the specified kernel"),  
     _("kernel-path") },  
  { "version", 'v', 0, NULL, 'v',  
     _("print the version of this program and exit"), NULL },  
  { "yaboot", 0, POPT_ARG_NONE, &configureYaboot, 0,  
     _("configure yaboot bootloader") },  
  { "zipl", 0, POPT_ARG_NONE, &configureZipl, 0,  
     _("configure zipl bootloader") },  
  POPT_AUTOHELP  
  { 0, 0, 0, 0, 0 }  
     };  
   
     useextlinuxmenu=0;  
   
     signal(SIGSEGV, traceback);  
   
     optCon = poptGetContext("grubby", argc, argv, options, 0);  
     poptReadDefaultConfig(optCon, 1);  
   
     while ((arg = poptGetNextOpt(optCon)) >= 0) {  
  switch (arg) {  
   case 'v':  
     printf("grubby version %s\n", VERSION);  
     exit(0);  
     break;  
   case 'i':  
     if (extraInitrdCount < MAX_EXTRA_INITRDS) {  
      extraInitrds[extraInitrdCount++] = strdup(poptGetOptArg(optCon));  
     } else {  
  fprintf(stderr, _("grubby: extra initrd maximum is %d\n"), extraInitrdCount);  
3109   return 1;   return 1;
     }  
     break;  
  }  
     }  
   
     if (arg < -1) {  
  fprintf(stderr, _("grubby: bad argument %s: %s\n"),  
  poptBadOption(optCon, POPT_BADOPTION_NOALIAS),  
  poptStrerror(arg));  
  return 1;  
     }  
   
     if ((chptr = poptGetArg(optCon))) {  
  fprintf(stderr, _("grubby: unexpected argument %s\n"), chptr);  
  return 1;  
     }  
   
     if ((configureLilo + configureGrub2 + configureGrub + configureELilo +  
  configureYaboot + configureSilo + configureZipl +  
  configureExtLinux ) > 1) {  
  fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));  
  return 1;  
     } else if (bootloaderProbe && grubConfig) {  
  fprintf(stderr,  
     _("grubby: cannot specify config file with --bootloader-probe\n"));  
  return 1;  
     } else if (configureGrub2) {  
  cfi = &grub2ConfigType;  
     } else if (configureLilo) {  
  cfi = &liloConfigType;  
     } else if (configureGrub) {  
  cfi = &grubConfigType;  
     } else if (configureELilo) {  
  cfi = &eliloConfigType;  
     } else if (configureYaboot) {  
  cfi = &yabootConfigType;  
     } else if (configureSilo) {  
         cfi = &siloConfigType;  
     } else if (configureZipl) {  
         cfi = &ziplConfigType;  
     } else if (configureExtLinux) {  
  cfi = &extlinuxConfigType;  
  useextlinuxmenu=1;  
     }  
3110    
3111      if (!cfi) {   if (lbaPtr)
3112        #ifdef __ia64__   *lbaPtr = 0;
3113   cfi = &eliloConfigType;   if (bootPtr)
3114        #elif __powerpc__   *bootPtr = NULL;
3115   cfi = &yabootConfigType;  
3116        #elif __sparc__   while (fgets(buf, sizeof(buf), in)) {
3117          cfi = &siloConfigType;   start = buf;
3118        #elif __s390__   while (isspace(*start))
3119          cfi = &ziplConfigType;   start++;
3120        #elif __s390x__   if (*start == '#')
3121          cfi = &ziplConfigtype;   continue;
3122        #else  
3123          if (grub2FindConfig(&grub2ConfigType))   chptr = strchr(start, '=');
3124      cfi = &grub2ConfigType;   if (!chptr)
3125   else   continue;
3126      cfi = &grubConfigType;   chptr--;
3127        #endif   while (*chptr && isspace(*chptr))
3128      }   chptr--;
3129     chptr++;
3130      if (!grubConfig) {   *chptr = '\0';
3131   if (cfi->findConfig)  
3132      grubConfig = cfi->findConfig(cfi);   param = chptr + 1;
3133   if (!grubConfig)   while (*param && isspace(*param))
3134      grubConfig = cfi->defaultConfig;   param++;
3135      }   if (*param == '=') {
3136     param++;
3137     while (*param && isspace(*param))
3138     param++;
3139     }
3140    
3141      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||   chptr = param;
3142    newKernelPath || removeKernelPath || makeDefault ||   while (*chptr && !isspace(*chptr))
3143    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {   chptr++;
3144   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   *chptr = '\0';
3145    "specified option"));  
3146   return 1;   if (!strcmp(start, "forcelba") && !strcmp(param, "1") && lbaPtr)
3147      }   *lbaPtr = 1;
3148     else if (!strcmp(start, "boot") && bootPtr)
3149     *bootPtr = strdup(param);
3150     }
3151    
3152      if ((displayDefault || kernelInfo) && (newKernelVersion || newKernelPath ||   fclose(in);
    removeKernelPath)) {  
  fprintf(stderr, _("grubby: --default-kernel and --info may not "  
   "be used when adding or removing kernels\n"));  
  return 1;  
     }  
3153    
3154      if (newKernelPath && !newKernelTitle) {   return 0;
3155   fprintf(stderr, _("grubby: kernel title must be specified\n"));  }
  return 1;  
     } else if (!newKernelPath && (newKernelTitle  || copyDefault ||  
   (newKernelInitrd && !updateKernelPath)||  
   makeDefault || extraInitrdCount > 0)) {  
  fprintf(stderr, _("grubby: kernel path expected\n"));  
  return 1;  
     }  
3156    
3157      if (newKernelPath && updateKernelPath) {  void dumpSysconfigGrub(void)
3158   fprintf(stderr, _("grubby: --add-kernel and --update-kernel may"  {
3159            "not be used together"));   char *boot = NULL;
3160   return 1;   int lba;
     }  
3161    
3162      if (makeDefault && defaultKernel) {   if (isSuseSystem()) {
3163   fprintf(stderr, _("grubby: --make-default and --default-kernel "   if (parseSuseGrubConf(&lba, &boot)) {
3164    "may not be used together\n"));   free(boot);
3165   return 1;   return;
3166      } else if (defaultKernel && removeKernelPath &&   }
3167   !strcmp(defaultKernel, removeKernelPath)) {   } else {
3168   fprintf(stderr, _("grubby: cannot make removed kernel the default\n"));   if (parseSysconfigGrub(&lba, &boot)) {
3169   return 1;   free(boot);
3170      } else if (defaultKernel && newKernelPath &&   return;
3171   !strcmp(defaultKernel, newKernelPath)) {   }
3172   makeDefault = 1;   }
  defaultKernel = NULL;  
     }  
3173    
3174      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {   if (lba)
3175   fprintf(stderr, _("grubby: output file must be specified if stdin "   printf("lba\n");
3176   "is used\n"));   if (boot) {
3177   return 1;   printf("boot=%s\n", boot);
3178      }   free(boot);
3179     }
3180    }
3181    
3182      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel  int displayInfo(struct grubConfig *config, char *kernel, const char *prefix)
3183   && !kernelInfo && !bootloaderProbe && !updateKernelPath  {
3184          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {   int i = 0;
3185   fprintf(stderr, _("grubby: no action specified\n"));   struct singleEntry *entry;
3186   return 1;   struct singleLine *line;
3187      }  
3188     entry = findEntryByPath(config, kernel, prefix, &i);
3189     if (!entry) {
3190     fprintf(stderr, _("grubby: kernel not found\n"));
3191     return 1;
3192     }
3193    
3194      flags |= badImageOkay ? GRUBBY_BADIMAGE_OKAY : 0;   /* this is a horrible hack to support /etc/sysconfig/grub; there must
3195       be a better way */
3196     if (config->cfi == &grubConfigType) {
3197     dumpSysconfigGrub();
3198     } else {
3199     line = getLineByType(LT_BOOT, config->theLines);
3200     if (line && line->numElements >= 1) {
3201     printf("boot=%s\n", line->elements[1].item);
3202     }
3203    
3204      if (cfi->needsBootPrefix) {   line = getLineByType(LT_LBA, config->theLines);
3205   if (!bootPrefix) {   if (line)
3206      bootPrefix = findBootPrefix();   printf("lba\n");
     if (!bootPrefix) return 1;  
  } else {  
     /* this shouldn't end with a / */  
     if (bootPrefix[strlen(bootPrefix) - 1] == '/')  
  bootPrefix[strlen(bootPrefix) - 1] = '\0';  
3207   }   }
     } else {  
  bootPrefix = "";  
     }  
3208    
3209      if (!cfi->mbAllowExtraInitRds &&   displayEntry(config, entry, prefix, i);
  extraInitrdCount > 0) {  
  fprintf(stderr, _("grubby: %s doesn't allow multiple initrds\n"), cfi->defaultConfig);  
  return 1;  
     }  
3210    
3211      if (bootloaderProbe) {   i++;
3212   int lrc = 0, grc = 0, gr2c = 0, erc = 0;   while ((entry = findEntryByPath(config, kernel, prefix, &i))) {
3213   struct grubConfig * lconfig, * gconfig;   displayEntry(config, entry, prefix, i);
3214     i++;
3215   const char *grub2config = grub2FindConfig(&grub2ConfigType);   }
  if (grub2config) {  
     gconfig = readConfig(grub2config, &grub2ConfigType);  
     if (!gconfig)  
  gr2c = 1;  
     else  
  gr2c = checkForGrub2(gconfig);  
  }  
   
  const char *grubconfig = grubFindConfig(&grubConfigType);  
  if (!access(grubconfig, F_OK)) {  
     gconfig = readConfig(grubconfig, &grubConfigType);  
     if (!gconfig)  
  grc = 1;  
     else  
  grc = checkForGrub(gconfig);  
  }  
   
  if (!access(liloConfigType.defaultConfig, F_OK)) {  
     lconfig = readConfig(liloConfigType.defaultConfig, &liloConfigType);  
     if (!lconfig)  
  lrc = 1;  
     else  
  lrc = checkForLilo(lconfig);  
  }  
   
  if (!access(extlinuxConfigType.defaultConfig, F_OK)) {  
     lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);  
     if (!lconfig)  
  erc = 1;  
     else  
  erc = checkForExtLinux(lconfig);  
  }  
   
  if (lrc == 1 || grc == 1 || gr2c == 1) return 1;  
   
  if (lrc == 2) printf("lilo\n");  
  if (gr2c == 2) printf("grub2\n");  
  if (grc == 2) printf("grub\n");  
  if (erc == 2) printf("extlinux\n");  
3216    
3217   return 0;   return 0;
3218      }  }
   
     config = readConfig(grubConfig, cfi);  
     if (!config) return 1;  
3219    
3220      if (displayDefault) {  struct singleLine *addLineTmpl(struct singleEntry *entry,
3221   struct singleLine * line;         struct singleLine *tmplLine,
3222   struct singleEntry * entry;         struct singleLine *prevLine,
3223          char * rootspec;         const char *val, struct configFileInfo *cfi)
3224    {
3225   if (config->defaultImage == -1) return 0;   struct singleLine *newLine = lineDup(tmplLine);
  entry = findEntryByIndex(config, config->defaultImage);  
  if (!entry) return 0;  
  if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;  
   
  line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);  
  if (!line) return 0;  
   
         rootspec = getRootSpecifier(line->elements[1].item);  
         printf("%s%s\n", bootPrefix, line->elements[1].item +  
                ((rootspec != NULL) ? strlen(rootspec) : 0));  
3226    
3227   return 0;   if (isEfi && cfi == &grub2ConfigType) {
3228     enum lineType_e old = newLine->type;
3229     newLine->type = preferredLineType(newLine->type, cfi);
3230     if (old != newLine->type)
3231     newLine->elements[0].item =
3232        getKeyByType(newLine->type, cfi);
3233     }
3234    
3235      } else if (displayDefaultTitle) {   if (val) {
3236   struct singleLine * line;   /* override the inherited value with our own.
3237   struct singleEntry * entry;   * This is a little weak because it only applies to elements[1]
3238     */
3239   if (config->defaultImage == -1) return 0;   if (newLine->numElements > 1)
3240   entry = findEntryByIndex(config, config->defaultImage);   removeElement(newLine, 1);
3241   if (!entry) return 0;   insertElement(newLine, val, 1, cfi);
3242    
3243   if (!configureGrub2) {   /* but try to keep the rootspec from the template... sigh */
3244    line = getLineByType(LT_TITLE, entry->lines);   if (tmplLine->
3245    if (!line) return 0;      type & (LT_HYPER | LT_KERNEL | LT_MBMODULE | LT_INITRD |
3246    printf("%s\n", line->elements[1].item);      LT_KERNEL_EFI | LT_INITRD_EFI | LT_KERNEL_16 |
3247        LT_INITRD_16)) {
3248   } else {   char *rootspec =
3249    int i;      getRootSpecifier(tmplLine->elements[1].item);
3250    size_t len;   if (rootspec != NULL) {
3251    char * start;   free(newLine->elements[1].item);
3252    char * tmp;   newLine->elements[1].item =
3253        sdupprintf("%s%s", rootspec, val);
3254    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");   }
3255    line = getLineByType(LT_MENUENTRY, entry->lines);   }
   if (!line) return 0;  
   
   for (i = 0; i < line->numElements; i++) {  
   
     if (!strcmp(line->elements[i].item, "menuentry"))  
       continue;  
   
     if (*line->elements[i].item == '\'')  
       start = line->elements[i].item + 1;  
     else  
       start = line->elements[i].item;  
   
     len = strlen(start);  
     if (*(start + len - 1) == '\'') {  
       tmp = strdup(start);  
       *(tmp + len - 1) = '\0';  
       printf("%s", tmp);  
       free(tmp);  
       break;  
     } else {  
       printf("%s ", start);  
     }  
   }  
   printf("\n");  
3256   }   }
  return 0;  
3257    
3258      } else if (displayDefaultIndex) {   dbgPrintf("addLineTmpl(%s)\n", newLine->numElements ?
3259          if (config->defaultImage == -1) return 0;    newLine->elements[0].item : "");
         printf("%i\n", config->defaultImage);  
   
     } else if (kernelInfo)  
  return displayInfo(config, kernelInfo, bootPrefix);  
   
     if (copyDefault) {  
  template = findTemplate(config, bootPrefix, NULL, 0, flags);  
  if (!template) return 1;  
     }  
3260    
3261      markRemovedImage(config, removeKernelPath, bootPrefix);   if (!entry->lines) {
3262      markRemovedImage(config, removeMBKernel, bootPrefix);   /* first one on the list */
3263      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,   entry->lines = newLine;
3264      bootPrefix, flags);   } else if (prevLine) {
3265      setFallbackImage(config, newKernelPath != NULL);   /* add after prevLine */
3266      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,   newLine->next = prevLine->next;
3267                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;   prevLine->next = newLine;
3268      if (updateKernelPath && newKernelInitrd) {   }
3269              if (updateInitrd(config, updateKernelPath, bootPrefix,  
3270                               newKernelInitrd)) return 1;   return newLine;
3271      }  }
3272      if (addNewKernel(config, template, bootPrefix, newKernelPath,  
3273                       newKernelTitle, newKernelArgs, newKernelInitrd,  /* val may be NULL */
3274                       extraInitrds, extraInitrdCount,  struct singleLine *addLine(struct singleEntry *entry,
3275                       newMBKernel, newMBKernelArgs)) return 1;     struct configFileInfo *cfi,
3276           enum lineType_e type, char *defaultIndent,
3277       const char *val)
3278      if (numEntries(config) == 0) {  {
3279          fprintf(stderr, _("grubby: doing this would leave no kernel entries. "   struct singleLine *line, *prev;
3280                            "Not writing out new config.\n"));   struct keywordTypes *kw;
3281          return 1;   struct singleLine tmpl;
3282      }  
3283     /* NB: This function shouldn't allocate items on the heap, rather on
3284     * the stack since it calls addLineTmpl which will make copies.
3285     */
3286     if (type == LT_TITLE && cfi->titleBracketed) {
3287     /* we're doing a bracketed title (zipl) */
3288     tmpl.type = type;
3289     tmpl.numElements = 1;
3290     tmpl.elements = alloca(sizeof(*tmpl.elements));
3291     tmpl.elements[0].item = alloca(strlen(val) + 3);
3292     sprintf(tmpl.elements[0].item, "[%s]", val);
3293     tmpl.elements[0].indent = "";
3294     val = NULL;
3295     } else if (type == LT_MENUENTRY) {
3296     char *lineend = "--class gnu-linux --class gnu --class os {";
3297     if (!val) {
3298     fprintf(stderr,
3299     "Line type LT_MENUENTRY requires a value\n");
3300     abort();
3301     }
3302     kw = getKeywordByType(type, cfi);
3303     if (!kw) {
3304     fprintf(stderr,
3305     "Looking up keyword for unknown type %d\n",
3306     type);
3307     abort();
3308     }
3309     tmpl.indent = "";
3310     tmpl.type = type;
3311     tmpl.numElements = 3;
3312     tmpl.elements =
3313        alloca(sizeof(*tmpl.elements) * tmpl.numElements);
3314     tmpl.elements[0].item = kw->key;
3315     tmpl.elements[0].indent = alloca(2);
3316     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
3317     tmpl.elements[1].item = (char *)val;
3318     tmpl.elements[1].indent = alloca(2);
3319     sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);
3320     tmpl.elements[2].item = alloca(strlen(lineend) + 1);
3321     strcpy(tmpl.elements[2].item, lineend);
3322     tmpl.elements[2].indent = "";
3323     } else {
3324     kw = getKeywordByType(type, cfi);
3325     if (!kw) {
3326     fprintf(stderr,
3327     "Looking up keyword for unknown type %d\n",
3328     type);
3329     abort();
3330     }
3331     tmpl.type = type;
3332     tmpl.numElements = val ? 2 : 1;
3333     tmpl.elements =
3334        alloca(sizeof(*tmpl.elements) * tmpl.numElements);
3335     tmpl.elements[0].item = kw->key;
3336     tmpl.elements[0].indent = alloca(2);
3337     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
3338     if (val) {
3339     tmpl.elements[1].item = (char *)val;
3340     tmpl.elements[1].indent = "";
3341     }
3342     }
3343    
3344     /* The last non-empty line gives us the indention to us and the line
3345     * to insert after. Note that comments are considered empty lines,
3346     * which may not be ideal? If there are no lines or we are looking at
3347     * the first line, we use defaultIndent (the first line is normally
3348     * indented differently from the rest) */
3349     for (line = entry->lines, prev = NULL; line; line = line->next) {
3350     if (line->numElements)
3351     prev = line;
3352     /* fall back on the last line if prev isn't otherwise set */
3353     if (!line->next && !prev)
3354     prev = line;
3355     }
3356    
3357     struct singleLine *menuEntry;
3358     menuEntry = getLineByType(LT_MENUENTRY, entry->lines);
3359     if (tmpl.type == LT_ENTRY_END) {
3360     if (menuEntry)
3361     tmpl.indent = menuEntry->indent;
3362     else
3363     tmpl.indent = defaultIndent ? : "";
3364     } else if (tmpl.type != LT_MENUENTRY) {
3365     if (menuEntry)
3366     tmpl.indent = "\t";
3367     else if (prev == entry->lines)
3368     tmpl.indent = defaultIndent ? : "";
3369     else
3370     tmpl.indent = prev->indent;
3371     }
3372    
3373     return addLineTmpl(entry, &tmpl, prev, val, cfi);
3374    }
3375    
3376    void removeLine(struct singleEntry *entry, struct singleLine *line)
3377    {
3378     struct singleLine *prev;
3379     int i;
3380    
3381     for (i = 0; i < line->numElements; i++) {
3382     free(line->elements[i].item);
3383     free(line->elements[i].indent);
3384     }
3385     free(line->elements);
3386     free(line->indent);
3387    
3388     if (line == entry->lines) {
3389     entry->lines = line->next;
3390     } else {
3391     prev = entry->lines;
3392     while (prev->next != line)
3393     prev = prev->next;
3394     prev->next = line->next;
3395     }
3396    
3397     free(line);
3398    }
3399    
3400    static void requote(struct singleLine *tmplLine, struct configFileInfo *cfi)
3401    {
3402     struct singleLine newLine = {
3403     .indent = tmplLine->indent,
3404     .type = tmplLine->type,
3405     .next = tmplLine->next,
3406     };
3407     int firstQuotedItem = -1;
3408     int quoteLen = 0;
3409     int j;
3410     int element = 0;
3411     char *c;
3412    
3413     c = malloc(strlen(tmplLine->elements[0].item) + 1);
3414     strcpy(c, tmplLine->elements[0].item);
3415     insertElement(&newLine, c, element++, cfi);
3416     free(c);
3417     c = NULL;
3418    
3419     for (j = 1; j < tmplLine->numElements; j++) {
3420     if (firstQuotedItem == -1) {
3421     quoteLen += strlen(tmplLine->elements[j].item);
3422    
3423     if (isquote(tmplLine->elements[j].item[0])) {
3424     firstQuotedItem = j;
3425     quoteLen +=
3426        strlen(tmplLine->elements[j].indent);
3427     } else {
3428     c = malloc(quoteLen + 1);
3429     strcpy(c, tmplLine->elements[j].item);
3430     insertElement(&newLine, c, element++, cfi);
3431     free(c);
3432     quoteLen = 0;
3433     }
3434     } else {
3435     int itemlen = strlen(tmplLine->elements[j].item);
3436     quoteLen += itemlen;
3437     quoteLen += strlen(tmplLine->elements[j].indent);
3438    
3439     if (isquote(tmplLine->elements[j].item[itemlen - 1])) {
3440     c = malloc(quoteLen + 1);
3441     c[0] = '\0';
3442     for (int i = firstQuotedItem; i < j + 1; i++) {
3443     strcat(c, tmplLine->elements[i].item);
3444     strcat(c, tmplLine->elements[i].indent);
3445     }
3446     insertElement(&newLine, c, element++, cfi);
3447     free(c);
3448    
3449     firstQuotedItem = -1;
3450     quoteLen = 0;
3451     }
3452     }
3453     }
3454     while (tmplLine->numElements)
3455     removeElement(tmplLine, 0);
3456     if (tmplLine->elements)
3457     free(tmplLine->elements);
3458    
3459     tmplLine->numElements = newLine.numElements;
3460     tmplLine->elements = newLine.elements;
3461    }
3462    
3463    static void insertElement(struct singleLine *line,
3464      const char *item, int insertHere,
3465      struct configFileInfo *cfi)
3466    {
3467     struct keywordTypes *kw;
3468     char indent[2] = "";
3469    
3470     /* sanity check */
3471     if (insertHere > line->numElements) {
3472     dbgPrintf
3473        ("insertElement() adjusting insertHere from %d to %d\n",
3474         insertHere, line->numElements);
3475     insertHere = line->numElements;
3476     }
3477    
3478     line->elements = realloc(line->elements, (line->numElements + 1) *
3479     sizeof(*line->elements));
3480     memmove(&line->elements[insertHere + 1],
3481     &line->elements[insertHere],
3482     (line->numElements - insertHere) * sizeof(*line->elements));
3483     line->elements[insertHere].item = strdup(item);
3484    
3485     kw = getKeywordByType(line->type, cfi);
3486    
3487     if (line->numElements == 0) {
3488     indent[0] = '\0';
3489     } else if (insertHere == 0) {
3490     indent[0] = kw->nextChar;
3491     } else if (kw->separatorChar != '\0') {
3492     indent[0] = kw->separatorChar;
3493     } else {
3494     indent[0] = ' ';
3495     }
3496    
3497     if (insertHere > 0 && line->elements[insertHere - 1].indent[0] == '\0') {
3498     /* move the end-of-line forward */
3499     line->elements[insertHere].indent =
3500        line->elements[insertHere - 1].indent;
3501     line->elements[insertHere - 1].indent = strdup(indent);
3502     } else {
3503     line->elements[insertHere].indent = strdup(indent);
3504     }
3505    
3506     line->numElements++;
3507    
3508     dbgPrintf("insertElement(%s, '%s%s', %d)\n",
3509      line->elements[0].item,
3510      line->elements[insertHere].item,
3511      line->elements[insertHere].indent, insertHere);
3512    }
3513    
3514    static void removeElement(struct singleLine *line, int removeHere)
3515    {
3516     int i;
3517    
3518     /* sanity check */
3519     if (removeHere >= line->numElements)
3520     return;
3521    
3522     dbgPrintf("removeElement(%s, %d:%s)\n", line->elements[0].item,
3523      removeHere, line->elements[removeHere].item);
3524    
3525     free(line->elements[removeHere].item);
3526    
3527     if (removeHere > 1) {
3528     /* previous argument gets this argument's post-indentation */
3529     free(line->elements[removeHere - 1].indent);
3530     line->elements[removeHere - 1].indent =
3531        line->elements[removeHere].indent;
3532     } else {
3533     free(line->elements[removeHere].indent);
3534     }
3535    
3536     /* now collapse the array, but don't bother to realloc smaller */
3537     for (i = removeHere; i < line->numElements - 1; i++)
3538     line->elements[i] = line->elements[i + 1];
3539    
3540     line->numElements--;
3541    }
3542    
3543    int argMatch(const char *one, const char *two)
3544    {
3545     char *first, *second;
3546     char *chptr;
3547    
3548     first = strcpy(alloca(strlen(one) + 1), one);
3549     second = strcpy(alloca(strlen(two) + 1), two);
3550    
3551     chptr = strchr(first, '=');
3552     if (chptr)
3553     *chptr = '\0';
3554    
3555     chptr = strchr(second, '=');
3556     if (chptr)
3557     *chptr = '\0';
3558    
3559     return strcmp(first, second);
3560    }
3561    
3562    int updateActualImage(struct grubConfig *cfg, const char *image,
3563          const char *prefix, const char *addArgs,
3564          const char *removeArgs, int multibootArgs)
3565    {
3566     struct singleEntry *entry;
3567     struct singleLine *line, *rootLine;
3568     int index = 0;
3569     int i, k;
3570     const char **newArgs, **oldArgs;
3571     const char **arg;
3572     int useKernelArgs, useRoot;
3573     int firstElement;
3574     int *usedElements;
3575     int doreplace;
3576    
3577     if (!image)
3578     return 0;
3579    
3580     if (!addArgs) {
3581     newArgs = malloc(sizeof(*newArgs));
3582     *newArgs = NULL;
3583     } else {
3584     if (poptParseArgvString(addArgs, NULL, &newArgs)) {
3585     fprintf(stderr,
3586     _("grubby: error separating arguments '%s'\n"),
3587     addArgs);
3588     return 1;
3589     }
3590     }
3591    
3592     if (!removeArgs) {
3593     oldArgs = malloc(sizeof(*oldArgs));
3594     *oldArgs = NULL;
3595     } else {
3596     if (poptParseArgvString(removeArgs, NULL, &oldArgs)) {
3597     fprintf(stderr,
3598     _("grubby: error separating arguments '%s'\n"),
3599     removeArgs);
3600     free(newArgs);
3601     return 1;
3602     }
3603     }
3604    
3605     useKernelArgs = (getKeywordByType(LT_KERNELARGS, cfg->cfi)
3606     && (!multibootArgs || cfg->cfi->mbConcatArgs));
3607    
3608     useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)
3609       && !multibootArgs);
3610    
3611     for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3612    
3613     if (multibootArgs && !entry->multiboot)
3614     continue;
3615    
3616     /* Determine where to put the args.  If this config supports
3617     * LT_KERNELARGS, use that.  Otherwise use
3618     * LT_HYPER/LT_KERNEL/LT_MBMODULE lines.
3619     */
3620     if (useKernelArgs) {
3621     line = getLineByType(LT_KERNELARGS, entry->lines);
3622     if (!line) {
3623     /* no LT_KERNELARGS, need to add it */
3624     line = addLine(entry, cfg->cfi, LT_KERNELARGS,
3625           cfg->secondaryIndent, NULL);
3626     }
3627     firstElement = 1;
3628    
3629     } else if (multibootArgs) {
3630     line = getLineByType(LT_HYPER, entry->lines);
3631     if (!line) {
3632     /* a multiboot entry without LT_HYPER? */
3633     continue;
3634     }
3635     firstElement = 2;
3636    
3637     } else {
3638     line =
3639        getLineByType(LT_KERNEL | LT_MBMODULE |
3640      LT_KERNEL_EFI | LT_KERNEL_16,
3641      entry->lines);
3642     if (!line) {
3643     /* no LT_KERNEL or LT_MBMODULE in this entry? */
3644     continue;
3645     }
3646     firstElement = 2;
3647     }
3648    
3649     /* handle the elilo case which does:
3650     *   append="hypervisor args -- kernel args"
3651     */
3652     if (entry->multiboot && cfg->cfi->mbConcatArgs) {
3653     /* this is a multiboot entry, make sure there's
3654     * -- on the args line
3655     */
3656     for (i = firstElement; i < line->numElements; i++) {
3657     if (!strcmp(line->elements[i].item, "--"))
3658     break;
3659     }
3660     if (i == line->numElements) {
3661     /* assume all existing args are kernel args,
3662     * prepend -- to make it official
3663     */
3664     insertElement(line, "--", firstElement,
3665          cfg->cfi);
3666     i = firstElement;
3667     }
3668     if (!multibootArgs) {
3669     /* kernel args start after the -- */
3670     firstElement = i + 1;
3671     }
3672     } else if (cfg->cfi->mbConcatArgs) {
3673     /* this is a non-multiboot entry, remove hyper args */
3674     for (i = firstElement; i < line->numElements; i++) {
3675     if (!strcmp(line->elements[i].item, "--"))
3676     break;
3677     }
3678     if (i < line->numElements) {
3679     /* remove args up to -- */
3680     while (strcmp
3681           (line->elements[firstElement].item,
3682     "--"))
3683     removeElement(line, firstElement);
3684     /* remove -- */
3685     removeElement(line, firstElement);
3686     }
3687     }
3688    
3689     usedElements = calloc(line->numElements, sizeof(*usedElements));
3690    
3691     for (k = 0, arg = newArgs; *arg; arg++, k++) {
3692    
3693     doreplace = 1;
3694     for (i = firstElement; i < line->numElements; i++) {
3695     if (multibootArgs && cfg->cfi->mbConcatArgs &&
3696        !strcmp(line->elements[i].item, "--")) {
3697     /* reached the end of hyper args, insert here */
3698     doreplace = 0;
3699     break;
3700     }
3701     if (usedElements[i])
3702     continue;
3703     if (!argMatch(line->elements[i].item, *arg)) {
3704     usedElements[i] = 1;
3705     break;
3706     }
3707     }
3708    
3709     if (i < line->numElements && doreplace) {
3710     /* direct replacement */
3711     free(line->elements[i].item);
3712     line->elements[i].item = strdup(*arg);
3713    
3714     } else if (useRoot && !strncmp(*arg, "root=/dev/", 10)) {
3715     /* root= replacement */
3716     rootLine = getLineByType(LT_ROOT, entry->lines);
3717     if (rootLine) {
3718     free(rootLine->elements[1].item);
3719     rootLine->elements[1].item =
3720        strdup(*arg + 5);
3721     } else {
3722     rootLine =
3723        addLine(entry, cfg->cfi, LT_ROOT,
3724        cfg->secondaryIndent,
3725        *arg + 5);
3726     }
3727     }
3728    
3729     else {
3730     /* insert/append */
3731     insertElement(line, *arg, i, cfg->cfi);
3732     usedElements =
3733        realloc(usedElements,
3734        line->numElements *
3735        sizeof(*usedElements));
3736     memmove(&usedElements[i + 1], &usedElements[i],
3737     line->numElements - i - 1);
3738     usedElements[i] = 1;
3739    
3740     /* if we updated a root= here even though
3741     * there is a LT_ROOT available we need to
3742     * remove the LT_ROOT entry (this will happen
3743     * if we switch from a device to a label) */
3744     if (useRoot && !strncmp(*arg, "root=", 5)) {
3745     rootLine =
3746        getLineByType(LT_ROOT,
3747      entry->lines);
3748     if (rootLine)
3749     removeLine(entry, rootLine);
3750     }
3751     }
3752     }
3753    
3754     free(usedElements);
3755    
3756     for (arg = oldArgs; *arg; arg++) {
3757     for (i = firstElement; i < line->numElements; i++) {
3758     if (multibootArgs && cfg->cfi->mbConcatArgs &&
3759        !strcmp(line->elements[i].item, "--"))
3760     /* reached the end of hyper args, stop here */
3761     break;
3762     if (!argMatch(line->elements[i].item, *arg)) {
3763     removeElement(line, i);
3764     break;
3765     }
3766     }
3767     /* handle removing LT_ROOT line too */
3768     if (useRoot && !strncmp(*arg, "root=", 5)) {
3769     rootLine = getLineByType(LT_ROOT, entry->lines);
3770     if (rootLine)
3771     removeLine(entry, rootLine);
3772     }
3773     }
3774    
3775     if (line->numElements == 1) {
3776     /* don't need the line at all (note it has to be a
3777       LT_KERNELARGS for this to happen */
3778     removeLine(entry, line);
3779     }
3780     }
3781    
3782     free(newArgs);
3783     free(oldArgs);
3784    
3785     return 0;
3786    }
3787    
3788    int updateImage(struct grubConfig *cfg, const char *image,
3789     const char *prefix, const char *addArgs,
3790     const char *removeArgs,
3791     const char *addMBArgs, const char *removeMBArgs)
3792    {
3793     int rc = 0;
3794    
3795     if (!image)
3796     return rc;
3797    
3798     /* update the main args first... */
3799     if (addArgs || removeArgs)
3800     rc = updateActualImage(cfg, image, prefix, addArgs, removeArgs,
3801           0);
3802     if (rc)
3803     return rc;
3804    
3805     /* and now any multiboot args */
3806     if (addMBArgs || removeMBArgs)
3807     rc = updateActualImage(cfg, image, prefix, addMBArgs,
3808           removeMBArgs, 1);
3809     return rc;
3810    }
3811    
3812    int addMBInitrd(struct grubConfig *cfg, const char *newMBKernel,
3813     const char *image, const char *prefix, const char *initrd,
3814     const char *title)
3815    {
3816     struct singleEntry *entry;
3817     struct singleLine *line, *kernelLine, *endLine = NULL;
3818     int index = 0;
3819    
3820     if (!image)
3821     return 0;
3822    
3823     for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3824     kernelLine = getLineByType(LT_MBMODULE, entry->lines);
3825     if (!kernelLine)
3826     continue;
3827    
3828     /* if title is supplied, the entry's title must match it. */
3829     if (title) {
3830     char *linetitle;
3831    
3832     line =
3833        getLineByType(LT_TITLE | LT_MENUENTRY,
3834      entry->lines);
3835     if (!line)
3836     continue;
3837    
3838     linetitle = extractTitle(cfg, line);
3839     if (!linetitle)
3840     continue;
3841     if (strcmp(title, linetitle)) {
3842     free(linetitle);
3843     continue;
3844     }
3845     free(linetitle);
3846     }
3847    
3848     if (prefix) {
3849     int prefixLen = strlen(prefix);
3850     if (!strncmp(initrd, prefix, prefixLen))
3851     initrd += prefixLen;
3852     }
3853     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3854     if (endLine)
3855     removeLine(entry, endLine);
3856     line =
3857        addLine(entry, cfg->cfi,
3858        preferredLineType(LT_MBMODULE, cfg->cfi),
3859        kernelLine->indent, initrd);
3860     if (!line)
3861     return 1;
3862     if (endLine) {
3863     line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3864     if (!line)
3865     return 1;
3866     }
3867    
3868     break;
3869     }
3870    
3871     return 0;
3872    }
3873    
3874    int updateInitrd(struct grubConfig *cfg, const char *image,
3875     const char *prefix, const char *initrd, const char *title)
3876    {
3877     struct singleEntry *entry;
3878     struct singleLine *line, *kernelLine, *endLine = NULL;
3879     int index = 0;
3880    
3881     if (!image)
3882     return 0;
3883    
3884     for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3885     kernelLine =
3886        getLineByType(LT_KERNEL | LT_KERNEL_EFI | LT_KERNEL_16,
3887      entry->lines);
3888     if (!kernelLine)
3889     continue;
3890    
3891     /* if title is supplied, the entry's title must match it. */
3892     if (title) {
3893     char *linetitle;
3894    
3895     line =
3896        getLineByType(LT_TITLE | LT_MENUENTRY,
3897      entry->lines);
3898     if (!line)
3899     continue;
3900    
3901     linetitle = extractTitle(cfg, line);
3902     if (!linetitle)
3903     continue;
3904     if (strcmp(title, linetitle)) {
3905     free(linetitle);
3906     continue;
3907     }
3908     free(linetitle);
3909     }
3910    
3911     line =
3912        getLineByType(LT_INITRD | LT_INITRD_EFI | LT_INITRD_16,
3913      entry->lines);
3914     if (line)
3915     removeLine(entry, line);
3916     if (prefix) {
3917     int prefixLen = strlen(prefix);
3918     if (!strncmp(initrd, prefix, prefixLen))
3919     initrd += prefixLen;
3920     }
3921     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3922     if (endLine)
3923     removeLine(entry, endLine);
3924     enum lineType_e lt;
3925     switch (kernelLine->type) {
3926     case LT_KERNEL:
3927     lt = LT_INITRD;
3928     break;
3929     case LT_KERNEL_EFI:
3930     lt = LT_INITRD_EFI;
3931     break;
3932     case LT_KERNEL_16:
3933     lt = LT_INITRD_16;
3934     break;
3935     default:
3936     lt = preferredLineType(LT_INITRD, cfg->cfi);
3937     }
3938     line = addLine(entry, cfg->cfi, lt, kernelLine->indent, initrd);
3939     if (!line)
3940     return 1;
3941     if (endLine) {
3942     line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3943     if (!line)
3944     return 1;
3945     }
3946    
3947     break;
3948     }
3949    
3950     return 0;
3951    }
3952    
3953    int checkDeviceBootloader(const char *device, const unsigned char *boot)
3954    {
3955     int fd;
3956     unsigned char bootSect[512];
3957     int offset;
3958    
3959     fd = open(device, O_RDONLY);
3960     if (fd < 0) {
3961     fprintf(stderr, _("grubby: unable to open %s: %s\n"),
3962     device, strerror(errno));
3963     return 1;
3964     }
3965    
3966     if (read(fd, bootSect, 512) != 512) {
3967     fprintf(stderr, _("grubby: unable to read %s: %s\n"),
3968     device, strerror(errno));
3969     return 1;
3970     }
3971     close(fd);
3972    
3973     /* first three bytes should match, a jmp short should be in there */
3974     if (memcmp(boot, bootSect, 3))
3975     return 0;
3976    
3977     if (boot[1] == JMP_SHORT_OPCODE) {
3978     offset = boot[2] + 2;
3979     } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {
3980     offset = (boot[3] << 8) + boot[2] + 2;
3981     } else if (boot[0] == JMP_SHORT_OPCODE) {
3982     offset = boot[1] + 2;
3983     /*
3984     * it looks like grub, when copying stage1 into the mbr,
3985     * patches stage1 right after the JMP location, replacing
3986     * other instructions such as JMPs for NOOPs. So, relax the
3987     * check a little bit by skipping those different bytes.
3988     */
3989     if ((bootSect[offset + 1] == NOOP_OPCODE)
3990        && (bootSect[offset + 2] == NOOP_OPCODE)) {
3991     offset = offset + 3;
3992     }
3993     } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {
3994     offset = (boot[2] << 8) + boot[1] + 2;
3995     } else {
3996     return 0;
3997     }
3998    
3999     if (memcmp(boot + offset, bootSect + offset, CODE_SEG_SIZE))
4000     return 0;
4001    
4002     return 2;
4003    }
4004    
4005    int checkLiloOnRaid(char *mdDev, const unsigned char *boot)
4006    {
4007     int fd;
4008     char buf[65536];
4009     char *end;
4010     char *chptr;
4011     char *chptr2;
4012     int rc;
4013    
4014     /* it's on raid; we need to parse /proc/mdstat and check all of the
4015     *raw* devices listed in there */
4016    
4017     if (!strncmp(mdDev, "/dev/", 5))
4018     mdDev += 5;
4019    
4020     if ((fd = open("/proc/mdstat", O_RDONLY)) < 0) {
4021     fprintf(stderr, _("grubby: failed to open /proc/mdstat: %s\n"),
4022     strerror(errno));
4023     return 2;
4024     }
4025    
4026     rc = read(fd, buf, sizeof(buf) - 1);
4027     if (rc < 0 || rc == (sizeof(buf) - 1)) {
4028     fprintf(stderr, _("grubby: failed to read /proc/mdstat: %s\n"),
4029     strerror(errno));
4030     close(fd);
4031     return 2;
4032     }
4033     close(fd);
4034     buf[rc] = '\0';
4035    
4036     chptr = buf;
4037     while (*chptr) {
4038     end = strchr(chptr, '\n');
4039     if (!end)
4040     break;
4041     *end = '\0';
4042    
4043     if (!strncmp(chptr, mdDev, strlen(mdDev)) &&
4044        chptr[strlen(mdDev)] == ' ') {
4045    
4046     /* found the device */
4047     while (*chptr && *chptr != ':')
4048     chptr++;
4049     chptr++;
4050     while (*chptr && isspace(*chptr))
4051     chptr++;
4052    
4053     /* skip the "active" bit */
4054     while (*chptr && !isspace(*chptr))
4055     chptr++;
4056     while (*chptr && isspace(*chptr))
4057     chptr++;
4058    
4059     /* skip the raid level */
4060     while (*chptr && !isspace(*chptr))
4061     chptr++;
4062     while (*chptr && isspace(*chptr))
4063     chptr++;
4064    
4065     /* everything else is partition stuff */
4066     while (*chptr) {
4067     chptr2 = chptr;
4068     while (*chptr2 && *chptr2 != '[')
4069     chptr2++;
4070     if (!*chptr2)
4071     break;
4072    
4073     /* yank off the numbers at the end */
4074     chptr2--;
4075     while (isdigit(*chptr2) && chptr2 > chptr)
4076     chptr2--;
4077     chptr2++;
4078     *chptr2 = '\0';
4079    
4080     /* Better, now we need the /dev/ back. We're
4081     * done with everything before this point, so
4082     * we can just put the /dev/ part there.
4083     * There will always be room. */
4084     memcpy(chptr - 5, "/dev/", 5);
4085     rc = checkDeviceBootloader(chptr - 5, boot);
4086     if (rc != 2) {
4087     return rc;
4088     }
4089    
4090     chptr = chptr2 + 1;
4091     /* skip the [11] bit */
4092     while (*chptr && !isspace(*chptr))
4093     chptr++;
4094     /* and move to the next one */
4095     while (*chptr && isspace(*chptr))
4096     chptr++;
4097     }
4098    
4099     /*  we're good to go */
4100     return 2;
4101     }
4102    
4103     chptr = end + 1;
4104     }
4105    
4106     fprintf(stderr,
4107     _("grubby: raid device /dev/%s not found in /proc/mdstat\n"),
4108     mdDev);
4109     return 0;
4110    }
4111    
4112    int checkForLilo(struct grubConfig *config)
4113    {
4114     int fd;
4115     unsigned char boot[512];
4116     struct singleLine *line;
4117    
4118     for (line = config->theLines; line; line = line->next)
4119     if (line->type == LT_BOOT)
4120     break;
4121    
4122     if (!line) {
4123     fprintf(stderr,
4124     _
4125     ("grubby: no boot line found in lilo configuration\n"));
4126     return 1;
4127     }
4128    
4129     if (line->numElements != 2)
4130     return 1;
4131    
4132     fd = open("/boot/boot.b", O_RDONLY);
4133     if (fd < 0) {
4134     fprintf(stderr, _("grubby: unable to open %s: %s\n"),
4135     "/boot/boot.b", strerror(errno));
4136     return 1;
4137     }
4138    
4139     if (read(fd, boot, 512) != 512) {
4140     fprintf(stderr, _("grubby: unable to read %s: %s\n"),
4141     "/boot/boot.b", strerror(errno));
4142     return 1;
4143     }
4144     close(fd);
4145    
4146     if (!strncmp("/dev/md", line->elements[1].item, 7))
4147     return checkLiloOnRaid(line->elements[1].item, boot);
4148    
4149     return checkDeviceBootloader(line->elements[1].item, boot);
4150    }
4151    
4152    int checkForGrub2(struct grubConfig *config)
4153    {
4154     if (!access("/etc/grub.d/", R_OK))
4155     return 2;
4156    
4157     return 1;
4158    }
4159    
4160    int checkForGrub(struct grubConfig *config)
4161    {
4162     int fd;
4163     unsigned char bootSect[512];
4164     char *boot;
4165     int onSuse = isSuseSystem();
4166    
4167     if (onSuse) {
4168     if (parseSuseGrubConf(NULL, &boot))
4169     return 0;
4170     } else {
4171     if (parseSysconfigGrub(NULL, &boot))
4172     return 0;
4173     }
4174    
4175     /* assume grub is not installed -- not an error condition */
4176     if (!boot)
4177     return 0;
4178    
4179     fd = open("/boot/grub/stage1", O_RDONLY);
4180     if (fd < 0)
4181     /* this doesn't exist if grub hasn't been installed */
4182     return 0;
4183    
4184     if (read(fd, bootSect, 512) != 512) {
4185     fprintf(stderr, _("grubby: unable to read %s: %s\n"),
4186     "/boot/grub/stage1", strerror(errno));
4187     close(fd);
4188     return 1;
4189     }
4190     close(fd);
4191    
4192     /* The more elaborate checks do not work on SuSE. The checks done
4193     * seem to be reasonble (at least for now), so just return success
4194     */
4195     if (onSuse)
4196     return 2;
4197    
4198     return checkDeviceBootloader(boot, bootSect);
4199    }
4200    
4201    int checkForExtLinux(struct grubConfig *config)
4202    {
4203     int fd;
4204     unsigned char bootSect[512];
4205     char *boot;
4206     char executable[] = "/boot/extlinux/extlinux";
4207    
4208     printf("entered: checkForExtLinux()\n");
4209    
4210     if (parseSysconfigGrub(NULL, &boot))
4211     return 0;
4212    
4213     /* assume grub is not installed -- not an error condition */
4214     if (!boot)
4215     return 0;
4216    
4217     fd = open(executable, O_RDONLY);
4218     if (fd < 0)
4219     /* this doesn't exist if grub hasn't been installed */
4220     return 0;
4221    
4222     if (read(fd, bootSect, 512) != 512) {
4223     fprintf(stderr, _("grubby: unable to read %s: %s\n"),
4224     executable, strerror(errno));
4225     return 1;
4226     }
4227     close(fd);
4228    
4229     return checkDeviceBootloader(boot, bootSect);
4230    }
4231    
4232    int checkForYaboot(struct grubConfig *config)
4233    {
4234     /*
4235     * This is a simplistic check that we consider good enough for own puporses
4236     *
4237     * If we were to properly check if yaboot is *installed* we'd need to:
4238     * 1) get the system boot device (LT_BOOT)
4239     * 2) considering it's a raw filesystem, check if the yaboot binary matches
4240     *    the content on the boot device
4241     * 3) if not, copy the binary to a temporary file and run "addnote" on it
4242     * 4) check again if binary and boot device contents match
4243     */
4244     if (!access("/etc/yaboot.conf", R_OK))
4245     return 2;
4246    
4247     return 1;
4248    }
4249    
4250    int checkForElilo(struct grubConfig *config)
4251    {
4252     if (!access("/etc/elilo.conf", R_OK))
4253     return 2;
4254    
4255     return 1;
4256    }
4257    
4258    static char *getRootSpecifier(char *str)
4259    {
4260     char *idx, *rootspec = NULL;
4261    
4262     if (*str == '(') {
4263     idx = rootspec = strdup(str);
4264     while (*idx && (*idx != ')') && (!isspace(*idx)))
4265     idx++;
4266     *(++idx) = '\0';
4267     }
4268     return rootspec;
4269    }
4270    
4271    static char *getInitrdVal(struct grubConfig *config,
4272      const char *prefix, struct singleLine *tmplLine,
4273      const char *newKernelInitrd,
4274      const char **extraInitrds, int extraInitrdCount)
4275    {
4276     char *initrdVal, *end;
4277     int i;
4278     size_t totalSize;
4279     size_t prefixLen;
4280     char separatorChar;
4281    
4282     prefixLen = strlen(prefix);
4283     totalSize = strlen(newKernelInitrd) - prefixLen + 1 /* \0 */ ;
4284    
4285     for (i = 0; i < extraInitrdCount; i++) {
4286     totalSize += sizeof(separatorChar);
4287     totalSize += strlen(extraInitrds[i]) - prefixLen;
4288     }
4289    
4290     initrdVal = end = malloc(totalSize);
4291    
4292     end = stpcpy(end, newKernelInitrd + prefixLen);
4293    
4294     separatorChar = getKeywordByType(LT_INITRD, config->cfi)->separatorChar;
4295     for (i = 0; i < extraInitrdCount; i++) {
4296     const char *extraInitrd;
4297     int j;
4298    
4299     extraInitrd = extraInitrds[i] + prefixLen;
4300     /* Don't add entries that are already there */
4301     if (tmplLine != NULL) {
4302     for (j = 2; j < tmplLine->numElements; j++)
4303     if (strcmp
4304        (extraInitrd,
4305         tmplLine->elements[j].item) == 0)
4306     break;
4307    
4308     if (j != tmplLine->numElements)
4309     continue;
4310     }
4311    
4312     *end++ = separatorChar;
4313     end = stpcpy(end, extraInitrd);
4314     }
4315    
4316     return initrdVal;
4317    }
4318    
4319    int addNewKernel(struct grubConfig *config, struct singleEntry *template,
4320     const char *prefix,
4321     const char *newKernelPath, const char *newKernelTitle,
4322     const char *newKernelArgs, const char *newKernelInitrd,
4323     const char **extraInitrds, int extraInitrdCount,
4324     const char *newMBKernel, const char *newMBKernelArgs,
4325     const char *newDevTreePath, int newIndex)
4326    {
4327     struct singleEntry *new, *entry, *prev = NULL;
4328     struct singleLine *newLine = NULL, *tmplLine = NULL, *masterLine = NULL;
4329     int needs;
4330     char *indexs;
4331     char *chptr;
4332     int rc;
4333    
4334     if (!newKernelPath)
4335     return 0;
4336    
4337     rc = asprintf(&indexs, "%d", newIndex);
4338     if (rc < 0)
4339     return 1;
4340    
4341     /* if the newKernelTitle is too long silently munge it into something
4342     * we can live with. truncating is first check, then we'll just mess with
4343     * it until it looks better */
4344     if (config->cfi->maxTitleLength &&
4345        (strlen(newKernelTitle) > config->cfi->maxTitleLength)) {
4346     char *buf = alloca(config->cfi->maxTitleLength + 7);
4347     char *numBuf = alloca(config->cfi->maxTitleLength + 1);
4348     int i = 1;
4349    
4350     sprintf(buf, "TITLE=%.*s", config->cfi->maxTitleLength,
4351     newKernelTitle);
4352     while (findEntryByPath(config, buf, NULL, NULL)) {
4353     sprintf(numBuf, "%d", i++);
4354     strcpy(buf + strlen(buf) - strlen(numBuf), numBuf);
4355     }
4356    
4357     newKernelTitle = buf + 6;
4358     }
4359    
4360     new = malloc(sizeof(*new));
4361     new->skip = 0;
4362     new->multiboot = 0;
4363     new->lines = NULL;
4364     entry = config->entries;
4365     for (unsigned int i = 0; i < newIndex; i++) {
4366     if (!entry)
4367     break;
4368     prev = entry;
4369     entry = entry->next;
4370     }
4371     new->next = entry;
4372    
4373     if (prev)
4374     prev->next = new;
4375     else
4376     config->entries = new;
4377    
4378     /* copy/update from the template */
4379     needs = NEED_KERNEL | NEED_TITLE;
4380     if (newKernelInitrd)
4381     needs |= NEED_INITRD;
4382     if (newMBKernel) {
4383     needs |= NEED_MB;
4384     new->multiboot = 1;
4385     }
4386     if (newDevTreePath && getKeywordByType(LT_DEVTREE, config->cfi))
4387     needs |= NEED_DEVTREE;
4388    
4389     if (template) {
4390     for (masterLine = template->lines;
4391         masterLine && (tmplLine = lineDup(masterLine));
4392         lineFree(tmplLine), masterLine = masterLine->next) {
4393     dbgPrintf("addNewKernel processing %d\n",
4394      tmplLine->type);
4395    
4396     /* skip comments */
4397     chptr = tmplLine->indent;
4398     while (*chptr && isspace(*chptr))
4399     chptr++;
4400     if (*chptr == '#')
4401     continue;
4402    
4403     if (iskernel(tmplLine->type)
4404        && tmplLine->numElements >= 2) {
4405     if (!template->multiboot && (needs & NEED_MB)) {
4406     /* it's not a multiboot template and
4407     * this is the kernel line.  Try to
4408     * be intelligent about inserting the
4409     * hypervisor at the same time.
4410     */
4411     if (config->cfi->mbHyperFirst) {
4412     /* insert the hypervisor first */
4413     newLine =
4414        addLine(new, config->cfi,
4415        LT_HYPER,
4416        tmplLine->indent,
4417        newMBKernel +
4418        strlen(prefix));
4419     /* set up for adding the
4420     * kernel line */
4421     free(tmplLine->indent);
4422     tmplLine->indent =
4423        strdup(config->
4424       secondaryIndent);
4425     needs &= ~NEED_MB;
4426     }
4427     if (needs & NEED_KERNEL) {
4428     /* use addLineTmpl to
4429     * preserve line elements,
4430     * otherwise we could just
4431     * call addLine.
4432     * Unfortunately this means
4433     * making some changes to the
4434     * template such as the
4435     * indent change above and
4436     * the type change below.
4437     */
4438     struct keywordTypes *mbm_kw =
4439        getKeywordByType
4440        (LT_MBMODULE, config->cfi);
4441     if (mbm_kw) {
4442     tmplLine->type =
4443        LT_MBMODULE;
4444     free(tmplLine->
4445         elements[0].item);
4446     tmplLine->elements[0].
4447        item =
4448        strdup(mbm_kw->key);
4449     }
4450     newLine =
4451        addLineTmpl(new, tmplLine,
4452     newLine,
4453     newKernelPath +
4454     strlen(prefix),
4455     config->cfi);
4456     needs &= ~NEED_KERNEL;
4457     }
4458     if (needs & NEED_MB) { /* !mbHyperFirst */
4459     newLine =
4460        addLine(new, config->cfi,
4461        LT_HYPER,
4462        config->
4463        secondaryIndent,
4464        newMBKernel +
4465        strlen(prefix));
4466     needs &= ~NEED_MB;
4467     }
4468     } else if (needs & NEED_KERNEL) {
4469     newLine =
4470        addLineTmpl(new, tmplLine, newLine,
4471     newKernelPath +
4472     strlen(prefix),
4473     config->cfi);
4474     needs &= ~NEED_KERNEL;
4475     }
4476    
4477     } else if (tmplLine->type == LT_HYPER &&
4478       tmplLine->numElements >= 2) {
4479     if (needs & NEED_MB) {
4480     newLine =
4481        addLineTmpl(new, tmplLine, newLine,
4482     newMBKernel +
4483     strlen(prefix),
4484     config->cfi);
4485     needs &= ~NEED_MB;
4486     }
4487    
4488     } else if (tmplLine->type == LT_MBMODULE &&
4489       tmplLine->numElements >= 2) {
4490     if (new->multiboot) {
4491     if (needs & NEED_KERNEL) {
4492     newLine =
4493        addLineTmpl(new, tmplLine,
4494     newLine,
4495     newKernelPath +
4496     strlen(prefix),
4497     config->cfi);
4498     needs &= ~NEED_KERNEL;
4499     } else if (config->cfi->mbInitRdIsModule
4500       && (needs & NEED_INITRD)) {
4501     char *initrdVal;
4502     initrdVal =
4503        getInitrdVal(config, prefix,
4504     tmplLine,
4505     newKernelInitrd,
4506     extraInitrds,
4507     extraInitrdCount);
4508     newLine =
4509        addLineTmpl(new, tmplLine,
4510     newLine,
4511     initrdVal,
4512     config->cfi);
4513     free(initrdVal);
4514     needs &= ~NEED_INITRD;
4515     }
4516     } else if (needs & NEED_KERNEL) {
4517     /* template is multi but new is not,
4518     * insert the kernel in the first
4519     * module slot
4520     */
4521     tmplLine->type =
4522        preferredLineType(LT_KERNEL,
4523          config->cfi);
4524     free(tmplLine->elements[0].item);
4525     tmplLine->elements[0].item =
4526        strdup(getKeywordByType
4527       (tmplLine->type,
4528        config->cfi)->key);
4529     newLine =
4530        addLineTmpl(new, tmplLine, newLine,
4531     newKernelPath +
4532     strlen(prefix),
4533     config->cfi);
4534     needs &= ~NEED_KERNEL;
4535     } else if (needs & NEED_INITRD) {
4536     char *initrdVal;
4537     /* template is multi but new is not,
4538     * insert the initrd in the second
4539     * module slot
4540     */
4541     tmplLine->type =
4542        preferredLineType(LT_INITRD,
4543          config->cfi);
4544     free(tmplLine->elements[0].item);
4545     tmplLine->elements[0].item =
4546        strdup(getKeywordByType
4547       (tmplLine->type,
4548        config->cfi)->key);
4549     initrdVal =
4550        getInitrdVal(config, prefix,
4551     tmplLine,
4552     newKernelInitrd,
4553     extraInitrds,
4554     extraInitrdCount);
4555     newLine =
4556        addLineTmpl(new, tmplLine, newLine,
4557     initrdVal, config->cfi);
4558     free(initrdVal);
4559     needs &= ~NEED_INITRD;
4560     }
4561    
4562     } else if (isinitrd(tmplLine->type)
4563       && tmplLine->numElements >= 2) {
4564     if (needs & NEED_INITRD && new->multiboot
4565        && !template->multiboot
4566        && config->cfi->mbInitRdIsModule) {
4567     /* make sure we don't insert the
4568     * module initrd before the module
4569     * kernel... if we don't do it here,
4570     * it will be inserted following the
4571     * template.
4572     */
4573     if (!needs & NEED_KERNEL) {
4574     char *initrdVal;
4575    
4576     initrdVal =
4577        getInitrdVal(config, prefix,
4578     tmplLine,
4579     newKernelInitrd,
4580     extraInitrds,
4581     extraInitrdCount);
4582     newLine =
4583        addLine(new, config->cfi,
4584        LT_MBMODULE,
4585        config->
4586        secondaryIndent,
4587        initrdVal);
4588     free(initrdVal);
4589     needs &= ~NEED_INITRD;
4590     }
4591     } else if (needs & NEED_INITRD) {
4592     char *initrdVal;
4593     initrdVal =
4594        getInitrdVal(config, prefix,
4595     tmplLine,
4596     newKernelInitrd,
4597     extraInitrds,
4598     extraInitrdCount);
4599     newLine =
4600        addLineTmpl(new, tmplLine, newLine,
4601     initrdVal, config->cfi);
4602     free(initrdVal);
4603     needs &= ~NEED_INITRD;
4604     }
4605    
4606     } else if (tmplLine->type == LT_MENUENTRY &&
4607       (needs & NEED_TITLE)) {
4608     requote(tmplLine, config->cfi);
4609     char *nkt = malloc(strlen(newKernelTitle) + 3);
4610     strcpy(nkt, "'");
4611     strcat(nkt, newKernelTitle);
4612     strcat(nkt, "'");
4613     newLine =
4614        addLineTmpl(new, tmplLine, newLine, nkt,
4615     config->cfi);
4616     free(nkt);
4617     needs &= ~NEED_TITLE;
4618     } else if (tmplLine->type == LT_TITLE &&
4619       (needs & NEED_TITLE)) {
4620     if (tmplLine->numElements >= 2) {
4621     newLine =
4622        addLineTmpl(new, tmplLine, newLine,
4623     newKernelTitle,
4624     config->cfi);
4625     needs &= ~NEED_TITLE;
4626     } else if (tmplLine->numElements == 1 &&
4627       config->cfi->titleBracketed) {
4628     /* addLineTmpl doesn't handle
4629     * titleBracketed */
4630     newLine =
4631        addLine(new, config->cfi, LT_TITLE,
4632        tmplLine->indent,
4633        newKernelTitle);
4634     needs &= ~NEED_TITLE;
4635     }
4636     } else if (tmplLine->type == LT_ECHO) {
4637     requote(tmplLine, config->cfi);
4638     static const char *prefix = "'Loading ";
4639     if (tmplLine->numElements > 1 &&
4640        strstr(tmplLine->elements[1].item, prefix)
4641        && masterLine->next
4642        && iskernel(masterLine->next->type)) {
4643     char *newTitle =
4644        malloc(strlen(prefix) +
4645       strlen(newKernelTitle) + 2);
4646    
4647     strcpy(newTitle, prefix);
4648     strcat(newTitle, newKernelTitle);
4649     strcat(newTitle, "'");
4650     newLine =
4651        addLine(new, config->cfi, LT_ECHO,
4652        tmplLine->indent, newTitle);
4653     free(newTitle);
4654     } else {
4655     /* pass through other lines from the
4656     * template */
4657     newLine =
4658        addLineTmpl(new, tmplLine, newLine,
4659     NULL, config->cfi);
4660     }
4661     } else if (tmplLine->type == LT_DEVTREE &&
4662       tmplLine->numElements == 2
4663       && newDevTreePath) {
4664     newLine =
4665        addLineTmpl(new, tmplLine, newLine,
4666     newDevTreePath + strlen(prefix),
4667     config->cfi);
4668     needs &= ~NEED_DEVTREE;
4669     } else if (tmplLine->type == LT_ENTRY_END
4670       && needs & NEED_DEVTREE) {
4671     const char *ndtp = newDevTreePath;
4672     if (!strncmp
4673        (newDevTreePath, prefix, strlen(prefix)))
4674     ndtp += strlen(prefix);
4675     newLine = addLine(new, config->cfi, LT_DEVTREE,
4676      config->secondaryIndent,
4677      ndtp);
4678     needs &= ~NEED_DEVTREE;
4679     newLine =
4680        addLineTmpl(new, tmplLine, newLine, NULL,
4681     config->cfi);
4682     } else {
4683     /* pass through other lines from the template */
4684     newLine =
4685        addLineTmpl(new, tmplLine, newLine, NULL,
4686     config->cfi);
4687     }
4688     }
4689    
4690     } else {
4691     /* don't have a template, so start the entry with the
4692     * appropriate starting line
4693     */
4694     switch (config->cfi->entryStart) {
4695     case LT_KERNEL:
4696     case LT_KERNEL_EFI:
4697     case LT_KERNEL_16:
4698     if (new->multiboot && config->cfi->mbHyperFirst) {
4699     /* fall through to LT_HYPER */
4700     } else {
4701     newLine = addLine(new, config->cfi,
4702      preferredLineType(LT_KERNEL,
4703        config->
4704        cfi),
4705      config->primaryIndent,
4706      newKernelPath +
4707      strlen(prefix));
4708     needs &= ~NEED_KERNEL;
4709     break;
4710     }
4711    
4712     case LT_HYPER:
4713     newLine = addLine(new, config->cfi, LT_HYPER,
4714      config->primaryIndent,
4715      newMBKernel + strlen(prefix));
4716     needs &= ~NEED_MB;
4717     break;
4718    
4719     case LT_MENUENTRY:{
4720     char *nkt = malloc(strlen(newKernelTitle) + 3);
4721     strcpy(nkt, "'");
4722     strcat(nkt, newKernelTitle);
4723     strcat(nkt, "'");
4724     newLine =
4725        addLine(new, config->cfi, LT_MENUENTRY,
4726        config->primaryIndent, nkt);
4727     free(nkt);
4728     needs &= ~NEED_TITLE;
4729     needs |= NEED_END;
4730     break;
4731     }
4732     case LT_TITLE:
4733     if (useextlinuxmenu != 0) { // We just need useextlinuxmenu to not be zero (set above)
4734     char *templabel;
4735     int x = 0, y = 0;
4736    
4737     templabel = strdup(newKernelTitle);
4738     while (templabel[x]) {
4739     if (templabel[x] == ' ') {
4740     y = x;
4741     while (templabel[y]) {
4742     templabel[y] =
4743        templabel[y + 1];
4744     y++;
4745     }
4746     }
4747     x++;
4748     }
4749     newLine = addLine(new, config->cfi, LT_TITLE,
4750      config->primaryIndent,
4751      templabel);
4752     free(templabel);
4753     } else {
4754     newLine = addLine(new, config->cfi, LT_TITLE,
4755      config->primaryIndent,
4756      newKernelTitle);
4757     }
4758     needs &= ~NEED_TITLE;
4759     break;
4760    
4761     default:
4762     abort();
4763     }
4764     }
4765    
4766     struct singleLine *endLine = NULL;
4767     endLine = getLineByType(LT_ENTRY_END, new->lines);
4768     if (endLine) {
4769     removeLine(new, endLine);
4770     needs |= NEED_END;
4771     }
4772    
4773     /* add the remainder of the lines, i.e. those that either
4774     * weren't present in the template, or in the case of no template,
4775     * all the lines following the entryStart.
4776     */
4777     if (needs & NEED_TITLE) {
4778     newLine = addLine(new, config->cfi, LT_TITLE,
4779      config->secondaryIndent, newKernelTitle);
4780     needs &= ~NEED_TITLE;
4781     }
4782     if ((needs & NEED_MB) && config->cfi->mbHyperFirst) {
4783     newLine = addLine(new, config->cfi, LT_HYPER,
4784      config->secondaryIndent,
4785      newMBKernel + strlen(prefix));
4786     needs &= ~NEED_MB;
4787     }
4788     if (needs & NEED_KERNEL) {
4789     newLine = addLine(new, config->cfi,
4790      (new->multiboot
4791       && getKeywordByType(LT_MBMODULE,
4792           config->cfi))
4793      ? LT_MBMODULE : preferredLineType(LT_KERNEL,
4794        config->
4795        cfi),
4796      config->secondaryIndent,
4797      newKernelPath + strlen(prefix));
4798     needs &= ~NEED_KERNEL;
4799     }
4800     if (needs & NEED_MB) {
4801     newLine = addLine(new, config->cfi, LT_HYPER,
4802      config->secondaryIndent,
4803      newMBKernel + strlen(prefix));
4804     needs &= ~NEED_MB;
4805     }
4806     if (needs & NEED_INITRD) {
4807     char *initrdVal;
4808     initrdVal =
4809        getInitrdVal(config, prefix, NULL, newKernelInitrd,
4810     extraInitrds, extraInitrdCount);
4811     newLine =
4812        addLine(new, config->cfi,
4813        (new->multiboot
4814         && getKeywordByType(LT_MBMODULE, config->cfi))
4815        ? LT_MBMODULE : preferredLineType(LT_INITRD,
4816          config->cfi),
4817        config->secondaryIndent, initrdVal);
4818     free(initrdVal);
4819     needs &= ~NEED_INITRD;
4820     }
4821     if (needs & NEED_DEVTREE) {
4822     newLine = addLine(new, config->cfi, LT_DEVTREE,
4823      config->secondaryIndent, newDevTreePath);
4824     needs &= ~NEED_DEVTREE;
4825     }
4826    
4827     /* NEEDS_END must be last on bootloaders that need it... */
4828     if (needs & NEED_END) {
4829     newLine = addLine(new, config->cfi, LT_ENTRY_END,
4830      config->secondaryIndent, NULL);
4831     needs &= ~NEED_END;
4832     }
4833    
4834     if (needs) {
4835     printf(_("grubby: needs=%d, aborting\n"), needs);
4836     abort();
4837     }
4838    
4839     if (updateImage(config, indexs, prefix, newKernelArgs, NULL,
4840     newMBKernelArgs, NULL)) {
4841     config->isModified = 1;
4842     return 1;
4843     }
4844    
4845     return 0;
4846    }
4847    
4848    int main(int argc, const char **argv)
4849    {
4850     poptContext optCon;
4851     const char *grubConfig = NULL;
4852     char *outputFile = NULL;
4853     int arg = 0;
4854     int flags = 0;
4855     int badImageOkay = 0;
4856     int configureGrub2 = 0;
4857     int configureLilo = 0, configureELilo = 0, configureGrub = 0;
4858     int configureYaboot = 0, configureSilo = 0, configureZipl = 0;
4859     int configureExtLinux = 0;
4860     int bootloaderProbe = 0;
4861     int extraInitrdCount = 0;
4862     char *updateKernelPath = NULL;
4863     char *newKernelPath = NULL;
4864     char *removeKernelPath = NULL;
4865     char *newKernelArgs = NULL;
4866     char *newKernelInitrd = NULL;
4867     char *newKernelTitle = NULL;
4868     char *newDevTreePath = NULL;
4869     char *newMBKernel = NULL;
4870     char *newMBKernelArgs = NULL;
4871     int newIndex = 0;
4872     char *removeMBKernelArgs = NULL;
4873     char *removeMBKernel = NULL;
4874     char *bootPrefix = NULL;
4875     char *defaultKernel = NULL;
4876     char *removeArgs = NULL;
4877     char *kernelInfo = NULL;
4878     char *extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4879     char *envPath = NULL;
4880     const char *chptr = NULL;
4881     struct configFileInfo *cfi = NULL;
4882     struct grubConfig *config;
4883     struct singleEntry *template = NULL;
4884     int copyDefault = 0, makeDefault = 0;
4885     int displayDefault = 0;
4886     int displayDefaultIndex = 0;
4887     int displayDefaultTitle = 0;
4888     int defaultIndex = -1;
4889     struct poptOption options[] = {
4890     {"add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
4891     _("add an entry for the specified kernel"), _("kernel-path")},
4892     {"add-multiboot", 0, POPT_ARG_STRING, &newMBKernel, 0,
4893     _("add an entry for the specified multiboot kernel"), NULL},
4894     {"args", 0, POPT_ARG_STRING, &newKernelArgs, 0,
4895     _("default arguments for the new kernel or new arguments for "
4896       "kernel being updated"), _("args")},
4897     {"mbargs", 0, POPT_ARG_STRING, &newMBKernelArgs, 0,
4898     _("default arguments for the new multiboot kernel or "
4899       "new arguments for multiboot kernel being updated"), NULL},
4900     {"bad-image-okay", 0, 0, &badImageOkay, 0,
4901     _
4902     ("don't sanity check images in boot entries (for testing only)"),
4903     NULL},
4904     {"boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4905     _
4906     ("filesystem which contains /boot directory (for testing only)"),
4907     _("bootfs")},
4908    #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4909     {"bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
4910     _("check which bootloader is installed on boot sector")},
4911    #endif
4912     {"config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
4913     _("path to grub config file to update (\"-\" for stdin)"),
4914     _("path")},
4915     {"copy-default", 0, 0, &copyDefault, 0,
4916     _("use the default boot entry as a template for the new entry "
4917       "being added; if the default is not a linux image, or if "
4918       "the kernel referenced by the default image does not exist, "
4919       "the first linux entry whose kernel does exist is used as the "
4920       "template"), NULL},
4921     {"debug", 0, 0, &debug, 0,
4922     _("print debugging information for failures")},
4923     {"default-kernel", 0, 0, &displayDefault, 0,
4924     _("display the path of the default kernel")},
4925     {"default-index", 0, 0, &displayDefaultIndex, 0,
4926     _("display the index of the default kernel")},
4927     {"default-title", 0, 0, &displayDefaultTitle, 0,
4928     _("display the title of the default kernel")},
4929     {"devtree", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4930     _("device tree file for new stanza"), _("dtb-path")},
4931     {"devtreedir", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4932     _("device tree directory for new stanza"), _("dtb-path")},
4933     {"elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4934     _("configure elilo bootloader")},
4935     {"efi", 0, POPT_ARG_NONE, &isEfi, 0,
4936     _("force grub2 stanzas to use efi")},
4937     {"env", 0, POPT_ARG_STRING, &envPath, 0,
4938     _("path for environment data"),
4939     _("path")},
4940     {"extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4941     _("configure extlinux bootloader (from syslinux)")},
4942     {"grub", 0, POPT_ARG_NONE, &configureGrub, 0,
4943     _("configure grub bootloader")},
4944     {"grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,
4945     _("configure grub2 bootloader")},
4946     {"info", 0, POPT_ARG_STRING, &kernelInfo, 0,
4947     _("display boot information for specified kernel"),
4948     _("kernel-path")},
4949     {"initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4950     _("initrd image for the new kernel"), _("initrd-path")},
4951     {"extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4952     _
4953     ("auxiliary initrd image for things other than the new kernel"),
4954     _("initrd-path")},
4955     {"lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4956     _("configure lilo bootloader")},
4957     {"make-default", 0, 0, &makeDefault, 0,
4958     _("make the newly added entry the default boot entry"), NULL},
4959     {"output-file", 'o', POPT_ARG_STRING, &outputFile, 0,
4960     _("path to output updated config file (\"-\" for stdout)"),
4961     _("path")},
4962     {"remove-args", 0, POPT_ARG_STRING, &removeArgs, 0,
4963     _("remove kernel arguments"), NULL},
4964     {"remove-mbargs", 0, POPT_ARG_STRING, &removeMBKernelArgs, 0,
4965     _("remove multiboot kernel arguments"), NULL},
4966     {"remove-kernel", 0, POPT_ARG_STRING, &removeKernelPath, 0,
4967     _("remove all entries for the specified kernel"),
4968     _("kernel-path")},
4969     {"remove-multiboot", 0, POPT_ARG_STRING, &removeMBKernel, 0,
4970     _("remove all entries for the specified multiboot kernel"),
4971     NULL},
4972     {"set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
4973     _("make the first entry referencing the specified kernel "
4974       "the default"), _("kernel-path")},
4975     {"set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4976     _("make the given entry index the default entry"),
4977     _("entry-index")},
4978     {"set-index", 0, POPT_ARG_INT, &newIndex, 0,
4979     _("use the given index when creating a new entry"),
4980     _("entry-index")},
4981     {"silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4982     _("configure silo bootloader")},
4983     {"title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
4984     _("title to use for the new kernel entry"), _("entry-title")},
4985     {"update-kernel", 0, POPT_ARG_STRING, &updateKernelPath, 0,
4986     _("updated information for the specified kernel"),
4987     _("kernel-path")},
4988     {"version", 'v', 0, NULL, 'v',
4989     _("print the version of this program and exit"), NULL},
4990     {"yaboot", 0, POPT_ARG_NONE, &configureYaboot, 0,
4991     _("configure yaboot bootloader")},
4992     {"zipl", 0, POPT_ARG_NONE, &configureZipl, 0,
4993     _("configure zipl bootloader")},
4994     POPT_AUTOHELP {0, 0, 0, 0, 0}
4995     };
4996    
4997     useextlinuxmenu = 0;
4998    
4999     int i = 0;
5000     for (int j = 1; j < argc; j++)
5001     i += strlen(argv[j]) + 1;
5002     saved_command_line = malloc(i);
5003     if (!saved_command_line) {
5004     fprintf(stderr, "grubby: %m\n");
5005     exit(1);
5006     }
5007     saved_command_line[0] = '\0';
5008     for (int j = 1; j < argc; j++) {
5009     strcat(saved_command_line, argv[j]);
5010     strncat(saved_command_line, j == argc - 1 ? "" : " ", 1);
5011     }
5012    
5013     optCon = poptGetContext("grubby", argc, argv, options, 0);
5014     poptReadDefaultConfig(optCon, 1);
5015    
5016     while ((arg = poptGetNextOpt(optCon)) >= 0) {
5017     switch (arg) {
5018     case 'v':
5019     printf("grubby version %s\n", VERSION);
5020     exit(0);
5021     break;
5022     case 'i':
5023     if (extraInitrdCount < MAX_EXTRA_INITRDS) {
5024     extraInitrds[extraInitrdCount++] =
5025        strdup(poptGetOptArg(optCon));
5026     } else {
5027     fprintf(stderr,
5028     _
5029     ("grubby: extra initrd maximum is %d\n"),
5030     extraInitrdCount);
5031     return 1;
5032     }
5033     break;
5034     }
5035     }
5036    
5037     if (arg < -1) {
5038     fprintf(stderr, _("grubby: bad argument %s: %s\n"),
5039     poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
5040     poptStrerror(arg));
5041     return 1;
5042     }
5043    
5044     if ((chptr = poptGetArg(optCon))) {
5045     fprintf(stderr, _("grubby: unexpected argument %s\n"), chptr);
5046     return 1;
5047     }
5048    
5049     if ((configureLilo + configureGrub2 + configureGrub + configureELilo +
5050         configureYaboot + configureSilo + configureZipl +
5051         configureExtLinux) > 1) {
5052     fprintf(stderr,
5053     _("grubby: cannot specify multiple bootloaders\n"));
5054     return 1;
5055     } else if (bootloaderProbe && grubConfig) {
5056     fprintf(stderr,
5057     _
5058     ("grubby: cannot specify config file with --bootloader-probe\n"));
5059     return 1;
5060     } else if (configureGrub2) {
5061     cfi = &grub2ConfigType;
5062     if (envPath)
5063     cfi->envFile = envPath;
5064     } else if (configureLilo) {
5065     cfi = &liloConfigType;
5066     } else if (configureGrub) {
5067     cfi = &grubConfigType;
5068     } else if (configureELilo) {
5069     cfi = &eliloConfigType;
5070     } else if (configureYaboot) {
5071     cfi = &yabootConfigType;
5072     } else if (configureSilo) {
5073     cfi = &siloConfigType;
5074     } else if (configureZipl) {
5075     cfi = &ziplConfigType;
5076     } else if (configureExtLinux) {
5077     cfi = &extlinuxConfigType;
5078     useextlinuxmenu = 1;
5079     }
5080    
5081     if (!cfi) {
5082     if (grub2FindConfig(&grub2ConfigType)) {
5083     cfi = &grub2ConfigType;
5084     if (envPath)
5085     cfi->envFile = envPath;
5086     } else
5087    #ifdef __ia64__
5088     cfi = &eliloConfigType;
5089    #elif __powerpc__
5090     cfi = &yabootConfigType;
5091    #elif __sparc__
5092     cfi = &siloConfigType;
5093    #elif __s390__
5094     cfi = &ziplConfigType;
5095    #elif __s390x__
5096     cfi = &ziplConfigtype;
5097    #else
5098     cfi = &grubConfigType;
5099    #endif
5100     }
5101    
5102     if (!grubConfig) {
5103     if (cfi->findConfig)
5104     grubConfig = cfi->findConfig(cfi);
5105     if (!grubConfig)
5106     grubConfig = cfi->defaultConfig;
5107     }
5108    
5109     if (bootloaderProbe && (displayDefault || kernelInfo ||
5110     newKernelPath || removeKernelPath || makeDefault
5111     || defaultKernel || displayDefaultIndex
5112     || displayDefaultTitle
5113     || (defaultIndex >= 0))) {
5114     fprintf(stderr,
5115     _("grubby: --bootloader-probe may not be used with "
5116      "specified option"));
5117     return 1;
5118     }
5119    
5120     if ((displayDefault || kernelInfo) && (newKernelPath ||
5121           removeKernelPath)) {
5122     fprintf(stderr, _("grubby: --default-kernel and --info may not "
5123      "be used when adding or removing kernels\n"));
5124     return 1;
5125     }
5126    
5127     if (newKernelPath && !newKernelTitle) {
5128     fprintf(stderr, _("grubby: kernel title must be specified\n"));
5129     return 1;
5130     } else if (!newKernelPath && (copyDefault ||
5131          (newKernelInitrd && !updateKernelPath) ||
5132          makeDefault || extraInitrdCount > 0)) {
5133     fprintf(stderr, _("grubby: kernel path expected\n"));
5134     return 1;
5135     }
5136    
5137     if (newKernelPath && updateKernelPath) {
5138     fprintf(stderr, _("grubby: --add-kernel and --update-kernel may"
5139      "not be used together"));
5140     return 1;
5141     }
5142    
5143     if (makeDefault && defaultKernel) {
5144     fprintf(stderr, _("grubby: --make-default and --default-kernel "
5145      "may not be used together\n"));
5146     return 1;
5147     } else if (defaultKernel && removeKernelPath &&
5148       !strcmp(defaultKernel, removeKernelPath)) {
5149     fprintf(stderr,
5150     _("grubby: cannot make removed kernel the default\n"));
5151     return 1;
5152     } else if (defaultKernel && newKernelPath &&
5153       !strcmp(defaultKernel, newKernelPath)) {
5154     makeDefault = 1;
5155     defaultKernel = NULL;
5156     } else if (defaultKernel && (defaultIndex >= 0)) {
5157     fprintf(stderr,
5158     _("grubby: --set-default and --set-default-index "
5159      "may not be used together\n"));
5160     return 1;
5161     }
5162    
5163     if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
5164     fprintf(stderr,
5165     _("grubby: output file must be specified if stdin "
5166      "is used\n"));
5167     return 1;
5168     }
5169    
5170     if (!removeKernelPath && !newKernelPath && !displayDefault
5171        && !defaultKernel && !kernelInfo && !bootloaderProbe
5172        && !updateKernelPath && !removeMBKernel && !displayDefaultIndex
5173        && !displayDefaultTitle && (defaultIndex == -1)) {
5174     fprintf(stderr, _("grubby: no action specified\n"));
5175     return 1;
5176     }
5177    
5178     flags |= badImageOkay ? GRUBBY_BADIMAGE_OKAY : 0;
5179    
5180     if (cfi->needsBootPrefix) {
5181     if (!bootPrefix) {
5182     bootPrefix = findBootPrefix();
5183     if (!bootPrefix)
5184     return 1;
5185     } else {
5186     /* this shouldn't end with a / */
5187     if (bootPrefix[strlen(bootPrefix) - 1] == '/')
5188     bootPrefix[strlen(bootPrefix) - 1] = '\0';
5189     }
5190     } else {
5191     bootPrefix = "";
5192     }
5193    
5194     if (!cfi->mbAllowExtraInitRds && extraInitrdCount > 0) {
5195     fprintf(stderr,
5196     _("grubby: %s doesn't allow multiple initrds\n"),
5197     cfi->defaultConfig);
5198     return 1;
5199     }
5200    
5201     if (bootloaderProbe) {
5202     int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
5203     struct grubConfig *lconfig, *gconfig, *yconfig, *econfig;
5204    
5205     const char *grub2config = grub2FindConfig(&grub2ConfigType);
5206     if (grub2config) {
5207     gconfig = readConfig(grub2config, &grub2ConfigType);
5208     if (!gconfig)
5209     gr2c = 1;
5210     else
5211     gr2c = checkForGrub2(gconfig);
5212     }
5213    
5214     const char *grubconfig = grubFindConfig(&grubConfigType);
5215     if (!access(grubconfig, F_OK)) {
5216     gconfig = readConfig(grubconfig, &grubConfigType);
5217     if (!gconfig)
5218     grc = 1;
5219     else
5220     grc = checkForGrub(gconfig);
5221     }
5222    
5223     if (!access(liloConfigType.defaultConfig, F_OK)) {
5224     lconfig =
5225        readConfig(liloConfigType.defaultConfig,
5226           &liloConfigType);
5227     if (!lconfig)
5228     lrc = 1;
5229     else
5230     lrc = checkForLilo(lconfig);
5231     }
5232    
5233     if (!access(eliloConfigType.defaultConfig, F_OK)) {
5234     econfig = readConfig(eliloConfigType.defaultConfig,
5235         &eliloConfigType);
5236     if (!econfig)
5237     erc = 1;
5238     else
5239     erc = checkForElilo(econfig);
5240     }
5241    
5242     if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
5243     lconfig =
5244        readConfig(extlinuxConfigType.defaultConfig,
5245           &extlinuxConfigType);
5246     if (!lconfig)
5247     extrc = 1;
5248     else
5249     extrc = checkForExtLinux(lconfig);
5250     }
5251    
5252     if (!access(yabootConfigType.defaultConfig, F_OK)) {
5253     yconfig = readConfig(yabootConfigType.defaultConfig,
5254         &yabootConfigType);
5255     if (!yconfig)
5256     yrc = 1;
5257     else
5258     yrc = checkForYaboot(yconfig);
5259     }
5260    
5261     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1
5262        || erc == 1)
5263     return 1;
5264    
5265     if (lrc == 2)
5266     printf("lilo\n");
5267     if (gr2c == 2)
5268     printf("grub2\n");
5269     if (grc == 2)
5270     printf("grub\n");
5271     if (extrc == 2)
5272     printf("extlinux\n");
5273     if (yrc == 2)
5274     printf("yaboot\n");
5275     if (erc == 2)
5276     printf("elilo\n");
5277    
5278     return 0;
5279     }
5280    
5281     if (grubConfig == NULL) {
5282     printf("Could not find bootloader configuration file.\n");
5283     exit(1);
5284     }
5285    
5286     config = readConfig(grubConfig, cfi);
5287     if (!config)
5288     return 1;
5289    
5290     if (displayDefault) {
5291     struct singleLine *line;
5292     struct singleEntry *entry;
5293     char *rootspec;
5294    
5295     if (config->defaultImage == NO_DEFAULT_ENTRY)
5296     return 0;
5297     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
5298        cfi->defaultIsSaved)
5299     config->defaultImage = FIRST_ENTRY_INDEX;
5300     entry = findEntryByIndex(config, config->defaultImage);
5301     if (!entry)
5302     return 0;
5303     if (!suitableImage(entry, bootPrefix, 0, flags))
5304     return 0;
5305    
5306     line =
5307        getLineByType(LT_KERNEL | LT_HYPER | LT_KERNEL_EFI |
5308      LT_KERNEL_16, entry->lines);
5309     if (!line)
5310     return 0;
5311    
5312     rootspec = getRootSpecifier(line->elements[1].item);
5313     printf("%s%s\n", bootPrefix, line->elements[1].item +
5314           ((rootspec != NULL) ? strlen(rootspec) : 0));
5315    
5316     return 0;
5317    
5318     } else if (displayDefaultTitle) {
5319     struct singleLine *line;
5320     struct singleEntry *entry;
5321    
5322     if (config->defaultImage == NO_DEFAULT_ENTRY)
5323     return 0;
5324     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
5325        cfi->defaultIsSaved)
5326     config->defaultImage = FIRST_ENTRY_INDEX;
5327     entry = findEntryByIndex(config, config->defaultImage);
5328     if (!entry)
5329     return 0;
5330    
5331     if (!configureGrub2) {
5332     char *title;
5333     line = getLineByType(LT_TITLE, entry->lines);
5334     if (!line)
5335     return 0;
5336     title = extractTitle(config, line);
5337     if (!title)
5338     return 0;
5339     printf("%s\n", title);
5340     free(title);
5341     } else {
5342     char *title;
5343    
5344     dbgPrintf
5345        ("This is GRUB2, default title is embeded in menuentry\n");
5346     line = getLineByType(LT_MENUENTRY, entry->lines);
5347     if (!line)
5348     return 0;
5349     title = grub2ExtractTitle(line);
5350     if (title)
5351     printf("%s\n", title);
5352     }
5353     return 0;
5354    
5355     } else if (displayDefaultIndex) {
5356     if (config->defaultImage == NO_DEFAULT_ENTRY)
5357     return 0;
5358     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
5359        cfi->defaultIsSaved)
5360     config->defaultImage = FIRST_ENTRY_INDEX;
5361     printf("%i\n", config->defaultImage);
5362     return 0;
5363    
5364     } else if (kernelInfo)
5365     return displayInfo(config, kernelInfo, bootPrefix);
5366    
5367     if (copyDefault) {
5368     template = findTemplate(config, bootPrefix, NULL, 0, flags);
5369     if (!template)
5370     return 1;
5371     }
5372    
5373     markRemovedImage(config, removeKernelPath, bootPrefix);
5374     markRemovedImage(config, removeMBKernel, bootPrefix);
5375     setDefaultImage(config, newKernelPath != NULL, defaultKernel,
5376     makeDefault, bootPrefix, flags, defaultIndex,
5377     newIndex);
5378     setFallbackImage(config, newKernelPath != NULL);
5379     if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
5380     removeArgs, newMBKernelArgs, removeMBKernelArgs))
5381     return 1;
5382     if (updateKernelPath && newKernelInitrd) {
5383     if (newMBKernel) {
5384     if (addMBInitrd(config, newMBKernel, updateKernelPath,
5385     bootPrefix, newKernelInitrd,
5386     newKernelTitle))
5387     return 1;
5388     } else {
5389     if (updateInitrd(config, updateKernelPath, bootPrefix,
5390     newKernelInitrd, newKernelTitle))
5391     return 1;
5392     }
5393     }
5394     if (addNewKernel(config, template, bootPrefix, newKernelPath,
5395     newKernelTitle, newKernelArgs, newKernelInitrd,
5396     (const char **)extraInitrds, extraInitrdCount,
5397     newMBKernel, newMBKernelArgs, newDevTreePath,
5398     newIndex))
5399     return 1;
5400    
5401     if (numEntries(config) == 0) {
5402     fprintf(stderr,
5403     _("grubby: doing this would leave no kernel entries. "
5404      "Not writing out new config.\n"));
5405     return 1;
5406     }
5407    
5408      if (!outputFile)   if (!outputFile)
5409   outputFile = (char *)grubConfig;   outputFile = (char *)grubConfig;
5410    
5411      return writeConfig(config, outputFile, bootPrefix);   return writeConfig(config, outputFile, bootPrefix);
5412  }  }

Legend:
Removed from v.1722  
changed lines
  Added in v.3029