Magellan Linux

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

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

trunk/mkinitrd-magellan/grubby/grubby.c revision 926 by niro, Wed Oct 28 13:29:38 2009 UTC trunk/grubby/grubby.c revision 1868 by niro, Mon Jul 2 13:22:30 2012 UTC
# Line 36  Line 36 
36  #include <signal.h>  #include <signal.h>
37  #include <blkid/blkid.h>  #include <blkid/blkid.h>
38    
39    #ifndef DEBUG
40  #define DEBUG 0  #define DEBUG 0
41    #endif
42    
43  #if DEBUG  #if DEBUG
44  #define dbgPrintf(format, args...) fprintf(stderr, format , ## args)  #define dbgPrintf(format, args...) fprintf(stderr, format , ## args)
# Line 44  Line 46 
46  #define dbgPrintf(format, args...)  #define dbgPrintf(format, args...)
47  #endif  #endif
48    
49    int debug = 0; /* Currently just for template debugging */
50    
51  #define _(A) (A)  #define _(A) (A)
52    
53  #define MAX_EXTRA_INITRDS  16 /* code segment checked by --bootloader-probe */  #define MAX_EXTRA_INITRDS  16 /* code segment checked by --bootloader-probe */
54  #define CODE_SEG_SIZE  128 /* code segment checked by --bootloader-probe */  #define CODE_SEG_SIZE  128 /* code segment checked by --bootloader-probe */
55    
56    #define NOOP_OPCODE 0x90
57    #define JMP_SHORT_OPCODE 0xeb
58    
59  /* comments get lumped in with indention */  /* comments get lumped in with indention */
60  struct lineElement {  struct lineElement {
61      char * item;      char * item;
# Line 56  struct lineElement { Line 63  struct lineElement {
63  };  };
64    
65  enum lineType_e {  enum lineType_e {
66      LT_WHITESPACE = 1 << 0,      LT_WHITESPACE   = 1 << 0,
67      LT_TITLE      = 1 << 1,      LT_TITLE        = 1 << 1,
68      LT_KERNEL     = 1 << 2,      LT_KERNEL       = 1 << 2,
69      LT_INITRD     = 1 << 3,      LT_INITRD       = 1 << 3,
70      LT_HYPER      = 1 << 4,      LT_HYPER        = 1 << 4,
71      LT_DEFAULT    = 1 << 5,      LT_DEFAULT      = 1 << 5,
72      LT_MBMODULE   = 1 << 6,      LT_MBMODULE     = 1 << 6,
73      LT_ROOT       = 1 << 7,      LT_ROOT         = 1 << 7,
74      LT_FALLBACK   = 1 << 8,      LT_FALLBACK     = 1 << 8,
75      LT_KERNELARGS = 1 << 9,      LT_KERNELARGS   = 1 << 9,
76      LT_BOOT       = 1 << 10,      LT_BOOT         = 1 << 10,
77      LT_BOOTROOT   = 1 << 11,      LT_BOOTROOT     = 1 << 11,
78      LT_LBA        = 1 << 12,      LT_LBA          = 1 << 12,
79      LT_OTHER      = 1 << 13,      LT_OTHER        = 1 << 13,
80      LT_GENERIC    = 1 << 14,      LT_GENERIC      = 1 << 14,
81      LT_UNKNOWN    = 1 << 15,      LT_ECHO    = 1 << 16,
82        LT_MENUENTRY    = 1 << 17,
83        LT_ENTRY_END    = 1 << 18,
84        LT_SET_VARIABLE = 1 << 19,
85        LT_UNKNOWN      = 1 << 20,
86  };  };
87    
88  struct singleLine {  struct singleLine {
# Line 99  struct singleEntry { Line 110  struct singleEntry {
110  #define NEED_TITLE   (1 << 2)  #define NEED_TITLE   (1 << 2)
111  #define NEED_ARGS    (1 << 3)  #define NEED_ARGS    (1 << 3)
112  #define NEED_MB      (1 << 4)  #define NEED_MB      (1 << 4)
113    #define NEED_END     (1 << 5)
114    
115  #define MAIN_DEFAULT    (1 << 0)  #define MAIN_DEFAULT    (1 << 0)
116  #define DEFAULT_SAVED       -2  #define DEFAULT_SAVED       -2
117    #define DEFAULT_SAVED_GRUB2 -3
118    
119  struct keywordTypes {  struct keywordTypes {
120      char * key;      char * key;
# Line 110  struct keywordTypes { Line 123  struct keywordTypes {
123      char separatorChar;      char separatorChar;
124  };  };
125    
126    struct configFileInfo;
127    
128    typedef const char *(*findConfigFunc)(struct configFileInfo *);
129    typedef const int (*writeLineFunc)(struct configFileInfo *,
130     struct singleLine *line);
131    
132  struct configFileInfo {  struct configFileInfo {
133      char * defaultConfig;      char * defaultConfig;
134        findConfigFunc findConfig;
135        writeLineFunc writeLine;
136      struct keywordTypes * keywords;      struct keywordTypes * keywords;
137      int defaultIsIndex;      int defaultIsIndex;
138        int defaultIsVariable;
139      int defaultSupportSaved;      int defaultSupportSaved;
140      enum lineType_e entrySeparator;      enum lineType_e entryStart;
141        enum lineType_e entryEnd;
142      int needsBootPrefix;      int needsBootPrefix;
143      int argsInQuotes;      int argsInQuotes;
144      int maxTitleLength;      int maxTitleLength;
145      int titleBracketed;      int titleBracketed;
146        int titlePosition;
147      int mbHyperFirst;      int mbHyperFirst;
148      int mbInitRdIsModule;      int mbInitRdIsModule;
149      int mbConcatArgs;      int mbConcatArgs;
# Line 138  struct keywordTypes grubKeywords[] = { Line 162  struct keywordTypes grubKeywords[] = {
162      { NULL,    0, 0 },      { NULL,    0, 0 },
163  };  };
164    
165    const char *grubFindConfig(struct configFileInfo *cfi) {
166        static const char *configFiles[] = {
167     "/boot/grub/grub.conf",
168     "/boot/grub/menu.lst",
169     "/etc/grub.conf",
170     NULL
171        };
172        static int i = -1;
173    
174        if (i == -1) {
175     for (i = 0; configFiles[i] != NULL; i++) {
176        dbgPrintf("Checking \"%s\": ", configFiles[i]);
177        if (!access(configFiles[i], R_OK)) {
178     dbgPrintf("found\n");
179     return configFiles[i];
180        }
181        dbgPrintf("not found\n");
182     }
183        }
184        return configFiles[i];
185    }
186    
187  struct configFileInfo grubConfigType = {  struct configFileInfo grubConfigType = {
188      "/boot/grub/grub.conf",    /* defaultConfig */      .findConfig = grubFindConfig,
189      grubKeywords,    /* keywords */      .keywords = grubKeywords,
190      1,    /* defaultIsIndex */      .defaultIsIndex = 1,
191      1,    /* defaultSupportSaved */      .defaultSupportSaved = 1,
192      LT_TITLE,    /* entrySeparator */      .entryStart = LT_TITLE,
193      1,    /* needsBootPrefix */      .needsBootPrefix = 1,
194      0,    /* argsInQuotes */      .mbHyperFirst = 1,
195      0,    /* maxTitleLength */      .mbInitRdIsModule = 1,
196      0,                                      /* titleBracketed */      .mbAllowExtraInitRds = 1,
197      1,                                      /* mbHyperFirst */  };
198      1,                                      /* mbInitRdIsModule */  
199      0,                                      /* mbConcatArgs */  struct keywordTypes grub2Keywords[] = {
200      1,                                      /* mbAllowExtraInitRds */      { "menuentry",  LT_MENUENTRY,   ' ' },
201        { "}",          LT_ENTRY_END,   ' ' },
202        { "echo",       LT_ECHO,        ' ' },
203        { "set",        LT_SET_VARIABLE,' ', '=' },
204        { "root",       LT_BOOTROOT,    ' ' },
205        { "default",    LT_DEFAULT,     ' ' },
206        { "fallback",   LT_FALLBACK,    ' ' },
207        { "linux",      LT_KERNEL,      ' ' },
208        { "initrd",     LT_INITRD,      ' ', ' ' },
209        { "module",     LT_MBMODULE,    ' ' },
210        { "kernel",     LT_HYPER,       ' ' },
211        { NULL, 0, 0 },
212    };
213    
214    const char *grub2FindConfig(struct configFileInfo *cfi) {
215        static const char *configFiles[] = {
216     "/boot/grub/grub-efi.cfg",
217     "/boot/grub/grub.cfg",
218     NULL
219        };
220        static int i = -1;
221        static const char *grub_cfg = "/boot/grub/grub.cfg";
222    
223        if (i == -1) {
224     for (i = 0; configFiles[i] != NULL; i++) {
225        dbgPrintf("Checking \"%s\": ", configFiles[i]);
226        if (!access(configFiles[i], R_OK)) {
227     dbgPrintf("found\n");
228     return configFiles[i];
229        }
230     }
231        }
232    
233        /* Ubuntu renames grub2 to grub, so check for the grub.d directory
234         * that isn't in grub1, and if it exists, return the config file path
235         * that they use. */
236        if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) {
237     dbgPrintf("found\n");
238     return grub_cfg;
239        }
240    
241        dbgPrintf("not found\n");
242        return configFiles[i];
243    }
244    
245    int sizeOfSingleLine(struct singleLine * line) {
246      int count = 0;
247    
248      for (int i = 0; i < line->numElements; i++) {
249        int indentSize = 0;
250    
251        count = count + strlen(line->elements[i].item);
252    
253        indentSize = strlen(line->elements[i].indent);
254        if (indentSize > 0)
255          count = count + indentSize;
256        else
257          /* be extra safe and add room for whitespaces */
258          count = count + 1;
259      }
260    
261      /* room for trailing terminator */
262      count = count + 1;
263    
264      return count;
265    }
266    
267    static int isquote(char q)
268    {
269        if (q == '\'' || q == '\"')
270     return 1;
271        return 0;
272    }
273    
274    char *grub2ExtractTitle(struct singleLine * line) {
275        char * current;
276        char * current_indent;
277        int current_len;
278        int current_indent_len;
279        int i;
280    
281        /* bail out if line does not start with menuentry */
282        if (strcmp(line->elements[0].item, "menuentry"))
283          return NULL;
284    
285        i = 1;
286        current = line->elements[i].item;
287        current_len = strlen(current);
288    
289        /* if second word is quoted, strip the quotes and return single word */
290        if (isquote(*current) && isquote(current[current_len - 1])) {
291     char *tmp;
292    
293     tmp = strdup(current);
294     *(tmp + current_len - 1) = '\0';
295     return ++tmp;
296        }
297    
298        /* if no quotes, return second word verbatim */
299        if (!isquote(*current))
300     return current;
301    
302        /* second element start with a quote, so we have to find the element
303         * whose last character is also quote (assuming it's the closing one) */
304        int resultMaxSize;
305        char * result;
306        
307        resultMaxSize = sizeOfSingleLine(line);
308        result = malloc(resultMaxSize);
309        snprintf(result, resultMaxSize, "%s", ++current);
310        
311        i++;
312        for (; i < line->numElements; ++i) {
313     current = line->elements[i].item;
314     current_len = strlen(current);
315     current_indent = line->elements[i].indent;
316     current_indent_len = strlen(current_indent);
317    
318     strncat(result, current_indent, current_indent_len);
319     if (!isquote(current[current_len-1])) {
320        strncat(result, current, current_len);
321     } else {
322        strncat(result, current, current_len - 1);
323        break;
324     }
325        }
326        return result;
327    }
328    
329    struct configFileInfo grub2ConfigType = {
330        .findConfig = grub2FindConfig,
331        .keywords = grub2Keywords,
332        .defaultIsIndex = 1,
333        .defaultSupportSaved = 1,
334        .defaultIsVariable = 1,
335        .entryStart = LT_MENUENTRY,
336        .entryEnd = LT_ENTRY_END,
337        .titlePosition = 1,
338        .needsBootPrefix = 1,
339        .mbHyperFirst = 1,
340        .mbInitRdIsModule = 1,
341        .mbAllowExtraInitRds = 1,
342  };  };
343    
344  struct keywordTypes yabootKeywords[] = {  struct keywordTypes yabootKeywords[] = {
# Line 249  struct keywordTypes extlinuxKeywords[] = Line 436  struct keywordTypes extlinuxKeywords[] =
436  };  };
437  int useextlinuxmenu;  int useextlinuxmenu;
438  struct configFileInfo eliloConfigType = {  struct configFileInfo eliloConfigType = {
439      "/boot/efi/EFI/redhat/elilo.conf",    /* defaultConfig */      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",
440      eliloKeywords,    /* keywords */      .keywords = eliloKeywords,
441      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
442      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
443      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
444      1,                    /* needsBootPrefix */      .mbConcatArgs = 1,
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     1,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
445  };  };
446    
447  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
448      "/etc/lilo.conf",    /* defaultConfig */      .defaultConfig = "/etc/lilo.conf",
449      liloKeywords,    /* keywords */      .keywords = liloKeywords,
450      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
451      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
452      LT_KERNEL,    /* entrySeparator */      .maxTitleLength = 15,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
453  };  };
454    
455  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
456      "/etc/yaboot.conf",    /* defaultConfig */      .defaultConfig = "/etc/yaboot.conf",
457      yabootKeywords,    /* keywords */      .keywords = yabootKeywords,
458      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
459      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
460      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
461      1,    /* needsBootPrefix */      .maxTitleLength = 15,
462      1,    /* argsInQuotes */      .mbAllowExtraInitRds = 1,
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
463  };  };
464    
465  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
466      "/etc/silo.conf",    /* defaultConfig */      .defaultConfig = "/etc/silo.conf",
467      siloKeywords,    /* keywords */      .keywords = siloKeywords,
468      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
469      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
470      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
471      1,    /* needsBootPrefix */      .maxTitleLength = 15,
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
472  };  };
473    
474  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
475      "/etc/zipl.conf",    /* defaultConfig */      .defaultConfig = "/etc/zipl.conf",
476      ziplKeywords,    /* keywords */      .keywords = ziplKeywords,
477      0,    /* defaultIsIndex */      .entryStart = LT_TITLE,
478      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
479      LT_TITLE,    /* entrySeparator */      .titleBracketed = 1,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     1,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
480  };  };
481    
482  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
483      "/boot/extlinux/extlinux.conf",         /* defaultConfig */      .defaultConfig = "/boot/extlinux/extlinux.conf",
484      extlinuxKeywords,                       /* keywords */      .keywords = extlinuxKeywords,
485      0,                                      /* defaultIsIndex */      .entryStart = LT_TITLE,
486      0,                                      /* defaultSupportSaved */      .needsBootPrefix = 1,
487      LT_TITLE,                               /* entrySeparator */      .maxTitleLength = 255,
488      1,                                      /* needsBootPrefix */      .mbAllowExtraInitRds = 1,
     0,                                      /* argsInQuotes */  
     255,                                    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
