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 1722 by niro, Sat Feb 18 00:53:14 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 49  Line 51 
51  #define MAX_EXTRA_INITRDS  16 /* code segment checked by --bootloader-probe */  #define MAX_EXTRA_INITRDS  16 /* code segment checked by --bootloader-probe */
52  #define CODE_SEG_SIZE  128 /* code segment checked by --bootloader-probe */  #define CODE_SEG_SIZE  128 /* code segment checked by --bootloader-probe */
53    
54    #define NOOP_OPCODE 0x90
55    #define JMP_SHORT_OPCODE 0xeb
56    
57  /* comments get lumped in with indention */  /* comments get lumped in with indention */
58  struct lineElement {  struct lineElement {
59      char * item;      char * item;
# Line 56  struct lineElement { Line 61  struct lineElement {
61  };  };
62    
63  enum lineType_e {  enum lineType_e {
64      LT_WHITESPACE = 1 << 0,      LT_WHITESPACE   = 1 << 0,
65      LT_TITLE      = 1 << 1,      LT_TITLE        = 1 << 1,
66      LT_KERNEL     = 1 << 2,      LT_KERNEL       = 1 << 2,
67      LT_INITRD     = 1 << 3,      LT_INITRD       = 1 << 3,
68      LT_HYPER      = 1 << 4,      LT_HYPER        = 1 << 4,
69      LT_DEFAULT    = 1 << 5,      LT_DEFAULT      = 1 << 5,
70      LT_MBMODULE   = 1 << 6,      LT_MBMODULE     = 1 << 6,
71      LT_ROOT       = 1 << 7,      LT_ROOT         = 1 << 7,
72      LT_FALLBACK   = 1 << 8,      LT_FALLBACK     = 1 << 8,
73      LT_KERNELARGS = 1 << 9,      LT_KERNELARGS   = 1 << 9,
74      LT_BOOT       = 1 << 10,      LT_BOOT         = 1 << 10,
75      LT_BOOTROOT   = 1 << 11,      LT_BOOTROOT     = 1 << 11,
76      LT_LBA        = 1 << 12,      LT_LBA          = 1 << 12,
77      LT_OTHER      = 1 << 13,      LT_OTHER        = 1 << 13,
78      LT_GENERIC    = 1 << 14,      LT_GENERIC      = 1 << 14,
79      LT_UNKNOWN    = 1 << 15,      LT_ECHO    = 1 << 16,
80        LT_MENUENTRY    = 1 << 17,
81        LT_ENTRY_END    = 1 << 18,
82        LT_SET_VARIABLE = 1 << 19,
83        LT_UNKNOWN      = 1 << 20,
84  };  };
85    
86  struct singleLine {  struct singleLine {
# Line 99  struct singleEntry { Line 108  struct singleEntry {
108  #define NEED_TITLE   (1 << 2)  #define NEED_TITLE   (1 << 2)
109  #define NEED_ARGS    (1 << 3)  #define NEED_ARGS    (1 << 3)
110  #define NEED_MB      (1 << 4)  #define NEED_MB      (1 << 4)
111    #define NEED_END     (1 << 5)
112    
113  #define MAIN_DEFAULT    (1 << 0)  #define MAIN_DEFAULT    (1 << 0)
114  #define DEFAULT_SAVED       -2  #define DEFAULT_SAVED       -2
# Line 110  struct keywordTypes { Line 120  struct keywordTypes {
120      char separatorChar;      char separatorChar;
121  };  };
122    
123    struct configFileInfo;
124    
125    typedef const char *(*findConfigFunc)(struct configFileInfo *);
126    typedef const int (*writeLineFunc)(struct configFileInfo *,
127     struct singleLine *line);
128    
129  struct configFileInfo {  struct configFileInfo {
130      char * defaultConfig;      char * defaultConfig;
131        findConfigFunc findConfig;
132        writeLineFunc writeLine;
133      struct keywordTypes * keywords;      struct keywordTypes * keywords;
134      int defaultIsIndex;      int defaultIsIndex;
135        int defaultIsVariable;
136      int defaultSupportSaved;      int defaultSupportSaved;
137      enum lineType_e entrySeparator;      enum lineType_e entryStart;
138        enum lineType_e entryEnd;
139      int needsBootPrefix;      int needsBootPrefix;
140      int argsInQuotes;      int argsInQuotes;
141      int maxTitleLength;      int maxTitleLength;
142      int titleBracketed;      int titleBracketed;
143        int titlePosition;
144      int mbHyperFirst;      int mbHyperFirst;
145      int mbInitRdIsModule;      int mbInitRdIsModule;
146      int mbConcatArgs;      int mbConcatArgs;
# Line 138  struct keywordTypes grubKeywords[] = { Line 159  struct keywordTypes grubKeywords[] = {
159      { NULL,    0, 0 },      { NULL,    0, 0 },
160  };  };
161    
162    const char *grubFindConfig(struct configFileInfo *cfi) {
163        static const char *configFiles[] = {
164     "/etc/grub.conf",
165     "/boot/grub/grub.conf",
166     "/boot/grub/menu.lst",
167     NULL
168        };
169        static int i = -1;
170    
171        if (i == -1) {
172     for (i = 0; configFiles[i] != NULL; i++) {
173        dbgPrintf("Checking \"%s\": ", configFiles[i]);
174        if (!access(configFiles[i], R_OK)) {
175     dbgPrintf("found\n");
176     return configFiles[i];
177        }
178        dbgPrintf("not found\n");
179     }
180        }
181        return configFiles[i];
182    }
183    
184  struct configFileInfo grubConfigType = {  struct configFileInfo grubConfigType = {
185      "/boot/grub/grub.conf",    /* defaultConfig */      .findConfig = grubFindConfig,
186      grubKeywords,    /* keywords */      .keywords = grubKeywords,
187      1,    /* defaultIsIndex */      .defaultIsIndex = 1,
188      1,    /* defaultSupportSaved */      .defaultSupportSaved = 1,
189      LT_TITLE,    /* entrySeparator */      .entryStart = LT_TITLE,
190      1,    /* needsBootPrefix */      .needsBootPrefix = 1,
191      0,    /* argsInQuotes */      .mbHyperFirst = 1,
192      0,    /* maxTitleLength */      .mbInitRdIsModule = 1,
193      0,                                      /* titleBracketed */      .mbAllowExtraInitRds = 1,
194      1,                                      /* mbHyperFirst */  };
195      1,                                      /* mbInitRdIsModule */  
196      0,                                      /* mbConcatArgs */  struct keywordTypes grub2Keywords[] = {
197      1,                                      /* mbAllowExtraInitRds */      { "menuentry",  LT_MENUENTRY,   ' ' },
198        { "}",          LT_ENTRY_END,   ' ' },
199        { "echo",       LT_ECHO,        ' ' },
200        { "set",        LT_SET_VARIABLE,' ', '=' },
201        { "root",       LT_BOOTROOT,    ' ' },
202        { "default",    LT_DEFAULT,     ' ' },
203        { "fallback",   LT_FALLBACK,    ' ' },
204        { "linux",      LT_KERNEL,      ' ' },
205        { "initrd",     LT_INITRD,      ' ', ' ' },
206        { "module",     LT_MBMODULE,    ' ' },
207        { "kernel",     LT_HYPER,       ' ' },
208        { NULL, 0, 0 },
209    };
210    
211    const char *grub2FindConfig(struct configFileInfo *cfi) {
212        static const char *configFiles[] = {
213     "/boot/grub/grub-efi.cfg",
214     "/boot/grub/grub.cfg",
215     NULL
216        };
217        static int i = -1;
218        static const char *grub_cfg = "/boot/grub/grub.cfg";
219    
220        if (i == -1) {
221     for (i = 0; configFiles[i] != NULL; i++) {
222        dbgPrintf("Checking \"%s\": ", configFiles[i]);
223        if (!access(configFiles[i], R_OK)) {
224     dbgPrintf("found\n");
225     return configFiles[i];
226        }
227     }
228        }
229    
230        /* Ubuntu renames grub2 to grub, so check for the grub.d directory
231         * that isn't in grub1, and if it exists, return the config file path
232         * that they use. */
233        if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) {
234     dbgPrintf("found\n");
235     return grub_cfg;
236        }
237    
238        dbgPrintf("not found\n");
239        return configFiles[i];
240    }
241    
242    struct configFileInfo grub2ConfigType = {
243        .findConfig = grub2FindConfig,
244        .keywords = grub2Keywords,
245        .defaultIsIndex = 1,
246        .defaultSupportSaved = 0,
247        .defaultIsVariable = 1,
248        .entryStart = LT_MENUENTRY,
249        .entryEnd = LT_ENTRY_END,
250        .titlePosition = 1,
251        .needsBootPrefix = 1,
252        .mbHyperFirst = 1,
253        .mbInitRdIsModule = 1,
254        .mbAllowExtraInitRds = 1,
255  };  };
256    
257  struct keywordTypes yabootKeywords[] = {  struct keywordTypes yabootKeywords[] = {
# Line 249  struct keywordTypes extlinuxKeywords[] = Line 349  struct keywordTypes extlinuxKeywords[] =
349  };  };
350  int useextlinuxmenu;  int useextlinuxmenu;
351  struct configFileInfo eliloConfigType = {  struct configFileInfo eliloConfigType = {
352      "/boot/efi/EFI/redhat/elilo.conf",    /* defaultConfig */      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",
353      eliloKeywords,    /* keywords */      .keywords = eliloKeywords,
354      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
355      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
356      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
357      1,                    /* needsBootPrefix */      .mbConcatArgs = 1,
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     1,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
358  };  };
359    
360  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
361      "/etc/lilo.conf",    /* defaultConfig */      .defaultConfig = "/etc/lilo.conf",
362      liloKeywords,    /* keywords */      .keywords = liloKeywords,
363      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
364      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
365      LT_KERNEL,    /* entrySeparator */      .maxTitleLength = 15,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
366  };  };
367    
368  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
369      "/etc/yaboot.conf",    /* defaultConfig */      .defaultConfig = "/etc/yaboot.conf",
370      yabootKeywords,    /* keywords */      .keywords = yabootKeywords,
371      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
372      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
373      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
374      1,    /* needsBootPrefix */      .maxTitleLength = 15,
375      1,    /* argsInQuotes */      .mbAllowExtraInitRds = 1,
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
376  };  };
377    
378  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
379      "/etc/silo.conf",    /* defaultConfig */      .defaultConfig = "/etc/silo.conf",
380      siloKeywords,    /* keywords */      .keywords = siloKeywords,
381      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
382      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
383      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
384      1,    /* needsBootPrefix */      .maxTitleLength = 15,
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
385  };  };
386    
387  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
388      "/etc/zipl.conf",    /* defaultConfig */      .defaultConfig = "/etc/zipl.conf",
389      ziplKeywords,    /* keywords */      .keywords = ziplKeywords,
390      0,    /* defaultIsIndex */      .entryStart = LT_TITLE,
391      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
392      LT_TITLE,    /* entrySeparator */      .titleBracketed = 1,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     1,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
393  };  };
394    
395  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
396      "/boot/extlinux/extlinux.conf",         /* defaultConfig */      .defaultConfig = "/boot/extlinux/extlinux.conf",
397      extlinuxKeywords,                       /* keywords */      .keywords = extlinuxKeywords,
398      0,                                      /* defaultIsIndex */      .entryStart = LT_TITLE,
399      0,                                      /* defaultSupportSaved */      .needsBootPrefix = 1,
400      LT_TITLE,                               /* entrySeparator */      .maxTitleLength = 255,
401      1,                                      /* needsBootPrefix */      .mbAllowExtraInitRds = 1,
     0,                                      /* argsInQuotes */  
     255,                                    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
