Magellan Linux

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

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

revision 1692 by niro, Fri Feb 17 23:14:54 2012 UTC revision 1751 by niro, Sat Feb 18 01:10:10 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     "/etc/grub.conf",
168     "/boot/grub/grub.conf",
169     "/boot/grub/menu.lst",
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      .defaultConfig = "/boot/grub/grub.conf",      .findConfig = grubFindConfig,
189      .keywords = grubKeywords,      .keywords = grubKeywords,
190      .defaultIsIndex = 1,      .defaultIsIndex = 1,
191      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
192      .entrySeparator = LT_TITLE,      .entryStart = LT_TITLE,
193        .needsBootPrefix = 1,
194        .mbHyperFirst = 1,
195        .mbInitRdIsModule = 1,
196        .mbAllowExtraInitRds = 1,
197    };
198    
199    struct keywordTypes grub2Keywords[] = {
200        { "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 i;
247      int count = 0;
248    
249      for (i = 0; i < line->numElements; i++) {
250        int indentSize = 0;
251    
252        count = count + strlen(line->elements[i].item);
253    
254        indentSize = strlen(line->elements[i].indent);
255        if (indentSize > 0)
256          count = count + indentSize;
257        else
258          /* be extra safe and add room for whitespaces */
259          count = count + 1;
260      }
261    
262      /* room for trailing terminator */
263      count = count + 1;
264    
265      return count;
266    }
267    
268    char *grub2ExtractTitle(struct singleLine * line) {
269        char * current;
270        char * current_indent;
271        int current_len;
272        int current_indent_len;
273        int i;
274    
275        /* bail out if line does not start with menuentry */
276        if (strcmp(line->elements[0].item, "menuentry"))
277          return NULL;
278    
279        i = 1;
280        current = line->elements[i].item;
281        current_len = strlen(current);
282    
283        /* if second word is quoted, strip the quotes and return single word */
284        if ((*current == '\'') && (*(current + current_len - 1) == '\'')) {
285          char *tmp;
286    
287          tmp = strdup(current);
288          *(tmp + current_len - 1) = '\0';
289          return ++tmp;
290        }
291    
292        /* if no quotes, return second word verbatim */
293        if (*current != '\'') {
294          return current;
295        }
296    
297        /* second element start with a quote, so we have to find the element
298         * whose last character is also quote (assuming it's the closing one) */
299        if (*current == '\'') {
300          int resultMaxSize;
301          char * result;
302    
303          resultMaxSize = sizeOfSingleLine(line);
304          result = malloc(resultMaxSize);
305          snprintf(result, resultMaxSize, "%s", ++current);
306    
307          i++;
308          for (; i < line->numElements; ++i) {
309     current = line->elements[i].item;
310     current_len = strlen(current);
311     current_indent = line->elements[i].indent;
312     current_indent_len = strlen(current_indent);
313    
314     strncat(result, current_indent, current_indent_len);
315     if (*(current + current_len - 1) != '\'') {
316      strncat(result, current, current_len);
317     } else {
318      strncat(result, current, current_len - 1);
319      break;
320     }
321          }
322          return result;
323        }
324    
325        return NULL;
326    }
327    
328    struct configFileInfo grub2ConfigType = {
329        .findConfig = grub2FindConfig,
330        .keywords = grub2Keywords,
331        .defaultIsIndex = 1,
332        .defaultSupportSaved = 1,
333        .defaultIsVariable = 1,
334        .entryStart = LT_MENUENTRY,
335        .entryEnd = LT_ENTRY_END,
336        .titlePosition = 1,
337      .needsBootPrefix = 1,      .needsBootPrefix = 1,
338      .mbHyperFirst = 1,      .mbHyperFirst = 1,
339      .mbInitRdIsModule = 1,      .mbInitRdIsModule = 1,
# Line 247  int useextlinuxmenu; Line 437  int useextlinuxmenu;
437  struct configFileInfo eliloConfigType = {  struct configFileInfo eliloConfigType = {
438      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",
439      .keywords = eliloKeywords,      .keywords = eliloKeywords,
440      .entrySeparator = LT_KERNEL,      .entryStart = LT_KERNEL,
441      .needsBootPrefix = 1,      .needsBootPrefix = 1,
442      .argsInQuotes = 1,      .argsInQuotes = 1,
443      .mbConcatArgs = 1,      .mbConcatArgs = 1,
# Line 256  struct configFileInfo eliloConfigType = Line 446  struct configFileInfo eliloConfigType =
446  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
447      .defaultConfig = "/etc/lilo.conf",      .defaultConfig = "/etc/lilo.conf",
448      .keywords = liloKeywords,      .keywords = liloKeywords,
449      .entrySeparator = LT_KERNEL,      .entryStart = LT_KERNEL,
450      .argsInQuotes = 1,      .argsInQuotes = 1,
451      .maxTitleLength = 15,      .maxTitleLength = 15,
452  };  };
# Line 264  struct configFileInfo liloConfigType = { Line 454  struct configFileInfo liloConfigType = {
454  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
455      .defaultConfig = "/etc/yaboot.conf",      .defaultConfig = "/etc/yaboot.conf",
456      .keywords = yabootKeywords,      .keywords = yabootKeywords,
457      .entrySeparator = LT_KERNEL,      .entryStart = LT_KERNEL,
458      .needsBootPrefix = 1,      .needsBootPrefix = 1,
459      .argsInQuotes = 1,      .argsInQuotes = 1,
460      .maxTitleLength = 15,      .maxTitleLength = 15,
# Line 274  struct configFileInfo yabootConfigType = Line 464  struct configFileInfo yabootConfigType =
464  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
465      .defaultConfig = "/etc/silo.conf",      .defaultConfig = "/etc/silo.conf",
466      .keywords = siloKeywords,      .keywords = siloKeywords,
467      .entrySeparator = LT_KERNEL,      .entryStart = LT_KERNEL,
468      .needsBootPrefix = 1,      .needsBootPrefix = 1,
469      .argsInQuotes = 1,      .argsInQuotes = 1,
470      .maxTitleLength = 15,      .maxTitleLength = 15,
# Line 283  struct configFileInfo siloConfigType = { Line 473  struct configFileInfo siloConfigType = {
473  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
474      .defaultConfig = "/etc/zipl.conf",      .defaultConfig = "/etc/zipl.conf",
475      .keywords = ziplKeywords,      .keywords = ziplKeywords,
476      .entrySeparator = LT_TITLE,      .entryStart = LT_TITLE,
477      .argsInQuotes = 1,      .argsInQuotes = 1,
478      .titleBracketed = 1,      .titleBracketed = 1,
479  };  };
# Line 291  struct configFileInfo ziplConfigType = { Line 481  struct configFileInfo ziplConfigType = {
481  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
482      .defaultConfig = "/boot/extlinux/extlinux.conf",      .defaultConfig = "/boot/extlinux/extlinux.conf",
483      .keywords = extlinuxKeywords,      .keywords = extlinuxKeywords,
484      .entrySeparator = LT_TITLE,      .entryStart = LT_TITLE,
485      .needsBootPrefix = 1,      .needsBootPrefix = 1,
486      .maxTitleLength = 255,      .maxTitleLength = 255,
487      .mbAllowExtraInitRds = 1,      .mbAllowExtraInitRds = 1,
# Line 324  static int lineWrite(FILE * out, struct Line 514  static int lineWrite(FILE * out, struct
514  static int getNextLine(char ** bufPtr, struct singleLine * line,  static int getNextLine(char ** bufPtr, struct singleLine * line,
515         struct configFileInfo * cfi);         struct configFileInfo * cfi);
516  static char * getRootSpecifier(char * str);  static char * getRootSpecifier(char * str);
517    static void requote(struct singleLine *line, struct configFileInfo * cfi);
518  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
519    const char * item, int insertHere,    const char * item, int insertHere,
520    struct configFileInfo * cfi);    struct configFileInfo * cfi);
# Line 388  static struct keywordTypes * getKeywordB Line 579  static struct keywordTypes * getKeywordB
579      return NULL;      return NULL;
580  }  }
581    
582    static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) {
583        struct keywordTypes *kt = getKeywordByType(type, cfi);
584        if (kt)
585     return kt->key;
586        return "unknown";
587    }
588    
589  static char * getpathbyspec(char *device) {  static char * getpathbyspec(char *device) {
590      if (!blkid)      if (!blkid)
591          blkid_get_cache(&blkid, NULL);          blkid_get_cache(&blkid, NULL);
# Line 437  static int isBracketedTitle(struct singl Line 635  static int isBracketedTitle(struct singl
635      return 0;      return 0;
636  }  }
637    
638  static int isEntrySeparator(struct singleLine * line,  static int isEntryStart(struct singleLine * line,
639                              struct configFileInfo * cfi) {                              struct configFileInfo * cfi) {
640      return line->type == cfi->entrySeparator || line->type == LT_OTHER ||      return line->type == cfi->entryStart || line->type == LT_OTHER ||
641   (cfi->titleBracketed && isBracketedTitle(line));   (cfi->titleBracketed && isBracketedTitle(line));
642  }  }
643    
# Line 762  static struct grubConfig * readConfig(co Line 960  static struct grubConfig * readConfig(co
960      cfg->secondaryIndent = strdup(line->indent);      cfg->secondaryIndent = strdup(line->indent);
961   }   }
962    
963   if (isEntrySeparator(line, cfi)) {   if (isEntryStart(line, cfi) || (cfg->entries && !sawEntry)) {
964      sawEntry = 1;      sawEntry = 1;
965      if (!entry) {      if (!entry) {
966   cfg->entries = malloc(sizeof(*entry));   cfg->entries = malloc(sizeof(*entry));
# Line 778  static struct grubConfig * readConfig(co Line 976  static struct grubConfig * readConfig(co
976      entry->next = NULL;      entry->next = NULL;
977   }   }
978    
979   if (line->type == LT_DEFAULT && line->numElements == 2) {   if (line->type == LT_SET_VARIABLE) {
980        int i;
981        dbgPrintf("found 'set' command (%d elements): ", line->numElements);
982        dbgPrintf("%s", line->indent);
983        for (i = 0; i < line->numElements; i++)
984     dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item);
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 891  static struct grubConfig * readConfig(co Line 1103  static struct grubConfig * readConfig(co
1103   entry->lines = line;   entry->lines = line;
1104      else      else
1105   last->next = line;   last->next = line;
1106      dbgPrintf("readConfig added %d to %p\n", line->type, entry);      dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry);
1107    
1108        /* we could have seen this outside of an entry... if so, we
1109         * ignore it like any other line we don't grok */
1110        if (line->type == LT_ENTRY_END && sawEntry)
1111     sawEntry = 0;
1112   } else {   } else {
1113      if (!cfg->theLines)      if (!cfg->theLines)
1114   cfg->theLines = line;   cfg->theLines = line;
1115      else      else
1116   last->next = line;   last->next = line;
1117      dbgPrintf("readConfig added %d to cfg\n", line->type);      dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi));
1118   }   }
1119    
1120   last = line;   last = line;
# Line 905  static struct grubConfig * readConfig(co Line 1122  static struct grubConfig * readConfig(co
1122    
1123      free(incoming);      free(incoming);
1124    
1125        dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1126      if (defaultLine) {      if (defaultLine) {
1127   if (cfi->defaultSupportSaved &&          if (defaultLine->numElements > 2 &&
1128        cfi->defaultSupportSaved &&
1129        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1130        cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1131     } else if (cfi->defaultIsVariable) {
1132        char *value = defaultLine->elements[2].item;
1133        while (*value && (*value == '"' || *value == '\'' ||
1134        *value == ' ' || *value == '\t'))
1135     value++;
1136        cfg->defaultImage = strtol(value, &end, 10);
1137        while (*end && (*end == '"' || *end == '\'' ||
1138        *end == ' ' || *end == '\t'))
1139     end++;
1140        if (*end) cfg->defaultImage = -1;
1141     } else if (cfi->defaultSupportSaved &&
1142   !strncmp(defaultLine->elements[1].item, "saved", 5)) {   !strncmp(defaultLine->elements[1].item, "saved", 5)) {
1143      cfg->defaultImage = DEFAULT_SAVED;      cfg->defaultImage = DEFAULT_SAVED;
1144   } else if (cfi->defaultIsIndex) {   } else if (cfi->defaultIsIndex) {
# Line 953  static void writeDefault(FILE * out, cha Line 1185  static void writeDefault(FILE * out, cha
1185    
1186      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1187   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1188        else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)
1189     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1190      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1191   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1192      fprintf(out, "%sdefault%s%d\n", indent, separator,      if (cfg->cfi->defaultIsVariable) {
1193      cfg->defaultImage);          fprintf(out, "%sset default=\"%d\"\n", indent,
1194     cfg->defaultImage);
1195        } else {
1196     fprintf(out, "%sdefault%s%d\n", indent, separator,
1197     cfg->defaultImage);
1198        }
1199   } else {   } else {
1200      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1201    
# Line 1046  static int writeConfig(struct grubConfig Line 1285  static int writeConfig(struct grubConfig
1285      }      }
1286    
1287      line = cfg->theLines;      line = cfg->theLines;
1288        struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);
1289      while (line) {      while (line) {
1290   if (line->type == LT_DEFAULT) {          if (line->type == LT_SET_VARIABLE && defaultKw &&
1291     line->numElements == 3 &&
1292     !strcmp(line->elements[1].item, defaultKw->key)) {
1293        writeDefault(out, line->indent, line->elements[0].indent, cfg);
1294        needs &= ~MAIN_DEFAULT;
1295     } else if (line->type == LT_DEFAULT) {
1296      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1297      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1298   } else if (line->type == LT_FALLBACK) {   } else if (line->type == LT_FALLBACK) {
# Line 1185  static char *findDiskForRoot() Line 1430  static char *findDiskForRoot()
1430      return NULL;      return NULL;
1431  }  }
1432    
1433    void printEntry(struct singleEntry * entry) {
1434        int i;
1435        struct singleLine * line;
1436    
1437        for (line = entry->lines; line; line = line->next) {
1438     fprintf(stderr, "DBG: %s", line->indent);
1439     for (i = 0; i < line->numElements; i++) {
1440     fprintf(stderr, "%s%s",
1441        line->elements[i].item, line->elements[i].indent);
1442     }
1443     fprintf(stderr, "\n");
1444        }
1445    }
1446    
1447    void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)
1448    {
1449        va_list argp;
1450    
1451        if (!debug)
1452     return;
1453    
1454        va_start(argp, fmt);
1455        fprintf(stderr, "DBG: Image entry failed: ");
1456        vfprintf(stderr, fmt, argp);
1457        printEntry(entry);
1458        va_end(argp);
1459    }
1460    
1461    #define beginswith(s, c) ((s) && (s)[0] == (c))
1462    
1463    static int endswith(const char *s, char c)
1464    {
1465     int slen;
1466    
1467     if (!s || !s[0])
1468     return 0;
1469     slen = strlen(s) - 1;
1470    
1471     return s[slen] == c;
1472    }
1473    
1474  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1475    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1476      struct singleLine * line;      struct singleLine * line;
# Line 1194  int suitableImage(struct singleEntry * e Line 1480  int suitableImage(struct singleEntry * e
1480      char * rootspec;      char * rootspec;
1481      char * rootdev;      char * rootdev;
1482    
1483      if (skipRemoved && entry->skip) return 0;      if (skipRemoved && entry->skip) {
1484     notSuitablePrintf(entry, "marked to skip\n");
1485     return 0;
1486        }
1487    
1488      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);
1489      if (!line || line->numElements < 2) return 0;      if (!line) {
1490     notSuitablePrintf(entry, "no line found\n");
1491     return 0;
1492        }
1493        if (line->numElements < 2) {
1494     notSuitablePrintf(entry, "line has only %d elements\n",
1495        line->numElements);
1496     return 0;
1497        }
1498    
1499      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;
1500    
1501      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1502        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
1503      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1504      sprintf(fullName, "%s%s", bootPrefix,      int rootspec_offset = rootspec ? strlen(rootspec) : 0;
1505              line->elements[1].item + (rootspec ? strlen(rootspec) : 0));      int hasslash = endswith(bootPrefix, '/') ||
1506      if (access(fullName, R_OK)) return 0;       beginswith(line->elements[1].item + rootspec_offset, '/');
1507        sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1508                line->elements[1].item + rootspec_offset);
1509        if (access(fullName, R_OK)) {
1510     notSuitablePrintf(entry, "access to %s failed\n", fullName);
1511     return 0;
1512        }
1513      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
1514   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1515      if (i < line->numElements) {      if (i < line->numElements) {
# Line 1225  int suitableImage(struct singleEntry * e Line 1527  int suitableImage(struct singleEntry * e
1527      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);
1528    
1529              /* failed to find one */              /* failed to find one */
1530              if (!line) return 0;              if (!line) {
1531     notSuitablePrintf(entry, "no line found\n");
1532     return 0;
1533                }
1534    
1535      for (i = 1; i < line->numElements; i++)      for (i = 1; i < line->numElements; i++)
1536          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1537      if (i < line->numElements)      if (i < line->numElements)
1538          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1539      else {      else {
1540     notSuitablePrintf(entry, "no root= entry found\n");
1541   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1542          return 0;          return 0;
1543              }              }
# Line 1239  int suitableImage(struct singleEntry * e Line 1545  int suitableImage(struct singleEntry * e
1545      }      }
1546    
1547      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1548      if (!dev)      if (!getpathbyspec(dev)) {
1549            notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);
1550          return 0;          return 0;
1551        } else
1552     dev = getpathbyspec(dev);
1553    
1554      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1555      if (!rootdev)      if (!rootdev) {
1556            notSuitablePrintf(entry, "can't find root device\n");
1557   return 0;   return 0;
1558        }
1559    
1560      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1561            notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",
1562     getuuidbydev(rootdev), getuuidbydev(dev));
1563          free(rootdev);          free(rootdev);
1564          return 0;          return 0;
1565      }      }
1566    
1567      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1568            notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",
1569     getuuidbydev(rootdev), getuuidbydev(dev));
1570   free(rootdev);   free(rootdev);
1571          return 0;          return 0;
1572      }      }
# Line 1479  void setDefaultImage(struct grubConfig * Line 1794  void setDefaultImage(struct grubConfig *
1794    
1795      /* 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
1796         changes */         changes */
1797      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
1798     (config->defaultImage == DEFAULT_SAVED_GRUB2))
1799        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
1800        return;        return;
1801    
# Line 1546  void displayEntry(struct singleEntry * e Line 1862  void displayEntry(struct singleEntry * e
1862          return;          return;
1863      }      }
1864    
1865      printf("kernel=%s\n", line->elements[1].item);      printf("kernel=%s%s\n", prefix, line->elements[1].item);
1866    
1867      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1868   printf("args=\"");   printf("args=\"");
# Line 1610  void displayEntry(struct singleEntry * e Line 1926  void displayEntry(struct singleEntry * e
1926      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
1927   printf("\n");   printf("\n");
1928      }      }
1929    
1930        line = getLineByType(LT_TITLE, entry->lines);
1931        if (line) {
1932     printf("title=%s\n", line->elements[1].item);
1933        } else {
1934     char * title;
1935     line = getLineByType(LT_MENUENTRY, entry->lines);
1936     title = grub2ExtractTitle(line);
1937     if (title)
1938        printf("title=%s\n", title);
1939        }
1940  }  }
1941    
1942  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 1770  struct singleLine *  addLine(struct sing Line 2097  struct singleLine *  addLine(struct sing
2097   sprintf(tmpl.elements[0].item, "[%s]", val);   sprintf(tmpl.elements[0].item, "[%s]", val);
2098   tmpl.elements[0].indent = "";   tmpl.elements[0].indent = "";
2099   val = NULL;   val = NULL;
2100        } else if (type == LT_MENUENTRY) {
2101     char *lineend = "--class gnu-linux --class gnu --class os {";
2102     if (!val) {
2103        fprintf(stderr, "Line type LT_MENUENTRY requires a value\n");
2104        abort();
2105     }
2106     kw = getKeywordByType(type, cfi);
2107     if (!kw) {
2108        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
2109        abort();
2110     }
2111     tmpl.indent = "";
2112     tmpl.type = type;
2113     tmpl.numElements = 3;
2114     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
2115     tmpl.elements[0].item = kw->key;
2116     tmpl.elements[0].indent = alloca(2);
2117     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
2118     tmpl.elements[1].item = (char *)val;
2119     tmpl.elements[1].indent = alloca(2);
2120     sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);
2121     tmpl.elements[2].item = alloca(strlen(lineend)+1);
2122     strcpy(tmpl.elements[2].item, lineend);
2123     tmpl.elements[2].indent = "";
2124      } else {      } else {
2125   kw = getKeywordByType(type, cfi);   kw = getKeywordByType(type, cfi);
2126   if (!kw) abort();   if (!kw) {
2127        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
2128        abort();
2129     }
2130   tmpl.type = type;   tmpl.type = type;
2131   tmpl.numElements = val ? 2 : 1;   tmpl.numElements = val ? 2 : 1;
2132   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
# Line 1796  struct singleLine *  addLine(struct sing Line 2150  struct singleLine *  addLine(struct sing
2150   if (!line->next && !prev) prev = line;   if (!line->next && !prev) prev = line;
2151      }      }
2152    
2153      if (prev == entry->lines)      struct singleLine *menuEntry;
2154   tmpl.indent = defaultIndent ?: "";      menuEntry = getLineByType(LT_MENUENTRY, entry->lines);
2155      else      if (tmpl.type == LT_ENTRY_END) {
2156   tmpl.indent = prev->indent;   if (menuEntry)
2157        tmpl.indent = menuEntry->indent;
2158     else
2159        tmpl.indent = defaultIndent ?: "";
2160        } else if (tmpl.type != LT_MENUENTRY) {
2161     if (menuEntry)
2162        tmpl.indent = "\t";
2163     else if (prev == entry->lines)
2164        tmpl.indent = defaultIndent ?: "";
2165     else
2166        tmpl.indent = prev->indent;
2167        }
2168    
2169      return addLineTmpl(entry, &tmpl, prev, val, cfi);      return addLineTmpl(entry, &tmpl, prev, val, cfi);
2170  }  }
# Line 1826  void removeLine(struct singleEntry * ent Line 2191  void removeLine(struct singleEntry * ent
2191      free(line);      free(line);
2192  }  }
2193    
2194    static int isquote(char q)
2195    {
2196        if (q == '\'' || q == '\"')
2197     return 1;
2198        return 0;
2199    }
2200    
2201    static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2202    {
2203        struct singleLine newLine = {
2204     .indent = tmplLine->indent,
2205     .type = tmplLine->type,
2206     .next = tmplLine->next,
2207        };
2208        int firstQuotedItem = -1;
2209        int quoteLen = 0;
2210        int j;
2211        int element = 0;
2212        char *c;
2213    
2214        c = malloc(strlen(tmplLine->elements[0].item) + 1);
2215        strcpy(c, tmplLine->elements[0].item);
2216        insertElement(&newLine, c, element++, cfi);
2217        free(c);
2218        c = NULL;
2219    
2220        for (j = 1; j < tmplLine->numElements; j++) {
2221     if (firstQuotedItem == -1) {
2222        quoteLen += strlen(tmplLine->elements[j].item);
2223        
2224        if (isquote(tmplLine->elements[j].item[0])) {
2225     firstQuotedItem = j;
2226            quoteLen += strlen(tmplLine->elements[j].indent);
2227        } else {
2228     c = malloc(quoteLen + 1);
2229     strcpy(c, tmplLine->elements[j].item);
2230     insertElement(&newLine, c, element++, cfi);
2231     free(c);
2232     quoteLen = 0;
2233        }
2234     } else {
2235        int itemlen = strlen(tmplLine->elements[j].item);
2236        quoteLen += itemlen;
2237        quoteLen += strlen(tmplLine->elements[j].indent);
2238        
2239        if (isquote(tmplLine->elements[j].item[itemlen - 1])) {
2240     c = malloc(quoteLen + 1);
2241     c[0] = '\0';
2242     for (int i = firstQuotedItem; i < j+1; i++) {
2243        strcat(c, tmplLine->elements[i].item);
2244        strcat(c, tmplLine->elements[i].indent);
2245     }
2246     insertElement(&newLine, c, element++, cfi);
2247     free(c);
2248    
2249     firstQuotedItem = -1;
2250     quoteLen = 0;
2251        }
2252     }
2253        }
2254        while (tmplLine->numElements)
2255     removeElement(tmplLine, 0);
2256        if (tmplLine->elements)
2257     free(tmplLine->elements);
2258    
2259        tmplLine->numElements = newLine.numElements;
2260        tmplLine->elements = newLine.elements;
2261    }
2262    
2263  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
2264    const char * item, int insertHere,    const char * item, int insertHere,
2265    struct configFileInfo * cfi)    struct configFileInfo * cfi)
# Line 2153  int updateImage(struct grubConfig * cfg, Line 2587  int updateImage(struct grubConfig * cfg,
2587  int updateInitrd(struct grubConfig * cfg, const char * image,  int updateInitrd(struct grubConfig * cfg, const char * image,
2588                   const char * prefix, const char * initrd) {                   const char * prefix, const char * initrd) {
2589      struct singleEntry * entry;      struct singleEntry * entry;
2590      struct singleLine * line, * kernelLine;      struct singleLine * line, * kernelLine, *endLine = NULL;
2591      int index = 0;      int index = 0;
2592    
2593      if (!image) return 0;      if (!image) return 0;
# Line 2170  int updateInitrd(struct grubConfig * cfg Line 2604  int updateInitrd(struct grubConfig * cfg
2604              if (!strncmp(initrd, prefix, prefixLen))              if (!strncmp(initrd, prefix, prefixLen))
2605                  initrd += prefixLen;                  initrd += prefixLen;
2606          }          }
2607     endLine = getLineByType(LT_ENTRY_END, entry->lines);
2608     if (endLine)
2609        removeLine(entry, endLine);
2610          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);
2611          if (!line) return 1;          if (!line)
2612        return 1;
2613     if (endLine) {
2614        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
2615                if (!line)
2616     return 1;
2617     }
2618    
2619          break;          break;
2620      }      }
2621    
# Line 2201  int checkDeviceBootloader(const char * d Line 2645  int checkDeviceBootloader(const char * d
2645      if (memcmp(boot, bootSect, 3))      if (memcmp(boot, bootSect, 3))
2646   return 0;   return 0;
2647    
2648      if (boot[1] == 0xeb) {      if (boot[1] == JMP_SHORT_OPCODE) {
2649   offset = boot[2] + 2;   offset = boot[2] + 2;
2650      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {
2651   offset = (boot[3] << 8) + boot[2] + 2;   offset = (boot[3] << 8) + boot[2] + 2;
2652      } else if (boot[0] == 0xeb) {      } else if (boot[0] == JMP_SHORT_OPCODE) {
2653   offset = boot[1] + 2;        offset = boot[1] + 2;
2654            /*
2655     * it looks like grub, when copying stage1 into the mbr, patches stage1
2656     * right after the JMP location, replacing other instructions such as
2657     * JMPs for NOOPs. So, relax the check a little bit by skipping those
2658     * different bytes.
2659     */
2660          if ((bootSect[offset + 1] == NOOP_OPCODE)
2661      && (bootSect[offset + 2] == NOOP_OPCODE)) {
2662     offset = offset + 3;
2663          }
2664      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {
2665   offset = (boot[2] << 8) + boot[1] + 2;   offset = (boot[2] << 8) + boot[1] + 2;
2666      } else {      } else {
# Line 2348  int checkForLilo(struct grubConfig * con Line 2802  int checkForLilo(struct grubConfig * con
2802      return checkDeviceBootloader(line->elements[1].item, boot);      return checkDeviceBootloader(line->elements[1].item, boot);
2803  }  }
2804    
2805    int checkForGrub2(struct grubConfig * config) {
2806        if (!access("/etc/grub.d/", R_OK))
2807     return 2;
2808    
2809        return 1;
2810    }
2811    
2812  int checkForGrub(struct grubConfig * config) {  int checkForGrub(struct grubConfig * config) {
2813      int fd;      int fd;
2814      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 2523  int addNewKernel(struct grubConfig * con Line 2984  int addNewKernel(struct grubConfig * con
2984      if (*chptr == '#') continue;      if (*chptr == '#') continue;
2985    
2986      if (tmplLine->type == LT_KERNEL &&      if (tmplLine->type == LT_KERNEL &&
2987   tmplLine->numElements >= 2) {      tmplLine->numElements >= 2) {
2988   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
2989      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
2990       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 2649  int addNewKernel(struct grubConfig * con Line 3110  int addNewKernel(struct grubConfig * con
3110      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3111   }   }
3112    
3113        } else if (tmplLine->type == LT_MENUENTRY &&
3114           (needs & NEED_TITLE)) {
3115     requote(tmplLine, config->cfi);
3116     char *nkt = malloc(strlen(newKernelTitle)+3);
3117     strcpy(nkt, "'");
3118     strcat(nkt, newKernelTitle);
3119     strcat(nkt, "'");
3120     newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi);
3121     free(nkt);
3122     needs &= ~NEED_TITLE;
3123      } else if (tmplLine->type == LT_TITLE &&      } else if (tmplLine->type == LT_TITLE &&
3124         (needs & NEED_TITLE)) {         (needs & NEED_TITLE)) {
3125   if (tmplLine->numElements >= 2) {   if (tmplLine->numElements >= 2) {
# Line 2662  int addNewKernel(struct grubConfig * con Line 3133  int addNewKernel(struct grubConfig * con
3133        tmplLine->indent, newKernelTitle);        tmplLine->indent, newKernelTitle);
3134      needs &= ~NEED_TITLE;      needs &= ~NEED_TITLE;
3135   }   }
3136        } else if (tmplLine->type == LT_ECHO) {
3137        requote(tmplLine, config->cfi);
3138        static const char *prefix = "'Loading ";
3139        if (tmplLine->numElements > 1 &&
3140        strstr(tmplLine->elements[1].item, prefix) &&
3141        masterLine->next && masterLine->next->type == LT_KERNEL) {
3142     char *newTitle = malloc(strlen(prefix) +
3143     strlen(newKernelTitle) + 2);
3144    
3145     strcpy(newTitle, prefix);
3146     strcat(newTitle, newKernelTitle);
3147     strcat(newTitle, "'");
3148     newLine = addLine(new, config->cfi, LT_ECHO,
3149     tmplLine->indent, newTitle);
3150     free(newTitle);
3151        } else {
3152     /* pass through other lines from the template */
3153     newLine = addLineTmpl(new, tmplLine, newLine, NULL,
3154     config->cfi);
3155        }
3156      } else {      } else {
3157   /* pass through other lines from the template */   /* pass through other lines from the template */
3158   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
# Line 2673  int addNewKernel(struct grubConfig * con Line 3163  int addNewKernel(struct grubConfig * con
3163   /* don't have a template, so start the entry with the   /* don't have a template, so start the entry with the
3164   * appropriate starting line   * appropriate starting line
3165   */   */
3166   switch (config->cfi->entrySeparator) {   switch (config->cfi->entryStart) {
3167      case LT_KERNEL:      case LT_KERNEL:
3168   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
3169      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
# Line 2692  int addNewKernel(struct grubConfig * con Line 3182  int addNewKernel(struct grubConfig * con
3182   needs &= ~NEED_MB;   needs &= ~NEED_MB;
3183   break;   break;
3184    
3185        case LT_MENUENTRY: {
3186     char *nkt = malloc(strlen(newKernelTitle)+3);
3187     strcpy(nkt, "'");
3188     strcat(nkt, newKernelTitle);
3189     strcat(nkt, "'");
3190            newLine = addLine(new, config->cfi, LT_MENUENTRY,
3191      config->primaryIndent, nkt);
3192     free(nkt);
3193     needs &= ~NEED_TITLE;
3194     needs |= NEED_END;
3195     break;
3196        }
3197      case LT_TITLE:      case LT_TITLE:
3198   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)
3199   char * templabel;   char * templabel;
# Line 2725  int addNewKernel(struct grubConfig * con Line 3227  int addNewKernel(struct grubConfig * con
3227    
3228      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
3229       * 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,
3230       * all the lines following the entrySeparator.       * all the lines following the entryStart.
3231       */       */
3232      if (needs & NEED_TITLE) {      if (needs & NEED_TITLE) {
3233   newLine = addLine(new, config->cfi, LT_TITLE,   newLine = addLine(new, config->cfi, LT_TITLE,
# Line 2766  int addNewKernel(struct grubConfig * con Line 3268  int addNewKernel(struct grubConfig * con
3268   free(initrdVal);   free(initrdVal);
3269   needs &= ~NEED_INITRD;   needs &= ~NEED_INITRD;
3270      }      }
3271        if (needs & NEED_END) {
3272     newLine = addLine(new, config->cfi, LT_ENTRY_END,
3273     config->secondaryIndent, NULL);
3274     needs &= ~NEED_END;
3275        }
3276    
3277      if (needs) {      if (needs) {
3278   printf(_("grubby: needs=%d, aborting\n"), needs);   printf(_("grubby: needs=%d, aborting\n"), needs);
# Line 2795  static void traceback(int signum) Line 3302  static void traceback(int signum)
3302    
3303  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
3304      poptContext optCon;      poptContext optCon;
3305      char * grubConfig = NULL;      const char * grubConfig = NULL;
3306      char * outputFile = NULL;      char * outputFile = NULL;
3307      int arg = 0;      int arg = 0;
3308      int flags = 0;      int flags = 0;
3309      int badImageOkay = 0;      int badImageOkay = 0;
3310        int configureGrub2 = 0;
3311      int configureLilo = 0, configureELilo = 0, configureGrub = 0;      int configureLilo = 0, configureELilo = 0, configureGrub = 0;
3312      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;
3313      int configureExtLinux = 0;      int configureExtLinux = 0;
# Line 2827  int main(int argc, const char ** argv) { Line 3335  int main(int argc, const char ** argv) {
3335      struct singleEntry * template = NULL;      struct singleEntry * template = NULL;
3336      int copyDefault = 0, makeDefault = 0;      int copyDefault = 0, makeDefault = 0;
3337      int displayDefault = 0;      int displayDefault = 0;
3338        int displayDefaultIndex = 0;
3339        int displayDefaultTitle = 0;
3340      struct poptOption options[] = {      struct poptOption options[] = {
3341   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
3342      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 2857  int main(int argc, const char ** argv) { Line 3367  int main(int argc, const char ** argv) {
3367        "the kernel referenced by the default image does not exist, "        "the kernel referenced by the default image does not exist, "
3368        "the first linux entry whose kernel does exist is used as the "        "the first linux entry whose kernel does exist is used as the "
3369        "template"), NULL },        "template"), NULL },
3370     { "debug", 0, 0, &debug, 0,
3371        _("print debugging information for failures") },
3372   { "default-kernel", 0, 0, &displayDefault, 0,   { "default-kernel", 0, 0, &displayDefault, 0,
3373      _("display the path of the default kernel") },      _("display the path of the default kernel") },
3374     { "default-index", 0, 0, &displayDefaultIndex, 0,
3375        _("display the index of the default kernel") },
3376     { "default-title", 0, 0, &displayDefaultTitle, 0,
3377        _("display the title of the default kernel") },
3378   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
3379      _("configure elilo bootloader") },      _("configure elilo bootloader") },
3380   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
3381      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
3382   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
3383      _("configure grub bootloader") },      _("configure grub bootloader") },
3384     { "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,
3385        _("configure grub2 bootloader") },
3386   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,
3387      _("display boot information for specified kernel"),      _("display boot information for specified kernel"),
3388      _("kernel-path") },      _("kernel-path") },
# Line 2944  int main(int argc, const char ** argv) { Line 3462  int main(int argc, const char ** argv) {
3462   return 1;   return 1;
3463      }      }
3464    
3465      if ((configureLilo + configureGrub + configureELilo +      if ((configureLilo + configureGrub2 + configureGrub + configureELilo +
3466   configureYaboot + configureSilo + configureZipl +   configureYaboot + configureSilo + configureZipl +
3467   configureExtLinux ) > 1) {   configureExtLinux ) > 1) {
3468   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));
# Line 2953  int main(int argc, const char ** argv) { Line 3471  int main(int argc, const char ** argv) {
3471   fprintf(stderr,   fprintf(stderr,
3472      _("grubby: cannot specify config file with --bootloader-probe\n"));      _("grubby: cannot specify config file with --bootloader-probe\n"));
3473   return 1;   return 1;
3474        } else if (configureGrub2) {
3475     cfi = &grub2ConfigType;
3476      } else if (configureLilo) {      } else if (configureLilo) {
3477   cfi = &liloConfigType;   cfi = &liloConfigType;
3478      } else if (configureGrub) {      } else if (configureGrub) {
# Line 2982  int main(int argc, const char ** argv) { Line 3502  int main(int argc, const char ** argv) {
3502        #elif __s390x__        #elif __s390x__
3503          cfi = &ziplConfigtype;          cfi = &ziplConfigtype;
3504        #else        #else
3505   cfi = &grubConfigType;          if (grub2FindConfig(&grub2ConfigType))
3506        cfi = &grub2ConfigType;
3507     else
3508        cfi = &grubConfigType;
3509        #endif        #endif
3510      }      }
3511    
3512      if (!grubConfig)      if (!grubConfig) {
3513   grubConfig = cfi->defaultConfig;   if (cfi->findConfig)
3514        grubConfig = cfi->findConfig(cfi);
3515     if (!grubConfig)
3516        grubConfig = cfi->defaultConfig;
3517        }
3518    
3519      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3520    newKernelPath || removeKernelPath || makeDefault ||    newKernelPath || removeKernelPath || makeDefault ||
3521    defaultKernel)) {    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {
3522   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
3523    "specified option"));    "specified option"));
3524   return 1;   return 1;
# Line 3034  int main(int argc, const char ** argv) { Line 3561  int main(int argc, const char ** argv) {
3561   defaultKernel = NULL;   defaultKernel = NULL;
3562      }      }
3563    
3564      if (!strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
3565   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
3566   "is used\n"));   "is used\n"));
3567   return 1;   return 1;
# Line 3042  int main(int argc, const char ** argv) { Line 3569  int main(int argc, const char ** argv) {
3569    
3570      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
3571   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
3572          && !removeMBKernel) {          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {
3573   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
3574   return 1;   return 1;
3575      }      }
# Line 3069  int main(int argc, const char ** argv) { Line 3596  int main(int argc, const char ** argv) {
3596      }      }
3597    
3598      if (bootloaderProbe) {      if (bootloaderProbe) {
3599   int lrc = 0, grc = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, erc = 0;
3600   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig;
3601    
3602   if (!access(grubConfigType.defaultConfig, F_OK)) {   const char *grub2config = grub2FindConfig(&grub2ConfigType);
3603      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);   if (grub2config) {
3604        gconfig = readConfig(grub2config, &grub2ConfigType);
3605        if (!gconfig)
3606     gr2c = 1;
3607        else
3608     gr2c = checkForGrub2(gconfig);
3609     }
3610    
3611     const char *grubconfig = grubFindConfig(&grubConfigType);
3612     if (!access(grubconfig, F_OK)) {
3613        gconfig = readConfig(grubconfig, &grubConfigType);
3614      if (!gconfig)      if (!gconfig)
3615   grc = 1;   grc = 1;
3616      else      else
# Line 3096  int main(int argc, const char ** argv) { Line 3633  int main(int argc, const char ** argv) {
3633   erc = checkForExtLinux(lconfig);   erc = checkForExtLinux(lconfig);
3634   }   }
3635    
3636   if (lrc == 1 || grc == 1) return 1;   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;
3637    
3638   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
3639     if (gr2c == 2) printf("grub2\n");
3640   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
3641   if (erc == 2) printf("extlinux\n");   if (erc == 2) printf("extlinux\n");
3642    
# Line 3126  int main(int argc, const char ** argv) { Line 3664  int main(int argc, const char ** argv) {
3664                 ((rootspec != NULL) ? strlen(rootspec) : 0));                 ((rootspec != NULL) ? strlen(rootspec) : 0));
3665    
3666   return 0;   return 0;
3667    
3668        } else if (displayDefaultTitle) {
3669     struct singleLine * line;
3670     struct singleEntry * entry;
3671    
3672     if (config->defaultImage == -1) return 0;
3673     entry = findEntryByIndex(config, config->defaultImage);
3674     if (!entry) return 0;
3675    
3676     if (!configureGrub2) {
3677      line = getLineByType(LT_TITLE, entry->lines);
3678      if (!line) return 0;
3679      printf("%s\n", line->elements[1].item);
3680    
3681     } else {
3682      char * title;
3683    
3684      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
3685      line = getLineByType(LT_MENUENTRY, entry->lines);
3686      if (!line) return 0;
3687      title = grub2ExtractTitle(line);
3688      if (title)
3689        printf("%s\n", title);
3690     }
3691     return 0;
3692    
3693        } else if (displayDefaultIndex) {
3694            if (config->defaultImage == -1) return 0;
3695            printf("%i\n", config->defaultImage);
3696    
3697      } else if (kernelInfo)      } else if (kernelInfo)
3698   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
3699    
# Line 3158  int main(int argc, const char ** argv) { Line 3726  int main(int argc, const char ** argv) {
3726      }      }
3727    
3728      if (!outputFile)      if (!outputFile)
3729   outputFile = grubConfig;   outputFile = (char *)grubConfig;
3730    
3731      return writeConfig(config, outputFile, bootPrefix);      return writeConfig(config, outputFile, bootPrefix);
3732  }  }

Legend:
Removed from v.1692  
changed lines
  Added in v.1751