489  };  };
490    
491  struct grubConfig {  struct grubConfig {
# Line 356  struct grubConfig { Line 500  struct grubConfig {
500      struct configFileInfo * cfi;      struct configFileInfo * cfi;
501  };  };
502    
503    blkid_cache blkid;
504    
505  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);
506  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
507       const char * path, const char * prefix,       const char * path, const char * prefix,
# Line 369  static int lineWrite(FILE * out, struct Line 515  static int lineWrite(FILE * out, struct
515  static int getNextLine(char ** bufPtr, struct singleLine * line,  static int getNextLine(char ** bufPtr, struct singleLine * line,
516         struct configFileInfo * cfi);         struct configFileInfo * cfi);
517  static char * getRootSpecifier(char * str);  static char * getRootSpecifier(char * str);
518    static void requote(struct singleLine *line, struct configFileInfo * cfi);
519  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
520    const char * item, int insertHere,    const char * item, int insertHere,
521    struct configFileInfo * cfi);    struct configFileInfo * cfi);
# Line 425  static char * sdupprintf(const char *for Line 572  static char * sdupprintf(const char *for
572    
573  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
574        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
575      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
     for (kw = cfi->keywords; kw->key; kw++) {  
576   if (kw->type == type)   if (kw->type == type)
577      return kw;      return kw;
578      }      }
579      return NULL;      return NULL;
580  }  }
581    
582  static char * getpathbyspec(char *device) {  static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) {
583      static blkid_cache blkid;      struct keywordTypes *kt = getKeywordByType(type, cfi);
584        if (kt)
585     return kt->key;
586        return "unknown";
587    }
588    
589    static char * getpathbyspec(char *device) {
590      if (!blkid)      if (!blkid)
591          blkid_get_cache(&blkid, NULL);          blkid_get_cache(&blkid, NULL);
592    
593      return blkid_get_devname(blkid, device, NULL);      return blkid_get_devname(blkid, device, NULL);
594  }  }
595    
596    static char * getuuidbydev(char *device) {
597        if (!blkid)
598     blkid_get_cache(&blkid, NULL);
599    
600        return blkid_get_tag_value(blkid, "UUID", device);
601    }
602    
603  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
604   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
605      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
     for (kw = cfi->keywords; kw->key; kw++) {  
606   if (!strcmp(keyword, kw->key))   if (!strcmp(keyword, kw->key))
607      return kw->type;      return kw->type;
608      }      }
# Line 477  static int isBracketedTitle(struct singl Line 634  static int isBracketedTitle(struct singl
634      return 0;      return 0;
635  }  }
636    
637  static int isEntrySeparator(struct singleLine * line,  static int isEntryStart(struct singleLine * line,
638                              struct configFileInfo * cfi) {                              struct configFileInfo * cfi) {
639      return line->type == cfi->entrySeparator || line->type == LT_OTHER ||      return line->type == cfi->entryStart || line->type == LT_OTHER ||
640   (cfi->titleBracketed && isBracketedTitle(line));   (cfi->titleBracketed && isBracketedTitle(line));
641  }  }
642    
# Line 532  static void lineInit(struct singleLine * Line 689  static void lineInit(struct singleLine *
689  }  }
690    
691  struct singleLine * lineDup(struct singleLine * line) {  struct singleLine * lineDup(struct singleLine * line) {
     int i;  
692      struct singleLine * newLine = malloc(sizeof(*newLine));      struct singleLine * newLine = malloc(sizeof(*newLine));
693    
694      newLine->indent = strdup(line->indent);      newLine->indent = strdup(line->indent);
# Line 542  struct singleLine * lineDup(struct singl Line 698  struct singleLine * lineDup(struct singl
698      newLine->elements = malloc(sizeof(*newLine->elements) *      newLine->elements = malloc(sizeof(*newLine->elements) *
699         newLine->numElements);         newLine->numElements);
700    
701      for (i = 0; i < newLine->numElements; i++) {      for (int i = 0; i < newLine->numElements; i++) {
702   newLine->elements[i].indent = strdup(line->elements[i].indent);   newLine->elements[i].indent = strdup(line->elements[i].indent);
703   newLine->elements[i].item = strdup(line->elements[i].item);   newLine->elements[i].item = strdup(line->elements[i].item);
704      }      }
# Line 551  struct singleLine * lineDup(struct singl Line 707  struct singleLine * lineDup(struct singl
707  }  }
708    
709  static void lineFree(struct singleLine * line) {  static void lineFree(struct singleLine * line) {
     int i;  
   
710      if (line->indent) free(line->indent);      if (line->indent) free(line->indent);
711    
712      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
713   free(line->elements[i].item);   free(line->elements[i].item);
714   free(line->elements[i].indent);   free(line->elements[i].indent);
715      }      }
# Line 566  static void lineFree(struct singleLine * Line 720  static void lineFree(struct singleLine *
720    
721  static int lineWrite(FILE * out, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine * line,
722       struct configFileInfo * cfi) {       struct configFileInfo * cfi) {
     int i;  
   
723      if (fprintf(out, "%s", line->indent) == -1) return -1;      if (fprintf(out, "%s", line->indent) == -1) return -1;
724    
725      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
726     /* Need to handle this, because we strip the quotes from
727     * menuentry when read it. */
728     if (line->type == LT_MENUENTRY && i == 1) {
729        if(!isquote(*line->elements[i].item))
730     fprintf(out, "\'%s\'", line->elements[i].item);
731        else
732     fprintf(out, "%s", line->elements[i].item);
733        fprintf(out, "%s", line->elements[i].indent);
734    
735        continue;
736     }
737    
738   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)
739      if (fputc('"', out) == EOF) return -1;      if (fputc('"', out) == EOF) return -1;
740    
# Line 664  static int getNextLine(char ** bufPtr, s Line 828  static int getNextLine(char ** bufPtr, s
828      if (*line->elements[0].item == '#') {      if (*line->elements[0].item == '#') {
829   char * fullLine;   char * fullLine;
830   int len;   int len;
  int i;  
831    
832   len = strlen(line->indent);   len = strlen(line->indent);
833   for (i = 0; i < line->numElements; i++)   for (int i = 0; i < line->numElements; i++)
834      len += strlen(line->elements[i].item) +      len += strlen(line->elements[i].item) +
835     strlen(line->elements[i].indent);     strlen(line->elements[i].indent);
836    
# Line 676  static int getNextLine(char ** bufPtr, s Line 839  static int getNextLine(char ** bufPtr, s
839   free(line->indent);   free(line->indent);
840   line->indent = fullLine;   line->indent = fullLine;
841    
842   for (i = 0; i < line->numElements; i++) {   for (int i = 0; i < line->numElements; i++) {
843      strcat(fullLine, line->elements[i].item);      strcat(fullLine, line->elements[i].item);
844      strcat(fullLine, line->elements[i].indent);      strcat(fullLine, line->elements[i].indent);
845      free(line->elements[i].item);      free(line->elements[i].item);
# Line 695  static int getNextLine(char ** bufPtr, s Line 858  static int getNextLine(char ** bufPtr, s
858   * elements up more   * elements up more
859   */   */
860   if (!isspace(kw->separatorChar)) {   if (!isspace(kw->separatorChar)) {
     int i;  
861      char indent[2] = "";      char indent[2] = "";
862      indent[0] = kw->separatorChar;      indent[0] = kw->separatorChar;
863      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
864   char *p;   char *p;
  int j;  
865   int numNewElements;   int numNewElements;
866    
867   numNewElements = 0;   numNewElements = 0;
# Line 716  static int getNextLine(char ** bufPtr, s Line 877  static int getNextLine(char ** bufPtr, s
877      sizeof(*line->elements) * elementsAlloced);      sizeof(*line->elements) * elementsAlloced);
878   }   }
879    
880   for (j = line->numElements; j > i; j--) {   for (int j = line->numElements; j > i; j--) {
881   line->elements[j + numNewElements] = line->elements[j];   line->elements[j + numNewElements] = line->elements[j];
882   }   }
883   line->numElements += numNewElements;   line->numElements += numNewElements;
# Line 729  static int getNextLine(char ** bufPtr, s Line 890  static int getNextLine(char ** bufPtr, s
890   break;   break;
891   }   }
892    
893   free(line->elements[i].indent);   line->elements[i + 1].indent = line->elements[i].indent;
894   line->elements[i].indent = strdup(indent);   line->elements[i].indent = strdup(indent);
895   *p++ = '\0';   *p++ = '\0';
896   i++;   i++;
897   line->elements[i].item = strdup(p);   line->elements[i].item = strdup(p);
  line->elements[i].indent = strdup("");  
  p = line->elements[i].item;  