402  };  };
403    
404  struct grubConfig {  struct grubConfig {
# Line 356  struct grubConfig { Line 413  struct grubConfig {
413      struct configFileInfo * cfi;      struct configFileInfo * cfi;
414  };  };
415    
416    blkid_cache blkid;
417    
418  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);
419  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
420       const char * path, const char * prefix,       const char * path, const char * prefix,
# Line 369  static int lineWrite(FILE * out, struct Line 428  static int lineWrite(FILE * out, struct
428  static int getNextLine(char ** bufPtr, struct singleLine * line,  static int getNextLine(char ** bufPtr, struct singleLine * line,
429         struct configFileInfo * cfi);         struct configFileInfo * cfi);
430  static char * getRootSpecifier(char * str);  static char * getRootSpecifier(char * str);
431    static void requote(struct singleLine *line, struct configFileInfo * cfi);
432  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
433    const char * item, int insertHere,    const char * item, int insertHere,
434    struct configFileInfo * cfi);    struct configFileInfo * cfi);
# Line 433  static struct keywordTypes * getKeywordB Line 493  static struct keywordTypes * getKeywordB
493      return NULL;      return NULL;
494  }  }
495    
496  static char * getpathbyspec(char *device) {  static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) {
497      static blkid_cache blkid;      struct keywordTypes *kt = getKeywordByType(type, cfi);
498        if (kt)
499     return kt->key;
500        return "unknown";
501    }
502    
503    static char * getpathbyspec(char *device) {
504      if (!blkid)      if (!blkid)
505          blkid_get_cache(&blkid, NULL);          blkid_get_cache(&blkid, NULL);
506    
507      return blkid_get_devname(blkid, device, NULL);      return blkid_get_devname(blkid, device, NULL);
508  }  }
509    
510    static char * getuuidbydev(char *device) {
511        if (!blkid)
512     blkid_get_cache(&blkid, NULL);
513    
514        return blkid_get_tag_value(blkid, "UUID", device);
515    }
516    
517  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
518   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
519      struct keywordTypes * kw;      struct keywordTypes * kw;
# Line 477  static int isBracketedTitle(struct singl Line 549  static int isBracketedTitle(struct singl
549      return 0;      return 0;
550  }  }
551    
552  static int isEntrySeparator(struct singleLine * line,  static int isEntryStart(struct singleLine * line,
553                              struct configFileInfo * cfi) {                              struct configFileInfo * cfi) {
554      return line->type == cfi->entrySeparator || line->type == LT_OTHER ||      return line->type == cfi->entryStart || line->type == LT_OTHER ||
555   (cfi->titleBracketed && isBracketedTitle(line));   (cfi->titleBracketed && isBracketedTitle(line));
556  }  }
557    
# Line 802  static struct grubConfig * readConfig(co Line 874  static struct grubConfig * readConfig(co
874      cfg->secondaryIndent = strdup(line->indent);      cfg->secondaryIndent = strdup(line->indent);
875   }   }
876    
877   if (isEntrySeparator(line, cfi)) {   if (isEntryStart(line, cfi)) {
878      sawEntry = 1;      sawEntry = 1;
879      if (!entry) {      if (!entry) {
880   cfg->entries = malloc(sizeof(*entry));   cfg->entries = malloc(sizeof(*entry));
# Line 818  static struct grubConfig * readConfig(co Line 890  static struct grubConfig * readConfig(co
890      entry->next = NULL;      entry->next = NULL;
891   }   }
892    
893   if (line->type == LT_DEFAULT && line->numElements == 2) {   if (line->type == LT_SET_VARIABLE) {
894        int i;
895        dbgPrintf("found 'set' command (%d elements): ", line->numElements);
896        dbgPrintf("%s", line->indent);
897        for (i = 0; i < line->numElements; i++)
898     dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item);
899        dbgPrintf("\n");
900        struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
901        if (kwType && line->numElements == 3 &&
902        !strcmp(line->elements[1].item, kwType->key)) {
903     dbgPrintf("Line sets default config\n");
904     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
905     defaultLine = line;
906        }
907     } else if (line->type == LT_DEFAULT && line->numElements == 2) {
908      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
909      defaultLine = line;      defaultLine = line;
910    
# Line 931  static struct grubConfig * readConfig(co Line 1017  static struct grubConfig * readConfig(co
1017   entry->lines = line;   entry->lines = line;
1018      else      else
1019   last->next = line;   last->next = line;
1020      dbgPrintf("readConfig added %d to %p\n", line->type, entry);      dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry);
1021    
1022        /* we could have seen this outside of an entry... if so, we
1023         * ignore it like any other line we don't grok */
1024        if (line->type == LT_ENTRY_END && sawEntry)
1025     sawEntry = 0;
1026   } else {   } else {
1027      if (!cfg->theLines)      if (!cfg->theLines)
1028   cfg->theLines = line;   cfg->theLines = line;
1029      else      else
1030   last->next = line;   last->next = line;
1031      dbgPrintf("readConfig added %d to cfg\n", line->type);      dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi));
1032   }   }
1033    
1034   last = line;   last = line;
# Line 945  static struct grubConfig * readConfig(co Line 1036  static struct grubConfig * readConfig(co
1036    
1037      free(incoming);      free(incoming);
1038    
1039        dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1040      if (defaultLine) {      if (defaultLine) {
1041   if (cfi->defaultSupportSaved &&   if (cfi->defaultIsVariable) {
1042        char *value = defaultLine->elements[2].item;
1043        while (*value && (*value == '"' || *value == '\'' ||
1044        *value == ' ' || *value == '\t'))
1045     value++;
1046        cfg->defaultImage = strtol(value, &end, 10);
1047        while (*end && (*end == '"' || *end == '\'' ||
1048        *end == ' ' || *end == '\t'))
1049     end++;
1050        if (*end) cfg->defaultImage = -1;
1051     } else if (cfi->defaultSupportSaved &&
1052   !strncmp(defaultLine->elements[1].item, "saved", 5)) {   !strncmp(defaultLine->elements[1].item, "saved", 5)) {
1053      cfg->defaultImage = DEFAULT_SAVED;      cfg->defaultImage = DEFAULT_SAVED;
1054   } else if (cfi->defaultIsIndex) {   } else if (cfi->defaultIsIndex) {
# Line 995  static void writeDefault(FILE * out, cha Line 1097  static void writeDefault(FILE * out, cha
1097   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1098      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1099   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1100      fprintf(out, "%sdefault%s%d\n", indent, separator,      if (cfg->cfi->defaultIsVariable) {
1101      cfg->defaultImage);          fprintf(out, "%sset default=\"%d\"\n", indent,
1102     cfg->defaultImage);
1103        } else {
1104     fprintf(out, "%sdefault%s%d\n", indent, separator,
1105     cfg->defaultImage);
1106        }
1107   } else {   } else {
1108      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1109    
# Line 1086  static int writeConfig(struct grubConfig Line 1193  static int writeConfig(struct grubConfig
1193      }      }
1194    
1195      line = cfg->theLines;      line = cfg->theLines;
1196        struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);
1197      while (line) {      while (line) {
1198   if (line->type == LT_DEFAULT) {          if (line->type == LT_SET_VARIABLE && defaultKw &&
1199     line->numElements == 3 &&
1200     !strcmp(line->elements[1].item, defaultKw->key)) {
1201        writeDefault(out, line->indent, line->elements[0].indent, cfg);
1202        needs &= ~MAIN_DEFAULT;
1203     } else if (line->type == LT_DEFAULT) {
1204      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1205      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1206   } else if (line->type == LT_FALLBACK) {   } else if (line->type == LT_FALLBACK) {
# Line 1155  static int numEntries(struct grubConfig Line 1268  static int numEntries(struct grubConfig
1268      return i;      return i;
1269  }  }
1270    
1271    static char *findDiskForRoot()
1272    {
1273        int fd;
1274        char buf[65536];
1275        char *devname;
1276        char *chptr;
1277        int rc;
1278    
1279        if ((fd = open(_PATH_MOUNTED, O_RDONLY)) < 0) {
1280            fprintf(stderr, "grubby: failed to open %s: %s\n",
1281                    _PATH_MOUNTED, strerror(errno));
1282            return NULL;
1283        }
1284    
1285        rc = read(fd, buf, sizeof(buf) - 1);
1286        if (rc <= 0) {
1287            fprintf(stderr, "grubby: failed to read %s: %s\n",
1288                    _PATH_MOUNTED, strerror(errno));
1289            close(fd);
1290            return NULL;
1291        }
1292        close(fd);
1293        buf[rc] = '\0';
1294        chptr = buf;
1295    
1296        while (chptr && chptr != buf+rc) {
1297            devname = chptr;
1298    
1299            /*
1300             * The first column of a mtab entry is the device, but if the entry is a
1301             * special device it won't start with /, so move on to the next line.
1302             */
1303            if (*devname != '/') {
1304                chptr = strchr(chptr, '\n');
1305                if (chptr)
1306                    chptr++;
1307                continue;
1308            }
1309    
1310            /* Seek to the next space */
1311            chptr = strchr(chptr, ' ');
1312            if (!chptr) {
1313                fprintf(stderr, "grubby: error parsing %s: %s\n",
1314                        _PATH_MOUNTED, strerror(errno));
1315                return NULL;
1316            }
1317    
1318            /*
1319             * The second column of a mtab entry is the mount point, we are looking
1320             * for '/' obviously.
1321             */
1322            if (*(++chptr) == '/' && *(++chptr) == ' ') {
1323                /*
1324                 * Move back 2, which is the first space after the device name, set
1325                 * it to \0 so strdup will just get the devicename.
1326                 */
1327                chptr -= 2;
1328                *chptr = '\0';
1329                return strdup(devname);
1330            }
1331    
1332            /* Next line */
1333            chptr = strchr(chptr, '\n');
1334            if (chptr)
1335                chptr++;
1336        }
1337    
1338        return NULL;
1339    }
1340    
1341  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1342    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1343      struct singleLine * line;      struct singleLine * line;
1344      char * fullName;      char * fullName;
1345      int i;      int i;
     struct stat sb, sb2;  
1346      char * dev;      char * dev;
1347      char * rootspec;      char * rootspec;
1348        char * rootdev;
1349    
1350      if (skipRemoved && entry->skip) return 0;      if (skipRemoved && entry->skip) return 0;
1351    
# Line 1212  int suitableImage(struct singleEntry * e Line 1395  int suitableImage(struct singleEntry * e
1395      if (!dev)      if (!dev)
1396          return 0;          return 0;
1397    
1398      i = stat(dev, &sb);      rootdev = findDiskForRoot();
1399      if (i)      if (!rootdev)
1400   return 0;   return 0;
1401    
1402      stat("/", &sb2);      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1403            free(rootdev);
1404            return 0;
1405        }
1406    
1407      if (sb.st_rdev != sb2.st_dev)      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1408     free(rootdev);
1409          return 0;          return 0;
1410        }
1411    
1412        free(rootdev);
1413    
1414      return 1;      return 1;
1415  }  }
# Line 1733  struct singleLine *  addLine(struct sing Line 1923  struct singleLine *  addLine(struct sing
1923   sprintf(tmpl.elements[0].item, "[%s]", val);   sprintf(tmpl.elements[0].item, "[%s]", val);
1924   tmpl.elements[0].indent = "";   tmpl.elements[0].indent = "";
1925   val = NULL;   val = NULL;
1926        } else if (type == LT_MENUENTRY) {
1927     char *lineend = "--class gnu-linux --class gnu --class os {";
1928     if (!val) {
1929        fprintf(stderr, "Line type LT_MENUENTRY requires a value\n");
1930        abort();
1931     }
1932     kw = getKeywordByType(type, cfi);
1933     if (!kw) {
1934        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
1935        abort();
1936     }
1937     tmpl.indent = "";
1938     tmpl.type = type;
1939     tmpl.numElements = 3;
1940     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
1941     tmpl.elements[0].item = kw->key;
1942     tmpl.elements[0].indent = alloca(2);
1943     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
1944     tmpl.elements[1].item = (char *)val;
1945     tmpl.elements[1].indent = alloca(2);
1946     sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);
1947     tmpl.elements[2].item = alloca(strlen(lineend)+1);
1948     strcpy(tmpl.elements[2].item, lineend);
1949     tmpl.elements[2].indent = "";
1950      } else {      } else {
1951   kw = getKeywordByType(type, cfi);   kw = getKeywordByType(type, cfi);
1952   if (!kw) abort();   if (!kw) {
1953        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
1954        abort();
1955     }
1956   tmpl.type = type;   tmpl.type = type;
1957   tmpl.numElements = val ? 2 : 1;   tmpl.numElements = val ? 2 : 1;
1958   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
# Line 1759  struct singleLine *  addLine(struct sing Line 1976  struct singleLine *  addLine(struct sing
1976   if (!line->next && !prev) prev = line;   if (!line->next && !prev) prev = line;
1977      }      }
1978    
1979      if (prev == entry->lines)      struct singleLine *menuEntry;
1980   tmpl.indent = defaultIndent ?: "";      menuEntry = getLineByType(LT_MENUENTRY, entry->lines);
1981      else      if (tmpl.type == LT_ENTRY_END) {
1982   tmpl.indent = prev->indent;   if (menuEntry)
1983        tmpl.indent = menuEntry->indent;
1984     else
1985        tmpl.indent = defaultIndent ?: "";
1986        } else if (tmpl.type != LT_MENUENTRY) {
1987     if (menuEntry)
1988        tmpl.indent = "\t";
1989     else if (prev == entry->lines)
1990        tmpl.indent = defaultIndent ?: "";
1991     else
1992        tmpl.indent = prev->indent;
1993        }
1994    
1995      return addLineTmpl(entry, &tmpl, prev, val, cfi);      return addLineTmpl(entry, &tmpl, prev, val, cfi);
1996  }  }
# Line 1789  void removeLine(struct singleEntry * ent Line 2017  void removeLine(struct singleEntry * ent
2017      free(line);      free(line);
2018  }  }
2019    
2020    static int isquote(char q)
2021    {
2022        if (q == '\'' || q == '\"')
2023     return 1;
2024        return 0;
2025    }
2026    
2027    static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2028    {
2029        struct singleLine newLine = {
2030     .indent = tmplLine->indent,
2031     .type = tmplLine->type,
2032     .next = tmplLine->next,
2033        };
2034        int firstQuotedItem = -1;
2035        int quoteLen = 0;
2036        int j;
2037        int element = 0;
2038        char *c;
2039    
2040        c = malloc(strlen(tmplLine->elements[0].item) + 1);
2041        strcpy(c, tmplLine->elements[0].item);
2042        insertElement(&newLine, c, element++, cfi);
2043        free(c);
2044        c = NULL;
2045    
2046        for (j = 1; j < tmplLine->numElements; j++) {
2047     if (firstQuotedItem == -1) {
2048        quoteLen += strlen(tmplLine->elements[j].item);
2049        
2050        if (isquote(tmplLine->elements[j].item[0])) {
2051     firstQuotedItem = j;
2052            quoteLen += strlen(tmplLine->elements[j].indent);
2053        } else {
2054     c = malloc(quoteLen + 1);
2055     strcpy(c, tmplLine->elements[j].item);
2056     insertElement(&newLine, c, element++, cfi);
2057     free(c);
2058     quoteLen = 0;
2059        }
2060     } else {
2061        int itemlen = strlen(tmplLine->elements[j].item);
2062        quoteLen += itemlen;
2063        quoteLen += strlen(tmplLine->elements[j].indent);
2064        
2065        if (isquote(tmplLine->elements[j].item[itemlen - 1])) {
2066     c = malloc(quoteLen + 1);
2067     c[0] = '\0';
2068     for (int i = firstQuotedItem; i < j+1; i++) {
2069        strcat(c, tmplLine->elements[i].item);
2070        strcat(c, tmplLine->elements[i].indent);
2071     }
2072     insertElement(&newLine, c, element++, cfi);
2073     free(c);
2074    
2075     firstQuotedItem = -1;
2076     quoteLen = 0;
2077        }
2078     }
2079        }
2080        while (tmplLine->numElements)
2081     removeElement(tmplLine, 0);
2082        if (tmplLine->elements)
2083     free(tmplLine->elements);
2084    
2085        tmplLine->numElements = newLine.numElements;
2086        tmplLine->elements = newLine.elements;
2087    }
2088    
2089  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
2090    const char * item, int insertHere,    const char * item, int insertHere,
2091    struct configFileInfo * cfi)    struct configFileInfo * cfi)
# Line 1895  int updateActualImage(struct grubConfig Line 2192  int updateActualImage(struct grubConfig
2192      const char ** arg;      const char ** arg;
2193      int useKernelArgs, useRoot;      int useKernelArgs, useRoot;
2194      int firstElement;      int firstElement;
2195      int *usedElements, *usedArgs;      int *usedElements;
2196      int doreplace;      int doreplace;
2197    
2198      if (!image) return 0;      if (!image) return 0;
# Line 1930  int updateActualImage(struct grubConfig Line 2227  int updateActualImage(struct grubConfig
2227      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)
2228         && !multibootArgs);         && !multibootArgs);
2229    
     for (k = 0, arg = newArgs; *arg; arg++, k++) ;  
     usedArgs = calloc(k, sizeof(*usedArgs));  
   
2230      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
2231    
2232   if (multibootArgs && !entry->multiboot)   if (multibootArgs && !entry->multiboot)
# Line 2008  int updateActualImage(struct grubConfig Line 2302  int updateActualImage(struct grubConfig
2302          usedElements = calloc(line->numElements, sizeof(*usedElements));          usedElements = calloc(line->numElements, sizeof(*usedElements));
2303    
2304   for (k = 0, arg = newArgs; *arg; arg++, k++) {   for (k = 0, arg = newArgs; *arg; arg++, k++) {
             if (usedArgs[k]) continue;  
2305    
2306      doreplace = 1;      doreplace = 1;
2307      for (i = firstElement; i < line->numElements; i++) {      for (i = firstElement; i < line->numElements; i++) {
# Line 2023  int updateActualImage(struct grubConfig Line 2316  int updateActualImage(struct grubConfig
2316                      continue;                      continue;
2317   if (!argMatch(line->elements[i].item, *arg)) {   if (!argMatch(line->elements[i].item, *arg)) {
2318                      usedElements[i]=1;                      usedElements[i]=1;
                     usedArgs[k]=1;  
2319      break;      break;
2320                  }                  }
2321              }              }
# Line 2093  int updateActualImage(struct grubConfig Line 2385  int updateActualImage(struct grubConfig
2385   }   }
2386      }      }
2387    
     free(usedArgs);  