898   }   }
899      }      }
900   }   }
# Line 756  static struct grubConfig * readConfig(co Line 915  static struct grubConfig * readConfig(co
915      struct singleLine * last = NULL, * line, * defaultLine = NULL;      struct singleLine * last = NULL, * line, * defaultLine = NULL;
916      char * end;      char * end;
917      struct singleEntry * entry = NULL;      struct singleEntry * entry = NULL;
918      int i, len;      int len;
919      char * buf;      char * buf;
920    
921      if (!strcmp(inName, "-")) {      if (!strcmp(inName, "-")) {
# Line 802  static struct grubConfig * readConfig(co Line 961  static struct grubConfig * readConfig(co
961      cfg->secondaryIndent = strdup(line->indent);      cfg->secondaryIndent = strdup(line->indent);
962   }   }
963    
964   if (isEntrySeparator(line, cfi)) {   if (isEntryStart(line, cfi) || (cfg->entries && !sawEntry)) {
965      sawEntry = 1;      sawEntry = 1;
966      if (!entry) {      if (!entry) {
967   cfg->entries = malloc(sizeof(*entry));   cfg->entries = malloc(sizeof(*entry));
# Line 818  static struct grubConfig * readConfig(co Line 977  static struct grubConfig * readConfig(co
977      entry->next = NULL;      entry->next = NULL;
978   }   }
979    
980   if (line->type == LT_DEFAULT && line->numElements == 2) {   if (line->type == LT_SET_VARIABLE) {
981        dbgPrintf("found 'set' command (%d elements): ", line->numElements);
982        dbgPrintf("%s", line->indent);
983        for (int i = 0; i < line->numElements; i++)
984     dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);
985        dbgPrintf("\n");
986        struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
987        if (kwType && line->numElements == 3 &&
988        !strcmp(line->elements[1].item, kwType->key)) {
989     dbgPrintf("Line sets default config\n");
990     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
991     defaultLine = line;
992        }
993     } else if (line->type == LT_DEFAULT && line->numElements == 2) {
994      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
995      defaultLine = line;      defaultLine = line;
996    
# Line 836  static struct grubConfig * readConfig(co Line 1008  static struct grubConfig * readConfig(co
1008       * This only applies to grub, but that's the only place we       * This only applies to grub, but that's the only place we
1009       * should find LT_MBMODULE lines anyway.       * should find LT_MBMODULE lines anyway.
1010       */       */
1011      struct singleLine * l;      for (struct singleLine *l = entry->lines; l; l = l->next) {
     for (l = entry->lines; l; l = l->next) {  
1012   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1013      break;      break;
1014   else if (l->type == LT_KERNEL) {   else if (l->type == LT_KERNEL) {
# Line 857  static struct grubConfig * readConfig(co Line 1028  static struct grubConfig * readConfig(co
1028   } else if (line->type == LT_TITLE && line->numElements > 1) {   } else if (line->type == LT_TITLE && line->numElements > 1) {
1029      /* make the title a single argument (undoing our parsing) */      /* make the title a single argument (undoing our parsing) */
1030      len = 0;      len = 0;
1031      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1032   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
1033   len += strlen(line->elements[i].indent);   len += strlen(line->elements[i].indent);
1034      }      }
1035      buf = malloc(len + 1);      buf = malloc(len + 1);
1036      *buf = '\0';      *buf = '\0';
1037    
1038      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1039   strcat(buf, line->elements[i].item);   strcat(buf, line->elements[i].item);
1040   free(line->elements[i].item);   free(line->elements[i].item);
1041    
# Line 878  static struct grubConfig * readConfig(co Line 1049  static struct grubConfig * readConfig(co
1049      line->elements[line->numElements - 1].indent;      line->elements[line->numElements - 1].indent;
1050      line->elements[1].item = buf;      line->elements[1].item = buf;
1051      line->numElements = 2;      line->numElements = 2;
1052     } else if (line->type == LT_MENUENTRY && line->numElements > 3) {
1053        /* let --remove-kernel="TITLE=what" work */
1054        len = 0;
1055        char *extras;
1056        char *title;
1057    
1058        for (int i = 1; i < line->numElements; i++) {
1059     len += strlen(line->elements[i].item);
1060     len += strlen(line->elements[i].indent);
1061        }
1062        buf = malloc(len + 1);
1063        *buf = '\0';
1064    
1065        /* allocate mem for extra flags. */
1066        extras = malloc(len + 1);
1067        *extras = '\0';
1068    
1069        /* get title. */
1070        for (int i = 0; i < line->numElements; i++) {
1071     if (!strcmp(line->elements[i].item, "menuentry"))
1072        continue;
1073     if (isquote(*line->elements[i].item))
1074        title = line->elements[i].item + 1;
1075     else
1076        title = line->elements[i].item;
1077    
1078     len = strlen(title);
1079            if (isquote(title[len-1])) {
1080        strncat(buf, title,len-1);
1081        break;
1082     } else {
1083        strcat(buf, title);
1084        strcat(buf, line->elements[i].indent);
1085     }
1086        }
1087    
1088        /* get extras */
1089        int count = 0;
1090        for (int i = 0; i < line->numElements; i++) {
1091     if (count >= 2) {
1092        strcat(extras, line->elements[i].item);
1093        strcat(extras, line->elements[i].indent);
1094     }
1095    
1096     if (!strcmp(line->elements[i].item, "menuentry"))
1097        continue;
1098    
1099     /* count ' or ", there should be two in menuentry line. */
1100     if (isquote(*line->elements[i].item))
1101        count++;
1102    
1103     len = strlen(line->elements[i].item);
1104    
1105     if (isquote(line->elements[i].item[len -1]))
1106        count++;
1107    
1108     /* ok, we get the final ' or ", others are extras. */
1109                }
1110        line->elements[1].indent =
1111     line->elements[line->numElements - 2].indent;
1112        line->elements[1].item = buf;
1113        line->elements[2].indent =
1114     line->elements[line->numElements - 2].indent;
1115        line->elements[2].item = extras;
1116        line->numElements = 3;
1117   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {
1118      /* Strip off any " which may be present; they'll be put back      /* Strip off any " which may be present; they'll be put back
1119         on write. This is one of the few (the only?) places that grubby         on write. This is one of the few (the only?) places that grubby
# Line 887  static struct grubConfig * readConfig(co Line 1122  static struct grubConfig * readConfig(co
1122      if (line->numElements >= 2) {      if (line->numElements >= 2) {
1123   int last, len;   int last, len;
1124    
1125   if (*line->elements[1].item == '"')   if (isquote(*line->elements[1].item))
1126      memmove(line->elements[1].item, line->elements[1].item + 1,      memmove(line->elements[1].item, line->elements[1].item + 1,
1127      strlen(line->elements[1].item + 1) + 1);      strlen(line->elements[1].item + 1) + 1);
1128    
1129   last = line->numElements - 1;   last = line->numElements - 1;
1130   len = strlen(line->elements[last].item) - 1;   len = strlen(line->elements[last].item) - 1;
1131   if (line->elements[last].item[len] == '"')   if (isquote(line->elements[last].item[len]))
1132      line->elements[last].item[len] = '\0';      line->elements[last].item[len] = '\0';
1133      }      }
1134   }   }
# Line 931  static struct grubConfig * readConfig(co Line 1166  static struct grubConfig * readConfig(co
1166   entry->lines = line;   entry->lines = line;
1167      else      else
1168   last->next = line;   last->next = line;
1169      dbgPrintf("readConfig added %d to %p\n", line->type, entry);      dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry);
1170    
1171        /* we could have seen this outside of an entry... if so, we
1172         * ignore it like any other line we don't grok */
1173        if (line->type == LT_ENTRY_END && sawEntry)
1174     sawEntry = 0;
1175   } else {   } else {
1176      if (!cfg->theLines)      if (!cfg->theLines)
1177   cfg->theLines = line;   cfg->theLines = line;
1178      else      else
1179   last->next = line;   last->next = line;
1180      dbgPrintf("readConfig added %d to cfg\n", line->type);      dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi));
1181   }   }
1182    
1183   last = line;   last = line;
# Line 945  static struct grubConfig * readConfig(co Line 1185  static struct grubConfig * readConfig(co
1185    
1186      free(incoming);      free(incoming);
1187    
1188        dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1189      if (defaultLine) {      if (defaultLine) {
1190   if (cfi->defaultSupportSaved &&          if (defaultLine->numElements > 2 &&
1191        cfi->defaultSupportSaved &&
1192        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1193        cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1194     } else if (cfi->defaultIsVariable) {
1195        char *value = defaultLine->elements[2].item;
1196        while (*value && (*value == '"' || *value == '\'' ||
1197        *value == ' ' || *value == '\t'))
1198     value++;
1199        cfg->defaultImage = strtol(value, &end, 10);
1200        while (*end && (*end == '"' || *end == '\'' ||
1201        *end == ' ' || *end == '\t'))
1202     end++;
1203        if (*end) cfg->defaultImage = -1;
1204     } else if (cfi->defaultSupportSaved &&
1205   !strncmp(defaultLine->elements[1].item, "saved", 5)) {   !strncmp(defaultLine->elements[1].item, "saved", 5)) {
1206      cfg->defaultImage = DEFAULT_SAVED;      cfg->defaultImage = DEFAULT_SAVED;
1207   } else if (cfi->defaultIsIndex) {   } else if (cfi->defaultIsIndex) {
1208      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);
1209      if (*end) cfg->defaultImage = -1;      if (*end) cfg->defaultImage = -1;
1210   } else if (defaultLine->numElements >= 2) {   } else if (defaultLine->numElements >= 2) {
1211      i = 0;      int i = 0;
1212      while ((entry = findEntryByIndex(cfg, i))) {      while ((entry = findEntryByIndex(cfg, i))) {
1213   for (line = entry->lines; line; line = line->next)   for (line = entry->lines; line; line = line->next)
1214      if (line->type == LT_TITLE) break;      if (line->type == LT_TITLE) break;
# Line 993  static void writeDefault(FILE * out, cha Line 1248  static void writeDefault(FILE * out, cha
1248    
1249      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1250   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1251        else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)
1252     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1253      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1254   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1255      fprintf(out, "%sdefault%s%d\n", indent, separator,      if (cfg->cfi->defaultIsVariable) {
1256      cfg->defaultImage);          fprintf(out, "%sset default=\"%d\"\n", indent,
1257     cfg->defaultImage);
1258        } else {
1259     fprintf(out, "%sdefault%s%d\n", indent, separator,
1260     cfg->defaultImage);
1261        }
1262   } else {   } else {
1263      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1264    
# Line 1048  static int writeConfig(struct grubConfig Line 1310  static int writeConfig(struct grubConfig
1310    
1311      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1312         directory to the dir of the symlink */         directory to the dir of the symlink */
1313              rc = chdir(dirname(strdupa(outName)));      char *dir = strdupa(outName);
1314                rc = chdir(dirname(dir));
1315        free(dir);
1316      do {      do {
1317   buf = alloca(len + 1);   buf = alloca(len + 1);
1318   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1086  static int writeConfig(struct grubConfig Line 1350  static int writeConfig(struct grubConfig
1350      }      }
1351    
1352      line = cfg->theLines;      line = cfg->theLines;
1353        struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);
1354      while (line) {      while (line) {
1355   if (line->type == LT_DEFAULT) {          if (line->type == LT_SET_VARIABLE && defaultKw &&
1356     line->numElements == 3 &&
1357     !strcmp(line->elements[1].item, defaultKw->key)) {
1358        writeDefault(out, line->indent, line->elements[0].indent, cfg);
1359        needs &= ~MAIN_DEFAULT;
1360     } else if (line->type == LT_DEFAULT) {
1361      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1362      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1363   } else if (line->type == LT_FALLBACK) {   } else if (line->type == LT_FALLBACK) {
# Line 1155  static int numEntries(struct grubConfig Line 1425  static int numEntries(struct grubConfig
1425      return i;      return i;
1426  }  }
1427    
1428    static char *findDiskForRoot()
1429    {
1430        int fd;
1431        char buf[65536];
1432        char *devname;
1433        char *chptr;
1434        int rc;
1435    
1436        if ((fd = open(_PATH_MOUNTED, O_RDONLY)) < 0) {
1437            fprintf(stderr, "grubby: failed to open %s: %s\n",
1438                    _PATH_MOUNTED, strerror(errno));
1439            return NULL;
1440        }
1441    
1442        rc = read(fd, buf, sizeof(buf) - 1);
1443        if (rc <= 0) {
1444            fprintf(stderr, "grubby: failed to read %s: %s\n",
1445                    _PATH_MOUNTED, strerror(errno));
1446            close(fd);
1447            return NULL;
1448        }
1449        close(fd);
1450        buf[rc] = '\0';
1451        chptr = buf;
1452    
1453        char *foundanswer = NULL;
1454    
1455        while (chptr && chptr != buf+rc) {
1456            devname = chptr;
1457    
1458            /*
1459             * The first column of a mtab entry is the device, but if the entry is a
1460             * special device it won't start with /, so move on to the next line.
1461             */
1462            if (*devname != '/') {
1463                chptr = strchr(chptr, '\n');
1464                if (chptr)
1465                    chptr++;
1466                continue;
1467            }
1468    
1469            /* Seek to the next space */
1470            chptr = strchr(chptr, ' ');
1471            if (!chptr) {
1472                fprintf(stderr, "grubby: error parsing %s: %s\n",
1473                        _PATH_MOUNTED, strerror(errno));
1474                return NULL;
1475            }
1476    
1477            /*
1478             * The second column of a mtab entry is the mount point, we are looking
1479             * for '/' obviously.
1480             */
1481            if (*(++chptr) == '/' && *(++chptr) == ' ') {
1482                /* remember the last / entry in mtab */
1483               foundanswer = devname;
1484            }
1485    
1486            /* Next line */
1487            chptr = strchr(chptr, '\n');
1488            if (chptr)
1489                chptr++;
1490        }
1491    
1492        /* Return the last / entry found */
1493        if (foundanswer) {
1494            chptr = strchr(foundanswer, ' ');
1495            *chptr = '\0';
1496            return strdup(foundanswer);
1497        }
1498    
1499        return NULL;
1500    }
1501    
1502    void printEntry(struct singleEntry * entry) {
1503        int i;
1504        struct singleLine * line;
1505    
1506        for (line = entry->lines; line; line = line->next) {
1507     fprintf(stderr, "DBG: %s", line->indent);
1508     for (i = 0; i < line->numElements; i++) {
1509        /* Need to handle this, because we strip the quotes from
1510         * menuentry when read it. */
1511        if (line->type == LT_MENUENTRY && i == 1) {
1512     if(!isquote(*line->elements[i].item))
1513        fprintf(stderr, "\'%s\'", line->elements[i].item);
1514     else
1515        fprintf(stderr, "%s", line->elements[i].item);
1516     fprintf(stderr, "%s", line->elements[i].indent);
1517    
1518     continue;
1519        }
1520        
1521        fprintf(stderr, "%s%s",
1522        line->elements[i].item, line->elements[i].indent);
1523     }
1524     fprintf(stderr, "\n");
1525        }
1526    }
1527    
1528    void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)
1529    {
1530        va_list argp;
1531    
1532        if (!debug)
1533     return;
1534    
1535        va_start(argp, fmt);
1536        fprintf(stderr, "DBG: Image entry failed: ");
1537        vfprintf(stderr, fmt, argp);
1538        printEntry(entry);
1539        va_end(argp);
1540    }
1541    
1542    #define beginswith(s, c) ((s) && (s)[0] == (c))
1543    
1544    static int endswith(const char *s, char c)
1545    {
1546     int slen;
1547    
1548     if (!s || !s[0])
1549     return 0;
1550     slen = strlen(s) - 1;
1551    
1552     return s[slen] == c;
1553    }
1554    
1555  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1556    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1557      struct singleLine * line;      struct singleLine * line;
1558      char * fullName;      char * fullName;
1559      int i;      int i;
     struct stat sb, sb2;  
1560      char * dev;      char * dev;
1561      char * rootspec;      char * rootspec;
1562        char * rootdev;
1563    
1564      if (skipRemoved && entry->skip) return 0;      if (skipRemoved && entry->skip) {
1565     notSuitablePrintf(entry, "marked to skip\n");
1566     return 0;
1567        }
1568    
1569      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);
1570      if (!line || line->numElements < 2) return 0;      if (!line) {
1571     notSuitablePrintf(entry, "no line found\n");
1572     return 0;
1573        }
1574        if (line->numElements < 2) {
1575     notSuitablePrintf(entry, "line has only %d elements\n",
1576        line->numElements);
1577     return 0;
1578        }
1579    
1580      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;
1581    
1582      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1583        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
1584      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1585      sprintf(fullName, "%s%s", bootPrefix,      int rootspec_offset = rootspec ? strlen(rootspec) : 0;
1586              line->elements[1].item + (rootspec ? strlen(rootspec) : 0));      int hasslash = endswith(bootPrefix, '/') ||
1587      if (access(fullName, R_OK)) return 0;       beginswith(line->elements[1].item + rootspec_offset, '/');
1588        sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1589                line->elements[1].item + rootspec_offset);
1590        if (access(fullName, R_OK)) {
1591     notSuitablePrintf(entry, "access to %s failed\n", fullName);
1592     return 0;
1593        }
1594      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
1595   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1596      if (i < line->numElements) {      if (i < line->numElements) {
# Line 1195  int suitableImage(struct singleEntry * e Line 1608  int suitableImage(struct singleEntry * e
1608      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);
1609    
1610              /* failed to find one */              /* failed to find one */
1611              if (!line) return 0;              if (!line) {
1612     notSuitablePrintf(entry, "no line found\n");
1613     return 0;
1614                }
1615    
1616      for (i = 1; i < line->numElements; i++)      for (i = 1; i < line->numElements; i++)
1617          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1618      if (i < line->numElements)      if (i < line->numElements)
1619          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1620      else {      else {
1621     notSuitablePrintf(entry, "no root= entry found\n");
1622   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1623          return 0;          return 0;
1624              }              }
# Line 1209  int suitableImage(struct singleEntry * e Line 1626  int suitableImage(struct singleEntry * e
1626      }      }
1627    
1628      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1629      if (!dev)      if (!getpathbyspec(dev)) {
1630            notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);
1631          return 0;          return 0;
1632        } else
1633     dev = getpathbyspec(dev);
1634    
1635      i = stat(dev, &sb);      rootdev = findDiskForRoot();
1636      if (i)      if (!rootdev) {
1637            notSuitablePrintf(entry, "can't find root device\n");
1638   return 0;   return 0;
1639        }
1640    
1641      stat("/", &sb2);      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1642            notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",
1643     getuuidbydev(rootdev), getuuidbydev(dev));
1644            free(rootdev);
1645            return 0;
1646        }
1647    
1648      if (sb.st_rdev != sb2.st_dev)      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1649            notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",
1650     getuuidbydev(rootdev), getuuidbydev(dev));
1651     free(rootdev);
1652          return 0;          return 0;
1653        }
1654    
1655        free(rootdev);
1656    
1657      return 1;      return 1;
1658  }  }
# Line 1301  struct singleEntry * findEntryByPath(str Line 1734  struct singleEntry * findEntryByPath(str
1734    
1735   if (!strncmp(kernel, "TITLE=", 6)) {   if (!strncmp(kernel, "TITLE=", 6)) {
1736      prefix = "";      prefix = "";
1737      checkType = LT_TITLE;      checkType = LT_TITLE|LT_MENUENTRY;
1738      kernel += 6;      kernel += 6;
1739   }   }
1740    
# Line 1317  struct singleEntry * findEntryByPath(str Line 1750  struct singleEntry * findEntryByPath(str
1750       checkType, line);       checkType, line);
1751   if (!line) break;  /* not found in this entry */   if (!line) break;  /* not found in this entry */
1752    
1753   if (line && line->numElements >= 2) {   if (line && line->type != LT_MENUENTRY &&
1754     line->numElements >= 2) {
1755      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1756      if (!strcmp(line->elements[1].item +      if (!strcmp(line->elements[1].item +
1757   ((rootspec != NULL) ? strlen(rootspec) : 0),   ((rootspec != NULL) ? strlen(rootspec) : 0),
1758   kernel + strlen(prefix)))   kernel + strlen(prefix)))
1759   break;   break;
1760   }   }
1761     if(line->type == LT_MENUENTRY &&
1762     !strcmp(line->elements[1].item, kernel))
1763        break;
1764      }      }
1765    
1766      /* make sure this entry has a kernel identifier; this skips      /* make sure this entry has a kernel identifier; this skips
# Line 1415  void markRemovedImage(struct grubConfig Line 1852  void markRemovedImage(struct grubConfig
1852        const char * prefix) {        const char * prefix) {
1853      struct singleEntry * entry;      struct singleEntry * entry;
1854    
1855      if (!image) return;      if (!image)
1856     return;
1857    
1858        /* check and see if we're removing the default image */
1859        if (isdigit(*image)) {
1860     entry = findEntryByPath(cfg, image, prefix, NULL);
1861     if(entry)
1862        entry->skip = 1;
1863     return;
1864        }
1865    
1866      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
1867   entry->skip = 1;   entry->skip = 1;
# Line 1423  void markRemovedImage(struct grubConfig Line 1869  void markRemovedImage(struct grubConfig
1869    
1870  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
1871       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
1872       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
1873      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
1874      int i, j;      int i, j;
1875    
1876      if (newIsDefault) {      if (newIsDefault) {
1877   config->defaultImage = 0;   config->defaultImage = 0;
1878   return;   return;
1879        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
1880     if (findEntryByIndex(config, index))
1881        config->defaultImage = index;
1882     else
1883        config->defaultImage = -1;
1884     return;
1885      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
1886   i = 0;   i = 0;
1887   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1442  void setDefaultImage(struct grubConfig * Line 1894  void setDefaultImage(struct grubConfig *
1894    
1895      /* defaultImage now points to what we'd like to use, but before any order      /* defaultImage now points to what we'd like to use, but before any order
1896         changes */         changes */
1897      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
1898     (config->defaultImage == DEFAULT_SAVED_GRUB2))
1899        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
1900        return;        return;
1901    
# Line 1509  void displayEntry(struct singleEntry * e Line 1962  void displayEntry(struct singleEntry * e
1962          return;          return;
1963      }      }
1964    
1965      printf("kernel=%s\n", line->elements[1].item);      if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
1966     printf("kernel=%s\n", line->elements[1].item);
1967        else
1968     printf("kernel=%s%s\n", prefix, line->elements[1].item);
1969    
1970      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1971   printf("args=\"");   printf("args=\"");
# Line 1568  void displayEntry(struct singleEntry * e Line 2024  void displayEntry(struct singleEntry * e
2024      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD, entry->lines);
2025    
2026      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2027   printf("initrd=%s", prefix);   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2028        printf("initrd=");
2029     else
2030        printf("initrd=%s", prefix);
2031    
2032   for (i = 1; i < line->numElements; i++)   for (i = 1; i < line->numElements; i++)
2033      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2034   printf("\n");   printf("\n");
2035      }      }
2036    
2037        line = getLineByType(LT_TITLE, entry->lines);
2038        if (line) {
2039     printf("title=%s\n", line->elements[1].item);
2040        } else {
2041     char * title;
2042     line = getLineByType(LT_MENUENTRY, entry->lines);
2043     title = grub2ExtractTitle(line);
2044     if (title)
2045        printf("title=%s\n", title);
2046        }
2047    }
2048    
2049    int isSuseSystem(void) {
2050        const char * path;
2051        const static char default_path[] = "/etc/SuSE-release";
2052    
2053        if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL)
2054     path = default_path;
2055    
2056        if (!access(path, R_OK))
2057     return 1;
2058        return 0;
2059    }
2060    
2061    int isSuseGrubConf(const char * path) {
2062        FILE * grubConf;
2063        char * line = NULL;
2064        size_t len = 0, res = 0;
2065    
2066        grubConf = fopen(path, "r");
2067        if (!grubConf) {
2068            dbgPrintf("Could not open SuSE configuration file '%s'\n", path);
2069     return 0;
2070        }
2071    
2072        while ((res = getline(&line, &len, grubConf)) != -1) {
2073     if (!strncmp(line, "setup", 5)) {
2074        fclose(grubConf);
2075        free(line);
2076        return 1;
2077     }
2078        }
2079    
2080        dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n",
2081          path);
2082    
2083        fclose(grubConf);
2084        free(line);
2085        return 0;
2086    }
2087    
2088    int suseGrubConfGetLba(const char * path, int * lbaPtr) {
2089        FILE * grubConf;
2090        char * line = NULL;
2091        size_t res = 0, len = 0;
2092    
2093        if (!path) return 1;
2094        if (!lbaPtr) return 1;
2095    
2096        grubConf = fopen(path, "r");
2097        if (!grubConf) return 1;
2098    
2099        while ((res = getline(&line, &len, grubConf)) != -1) {
2100     if (line[res - 1] == '\n')
2101        line[res - 1] = '\0';
2102     else if (len > res)
2103        line[res] = '\0';
2104     else {
2105        line = realloc(line, res + 1);
2106        line[res] = '\0';
2107     }
2108    
2109     if (!strncmp(line, "setup", 5)) {
2110        if (strstr(line, "--force-lba")) {
2111            *lbaPtr = 1;
2112        } else {
2113            *lbaPtr = 0;
2114        }
2115        dbgPrintf("lba: %i\n", *lbaPtr);
2116        break;
2117     }
2118        }
2119    
2120        free(line);
2121        fclose(grubConf);
2122        return 0;
2123    }
2124    
2125    int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) {
2126        FILE * grubConf;
2127        char * line = NULL;
2128        size_t res = 0, len = 0;
2129        char * lastParamPtr = NULL;
2130        char * secLastParamPtr = NULL;
2131        char installDeviceNumber = '\0';
2132        char * bounds = NULL;
2133    
2134        if (!path) return 1;
2135        if (!devicePtr) return 1;
2136    
2137        grubConf = fopen(path, "r");
2138        if (!grubConf) return 1;
2139    
2140        while ((res = getline(&line, &len, grubConf)) != -1) {
2141     if (strncmp(line, "setup", 5))
2142        continue;
2143    
2144     if (line[res - 1] == '\n')
2145        line[res - 1] = '\0';
2146     else if (len > res)
2147        line[res] = '\0';
2148     else {
2149        line = realloc(line, res + 1);
2150        line[res] = '\0';
2151     }
2152    
2153     lastParamPtr = bounds = line + res;
2154    
2155     /* Last parameter in grub may be an optional IMAGE_DEVICE */
2156     while (!isspace(*lastParamPtr))
2157        lastParamPtr--;
2158     lastParamPtr++;
2159    
2160     secLastParamPtr = lastParamPtr - 2;
2161     dbgPrintf("lastParamPtr: %s\n", lastParamPtr);
2162    
2163     if (lastParamPtr + 3 > bounds) {
2164        dbgPrintf("lastParamPtr going over boundary");
2165        fclose(grubConf);
2166        free(line);
2167        return 1;
2168     }
2169     if (!strncmp(lastParamPtr, "(hd", 3))
2170        lastParamPtr += 3;
2171     dbgPrintf("lastParamPtr: %c\n", *lastParamPtr);
2172    
2173     /*
2174     * Second last parameter will decide wether last parameter is
2175     * an IMAGE_DEVICE or INSTALL_DEVICE
2176     */
2177     while (!isspace(*secLastParamPtr))
2178        secLastParamPtr--;
2179     secLastParamPtr++;
2180    
2181     if (secLastParamPtr + 3 > bounds) {
2182        dbgPrintf("secLastParamPtr going over boundary");
2183        fclose(grubConf);
2184        free(line);
2185        return 1;
2186     }
2187     dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr);
2188     if (!strncmp(secLastParamPtr, "(hd", 3)) {
2189        secLastParamPtr += 3;
2190        dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr);
2191        installDeviceNumber = *secLastParamPtr;
2192     } else {
2193        installDeviceNumber = *lastParamPtr;
2194     }
2195    
2196     *devicePtr = malloc(6);
2197     snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber);
2198     dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber);
2199     fclose(grubConf);
2200     free(line);
2201     return 0;
2202        }
2203    
2204        free(line);
2205        fclose(grubConf);
2206        return 1;
2207    }
2208    
2209    int grubGetBootFromDeviceMap(const char * device,
2210         char ** bootPtr) {
2211        FILE * deviceMap;
2212        char * line = NULL;
2213        size_t res = 0, len = 0;
2214        char * devicePtr;
2215        char * bounds = NULL;
2216        const char * path;
2217        const static char default_path[] = "/boot/grub/device.map";
2218    
2219        if (!device) return 1;
2220        if (!bootPtr) return 1;
2221    
2222        if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL)
2223     path = default_path;
2224    
2225        dbgPrintf("opening grub device.map file from: %s\n", path);
2226        deviceMap = fopen(path, "r");
2227        if (!deviceMap)
2228     return 1;
2229    
2230        while ((res = getline(&line, &len, deviceMap)) != -1) {
2231            if (!strncmp(line, "#", 1))
2232        continue;
2233    
2234     if (line[res - 1] == '\n')
2235        line[res - 1] = '\0';
2236     else if (len > res)
2237        line[res] = '\0';
2238     else {
2239        line = realloc(line, res + 1);
2240        line[res] = '\0';
2241     }
2242    
2243     devicePtr = line;
2244     bounds = line + res;
2245    
2246     while ((isspace(*line) && ((devicePtr + 1) <= bounds)))
2247        devicePtr++;
2248     dbgPrintf("device: %s\n", devicePtr);
2249    
2250     if (!strncmp(devicePtr, device, strlen(device))) {
2251        devicePtr += strlen(device);
2252        while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds))
2253            devicePtr++;
2254    
2255        *bootPtr = strdup(devicePtr);
2256        break;
2257     }
2258        }
2259    
2260        free(line);
2261        fclose(deviceMap);
2262        return 0;
2263    }
2264    
2265    int suseGrubConfGetBoot(const char * path, char ** bootPtr) {
2266        char * grubDevice;
2267    
2268        if (suseGrubConfGetInstallDevice(path, &grubDevice))
2269     dbgPrintf("error looking for grub installation device\n");
2270        else
2271     dbgPrintf("grubby installation device: %s\n", grubDevice);
2272    
2273        if (grubGetBootFromDeviceMap(grubDevice, bootPtr))
2274     dbgPrintf("error looking for grub boot device\n");
2275        else
2276     dbgPrintf("grubby boot device: %s\n", *bootPtr);
2277    
2278        free(grubDevice);
2279        return 0;
2280    }
2281    
2282    int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) {
2283        /*
2284         * This SuSE grub configuration file at this location is not your average
2285         * grub configuration file, but instead the grub commands used to setup
2286         * grub on that system.
2287         */
2288        const char * path;
2289        const static char default_path[] = "/etc/grub.conf";
2290    
2291        if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL)
2292     path = default_path;
2293    
2294        if (!isSuseGrubConf(path)) return 1;
2295    
2296        if (lbaPtr) {
2297            *lbaPtr = 0;
2298            if (suseGrubConfGetLba(path, lbaPtr))
2299                return 1;
2300        }
2301    
2302        if (bootPtr) {
2303            *bootPtr = NULL;
2304            suseGrubConfGetBoot(path, bootPtr);
2305        }
2306    
2307        return 0;
2308  }  }
2309    
2310  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 1623  int parseSysconfigGrub(int * lbaPtr, cha Line 2355  int parseSysconfigGrub(int * lbaPtr, cha
2355  }  }
2356    
2357  void dumpSysconfigGrub(void) {  void dumpSysconfigGrub(void) {
2358      char * boot;      char * boot = NULL;
2359      int lba;      int lba;
2360    
2361      if (!parseSysconfigGrub(&lba, &boot)) {      if (isSuseSystem()) {
2362   if (lba) printf("lba\n");          if (parseSuseGrubConf(&lba, &boot)) {
2363   if (boot) printf("boot=%s\n", boot);      free(boot);
2364        return;
2365     }
2366        } else {
2367            if (parseSysconfigGrub(&lba, &boot)) {
2368        free(boot);
2369        return;
2370     }
2371        }
2372    
2373        if (lba) printf("lba\n");
2374        if (boot) {
2375     printf("boot=%s\n", boot);
2376     free(boot);
2377      }      }
2378  }  }
2379    
# Line 1733  struct singleLine *  addLine(struct sing Line 2478  struct singleLine *  addLine(struct sing
2478   sprintf(tmpl.elements[0].item, "[%s]", val);   sprintf(tmpl.elements[0].item, "[%s]", val);
2479   tmpl.elements[0].indent = "";   tmpl.elements[0].indent = "";
2480   val = NULL;   val = NULL;
2481        } else if (type == LT_MENUENTRY) {
2482     char *lineend = "--class gnu-linux --class gnu --class os {";
2483     if (!val) {
2484        fprintf(stderr, "Line type LT_MENUENTRY requires a value\n");
2485        abort();
2486     }
2487     kw = getKeywordByType(type, cfi);
2488     if (!kw) {
2489        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
2490        abort();
2491     }
2492     tmpl.indent = "";
2493     tmpl.type = type;
2494     tmpl.numElements = 3;
2495     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
2496     tmpl.elements[0].item = kw->key;
2497     tmpl.elements[0].indent = alloca(2);
2498     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
2499     tmpl.elements[1].item = (char *)val;
2500     tmpl.elements[1].indent = alloca(2);
2501     sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);
2502     tmpl.elements[2].item = alloca(strlen(lineend)+1);
2503     strcpy(tmpl.elements[2].item, lineend);
2504     tmpl.elements[2].indent = "";
2505      } else {      } else {
2506   kw = getKeywordByType(type, cfi);   kw = getKeywordByType(type, cfi);
2507   if (!kw) abort();   if (!kw) {
2508        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
2509        abort();
2510     }
2511   tmpl.type = type;   tmpl.type = type;
2512   tmpl.numElements = val ? 2 : 1;   tmpl.numElements = val ? 2 : 1;
2513   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
# Line 1759  struct singleLine *  addLine(struct sing Line 2531  struct singleLine *  addLine(struct sing
2531   if (!line->next && !prev) prev = line;   if (!line->next && !prev) prev = line;
2532      }      }
2533    
2534      if (prev == entry->lines)      struct singleLine *menuEntry;
2535   tmpl.indent = defaultIndent ?: "";      menuEntry = getLineByType(LT_MENUENTRY, entry->lines);
2536      else      if (tmpl.type == LT_ENTRY_END) {
2537   tmpl.indent = prev->indent;   if (menuEntry)
2538        tmpl.indent = menuEntry->indent;
2539     else
2540        tmpl.indent = defaultIndent ?: "";
2541        } else if (tmpl.type != LT_MENUENTRY) {
2542     if (menuEntry)
2543        tmpl.indent = "\t";
2544     else if (prev == entry->lines)
2545        tmpl.indent = defaultIndent ?: "";
2546     else
2547        tmpl.indent = prev->indent;
2548        }
2549    
2550      return addLineTmpl(entry, &tmpl, prev, val, cfi);      return addLineTmpl(entry, &tmpl, prev, val, cfi);
2551  }  }
# Line 1789  void removeLine(struct singleEntry * ent Line 2572  void removeLine(struct singleEntry * ent
2572      free(line);      free(line);
2573  }  }
2574    
2575    static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2576    {
2577        struct singleLine newLine = {
2578     .indent = tmplLine->indent,
2579     .type = tmplLine->type,
2580     .next = tmplLine->next,
2581        };
2582        int firstQuotedItem = -1;
2583        int quoteLen = 0;
2584        int j;
2585        int element = 0;
2586        char *c;
2587    
2588        c = malloc(strlen(tmplLine->elements[0].item) + 1);
2589        strcpy(c, tmplLine->elements[0].item);
2590        insertElement(&newLine, c, element++, cfi);
2591        free(c);
2592        c = NULL;
2593    
2594        for (j = 1; j < tmplLine->numElements; j++) {
2595     if (firstQuotedItem == -1) {
2596        quoteLen += strlen(tmplLine->elements[j].item);
2597        
2598        if (isquote(tmplLine->elements[j].item[0])) {
2599     firstQuotedItem = j;
2600            quoteLen += strlen(tmplLine->elements[j].indent);
2601        } else {
2602     c = malloc(quoteLen + 1);
2603     strcpy(c, tmplLine->elements[j].item);
2604     insertElement(&newLine, c, element++, cfi);
2605     free(c);
2606     quoteLen = 0;
2607        }
2608     } else {
2609        int itemlen = strlen(tmplLine->elements[j].item);
2610        quoteLen += itemlen;
2611        quoteLen += strlen(tmplLine->elements[j].indent);
2612        
2613        if (isquote(tmplLine->elements[j].item[itemlen - 1])) {
2614     c = malloc(quoteLen + 1);
2615     c[0] = '\0';
2616     for (int i = firstQuotedItem; i < j+1; i++) {
2617        strcat(c, tmplLine->elements[i].item);
2618        strcat(c, tmplLine->elements[i].indent);
2619     }
2620     insertElement(&newLine, c, element++, cfi);
2621     free(c);
2622    
2623     firstQuotedItem = -1;
2624     quoteLen = 0;
2625        }
2626     }
2627        }
2628        while (tmplLine->numElements)
2629     removeElement(tmplLine, 0);
2630        if (tmplLine->elements)
2631     free(tmplLine->elements);
2632    
2633        tmplLine->numElements = newLine.numElements;
2634        tmplLine->elements = newLine.elements;
2635    }
2636    
2637  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
2638    const char * item, int insertHere,    const char * item, int insertHere,
2639    struct configFileInfo * cfi)    struct configFileInfo * cfi)
# Line 1895  int updateActualImage(struct grubConfig Line 2740  int updateActualImage(struct grubConfig
2740      const char ** arg;      const char ** arg;
2741      int useKernelArgs, useRoot;      int useKernelArgs, useRoot;
2742      int firstElement;      int firstElement;
2743      int *usedElements, *usedArgs;      int *usedElements;
2744      int doreplace;      int doreplace;
2745    
2746      if (!image) return 0;      if (!image) return 0;
# Line 1930  int updateActualImage(struct grubConfig Line 2775  int updateActualImage(struct grubConfig
2775      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)
2776         && !multibootArgs);         && !multibootArgs);
2777    
     for (k = 0, arg = newArgs; *arg; arg++, k++) ;  
     usedArgs = calloc(k, sizeof(*usedArgs));  
   
2778      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
2779    
2780   if (multibootArgs && !entry->multiboot)   if (multibootArgs && !entry->multiboot)
# Line 2008  int updateActualImage(struct grubConfig Line 2850  int updateActualImage(struct grubConfig
2850          usedElements = calloc(line->numElements, sizeof(*usedElements));          usedElements = calloc(line->numElements, sizeof(*usedElements));
2851    
2852   for (k = 0, arg = newArgs; *arg; arg++, k++) {   for (k = 0, arg = newArgs; *arg; arg++, k++) {
             if (usedArgs[k]) continue;  
2853    
2854      doreplace = 1;      doreplace = 1;
2855      for (i = firstElement; i < line->numElements; i++) {      for (i = firstElement; i < line->numElements; i++) {
# Line 2023  int updateActualImage(struct grubConfig Line 2864  int updateActualImage(struct grubConfig
2864                      continue;                      continue;
2865   if (!argMatch(line->elements[i].item, *arg)) {   if (!argMatch(line->elements[i].item, *arg)) {
2866                      usedElements[i]=1;                      usedElements[i]=1;
                     usedArgs[k]=1;  
2867      break;      break;
2868                  }                  }
2869              }              }
# Line 2093  int updateActualImage(struct grubConfig Line 2933  int updateActualImage(struct grubConfig
2933   }   }
2934      }      }
2935    
     free(usedArgs);  