2388      free(newArgs);      free(newArgs);
2389      free(oldArgs);      free(oldArgs);
2390    
# Line 2119  int updateImage(struct grubConfig * cfg, Line 2410  int updateImage(struct grubConfig * cfg,
2410      return rc;      return rc;
2411  }  }
2412    
2413    int updateInitrd(struct grubConfig * cfg, const char * image,
2414                     const char * prefix, const char * initrd) {
2415        struct singleEntry * entry;
2416        struct singleLine * line, * kernelLine, *endLine = NULL;
2417        int index = 0;
2418    
2419        if (!image) return 0;
2420    
2421        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
2422            kernelLine = getLineByType(LT_KERNEL, entry->lines);
2423            if (!kernelLine) continue;
2424    
2425            line = getLineByType(LT_INITRD, entry->lines);
2426            if (line)
2427                removeLine(entry, line);
2428            if (prefix) {
2429                int prefixLen = strlen(prefix);
2430                if (!strncmp(initrd, prefix, prefixLen))
2431                    initrd += prefixLen;
2432            }
2433     endLine = getLineByType(LT_ENTRY_END, entry->lines);
2434     if (endLine)
2435        removeLine(entry, endLine);
2436            line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);
2437            if (!line)
2438        return 1;
2439     if (endLine) {
2440        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
2441                if (!line)
2442     return 1;
2443     }
2444    
2445            break;
2446        }
2447    
2448        return 0;
2449    }
2450    
2451  int checkDeviceBootloader(const char * device, const unsigned char * boot) {  int checkDeviceBootloader(const char * device, const unsigned char * boot) {
2452      int fd;      int fd;
2453      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 2142  int checkDeviceBootloader(const char * d Line 2471  int checkDeviceBootloader(const char * d
2471      if (memcmp(boot, bootSect, 3))      if (memcmp(boot, bootSect, 3))
2472   return 0;   return 0;
2473    
2474      if (boot[1] == 0xeb) {      if (boot[1] == JMP_SHORT_OPCODE) {
2475   offset = boot[2] + 2;   offset = boot[2] + 2;
2476      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {
2477   offset = (boot[3] << 8) + boot[2] + 2;   offset = (boot[3] << 8) + boot[2] + 2;
2478      } else if (boot[0] == 0xeb) {      } else if (boot[0] == JMP_SHORT_OPCODE) {
2479   offset = boot[1] + 2;        offset = boot[1] + 2;
2480            /*
2481     * it looks like grub, when copying stage1 into the mbr, patches stage1
2482     * right after the JMP location, replacing other instructions such as
2483     * JMPs for NOOPs. So, relax the check a little bit by skipping those
2484     * different bytes.
2485     */
2486          if ((bootSect[offset + 1] == NOOP_OPCODE)
2487      && (bootSect[offset + 2] == NOOP_OPCODE)) {
2488     offset = offset + 3;
2489          }
2490      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {
2491   offset = (boot[2] << 8) + boot[1] + 2;   offset = (boot[2] << 8) + boot[1] + 2;
2492      } else {      } else {
# Line 2289  int checkForLilo(struct grubConfig * con Line 2628  int checkForLilo(struct grubConfig * con
2628      return checkDeviceBootloader(line->elements[1].item, boot);      return checkDeviceBootloader(line->elements[1].item, boot);
2629  }  }
2630    
2631    int checkForGrub2(struct grubConfig * config) {
2632        if (!access("/etc/grub.d/", R_OK))
2633     return 2;
2634    
2635        return 1;
2636    }
2637    
2638  int checkForGrub(struct grubConfig * config) {  int checkForGrub(struct grubConfig * config) {
2639      int fd;      int fd;
2640      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 2464  int addNewKernel(struct grubConfig * con Line 2810  int addNewKernel(struct grubConfig * con
2810      if (*chptr == '#') continue;      if (*chptr == '#') continue;
2811    
2812      if (tmplLine->type == LT_KERNEL &&      if (tmplLine->type == LT_KERNEL &&
2813   tmplLine->numElements >= 2) {      tmplLine->numElements >= 2) {
2814   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
2815      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
2816       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 2590  int addNewKernel(struct grubConfig * con Line 2936  int addNewKernel(struct grubConfig * con
2936      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
2937   }   }
2938    
2939        } else if (tmplLine->type == LT_MENUENTRY &&
2940           (needs & NEED_TITLE)) {
2941     requote(tmplLine, config->cfi);
2942     char *nkt = malloc(strlen(newKernelTitle)+3);
2943     strcpy(nkt, "'");
2944     strcat(nkt, newKernelTitle);
2945     strcat(nkt, "'");
2946     newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi);
2947     free(nkt);
2948     needs &= ~NEED_TITLE;
2949      } else if (tmplLine->type == LT_TITLE &&      } else if (tmplLine->type == LT_TITLE &&
2950         (needs & NEED_TITLE)) {         (needs & NEED_TITLE)) {
2951   if (tmplLine->numElements >= 2) {   if (tmplLine->numElements >= 2) {
# Line 2603  int addNewKernel(struct grubConfig * con Line 2959  int addNewKernel(struct grubConfig * con
2959        tmplLine->indent, newKernelTitle);        tmplLine->indent, newKernelTitle);
2960      needs &= ~NEED_TITLE;      needs &= ~NEED_TITLE;
2961   }   }
2962        } else if (tmplLine->type == LT_ECHO) {
2963        requote(tmplLine, config->cfi);
2964        static const char *prefix = "'Loading ";
2965        if (tmplLine->numElements > 1 &&
2966        strstr(tmplLine->elements[1].item, prefix) &&
2967        masterLine->next && masterLine->next->type == LT_KERNEL) {
2968     char *newTitle = malloc(strlen(prefix) +
2969     strlen(newKernelTitle) + 2);
2970    
2971     strcpy(newTitle, prefix);
2972     strcat(newTitle, newKernelTitle);
2973     strcat(newTitle, "'");
2974     newLine = addLine(new, config->cfi, LT_ECHO,
2975     tmplLine->indent, newTitle);
2976     free(newTitle);
2977        } else {
2978     /* pass through other lines from the template */
2979     newLine = addLineTmpl(new, tmplLine, newLine, NULL,
2980     config->cfi);
2981        }
2982      } else {      } else {
2983   /* pass through other lines from the template */   /* pass through other lines from the template */
2984   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
# Line 2614  int addNewKernel(struct grubConfig * con Line 2989  int addNewKernel(struct grubConfig * con
2989   /* don't have a template, so start the entry with the   /* don't have a template, so start the entry with the
2990   * appropriate starting line   * appropriate starting line
2991   */   */
2992   switch (config->cfi->entrySeparator) {   switch (config->cfi->entryStart) {
2993      case LT_KERNEL:      case LT_KERNEL:
2994   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
2995      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
# Line 2633  int addNewKernel(struct grubConfig * con Line 3008  int addNewKernel(struct grubConfig * con
3008   needs &= ~NEED_MB;   needs &= ~NEED_MB;
3009   break;   break;
3010    
3011        case LT_MENUENTRY: {
3012     char *nkt = malloc(strlen(newKernelTitle)+3);
3013     strcpy(nkt, "'");
3014     strcat(nkt, newKernelTitle);
3015     strcat(nkt, "'");
3016            newLine = addLine(new, config->cfi, LT_MENUENTRY,
3017      config->primaryIndent, nkt);
3018     free(nkt);
3019     needs &= ~NEED_TITLE;
3020     needs |= NEED_END;
3021     break;
3022        }
3023      case LT_TITLE:      case LT_TITLE:
3024   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)
3025   char * templabel;   char * templabel;
# Line 2666  int addNewKernel(struct grubConfig * con Line 3053  int addNewKernel(struct grubConfig * con
3053    
3054      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
3055       * 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,
3056       * all the lines following the entrySeparator.       * all the lines following the entryStart.
3057       */       */
3058      if (needs & NEED_TITLE) {      if (needs & NEED_TITLE) {
3059   newLine = addLine(new, config->cfi, LT_TITLE,   newLine = addLine(new, config->cfi, LT_TITLE,
# Line 2707  int addNewKernel(struct grubConfig * con Line 3094  int addNewKernel(struct grubConfig * con
3094   free(initrdVal);   free(initrdVal);
3095   needs &= ~NEED_INITRD;   needs &= ~NEED_INITRD;
3096      }      }
3097        if (needs & NEED_END) {
3098     newLine = addLine(new, config->cfi, LT_ENTRY_END,
3099     config->secondaryIndent, NULL);
3100     needs &= ~NEED_END;
3101        }
3102    
3103      if (needs) {      if (needs) {
3104   printf(_("grubby: needs=%d, aborting\n"), needs);   printf(_("grubby: needs=%d, aborting\n"), needs);
# Line 2736  static void traceback(int signum) Line 3128  static void traceback(int signum)
3128    
3129  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
3130      poptContext optCon;      poptContext optCon;
3131      char * grubConfig = NULL;      const char * grubConfig = NULL;
3132      char * outputFile = NULL;      char * outputFile = NULL;
3133      int arg = 0;      int arg = 0;
3134      int flags = 0;      int flags = 0;
3135      int badImageOkay = 0;      int badImageOkay = 0;
3136        int configureGrub2 = 0;
3137      int configureLilo = 0, configureELilo = 0, configureGrub = 0;      int configureLilo = 0, configureELilo = 0, configureGrub = 0;
3138      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;
3139      int configureExtLinux = 0;      int configureExtLinux = 0;
# Line 2768  int main(int argc, const char ** argv) { Line 3161  int main(int argc, const char ** argv) {
3161      struct singleEntry * template = NULL;      struct singleEntry * template = NULL;
3162      int copyDefault = 0, makeDefault = 0;      int copyDefault = 0, makeDefault = 0;
3163      int displayDefault = 0;      int displayDefault = 0;
3164        int displayDefaultIndex = 0;
3165        int displayDefaultTitle = 0;
3166      struct poptOption options[] = {      struct poptOption options[] = {
3167   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
3168      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 2800  int main(int argc, const char ** argv) { Line 3195  int main(int argc, const char ** argv) {
3195        "template"), NULL },        "template"), NULL },
3196   { "default-kernel", 0, 0, &displayDefault, 0,   { "default-kernel", 0, 0, &displayDefault, 0,
3197      _("display the path of the default kernel") },      _("display the path of the default kernel") },
3198     { "default-index", 0, 0, &displayDefaultIndex, 0,
3199        _("display the index of the default kernel") },
3200     { "default-title", 0, 0, &displayDefaultTitle, 0,
3201        _("display the title of the default kernel") },
3202   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
3203      _("configure elilo bootloader") },      _("configure elilo bootloader") },
3204   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
3205      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
3206   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
3207      _("configure grub bootloader") },      _("configure grub bootloader") },
3208     { "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,
3209        _("configure grub2 bootloader") },
3210   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,
3211      _("display boot information for specified kernel"),      _("display boot information for specified kernel"),
3212      _("kernel-path") },      _("kernel-path") },
# Line 2885  int main(int argc, const char ** argv) { Line 3286  int main(int argc, const char ** argv) {
3286   return 1;   return 1;
3287      }      }
3288    
3289      if ((configureLilo + configureGrub + configureELilo +      if ((configureLilo + configureGrub2 + configureGrub + configureELilo +
3290   configureYaboot + configureSilo + configureZipl +   configureYaboot + configureSilo + configureZipl +
3291   configureExtLinux ) > 1) {   configureExtLinux ) > 1) {
3292   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 3295  int main(int argc, const char ** argv) {
3295   fprintf(stderr,   fprintf(stderr,
3296      _("grubby: cannot specify config file with --bootloader-probe\n"));      _("grubby: cannot specify config file with --bootloader-probe\n"));
3297   return 1;   return 1;
3298        } else if (configureGrub2) {
3299     cfi = &grub2ConfigType;
3300      } else if (configureLilo) {      } else if (configureLilo) {
3301   cfi = &liloConfigType;   cfi = &liloConfigType;
3302      } else if (configureGrub) {      } else if (configureGrub) {
# Line 2923  int main(int argc, const char ** argv) { Line 3326  int main(int argc, const char ** argv) {
3326        #elif __s390x__        #elif __s390x__
3327          cfi = &ziplConfigtype;          cfi = &ziplConfigtype;
3328        #else        #else
3329   cfi = &grubConfigType;          if (grub2FindConfig(&grub2ConfigType))
3330        cfi = &grub2ConfigType;
3331     else
3332        cfi = &grubConfigType;
3333        #endif        #endif
3334      }      }
3335    
3336      if (!grubConfig)      if (!grubConfig) {
3337   grubConfig = cfi->defaultConfig;   if (cfi->findConfig)
3338        grubConfig = cfi->findConfig(cfi);
3339     if (!grubConfig)
3340        grubConfig = cfi->defaultConfig;
3341        }
3342    
3343      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3344    newKernelPath || removeKernelPath || makeDefault ||    newKernelPath || removeKernelPath || makeDefault ||
3345    defaultKernel)) {    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {
3346   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
3347    "specified option"));    "specified option"));
3348   return 1;   return 1;
# Line 2948  int main(int argc, const char ** argv) { Line 3358  int main(int argc, const char ** argv) {
3358      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
3359   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
3360   return 1;   return 1;
3361      } else if (!newKernelPath && (newKernelTitle  || newKernelInitrd ||      } else if (!newKernelPath && (newKernelTitle  || copyDefault ||
3362    newKernelInitrd || copyDefault     ||    (newKernelInitrd && !updateKernelPath)||
3363    makeDefault || extraInitrdCount > 0)) {    makeDefault || extraInitrdCount > 0)) {
3364   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
3365   return 1;   return 1;
# Line 2975  int main(int argc, const char ** argv) { Line 3385  int main(int argc, const char ** argv) {
3385   defaultKernel = NULL;   defaultKernel = NULL;
3386      }      }
3387    
3388      if (!strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
3389   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
3390   "is used\n"));   "is used\n"));
3391   return 1;   return 1;
# Line 2983  int main(int argc, const char ** argv) { Line 3393  int main(int argc, const char ** argv) {
3393    
3394      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
3395   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
3396          && !removeMBKernel) {          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {
3397   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
3398   return 1;   return 1;
3399      }      }
# Line 3010  int main(int argc, const char ** argv) { Line 3420  int main(int argc, const char ** argv) {
3420      }      }
3421    
3422      if (bootloaderProbe) {      if (bootloaderProbe) {
3423   int lrc = 0, grc = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, erc = 0;
3424   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig;
3425    
3426   if (!access(grubConfigType.defaultConfig, F_OK)) {   const char *grub2config = grub2FindConfig(&grub2ConfigType);
3427      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);   if (grub2config) {
3428        gconfig = readConfig(grub2config, &grub2ConfigType);
3429        if (!gconfig)
3430     gr2c = 1;
3431        else
3432     gr2c = checkForGrub2(gconfig);
3433     }
3434    
3435     const char *grubconfig = grubFindConfig(&grubConfigType);
3436     if (!access(grubconfig, F_OK)) {
3437        gconfig = readConfig(grubconfig, &grubConfigType);
3438      if (!gconfig)      if (!gconfig)
3439   grc = 1;   grc = 1;
3440      else      else
# Line 3037  int main(int argc, const char ** argv) { Line 3457  int main(int argc, const char ** argv) {
3457   erc = checkForExtLinux(lconfig);   erc = checkForExtLinux(lconfig);
3458   }   }
3459    
3460   if (lrc == 1 || grc == 1) return 1;   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;
3461    
3462   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
3463     if (gr2c == 2) printf("grub2\n");
3464   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
3465   if (erc == 2) printf("extlinux\n");   if (erc == 2) printf("extlinux\n");
3466    
# Line 3067  int main(int argc, const char ** argv) { Line 3488  int main(int argc, const char ** argv) {
3488                 ((rootspec != NULL) ? strlen(rootspec) : 0));                 ((rootspec != NULL) ? strlen(rootspec) : 0));
3489    
3490   return 0;   return 0;
3491    
3492        } else if (displayDefaultTitle) {
3493     struct singleLine * line;
3494     struct singleEntry * entry;
3495    
3496     if (config->defaultImage == -1) return 0;
3497     entry = findEntryByIndex(config, config->defaultImage);
3498     if (!entry) return 0;
3499    
3500     if (!configureGrub2) {
3501      line = getLineByType(LT_TITLE, entry->lines);
3502      if (!line) return 0;
3503      printf("%s\n", line->elements[1].item);
3504    
3505     } else {
3506      int i;
3507      size_t len;
3508      char * start;
3509      char * tmp;
3510    
3511      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
3512      line = getLineByType(LT_MENUENTRY, entry->lines);
3513      if (!line) return 0;
3514    
3515      for (i = 0; i < line->numElements; i++) {
3516    
3517        if (!strcmp(line->elements[i].item, "menuentry"))
3518          continue;
3519    
3520        if (*line->elements[i].item == '\'')
3521          start = line->elements[i].item + 1;
3522        else
3523          start = line->elements[i].item;
3524    
3525        len = strlen(start);
3526        if (*(start + len - 1) == '\'') {
3527          tmp = strdup(start);
3528          *(tmp + len - 1) = '\0';
3529          printf("%s", tmp);
3530          free(tmp);
3531          break;
3532        } else {
3533          printf("%s ", start);
3534        }
3535      }
3536      printf("\n");
3537     }
3538     return 0;
3539    
3540        } else if (displayDefaultIndex) {
3541            if (config->defaultImage == -1) return 0;
3542            printf("%i\n", config->defaultImage);
3543    
3544      } else if (kernelInfo)      } else if (kernelInfo)
3545   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
3546    
# Line 3082  int main(int argc, const char ** argv) { Line 3556  int main(int argc, const char ** argv) {
3556      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
3557      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
3558                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
3559        if (updateKernelPath && newKernelInitrd) {
3560                if (updateInitrd(config, updateKernelPath, bootPrefix,
3561                                 newKernelInitrd)) return 1;
3562        }
3563      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
3564                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
3565                       extraInitrds, extraInitrdCount,                       extraInitrds, extraInitrdCount,
# Line 3095  int main(int argc, const char ** argv) { Line 3573  int main(int argc, const char ** argv) {
3573      }      }
3574    
3575      if (!outputFile)      if (!outputFile)
3576   outputFile = grubConfig;   outputFile = (char *)grubConfig;
3577    
3578      return writeConfig(config, outputFile, bootPrefix);      return writeConfig(config, outputFile, bootPrefix);
3579  }  }

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