2936      free(newArgs);      free(newArgs);
2937      free(oldArgs);      free(oldArgs);
2938    
# Line 2119  int updateImage(struct grubConfig * cfg, Line 2958  int updateImage(struct grubConfig * cfg,
2958      return rc;      return rc;
2959  }  }
2960    
2961    int updateInitrd(struct grubConfig * cfg, const char * image,
2962                     const char * prefix, const char * initrd) {
2963        struct singleEntry * entry;
2964        struct singleLine * line, * kernelLine, *endLine = NULL;
2965        int index = 0;
2966    
2967        if (!image) return 0;
2968    
2969        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
2970            kernelLine = getLineByType(LT_KERNEL, entry->lines);
2971            if (!kernelLine) continue;
2972    
2973            line = getLineByType(LT_INITRD, entry->lines);
2974            if (line)
2975                removeLine(entry, line);
2976            if (prefix) {
2977                int prefixLen = strlen(prefix);
2978                if (!strncmp(initrd, prefix, prefixLen))
2979                    initrd += prefixLen;
2980            }
2981     endLine = getLineByType(LT_ENTRY_END, entry->lines);
2982     if (endLine)
2983        removeLine(entry, endLine);
2984            line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);
2985            if (!line)
2986        return 1;
2987     if (endLine) {
2988        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
2989                if (!line)
2990     return 1;
2991     }
2992    
2993            break;
2994        }
2995    
2996        return 0;
2997    }
2998    
2999  int checkDeviceBootloader(const char * device, const unsigned char * boot) {  int checkDeviceBootloader(const char * device, const unsigned char * boot) {
3000      int fd;      int fd;
3001      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 2142  int checkDeviceBootloader(const char * d Line 3019  int checkDeviceBootloader(const char * d
3019      if (memcmp(boot, bootSect, 3))      if (memcmp(boot, bootSect, 3))
3020   return 0;   return 0;
3021    
3022      if (boot[1] == 0xeb) {      if (boot[1] == JMP_SHORT_OPCODE) {
3023   offset = boot[2] + 2;   offset = boot[2] + 2;
3024      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {
3025   offset = (boot[3] << 8) + boot[2] + 2;   offset = (boot[3] << 8) + boot[2] + 2;
3026      } else if (boot[0] == 0xeb) {      } else if (boot[0] == JMP_SHORT_OPCODE) {
3027   offset = boot[1] + 2;        offset = boot[1] + 2;
3028            /*
3029     * it looks like grub, when copying stage1 into the mbr, patches stage1
3030     * right after the JMP location, replacing other instructions such as
3031     * JMPs for NOOPs. So, relax the check a little bit by skipping those
3032     * different bytes.
3033     */
3034          if ((bootSect[offset + 1] == NOOP_OPCODE)
3035      && (bootSect[offset + 2] == NOOP_OPCODE)) {
3036     offset = offset + 3;
3037          }
3038      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {
3039   offset = (boot[2] << 8) + boot[1] + 2;   offset = (boot[2] << 8) + boot[1] + 2;
3040      } else {      } else {
# Line 2289  int checkForLilo(struct grubConfig * con Line 3176  int checkForLilo(struct grubConfig * con
3176      return checkDeviceBootloader(line->elements[1].item, boot);      return checkDeviceBootloader(line->elements[1].item, boot);
3177  }  }
3178    
3179    int checkForGrub2(struct grubConfig * config) {
3180        if (!access("/etc/grub.d/", R_OK))
3181     return 2;
3182    
3183        return 1;
3184    }
3185    
3186  int checkForGrub(struct grubConfig * config) {  int checkForGrub(struct grubConfig * config) {
3187      int fd;      int fd;
3188      unsigned char bootSect[512];      unsigned char bootSect[512];
3189      char * boot;      char * boot;
3190        int onSuse = isSuseSystem();
3191    
3192      if (parseSysconfigGrub(NULL, &boot))  
3193   return 0;      if (onSuse) {
3194     if (parseSuseGrubConf(NULL, &boot))
3195        return 0;
3196        } else {
3197     if (parseSysconfigGrub(NULL, &boot))
3198        return 0;
3199        }
3200    
3201      /* assume grub is not installed -- not an error condition */      /* assume grub is not installed -- not an error condition */
3202      if (!boot)      if (!boot)
# Line 2314  int checkForGrub(struct grubConfig * con Line 3215  int checkForGrub(struct grubConfig * con
3215      }      }
3216      close(fd);      close(fd);
3217    
3218        /* The more elaborate checks do not work on SuSE. The checks done
3219         * seem to be reasonble (at least for now), so just return success
3220         */
3221        if (onSuse)
3222     return 2;
3223    
3224      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3225  }  }
3226    
# Line 2347  int checkForExtLinux(struct grubConfig * Line 3254  int checkForExtLinux(struct grubConfig *
3254      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3255  }  }
3256    
3257    int checkForYaboot(struct grubConfig * config) {
3258        /*
3259         * This is a simplistic check that we consider good enough for own puporses
3260         *
3261         * If we were to properly check if yaboot is *installed* we'd need to:
3262         * 1) get the system boot device (LT_BOOT)
3263         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3264         *    the content on the boot device
3265         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3266         * 4) check again if binary and boot device contents match
3267         */
3268        if (!access("/etc/yaboot.conf", R_OK))
3269     return 2;
3270    
3271        return 1;
3272    }
3273    
3274    int checkForElilo(struct grubConfig * config) {
3275        if (!access("/etc/elilo.conf", R_OK))
3276     return 2;
3277    
3278        return 1;
3279    }
3280    
3281  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3282      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3283    
# Line 2361  static char * getRootSpecifier(char * st Line 3292  static char * getRootSpecifier(char * st
3292  static char * getInitrdVal(struct grubConfig * config,  static char * getInitrdVal(struct grubConfig * config,
3293     const char * prefix, struct singleLine *tmplLine,     const char * prefix, struct singleLine *tmplLine,
3294     const char * newKernelInitrd,     const char * newKernelInitrd,
3295     char ** extraInitrds, int extraInitrdCount)     const char ** extraInitrds, int extraInitrdCount)
3296  {  {
3297      char *initrdVal, *end;      char *initrdVal, *end;
3298      int i;      int i;
# Line 2406  static char * getInitrdVal(struct grubCo Line 3337  static char * getInitrdVal(struct grubCo
3337    
3338  int addNewKernel(struct grubConfig * config, struct singleEntry * template,  int addNewKernel(struct grubConfig * config, struct singleEntry * template,
3339           const char * prefix,           const char * prefix,
3340   char * newKernelPath, char * newKernelTitle,   const char * newKernelPath, const char * newKernelTitle,
3341   char * newKernelArgs, char * newKernelInitrd,   const char * newKernelArgs, const char * newKernelInitrd,
3342   char ** extraInitrds, int extraInitrdCount,   const char ** extraInitrds, int extraInitrdCount,
3343                   char * newMBKernel, char * newMBKernelArgs) {                   const char * newMBKernel, const char * newMBKernelArgs) {
3344      struct singleEntry * new;      struct singleEntry * new;
3345      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;
3346      int needs;      int needs;
# Line 2464  int addNewKernel(struct grubConfig * con Line 3395  int addNewKernel(struct grubConfig * con
3395      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3396    
3397      if (tmplLine->type == LT_KERNEL &&      if (tmplLine->type == LT_KERNEL &&
3398   tmplLine->numElements >= 2) {      tmplLine->numElements >= 2) {
3399   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
3400      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
3401       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 2590  int addNewKernel(struct grubConfig * con Line 3521  int addNewKernel(struct grubConfig * con
3521      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3522   }   }
3523    
3524        } else if (tmplLine->type == LT_MENUENTRY &&
3525           (needs & NEED_TITLE)) {
3526     requote(tmplLine, config->cfi);
3527     char *nkt = malloc(strlen(newKernelTitle)+3);
3528     strcpy(nkt, "'");
3529     strcat(nkt, newKernelTitle);
3530     strcat(nkt, "'");
3531     newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi);
3532     free(nkt);
3533     needs &= ~NEED_TITLE;
3534      } else if (tmplLine->type == LT_TITLE &&      } else if (tmplLine->type == LT_TITLE &&
3535         (needs & NEED_TITLE)) {         (needs & NEED_TITLE)) {
3536   if (tmplLine->numElements >= 2) {   if (tmplLine->numElements >= 2) {
# Line 2603  int addNewKernel(struct grubConfig * con Line 3544  int addNewKernel(struct grubConfig * con
3544        tmplLine->indent, newKernelTitle);        tmplLine->indent, newKernelTitle);
3545      needs &= ~NEED_TITLE;      needs &= ~NEED_TITLE;
3546   }   }
3547        } else if (tmplLine->type == LT_ECHO) {
3548        requote(tmplLine, config->cfi);
3549        static const char *prefix = "'Loading ";
3550        if (tmplLine->numElements > 1 &&
3551        strstr(tmplLine->elements[1].item, prefix) &&
3552        masterLine->next && masterLine->next->type == LT_KERNEL) {
3553     char *newTitle = malloc(strlen(prefix) +
3554     strlen(newKernelTitle) + 2);
3555    
3556     strcpy(newTitle, prefix);
3557     strcat(newTitle, newKernelTitle);
3558     strcat(newTitle, "'");
3559     newLine = addLine(new, config->cfi, LT_ECHO,
3560     tmplLine->indent, newTitle);
3561     free(newTitle);
3562        } else {
3563     /* pass through other lines from the template */
3564     newLine = addLineTmpl(new, tmplLine, newLine, NULL,
3565     config->cfi);
3566        }
3567      } else {      } else {
3568   /* pass through other lines from the template */   /* pass through other lines from the template */
3569   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
# Line 2614  int addNewKernel(struct grubConfig * con Line 3574  int addNewKernel(struct grubConfig * con
3574   /* don't have a template, so start the entry with the   /* don't have a template, so start the entry with the
3575   * appropriate starting line   * appropriate starting line
3576   */   */
3577   switch (config->cfi->entrySeparator) {   switch (config->cfi->entryStart) {
3578      case LT_KERNEL:      case LT_KERNEL:
3579   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
3580      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
# Line 2633  int addNewKernel(struct grubConfig * con Line 3593  int addNewKernel(struct grubConfig * con
3593   needs &= ~NEED_MB;   needs &= ~NEED_MB;
3594   break;   break;
3595    
3596        case LT_MENUENTRY: {
3597     char *nkt = malloc(strlen(newKernelTitle)+3);
3598     strcpy(nkt, "'");
3599     strcat(nkt, newKernelTitle);
3600     strcat(nkt, "'");
3601            newLine = addLine(new, config->cfi, LT_MENUENTRY,
3602      config->primaryIndent, nkt);
3603     free(nkt);
3604     needs &= ~NEED_TITLE;
3605     needs |= NEED_END;
3606     break;
3607        }
3608      case LT_TITLE:      case LT_TITLE:
3609   if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above)   if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above)
3610   char * templabel;   char * templabel;
# Line 2666  int addNewKernel(struct grubConfig * con Line 3638  int addNewKernel(struct grubConfig * con
3638    
3639      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
3640       * weren't present in the template, or in the case of no template,       * weren't present in the template, or in the case of no template,
3641       * all the lines following the entrySeparator.       * all the lines following the entryStart.
3642       */       */
3643      if (needs & NEED_TITLE) {      if (needs & NEED_TITLE) {
3644   newLine = addLine(new, config->cfi, LT_TITLE,   newLine = addLine(new, config->cfi, LT_TITLE,
# Line 2707  int addNewKernel(struct grubConfig * con Line 3679  int addNewKernel(struct grubConfig * con
3679   free(initrdVal);   free(initrdVal);
3680   needs &= ~NEED_INITRD;   needs &= ~NEED_INITRD;
3681      }      }
3682        if (needs & NEED_END) {
3683     newLine = addLine(new, config->cfi, LT_ENTRY_END,
3684     config->secondaryIndent, NULL);
3685     needs &= ~NEED_END;
3686        }
3687    
3688      if (needs) {      if (needs) {
3689   printf(_("grubby: needs=%d, aborting\n"), needs);   printf(_("grubby: needs=%d, aborting\n"), needs);
# Line 2736  static void traceback(int signum) Line 3713  static void traceback(int signum)
3713    
3714  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
3715      poptContext optCon;      poptContext optCon;
3716      char * grubConfig = NULL;      const char * grubConfig = NULL;
3717      char * outputFile = NULL;      char * outputFile = NULL;
3718      int arg = 0;      int arg = 0;
3719      int flags = 0;      int flags = 0;
3720      int badImageOkay = 0;      int badImageOkay = 0;
3721        int configureGrub2 = 0;
3722      int configureLilo = 0, configureELilo = 0, configureGrub = 0;      int configureLilo = 0, configureELilo = 0, configureGrub = 0;
3723      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;
3724      int configureExtLinux = 0;      int configureExtLinux = 0;
# Line 2768  int main(int argc, const char ** argv) { Line 3746  int main(int argc, const char ** argv) {
3746      struct singleEntry * template = NULL;      struct singleEntry * template = NULL;
3747      int copyDefault = 0, makeDefault = 0;      int copyDefault = 0, makeDefault = 0;
3748      int displayDefault = 0;      int displayDefault = 0;
3749        int displayDefaultIndex = 0;
3750        int displayDefaultTitle = 0;
3751        int defaultIndex = -1;
3752      struct poptOption options[] = {      struct poptOption options[] = {
3753   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
3754      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 2785  int main(int argc, const char ** argv) { Line 3766  int main(int argc, const char ** argv) {
3766   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
3767      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
3768      _("bootfs") },      _("bootfs") },
3769  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
3770   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
3771      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
3772  #endif  #endif
3773   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
3774      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 2798  int main(int argc, const char ** argv) { Line 3779  int main(int argc, const char ** argv) {
3779        "the kernel referenced by the default image does not exist, "        "the kernel referenced by the default image does not exist, "
3780        "the first linux entry whose kernel does exist is used as the "        "the first linux entry whose kernel does exist is used as the "
3781        "template"), NULL },        "template"), NULL },
3782     { "debug", 0, 0, &debug, 0,
3783        _("print debugging information for failures") },
3784   { "default-kernel", 0, 0, &displayDefault, 0,   { "default-kernel", 0, 0, &displayDefault, 0,
3785      _("display the path of the default kernel") },      _("display the path of the default kernel") },
3786     { "default-index", 0, 0, &displayDefaultIndex, 0,
3787        _("display the index of the default kernel") },
3788     { "default-title", 0, 0, &displayDefaultTitle, 0,
3789        _("display the title of the default kernel") },
3790   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
3791      _("configure elilo bootloader") },      _("configure elilo bootloader") },
3792   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
3793      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
3794   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
3795      _("configure grub bootloader") },      _("configure grub bootloader") },
3796     { "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,
3797        _("configure grub2 bootloader") },
3798   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,
3799      _("display boot information for specified kernel"),      _("display boot information for specified kernel"),
3800      _("kernel-path") },      _("kernel-path") },
# Line 2832  int main(int argc, const char ** argv) { Line 3821  int main(int argc, const char ** argv) {
3821   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
3822      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
3823        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
3824     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
3825        _("make the given entry index the default entry"),
3826        _("entry-index") },
3827   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
3828      _("configure silo bootloader") },      _("configure silo bootloader") },
3829   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 2885  int main(int argc, const char ** argv) { Line 3877  int main(int argc, const char ** argv) {
3877   return 1;   return 1;
3878      }      }
3879    
3880      if ((configureLilo + configureGrub + configureELilo +      if ((configureLilo + configureGrub2 + configureGrub + configureELilo +
3881   configureYaboot + configureSilo + configureZipl +   configureYaboot + configureSilo + configureZipl +
3882   configureExtLinux ) > 1) {   configureExtLinux ) > 1) {
3883   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));
# Line 2894  int main(int argc, const char ** argv) { Line 3886  int main(int argc, const char ** argv) {
3886   fprintf(stderr,   fprintf(stderr,
3887      _("grubby: cannot specify config file with --bootloader-probe\n"));      _("grubby: cannot specify config file with --bootloader-probe\n"));
3888   return 1;   return 1;
3889        } else if (configureGrub2) {
3890     cfi = &grub2ConfigType;
3891      } else if (configureLilo) {      } else if (configureLilo) {
3892   cfi = &liloConfigType;   cfi = &liloConfigType;
3893      } else if (configureGrub) {      } else if (configureGrub) {
# Line 2912  int main(int argc, const char ** argv) { Line 3906  int main(int argc, const char ** argv) {
3906      }      }
3907    
3908      if (!cfi) {      if (!cfi) {
3909            if (grub2FindConfig(&grub2ConfigType))
3910        cfi = &grub2ConfigType;
3911     else
3912        #ifdef __ia64__        #ifdef __ia64__
3913   cfi = &eliloConfigType;      cfi = &eliloConfigType;
3914        #elif __powerpc__        #elif __powerpc__
3915   cfi = &yabootConfigType;      cfi = &yabootConfigType;
3916        #elif __sparc__        #elif __sparc__
3917          cfi = &siloConfigType;              cfi = &siloConfigType;
3918        #elif __s390__        #elif __s390__
3919          cfi = &ziplConfigType;              cfi = &ziplConfigType;
3920        #elif __s390x__        #elif __s390x__
3921          cfi = &ziplConfigtype;              cfi = &ziplConfigtype;
3922        #else        #else
3923   cfi = &grubConfigType;      cfi = &grubConfigType;
3924        #endif        #endif
3925      }      }
3926    
3927      if (!grubConfig)      if (!grubConfig) {
3928   grubConfig = cfi->defaultConfig;   if (cfi->findConfig)
3929        grubConfig = cfi->findConfig(cfi);
3930     if (!grubConfig)
3931        grubConfig = cfi->defaultConfig;
3932        }
3933    
3934      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3935    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
3936    defaultKernel)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
3937        (defaultIndex >= 0))) {
3938   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
3939    "specified option"));    "specified option"));
3940   return 1;   return 1;
# Line 2948  int main(int argc, const char ** argv) { Line 3950  int main(int argc, const char ** argv) {
3950      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
3951   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
3952   return 1;   return 1;
3953      } else if (!newKernelPath && (newKernelTitle  || newKernelInitrd ||      } else if (!newKernelPath && (newKernelTitle  || copyDefault ||
3954    newKernelInitrd || copyDefault     ||    (newKernelInitrd && !updateKernelPath)||
3955    makeDefault || extraInitrdCount > 0)) {    makeDefault || extraInitrdCount > 0)) {
3956   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
3957   return 1;   return 1;
# Line 2974  int main(int argc, const char ** argv) { Line 3976  int main(int argc, const char ** argv) {
3976   makeDefault = 1;   makeDefault = 1;
3977   defaultKernel = NULL;   defaultKernel = NULL;
3978      }      }
3979        else if (defaultKernel && (defaultIndex >= 0)) {
3980     fprintf(stderr, _("grubby: --set-default and --set-default-index "
3981      "may not be used together\n"));
3982     return 1;
3983        }
3984    
3985      if (!strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
3986   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
3987   "is used\n"));   "is used\n"));
3988   return 1;   return 1;
3989      }      }
3990    
3991      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
3992   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
3993          && !removeMBKernel) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
3994     && (defaultIndex == -1)) {
3995   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
3996   return 1;   return 1;
3997      }      }
# Line 3010  int main(int argc, const char ** argv) { Line 4018  int main(int argc, const char ** argv) {
4018      }      }
4019    
4020      if (bootloaderProbe) {      if (bootloaderProbe) {
4021   int lrc = 0, grc = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4022   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4023    
4024     const char *grub2config = grub2FindConfig(&grub2ConfigType);
4025     if (grub2config) {
4026        gconfig = readConfig(grub2config, &grub2ConfigType);
4027        if (!gconfig)
4028     gr2c = 1;
4029        else
4030     gr2c = checkForGrub2(gconfig);
4031     }
4032    
4033   if (!access(grubConfigType.defaultConfig, F_OK)) {   const char *grubconfig = grubFindConfig(&grubConfigType);
4034      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);   if (!access(grubconfig, F_OK)) {
4035        gconfig = readConfig(grubconfig, &grubConfigType);
4036      if (!gconfig)      if (!gconfig)
4037   grc = 1;   grc = 1;
4038      else      else
# Line 3029  int main(int argc, const char ** argv) { Line 4047  int main(int argc, const char ** argv) {
4047   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4048   }   }
4049    
4050     if (!access(eliloConfigType.defaultConfig, F_OK)) {
4051        econfig = readConfig(eliloConfigType.defaultConfig,
4052     &eliloConfigType);
4053        if (!econfig)
4054     erc = 1;
4055        else
4056     erc = checkForElilo(econfig);
4057     }
4058    
4059   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4060      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4061      if (!lconfig)      if (!lconfig)
4062   erc = 1;   extrc = 1;
4063      else      else
4064   erc = checkForExtLinux(lconfig);   extrc = checkForExtLinux(lconfig);
4065   }   }
4066    
4067   if (lrc == 1 || grc == 1) return 1;  
4068     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4069        yconfig = readConfig(yabootConfigType.defaultConfig,
4070     &yabootConfigType);
4071        if (!yconfig)
4072     yrc = 1;
4073        else
4074     yrc = checkForYaboot(yconfig);
4075     }
4076    
4077     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4078     erc == 1)
4079        return 1;
4080    
4081   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4082     if (gr2c == 2) printf("grub2\n");
4083   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4084   if (erc == 2) printf("extlinux\n");   if (extrc == 2) printf("extlinux\n");
4085     if (yrc == 2) printf("yaboot\n");
4086     if (erc == 2) printf("elilo\n");
4087    
4088   return 0;   return 0;
4089      }      }
# Line 3067  int main(int argc, const char ** argv) { Line 4109  int main(int argc, const char ** argv) {
4109                 ((rootspec != NULL) ? strlen(rootspec) : 0));                 ((rootspec != NULL) ? strlen(rootspec) : 0));
4110    
4111   return 0;   return 0;
4112    
4113        } else if (displayDefaultTitle) {
4114     struct singleLine * line;
4115     struct singleEntry * entry;
4116    
4117     if (config->defaultImage == -1) return 0;
4118     entry = findEntryByIndex(config, config->defaultImage);
4119     if (!entry) return 0;
4120    
4121     if (!configureGrub2) {
4122      line = getLineByType(LT_TITLE, entry->lines);
4123      if (!line) return 0;
4124      printf("%s\n", line->elements[1].item);
4125    
4126     } else {
4127      char * title;
4128    
4129      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
4130      line = getLineByType(LT_MENUENTRY, entry->lines);
4131      if (!line) return 0;
4132      title = grub2ExtractTitle(line);
4133      if (title)
4134        printf("%s\n", title);
4135     }
4136     return 0;
4137    
4138        } else if (displayDefaultIndex) {
4139            if (config->defaultImage == -1) return 0;
4140            printf("%i\n", config->defaultImage);
4141    
4142      } else if (kernelInfo)      } else if (kernelInfo)
4143   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
4144    
# Line 3078  int main(int argc, const char ** argv) { Line 4150  int main(int argc, const char ** argv) {
4150      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4151      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4152      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4153      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4154      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4155      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4156                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
4157        if (updateKernelPath && newKernelInitrd) {
4158                if (updateInitrd(config, updateKernelPath, bootPrefix,
4159                                 newKernelInitrd)) return 1;
4160        }
4161      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
4162                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
4163                       extraInitrds, extraInitrdCount,                       (const char **)extraInitrds, extraInitrdCount,
4164                       newMBKernel, newMBKernelArgs)) return 1;                       newMBKernel, newMBKernelArgs)) return 1;
4165            
4166    
# Line 3095  int main(int argc, const char ** argv) { Line 4171  int main(int argc, const char ** argv) {
4171      }      }
4172    
4173      if (!outputFile)      if (!outputFile)
4174   outputFile = grubConfig;   outputFile = (char *)grubConfig;
4175    
4176      return writeConfig(config, outputFile, bootPrefix);      return writeConfig(config, outputFile, bootPrefix);
4177  }  }

Legend:
Removed from v.926  
changed lines
  Added in v.1868