Magellan Linux

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

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

revision 1849 by niro, Mon Jul 2 13:08:29 2012 UTC revision 2257 by niro, Mon Oct 21 14:01:48 2013 UTC
# Line 36  Line 36 
36  #include <signal.h>  #include <signal.h>
37  #include <blkid/blkid.h>  #include <blkid/blkid.h>
38    
39    #include "log.h"
40    
41  #ifndef DEBUG  #ifndef DEBUG
42  #define DEBUG 0  #define DEBUG 0
43  #endif  #endif
# Line 56  int debug = 0; /* Currently just for tem Line 58  int debug = 0; /* Currently just for tem
58  #define NOOP_OPCODE 0x90  #define NOOP_OPCODE 0x90
59  #define JMP_SHORT_OPCODE 0xeb  #define JMP_SHORT_OPCODE 0xeb
60    
61    int isEfi = 0;
62    
63    char *saved_command_line = NULL;
64    
65  /* comments get lumped in with indention */  /* comments get lumped in with indention */
66  struct lineElement {  struct lineElement {
67      char * item;      char * item;
# Line 82  enum lineType_e { Line 88  enum lineType_e {
88      LT_MENUENTRY    = 1 << 17,      LT_MENUENTRY    = 1 << 17,
89      LT_ENTRY_END    = 1 << 18,      LT_ENTRY_END    = 1 << 18,
90      LT_SET_VARIABLE = 1 << 19,      LT_SET_VARIABLE = 1 << 19,
91      LT_UNKNOWN      = 1 << 20,      LT_KERNEL_EFI   = 1 << 20,
92        LT_INITRD_EFI   = 1 << 21,
93        LT_UNKNOWN      = 1 << 22,
94  };  };
95    
96  struct singleLine {  struct singleLine {
# Line 128  struct configFileInfo; Line 136  struct configFileInfo;
136  typedef const char *(*findConfigFunc)(struct configFileInfo *);  typedef const char *(*findConfigFunc)(struct configFileInfo *);
137  typedef const int (*writeLineFunc)(struct configFileInfo *,  typedef const int (*writeLineFunc)(struct configFileInfo *,
138   struct singleLine *line);   struct singleLine *line);
139    typedef char *(*getEnvFunc)(struct configFileInfo *, char *name);
140    typedef int (*setEnvFunc)(struct configFileInfo *, char *name, char *value);
141    
142  struct configFileInfo {  struct configFileInfo {
143      char * defaultConfig;      char * defaultConfig;
144      findConfigFunc findConfig;      findConfigFunc findConfig;
145      writeLineFunc writeLine;      writeLineFunc writeLine;
146        getEnvFunc getEnv;
147        setEnvFunc setEnv;
148      struct keywordTypes * keywords;      struct keywordTypes * keywords;
149        int caseInsensitive;
150      int defaultIsIndex;      int defaultIsIndex;
151      int defaultIsVariable;      int defaultIsVariable;
152      int defaultSupportSaved;      int defaultSupportSaved;
153        int defaultIsSaved;
154      enum lineType_e entryStart;      enum lineType_e entryStart;
155      enum lineType_e entryEnd;      enum lineType_e entryEnd;
156      int needsBootPrefix;      int needsBootPrefix;
# Line 148  struct configFileInfo { Line 162  struct configFileInfo {
162      int mbInitRdIsModule;      int mbInitRdIsModule;
163      int mbConcatArgs;      int mbConcatArgs;
164      int mbAllowExtraInitRds;      int mbAllowExtraInitRds;
165        char *envFile;
166  };  };
167    
168  struct keywordTypes grubKeywords[] = {  struct keywordTypes grubKeywords[] = {
# Line 164  struct keywordTypes grubKeywords[] = { Line 179  struct keywordTypes grubKeywords[] = {
179    
180  const char *grubFindConfig(struct configFileInfo *cfi) {  const char *grubFindConfig(struct configFileInfo *cfi) {
181      static const char *configFiles[] = {      static const char *configFiles[] = {
  "/etc/grub.conf",  
182   "/boot/grub/grub.conf",   "/boot/grub/grub.conf",
183   "/boot/grub/menu.lst",   "/boot/grub/menu.lst",
184     "/etc/grub.conf",
185   NULL   NULL
186      };      };
187      static int i = -1;      static int i = -1;
# Line 205  struct keywordTypes grub2Keywords[] = { Line 220  struct keywordTypes grub2Keywords[] = {
220      { "default",    LT_DEFAULT,     ' ' },      { "default",    LT_DEFAULT,     ' ' },
221      { "fallback",   LT_FALLBACK,    ' ' },      { "fallback",   LT_FALLBACK,    ' ' },
222      { "linux",      LT_KERNEL,      ' ' },      { "linux",      LT_KERNEL,      ' ' },
223        { "linuxefi",   LT_KERNEL_EFI,  ' ' },
224      { "initrd",     LT_INITRD,      ' ', ' ' },      { "initrd",     LT_INITRD,      ' ', ' ' },
225        { "initrdefi",  LT_INITRD_EFI,  ' ', ' ' },
226      { "module",     LT_MBMODULE,    ' ' },      { "module",     LT_MBMODULE,    ' ' },
227      { "kernel",     LT_HYPER,       ' ' },      { "kernel",     LT_HYPER,       ' ' },
228      { NULL, 0, 0 },      { NULL, 0, 0 },
# Line 219  const char *grub2FindConfig(struct confi Line 236  const char *grub2FindConfig(struct confi
236      };      };
237      static int i = -1;      static int i = -1;
238      static const char *grub_cfg = "/boot/grub/grub.cfg";      static const char *grub_cfg = "/boot/grub/grub.cfg";
239        int rc = -1;
240    
241      if (i == -1) {      if (i == -1) {
242   for (i = 0; configFiles[i] != NULL; i++) {   for (i = 0; configFiles[i] != NULL; i++) {
243      dbgPrintf("Checking \"%s\": ", configFiles[i]);      dbgPrintf("Checking \"%s\": ", configFiles[i]);
244      if (!access(configFiles[i], R_OK)) {      if ((rc = access(configFiles[i], R_OK))) {
245     if (errno == EACCES) {
246        printf("Unable to access bootloader configuration file "
247           "\"%s\": %m\n", configFiles[i]);
248        exit(1);
249     }
250     continue;
251        } else {
252   dbgPrintf("found\n");   dbgPrintf("found\n");
253   return configFiles[i];   return configFiles[i];
254      }      }
# Line 242  const char *grub2FindConfig(struct confi Line 267  const char *grub2FindConfig(struct confi
267      return configFiles[i];      return configFiles[i];
268  }  }
269    
270    /* kind of hacky.  It'll give the first 1024 bytes, ish. */
271    static char *grub2GetEnv(struct configFileInfo *info, char *name)
272    {
273        static char buf[1025];
274        char *s = NULL;
275        char *ret = NULL;
276        char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";
277        int rc = asprintf(&s, "grub2-editenv %s list | grep '^%s='", envFile, name);
278    
279        if (rc < 0)
280     return NULL;
281    
282        FILE *f = popen(s, "r");
283        if (!f)
284     goto out;
285    
286        memset(buf, '\0', sizeof (buf));
287        ret = fgets(buf, 1024, f);
288        pclose(f);
289    
290        if (ret) {
291     ret += strlen(name) + 1;
292     ret[strlen(ret) - 1] = '\0';
293        }
294        dbgPrintf("grub2GetEnv(%s): %s\n", name, ret);
295    out:
296        free(s);
297        return ret;
298    }
299    
300    static int sPopCount(const char *s, const char *c)
301    {
302        int ret = 0;
303        if (!s)
304     return -1;
305        for (int i = 0; s[i] != '\0'; i++)
306     for (int j = 0; c[j] != '\0'; j++)
307        if (s[i] == c[j])
308     ret++;
309        return ret;
310    }
311    
312    static char *shellEscape(const char *s)
313    {
314        int l = strlen(s) + sPopCount(s, "'") * 2;
315    
316        char *ret = calloc(l+1, sizeof (*ret));
317        if (!ret)
318     return NULL;
319        for (int i = 0, j = 0; s[i] != '\0'; i++, j++) {
320     if (s[i] == '\'')
321        ret[j++] = '\\';
322     ret[j] = s[i];
323        }
324        return ret;
325    }
326    
327    static void unquote(char *s)
328    {
329        int l = strlen(s);
330    
331        if ((s[l-1] == '\'' && s[0] == '\'') || (s[l-1] == '"' && s[0] == '"')) {
332     memmove(s, s+1, l-2);
333     s[l-2] = '\0';
334        }
335    }
336    
337    static int grub2SetEnv(struct configFileInfo *info, char *name, char *value)
338    {
339        char *s = NULL;
340        int rc = 0;
341        char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";
342    
343        unquote(value);
344        value = shellEscape(value);
345        if (!value)
346        return -1;
347    
348        rc = asprintf(&s, "grub2-editenv %s set '%s=%s'", envFile, name, value);
349        free(value);
350        if (rc <0)
351     return -1;
352    
353        dbgPrintf("grub2SetEnv(%s): %s\n", name, s);
354        rc = system(s);
355        free(s);
356        return rc;
357    }
358    
359    /* this is a gigantic hack to avoid clobbering grub2 variables... */
360    static int is_special_grub2_variable(const char *name)
361    {
362        if (!strcmp(name,"\"${next_entry}\""))
363     return 1;
364        if (!strcmp(name,"\"${prev_saved_entry}\""))
365     return 1;
366        return 0;
367    }
368    
369  int sizeOfSingleLine(struct singleLine * line) {  int sizeOfSingleLine(struct singleLine * line) {
370    int count = 0;    int count = 0;
371    
# Line 271  static int isquote(char q) Line 395  static int isquote(char q)
395      return 0;      return 0;
396  }  }
397    
398    static int iskernel(enum lineType_e type) {
399        return (type == LT_KERNEL || type == LT_KERNEL_EFI);
400    }
401    
402    static int isinitrd(enum lineType_e type) {
403        return (type == LT_INITRD || type == LT_INITRD_EFI);
404    }
405    
406  char *grub2ExtractTitle(struct singleLine * line) {  char *grub2ExtractTitle(struct singleLine * line) {
407      char * current;      char * current;
408      char * current_indent;      char * current_indent;
# Line 328  char *grub2ExtractTitle(struct singleLin Line 460  char *grub2ExtractTitle(struct singleLin
460    
461  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
462      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
463        .getEnv = grub2GetEnv,
464        .setEnv = grub2SetEnv,
465      .keywords = grub2Keywords,      .keywords = grub2Keywords,
466      .defaultIsIndex = 1,      .defaultIsIndex = 1,
467      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
# Line 482  struct configFileInfo ziplConfigType = { Line 616  struct configFileInfo ziplConfigType = {
616  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
617      .defaultConfig = "/boot/extlinux/extlinux.conf",      .defaultConfig = "/boot/extlinux/extlinux.conf",
618      .keywords = extlinuxKeywords,      .keywords = extlinuxKeywords,
619        .caseInsensitive = 1,
620      .entryStart = LT_TITLE,      .entryStart = LT_TITLE,
621      .needsBootPrefix = 1,      .needsBootPrefix = 1,
622      .maxTitleLength = 255,      .maxTitleLength = 255,
# Line 506  struct singleEntry * findEntryByIndex(st Line 641  struct singleEntry * findEntryByIndex(st
641  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
642       const char * path, const char * prefix,       const char * path, const char * prefix,
643       int * index);       int * index);
644    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
645          int * index);
646  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
647  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
648  struct singleLine * lineDup(struct singleLine * line);  struct singleLine * lineDup(struct singleLine * line);
# Line 570  static char * sdupprintf(const char *for Line 707  static char * sdupprintf(const char *for
707      return buf;      return buf;
708  }  }
709    
710    static enum lineType_e preferredLineType(enum lineType_e type,
711     struct configFileInfo *cfi) {
712        if (isEfi && cfi == &grub2ConfigType) {
713     switch (type) {
714     case LT_KERNEL:
715        return LT_KERNEL_EFI;
716     case LT_INITRD:
717        return LT_INITRD_EFI;
718     default:
719        return type;
720     }
721        }
722        return type;
723    }
724    
725  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
726        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
727      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
# Line 603  static char * getuuidbydev(char *device) Line 755  static char * getuuidbydev(char *device)
755  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
756   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
757      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
758   if (!strcmp(keyword, kw->key))   if (cfi->caseInsensitive) {
759      return kw->type;      if (!strcasecmp(keyword, kw->key))
760                    return kw->type;
761     } else {
762        if (!strcmp(keyword, kw->key))
763            return kw->type;
764     }
765      }      }
766      return LT_UNKNOWN;      return LT_UNKNOWN;
767  }  }
# Line 643  static int isEntryStart(struct singleLin Line 800  static int isEntryStart(struct singleLin
800  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
801  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct singleLine * line) {
802      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it (leaks a byte) */
803      char * title;      char * title = NULL;
804      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
805      title++;   title = strdup(line->elements[0].item);
806      *(title + strlen(title) - 1) = '\0';   title++;
807     *(title + strlen(title) - 1) = '\0';
808        } else if (line->type == LT_MENUENTRY)
809     title = strdup(line->elements[1].item);
810        else
811     return NULL;
812      return title;      return title;
813  }  }
814    
# Line 918  static struct grubConfig * readConfig(co Line 1080  static struct grubConfig * readConfig(co
1080      int len;      int len;
1081      char * buf;      char * buf;
1082    
1083      if (!strcmp(inName, "-")) {      if (inName == NULL) {
1084            printf("Could not find bootloader configuration\n");
1085            exit(1);
1086        } else if (!strcmp(inName, "-")) {
1087   in = 0;   in = 0;
1088      } else {      } else {
1089   if ((in = open(inName, O_RDONLY)) < 0) {   if ((in = open(inName, O_RDONLY)) < 0) {
# Line 985  static struct grubConfig * readConfig(co Line 1150  static struct grubConfig * readConfig(co
1150      dbgPrintf("\n");      dbgPrintf("\n");
1151      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1152      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
1153      !strcmp(line->elements[1].item, kwType->key)) {      !strcmp(line->elements[1].item, kwType->key) &&
1154        !is_special_grub2_variable(line->elements[2].item)) {
1155   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1156   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1157   defaultLine = line;   defaultLine = line;
# Line 994  static struct grubConfig * readConfig(co Line 1160  static struct grubConfig * readConfig(co
1160      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1161      defaultLine = line;      defaultLine = line;
1162    
1163          } else if (line->type == LT_KERNEL) {          } else if (iskernel(line->type)) {
1164      /* if by some freak chance this is multiboot and the "module"      /* if by some freak chance this is multiboot and the "module"
1165       * lines came earlier in the template, make sure to use LT_HYPER       * lines came earlier in the template, make sure to use LT_HYPER
1166       * instead of LT_KERNEL now       * instead of LT_KERNEL now
# Line 1011  static struct grubConfig * readConfig(co Line 1177  static struct grubConfig * readConfig(co
1177      for (struct singleLine *l = entry->lines; l; l = l->next) {      for (struct singleLine *l = entry->lines; l; l = l->next) {
1178   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1179      break;      break;
1180   else if (l->type == LT_KERNEL) {   else if (iskernel(l->type)) {
1181      l->type = LT_HYPER;      l->type = LT_HYPER;
1182      break;      break;
1183   }   }
# Line 1190  static struct grubConfig * readConfig(co Line 1356  static struct grubConfig * readConfig(co
1356          if (defaultLine->numElements > 2 &&          if (defaultLine->numElements > 2 &&
1357      cfi->defaultSupportSaved &&      cfi->defaultSupportSaved &&
1358      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1359      cfg->defaultImage = DEFAULT_SAVED_GRUB2;   cfg->cfi->defaultIsSaved = 1;
1360     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1361     if (cfg->cfi->getEnv) {
1362        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1363        if (defTitle) {
1364     int index = 0;
1365     entry = findEntryByTitle(cfg, defTitle, &index);
1366     if (entry)
1367        cfg->defaultImage = index;
1368        }
1369     }
1370   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1371      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1372      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
# Line 1231  static struct grubConfig * readConfig(co Line 1407  static struct grubConfig * readConfig(co
1407          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1408      }      }
1409   }   }
1410        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1411     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1412     if (defTitle) {
1413        int index = 0;
1414        entry = findEntryByTitle(cfg, defTitle, &index);
1415        if (entry)
1416     cfg->defaultImage = index;
1417     }
1418      } else {      } else {
1419          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1420      }      }
# Line 1248  static void writeDefault(FILE * out, cha Line 1432  static void writeDefault(FILE * out, cha
1432    
1433      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1434   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1435      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1436   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1437      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1438        char *title;
1439        entry = findEntryByIndex(cfg, cfg->defaultImage);
1440        line = getLineByType(LT_MENUENTRY, entry->lines);
1441        if (!line)
1442     line = getLineByType(LT_TITLE, entry->lines);
1443        if (line) {
1444     title = extractTitle(line);
1445     if (title)
1446        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1447        }
1448     }
1449        } else if (cfg->defaultImage > -1) {
1450   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1451      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1452          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 1310  static int writeConfig(struct grubConfig Line 1506  static int writeConfig(struct grubConfig
1506    
1507      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1508         directory to the dir of the symlink */         directory to the dir of the symlink */
1509              rc = chdir(dirname(outName));      char *dir = strdupa(outName);
1510        rc = chdir(dirname(dir));
1511      do {      do {
1512   buf = alloca(len + 1);   buf = alloca(len + 1);
1513   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1352  static int writeConfig(struct grubConfig Line 1549  static int writeConfig(struct grubConfig
1549      while (line) {      while (line) {
1550          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1551   line->numElements == 3 &&   line->numElements == 3 &&
1552   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1553     !is_special_grub2_variable(line->elements[2].item)) {
1554      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1555      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1556   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1497  static char *findDiskForRoot() Line 1695  static char *findDiskForRoot()
1695      return NULL;      return NULL;
1696  }  }
1697    
1698  void printEntry(struct singleEntry * entry) {  void printEntry(struct singleEntry * entry, FILE *f) {
1699      int i;      int i;
1700      struct singleLine * line;      struct singleLine * line;
1701    
1702      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1703   fprintf(stderr, "DBG: %s", line->indent);   log_message(f, "DBG: %s", line->indent);
1704   for (i = 0; i < line->numElements; i++) {   for (i = 0; i < line->numElements; i++) {
1705      /* Need to handle this, because we strip the quotes from      /* Need to handle this, because we strip the quotes from
1706       * menuentry when read it. */       * menuentry when read it. */
1707      if (line->type == LT_MENUENTRY && i == 1) {      if (line->type == LT_MENUENTRY && i == 1) {
1708   if(!isquote(*line->elements[i].item))   if(!isquote(*line->elements[i].item))
1709      fprintf(stderr, "\'%s\'", line->elements[i].item);      log_message(f, "\'%s\'", line->elements[i].item);
1710   else   else
1711      fprintf(stderr, "%s", line->elements[i].item);      log_message(f, "%s", line->elements[i].item);
1712   fprintf(stderr, "%s", line->elements[i].indent);   log_message(f, "%s", line->elements[i].indent);
1713    
1714   continue;   continue;
1715      }      }
1716            
1717      fprintf(stderr, "%s%s",      log_message(f, "%s%s",
1718      line->elements[i].item, line->elements[i].indent);      line->elements[i].item, line->elements[i].indent);
1719   }   }
1720   fprintf(stderr, "\n");   log_message(f, "\n");
1721      }      }
1722  }  }
1723    
1724  void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)  void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1725  {  {
1726      va_list argp;      static int once;
1727        va_list argp, argq;
1728    
1729      if (!debug)      va_start(argp, fmt);
1730    
1731        va_copy(argq, argp);
1732        if (!once) {
1733     log_time(NULL);
1734     log_message(NULL, "command line: %s\n", saved_command_line);
1735        }
1736        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1737        log_vmessage(NULL, fmt, argq);
1738    
1739        printEntry(entry, NULL);
1740        va_end(argq);
1741    
1742        if (!debug) {
1743     once = 1;
1744         va_end(argp);
1745   return;   return;
1746        }
1747    
1748      va_start(argp, fmt);      if (okay) {
1749     va_end(argp);
1750     return;
1751        }
1752    
1753        if (!once)
1754     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1755        once = 1;
1756      fprintf(stderr, "DBG: Image entry failed: ");      fprintf(stderr, "DBG: Image entry failed: ");
1757      vfprintf(stderr, fmt, argp);      vfprintf(stderr, fmt, argp);
1758      printEntry(entry);      printEntry(entry, stderr);
1759      va_end(argp);      va_end(argp);
1760  }  }
1761    
# Line 1560  int suitableImage(struct singleEntry * e Line 1782  int suitableImage(struct singleEntry * e
1782      char * rootdev;      char * rootdev;
1783    
1784      if (skipRemoved && entry->skip) {      if (skipRemoved && entry->skip) {
1785   notSuitablePrintf(entry, "marked to skip\n");   notSuitablePrintf(entry, 0, "marked to skip\n");
1786   return 0;   return 0;
1787      }      }
1788    
1789      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1790      if (!line) {      if (!line) {
1791   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1792   return 0;   return 0;
1793      }      }
1794      if (line->numElements < 2) {      if (line->numElements < 2) {
1795   notSuitablePrintf(entry, "line has only %d elements\n",   notSuitablePrintf(entry, 0, "line has only %d elements\n",
1796      line->numElements);      line->numElements);
1797   return 0;   return 0;
1798      }      }
1799    
1800      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) {
1801        notSuitablePrintf(entry, 1, "\n");
1802        return 1;
1803        }
1804    
1805      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1806        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
# Line 1586  int suitableImage(struct singleEntry * e Line 1811  int suitableImage(struct singleEntry * e
1811      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1812              line->elements[1].item + rootspec_offset);              line->elements[1].item + rootspec_offset);
1813      if (access(fullName, R_OK)) {      if (access(fullName, R_OK)) {
1814   notSuitablePrintf(entry, "access to %s failed\n", fullName);   notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1815   return 0;   return 0;
1816      }      }
1817      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
# Line 1607  int suitableImage(struct singleEntry * e Line 1832  int suitableImage(struct singleEntry * e
1832    
1833              /* failed to find one */              /* failed to find one */
1834              if (!line) {              if (!line) {
1835   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1836   return 0;   return 0;
1837              }              }
1838    
# Line 1616  int suitableImage(struct singleEntry * e Line 1841  int suitableImage(struct singleEntry * e
1841      if (i < line->numElements)      if (i < line->numElements)
1842          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1843      else {      else {
1844   notSuitablePrintf(entry, "no root= entry found\n");   notSuitablePrintf(entry, 0, "no root= entry found\n");
1845   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1846          return 0;          return 0;
1847              }              }
# Line 1625  int suitableImage(struct singleEntry * e Line 1850  int suitableImage(struct singleEntry * e
1850    
1851      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1852      if (!getpathbyspec(dev)) {      if (!getpathbyspec(dev)) {
1853          notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);          notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1854          return 0;          return 0;
1855      } else      } else
1856   dev = getpathbyspec(dev);   dev = getpathbyspec(dev);
1857    
1858      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1859      if (!rootdev) {      if (!rootdev) {
1860          notSuitablePrintf(entry, "can't find root device\n");          notSuitablePrintf(entry, 0, "can't find root device\n");
1861   return 0;   return 0;
1862      }      }
1863    
1864      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1865          notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1866   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1867          free(rootdev);          free(rootdev);
1868          return 0;          return 0;
1869      }      }
1870    
1871      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1872          notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n",
1873   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1874   free(rootdev);   free(rootdev);
1875          return 0;          return 0;
1876      }      }
1877    
1878      free(rootdev);      free(rootdev);
1879        notSuitablePrintf(entry, 1, "\n");
1880    
1881      return 1;      return 1;
1882  }  }
# Line 1694  struct singleEntry * findEntryByPath(str Line 1920  struct singleEntry * findEntryByPath(str
1920   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
1921   if (!entry) return NULL;   if (!entry) return NULL;
1922    
1923   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1924   if (!line) return NULL;   if (!line) return NULL;
1925    
1926   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1743  struct singleEntry * findEntryByPath(str Line 1969  struct singleEntry * findEntryByPath(str
1969    
1970      /* check all the lines matching checkType */      /* check all the lines matching checkType */
1971      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1972   line = getLineByType(entry->multiboot && checkType == LT_KERNEL ?   enum lineType_e ct = checkType;
1973       LT_KERNEL|LT_MBMODULE|LT_HYPER :   if (entry->multiboot && checkType == LT_KERNEL)
1974       checkType, line);      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER;
1975   if (!line) break;  /* not found in this entry */   else if (checkType & LT_KERNEL)
1976        ct = checkType | LT_KERNEL_EFI;
1977     line = getLineByType(ct, line);
1978     if (!line)
1979        break;  /* not found in this entry */
1980    
1981   if (line && line->type != LT_MENUENTRY &&   if (line && line->type != LT_MENUENTRY &&
1982   line->numElements >= 2) {   line->numElements >= 2) {
# Line 1765  struct singleEntry * findEntryByPath(str Line 1995  struct singleEntry * findEntryByPath(str
1995       * non-Linux boot entries (could find netbsd etc, though, which is       * non-Linux boot entries (could find netbsd etc, though, which is
1996       * unfortunate)       * unfortunate)
1997       */       */
1998      if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines))      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines))
1999   break; /* found 'im! */   break; /* found 'im! */
2000   }   }
2001    
# Line 1775  struct singleEntry * findEntryByPath(str Line 2005  struct singleEntry * findEntryByPath(str
2005      return entry;      return entry;
2006  }  }
2007    
2008    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2009          int * index) {
2010        struct singleEntry * entry;
2011        struct singleLine * line;
2012        int i;
2013        char * newtitle;
2014    
2015        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2016     if (index && i < *index)
2017        continue;
2018     line = getLineByType(LT_TITLE, entry->lines);
2019     if (!line)
2020        line = getLineByType(LT_MENUENTRY, entry->lines);
2021     if (!line)
2022        continue;
2023     newtitle = grub2ExtractTitle(line);
2024     if (!newtitle)
2025        continue;
2026     if (!strcmp(title, newtitle))
2027        break;
2028        }
2029    
2030        if (!entry)
2031     return NULL;
2032    
2033        if (index)
2034     *index = i;
2035        return entry;
2036    }
2037    
2038  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
2039      struct singleEntry * entry;      struct singleEntry * entry;
2040    
# Line 1797  struct singleEntry * findTemplate(struct Line 2057  struct singleEntry * findTemplate(struct
2057      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2058      int index;      int index;
2059    
2060      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2061     if (cfg->cfi->getEnv) {
2062        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2063        if (defTitle) {
2064     int index = 0;
2065     entry = findEntryByTitle(cfg, defTitle, &index);
2066        }
2067     }
2068        } else if (cfg->defaultImage > -1) {
2069   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2070   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2071      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 1867  void markRemovedImage(struct grubConfig Line 2135  void markRemovedImage(struct grubConfig
2135    
2136  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
2137       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
2138       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
2139      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
2140      int i, j;      int i, j;
2141    
2142      if (newIsDefault) {      if (newIsDefault) {
2143   config->defaultImage = 0;   config->defaultImage = 0;
2144   return;   return;
2145        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
2146     if (findEntryByIndex(config, index))
2147        config->defaultImage = index;
2148     else
2149        config->defaultImage = -1;
2150     return;
2151      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
2152   i = 0;   i = 0;
2153   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1948  void displayEntry(struct singleEntry * e Line 2222  void displayEntry(struct singleEntry * e
2222    
2223      printf("index=%d\n", index);      printf("index=%d\n", index);
2224    
2225      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
2226      if (!line) {      if (!line) {
2227          printf("non linux entry\n");          printf("non linux entry\n");
2228          return;          return;
# Line 2013  void displayEntry(struct singleEntry * e Line 2287  void displayEntry(struct singleEntry * e
2287   printf("root=%s\n", s);   printf("root=%s\n", s);
2288      }      }
2289    
2290      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);
2291    
2292      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2293   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
# Line 2038  void displayEntry(struct singleEntry * e Line 2312  void displayEntry(struct singleEntry * e
2312      }      }
2313  }  }
2314    
2315    int isSuseSystem(void) {
2316        const char * path;
2317        const static char default_path[] = "/etc/SuSE-release";
2318    
2319        if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL)
2320     path = default_path;
2321    
2322        if (!access(path, R_OK))
2323     return 1;
2324        return 0;
2325    }
2326    
2327    int isSuseGrubConf(const char * path) {
2328        FILE * grubConf;
2329        char * line = NULL;
2330        size_t len = 0, res = 0;
2331    
2332        grubConf = fopen(path, "r");
2333        if (!grubConf) {
2334            dbgPrintf("Could not open SuSE configuration file '%s'\n", path);
2335     return 0;
2336        }
2337    
2338        while ((res = getline(&line, &len, grubConf)) != -1) {
2339     if (!strncmp(line, "setup", 5)) {
2340        fclose(grubConf);
2341        free(line);
2342        return 1;
2343     }
2344        }
2345    
2346        dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n",
2347          path);
2348    
2349        fclose(grubConf);
2350        free(line);
2351        return 0;
2352    }
2353    
2354    int suseGrubConfGetLba(const char * path, int * lbaPtr) {
2355        FILE * grubConf;
2356        char * line = NULL;
2357        size_t res = 0, len = 0;
2358    
2359        if (!path) return 1;
2360        if (!lbaPtr) return 1;
2361    
2362        grubConf = fopen(path, "r");
2363        if (!grubConf) return 1;
2364    
2365        while ((res = getline(&line, &len, grubConf)) != -1) {
2366     if (line[res - 1] == '\n')
2367        line[res - 1] = '\0';
2368     else if (len > res)
2369        line[res] = '\0';
2370     else {
2371        line = realloc(line, res + 1);
2372        line[res] = '\0';
2373     }
2374    
2375     if (!strncmp(line, "setup", 5)) {
2376        if (strstr(line, "--force-lba")) {
2377            *lbaPtr = 1;
2378        } else {
2379            *lbaPtr = 0;
2380        }
2381        dbgPrintf("lba: %i\n", *lbaPtr);
2382        break;
2383     }
2384        }
2385    
2386        free(line);
2387        fclose(grubConf);
2388        return 0;
2389    }
2390    
2391    int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) {
2392        FILE * grubConf;
2393        char * line = NULL;
2394        size_t res = 0, len = 0;
2395        char * lastParamPtr = NULL;
2396        char * secLastParamPtr = NULL;
2397        char installDeviceNumber = '\0';
2398        char * bounds = NULL;
2399    
2400        if (!path) return 1;
2401        if (!devicePtr) return 1;
2402    
2403        grubConf = fopen(path, "r");
2404        if (!grubConf) return 1;
2405    
2406        while ((res = getline(&line, &len, grubConf)) != -1) {
2407     if (strncmp(line, "setup", 5))
2408        continue;
2409    
2410     if (line[res - 1] == '\n')
2411        line[res - 1] = '\0';
2412     else if (len > res)
2413        line[res] = '\0';
2414     else {
2415        line = realloc(line, res + 1);
2416        line[res] = '\0';
2417     }
2418    
2419     lastParamPtr = bounds = line + res;
2420    
2421     /* Last parameter in grub may be an optional IMAGE_DEVICE */
2422     while (!isspace(*lastParamPtr))
2423        lastParamPtr--;
2424     lastParamPtr++;
2425    
2426     secLastParamPtr = lastParamPtr - 2;
2427     dbgPrintf("lastParamPtr: %s\n", lastParamPtr);
2428    
2429     if (lastParamPtr + 3 > bounds) {
2430        dbgPrintf("lastParamPtr going over boundary");
2431        fclose(grubConf);
2432        free(line);
2433        return 1;
2434     }
2435     if (!strncmp(lastParamPtr, "(hd", 3))
2436        lastParamPtr += 3;
2437     dbgPrintf("lastParamPtr: %c\n", *lastParamPtr);
2438    
2439     /*
2440     * Second last parameter will decide wether last parameter is
2441     * an IMAGE_DEVICE or INSTALL_DEVICE
2442     */
2443     while (!isspace(*secLastParamPtr))
2444        secLastParamPtr--;
2445     secLastParamPtr++;
2446    
2447     if (secLastParamPtr + 3 > bounds) {
2448        dbgPrintf("secLastParamPtr going over boundary");
2449        fclose(grubConf);
2450        free(line);
2451        return 1;
2452     }
2453     dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr);
2454     if (!strncmp(secLastParamPtr, "(hd", 3)) {
2455        secLastParamPtr += 3;
2456        dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr);
2457        installDeviceNumber = *secLastParamPtr;
2458     } else {
2459        installDeviceNumber = *lastParamPtr;
2460     }
2461    
2462     *devicePtr = malloc(6);
2463     snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber);
2464     dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber);
2465     fclose(grubConf);
2466     free(line);
2467     return 0;
2468        }
2469    
2470        free(line);
2471        fclose(grubConf);
2472        return 1;
2473    }
2474    
2475    int grubGetBootFromDeviceMap(const char * device,
2476         char ** bootPtr) {
2477        FILE * deviceMap;
2478        char * line = NULL;
2479        size_t res = 0, len = 0;
2480        char * devicePtr;
2481        char * bounds = NULL;
2482        const char * path;
2483        const static char default_path[] = "/boot/grub/device.map";
2484    
2485        if (!device) return 1;
2486        if (!bootPtr) return 1;
2487    
2488        if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL)
2489     path = default_path;
2490    
2491        dbgPrintf("opening grub device.map file from: %s\n", path);
2492        deviceMap = fopen(path, "r");
2493        if (!deviceMap)
2494     return 1;
2495    
2496        while ((res = getline(&line, &len, deviceMap)) != -1) {
2497            if (!strncmp(line, "#", 1))
2498        continue;
2499    
2500     if (line[res - 1] == '\n')
2501        line[res - 1] = '\0';
2502     else if (len > res)
2503        line[res] = '\0';
2504     else {
2505        line = realloc(line, res + 1);
2506        line[res] = '\0';
2507     }
2508    
2509     devicePtr = line;
2510     bounds = line + res;
2511    
2512     while ((isspace(*line) && ((devicePtr + 1) <= bounds)))
2513        devicePtr++;
2514     dbgPrintf("device: %s\n", devicePtr);
2515    
2516     if (!strncmp(devicePtr, device, strlen(device))) {
2517        devicePtr += strlen(device);
2518        while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds))
2519            devicePtr++;
2520    
2521        *bootPtr = strdup(devicePtr);
2522        break;
2523     }
2524        }
2525    
2526        free(line);
2527        fclose(deviceMap);
2528        return 0;
2529    }
2530    
2531    int suseGrubConfGetBoot(const char * path, char ** bootPtr) {
2532        char * grubDevice;
2533    
2534        if (suseGrubConfGetInstallDevice(path, &grubDevice))
2535     dbgPrintf("error looking for grub installation device\n");
2536        else
2537     dbgPrintf("grubby installation device: %s\n", grubDevice);
2538    
2539        if (grubGetBootFromDeviceMap(grubDevice, bootPtr))
2540     dbgPrintf("error looking for grub boot device\n");
2541        else
2542     dbgPrintf("grubby boot device: %s\n", *bootPtr);
2543    
2544        free(grubDevice);
2545        return 0;
2546    }
2547    
2548    int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) {
2549        /*
2550         * This SuSE grub configuration file at this location is not your average
2551         * grub configuration file, but instead the grub commands used to setup
2552         * grub on that system.
2553         */
2554        const char * path;
2555        const static char default_path[] = "/etc/grub.conf";
2556    
2557        if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL)
2558     path = default_path;
2559    
2560        if (!isSuseGrubConf(path)) return 1;
2561    
2562        if (lbaPtr) {
2563            *lbaPtr = 0;
2564            if (suseGrubConfGetLba(path, lbaPtr))
2565                return 1;
2566        }
2567    
2568        if (bootPtr) {
2569            *bootPtr = NULL;
2570            suseGrubConfGetBoot(path, bootPtr);
2571        }
2572    
2573        return 0;
2574    }
2575    
2576  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
2577      FILE * in;      FILE * in;
2578      char buf[1024];      char buf[1024];
# Line 2086  int parseSysconfigGrub(int * lbaPtr, cha Line 2621  int parseSysconfigGrub(int * lbaPtr, cha
2621  }  }
2622    
2623  void dumpSysconfigGrub(void) {  void dumpSysconfigGrub(void) {
2624      char * boot;      char * boot = NULL;
2625      int lba;      int lba;
2626    
2627      if (!parseSysconfigGrub(&lba, &boot)) {      if (isSuseSystem()) {
2628   if (lba) printf("lba\n");          if (parseSuseGrubConf(&lba, &boot)) {
2629   if (boot) printf("boot=%s\n", boot);      free(boot);
2630        return;
2631     }
2632        } else {
2633            if (parseSysconfigGrub(&lba, &boot)) {
2634        free(boot);
2635        return;
2636     }
2637        }
2638    
2639        if (lba) printf("lba\n");
2640        if (boot) {
2641     printf("boot=%s\n", boot);
2642     free(boot);
2643      }      }
2644  }  }
2645    
# Line 2140  struct singleLine * addLineTmpl(struct s Line 2688  struct singleLine * addLineTmpl(struct s
2688  {  {
2689      struct singleLine * newLine = lineDup(tmplLine);      struct singleLine * newLine = lineDup(tmplLine);
2690    
2691        if (isEfi && cfi == &grub2ConfigType) {
2692     enum lineType_e old = newLine->type;
2693     newLine->type = preferredLineType(newLine->type, cfi);
2694     if (old != newLine->type)
2695        newLine->elements[0].item = getKeyByType(newLine->type, cfi);
2696        }
2697    
2698      if (val) {      if (val) {
2699   /* override the inherited value with our own.   /* override the inherited value with our own.
2700   * This is a little weak because it only applies to elements[1]   * This is a little weak because it only applies to elements[1]
# Line 2149  struct singleLine * addLineTmpl(struct s Line 2704  struct singleLine * addLineTmpl(struct s
2704   insertElement(newLine, val, 1, cfi);   insertElement(newLine, val, 1, cfi);
2705    
2706   /* but try to keep the rootspec from the template... sigh */   /* but try to keep the rootspec from the template... sigh */
2707   if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD)) {   if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI)) {
2708      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2709      if (rootspec != NULL) {      if (rootspec != NULL) {
2710   free(newLine->elements[1].item);   free(newLine->elements[1].item);
# Line 2186  struct singleLine *  addLine(struct sing Line 2741  struct singleLine *  addLine(struct sing
2741      /* NB: This function shouldn't allocate items on the heap, rather on the      /* NB: This function shouldn't allocate items on the heap, rather on the
2742       * stack since it calls addLineTmpl which will make copies.       * stack since it calls addLineTmpl which will make copies.
2743       */       */
   
2744      if (type == LT_TITLE && cfi->titleBracketed) {      if (type == LT_TITLE && cfi->titleBracketed) {
2745   /* we're doing a bracketed title (zipl) */   /* we're doing a bracketed title (zipl) */
2746   tmpl.type = type;   tmpl.type = type;
# Line 2520  int updateActualImage(struct grubConfig Line 3074  int updateActualImage(struct grubConfig
3074      firstElement = 2;      firstElement = 2;
3075    
3076   } else {   } else {
3077      line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines);
3078      if (!line) {      if (!line) {
3079   /* no LT_KERNEL or LT_MBMODULE in this entry? */   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3080   continue;   continue;
# Line 2685  int updateInitrd(struct grubConfig * cfg Line 3239  int updateInitrd(struct grubConfig * cfg
3239      if (!image) return 0;      if (!image) return 0;
3240    
3241      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3242          kernelLine = getLineByType(LT_KERNEL, entry->lines);          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines);
3243          if (!kernelLine) continue;          if (!kernelLine) continue;
3244    
3245          line = getLineByType(LT_INITRD, entry->lines);          line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);
3246          if (line)          if (line)
3247              removeLine(entry, line);              removeLine(entry, line);
3248          if (prefix) {          if (prefix) {
# Line 2699  int updateInitrd(struct grubConfig * cfg Line 3253  int updateInitrd(struct grubConfig * cfg
3253   endLine = getLineByType(LT_ENTRY_END, entry->lines);   endLine = getLineByType(LT_ENTRY_END, entry->lines);
3254   if (endLine)   if (endLine)
3255      removeLine(entry, endLine);      removeLine(entry, endLine);
3256          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);          line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi),
3257     kernelLine->indent, initrd);
3258          if (!line)          if (!line)
3259      return 1;      return 1;
3260   if (endLine) {   if (endLine) {
# Line 2905  int checkForGrub(struct grubConfig * con Line 3460  int checkForGrub(struct grubConfig * con
3460      int fd;      int fd;
3461      unsigned char bootSect[512];      unsigned char bootSect[512];
3462      char * boot;      char * boot;
3463        int onSuse = isSuseSystem();
3464    
3465      if (parseSysconfigGrub(NULL, &boot))  
3466   return 0;      if (onSuse) {
3467     if (parseSuseGrubConf(NULL, &boot))
3468        return 0;
3469        } else {
3470     if (parseSysconfigGrub(NULL, &boot))
3471        return 0;
3472        }
3473    
3474      /* assume grub is not installed -- not an error condition */      /* assume grub is not installed -- not an error condition */
3475      if (!boot)      if (!boot)
# Line 2926  int checkForGrub(struct grubConfig * con Line 3488  int checkForGrub(struct grubConfig * con
3488      }      }
3489      close(fd);      close(fd);
3490    
3491        /* The more elaborate checks do not work on SuSE. The checks done
3492         * seem to be reasonble (at least for now), so just return success
3493         */
3494        if (onSuse)
3495     return 2;
3496    
3497      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3498  }  }
3499    
# Line 2959  int checkForExtLinux(struct grubConfig * Line 3527  int checkForExtLinux(struct grubConfig *
3527      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3528  }  }
3529    
3530    int checkForYaboot(struct grubConfig * config) {
3531        /*
3532         * This is a simplistic check that we consider good enough for own puporses
3533         *
3534         * If we were to properly check if yaboot is *installed* we'd need to:
3535         * 1) get the system boot device (LT_BOOT)
3536         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3537         *    the content on the boot device
3538         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3539         * 4) check again if binary and boot device contents match
3540         */
3541        if (!access("/etc/yaboot.conf", R_OK))
3542     return 2;
3543    
3544        return 1;
3545    }
3546    
3547    int checkForElilo(struct grubConfig * config) {
3548        if (!access("/etc/elilo.conf", R_OK))
3549     return 2;
3550    
3551        return 1;
3552    }
3553    
3554  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3555      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3556    
# Line 3075  int addNewKernel(struct grubConfig * con Line 3667  int addNewKernel(struct grubConfig * con
3667      while (*chptr && isspace(*chptr)) chptr++;      while (*chptr && isspace(*chptr)) chptr++;
3668      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3669    
3670      if (tmplLine->type == LT_KERNEL &&      if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) {
     tmplLine->numElements >= 2) {  
3671   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
3672      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
3673       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 3153  int addNewKernel(struct grubConfig * con Line 3744  int addNewKernel(struct grubConfig * con
3744      /* template is multi but new is not,      /* template is multi but new is not,
3745       * insert the kernel in the first module slot       * insert the kernel in the first module slot
3746       */       */
3747      tmplLine->type = LT_KERNEL;      tmplLine->type = preferredLineType(LT_KERNEL, config->cfi);
3748      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3749      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3750   strdup(getKeywordByType(LT_KERNEL, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3751     config->cfi)->key);
3752      newLine = addLineTmpl(new, tmplLine, newLine,      newLine = addLineTmpl(new, tmplLine, newLine,
3753    newKernelPath + strlen(prefix), config->cfi);    newKernelPath + strlen(prefix),
3754      config->cfi);
3755      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
3756   } else if (needs & NEED_INITRD) {   } else if (needs & NEED_INITRD) {
3757      char *initrdVal;      char *initrdVal;
3758      /* template is multi but new is not,      /* template is multi but new is not,
3759       * insert the initrd in the second module slot       * insert the initrd in the second module slot
3760       */       */
3761      tmplLine->type = LT_INITRD;      tmplLine->type = preferredLineType(LT_INITRD, config->cfi);
3762      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3763      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3764   strdup(getKeywordByType(LT_INITRD, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3765     config->cfi)->key);
3766      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3767      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3768      free(initrdVal);      free(initrdVal);
3769      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3770   }   }
3771    
3772      } else if (tmplLine->type == LT_INITRD &&      } else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) {
        tmplLine->numElements >= 2) {  
3773   if (needs & NEED_INITRD &&   if (needs & NEED_INITRD &&
3774      new->multiboot && !template->multiboot &&      new->multiboot && !template->multiboot &&
3775      config->cfi->mbInitRdIsModule) {      config->cfi->mbInitRdIsModule) {
# Line 3230  int addNewKernel(struct grubConfig * con Line 3823  int addNewKernel(struct grubConfig * con
3823      static const char *prefix = "'Loading ";      static const char *prefix = "'Loading ";
3824      if (tmplLine->numElements > 1 &&      if (tmplLine->numElements > 1 &&
3825      strstr(tmplLine->elements[1].item, prefix) &&      strstr(tmplLine->elements[1].item, prefix) &&
3826      masterLine->next && masterLine->next->type == LT_KERNEL) {      masterLine->next &&
3827        iskernel(masterLine->next->type)) {
3828   char *newTitle = malloc(strlen(prefix) +   char *newTitle = malloc(strlen(prefix) +
3829   strlen(newKernelTitle) + 2);   strlen(newKernelTitle) + 2);
3830    
# Line 3257  int addNewKernel(struct grubConfig * con Line 3851  int addNewKernel(struct grubConfig * con
3851   */   */
3852   switch (config->cfi->entryStart) {   switch (config->cfi->entryStart) {
3853      case LT_KERNEL:      case LT_KERNEL:
3854        case LT_KERNEL_EFI:
3855   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
3856      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
3857   } else {   } else {
3858      newLine = addLine(new, config->cfi, LT_KERNEL,      newLine = addLine(new, config->cfi,
3859              preferredLineType(LT_KERNEL, config->cfi),
3860        config->primaryIndent,        config->primaryIndent,
3861        newKernelPath + strlen(prefix));        newKernelPath + strlen(prefix));
3862      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
# Line 3336  int addNewKernel(struct grubConfig * con Line 3932  int addNewKernel(struct grubConfig * con
3932      if (needs & NEED_KERNEL) {      if (needs & NEED_KERNEL) {
3933   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
3934    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
3935        config->cfi)) ?        config->cfi))
3936    LT_MBMODULE : LT_KERNEL,     ? LT_MBMODULE
3937     : preferredLineType(LT_KERNEL, config->cfi),
3938    config->secondaryIndent,    config->secondaryIndent,
3939    newKernelPath + strlen(prefix));    newKernelPath + strlen(prefix));
3940   needs &= ~NEED_KERNEL;   needs &= ~NEED_KERNEL;
# Line 3353  int addNewKernel(struct grubConfig * con Line 3950  int addNewKernel(struct grubConfig * con
3950   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);
3951   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
3952    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
3953        config->cfi)) ?        config->cfi))
3954    LT_MBMODULE : LT_INITRD,     ? LT_MBMODULE
3955       : preferredLineType(LT_INITRD, config->cfi),
3956    config->secondaryIndent,    config->secondaryIndent,
3957    initrdVal);    initrdVal);
3958   free(initrdVal);   free(initrdVal);
# Line 3386  static void traceback(int signum) Line 3984  static void traceback(int signum)
3984      memset(array, '\0', sizeof (array));      memset(array, '\0', sizeof (array));
3985      size = backtrace(array, 40);      size = backtrace(array, 40);
3986    
3987      fprintf(stderr, "grubby recieved SIGSEGV!  Backtrace (%ld):\n",      fprintf(stderr, "grubby received SIGSEGV!  Backtrace (%ld):\n",
3988              (unsigned long)size);              (unsigned long)size);
3989      backtrace_symbols_fd(array, size, STDERR_FILENO);      backtrace_symbols_fd(array, size, STDERR_FILENO);
3990      exit(1);      exit(1);
# Line 3421  int main(int argc, const char ** argv) { Line 4019  int main(int argc, const char ** argv) {
4019      char * removeArgs = NULL;      char * removeArgs = NULL;
4020      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4021      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4022        char * envPath = NULL;
4023      const char * chptr = NULL;      const char * chptr = NULL;
4024      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4025      struct grubConfig * config;      struct grubConfig * config;
# Line 3429  int main(int argc, const char ** argv) { Line 4028  int main(int argc, const char ** argv) {
4028      int displayDefault = 0;      int displayDefault = 0;
4029      int displayDefaultIndex = 0;      int displayDefaultIndex = 0;
4030      int displayDefaultTitle = 0;      int displayDefaultTitle = 0;
4031        int defaultIndex = -1;
4032      struct poptOption options[] = {      struct poptOption options[] = {
4033   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
4034      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 3446  int main(int argc, const char ** argv) { Line 4046  int main(int argc, const char ** argv) {
4046   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4047      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
4048      _("bootfs") },      _("bootfs") },
4049  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4050   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
4051      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
4052  #endif  #endif
4053   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
4054      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 3469  int main(int argc, const char ** argv) { Line 4069  int main(int argc, const char ** argv) {
4069      _("display the title of the default kernel") },      _("display the title of the default kernel") },
4070   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4071      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4072     { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4073        _("force grub2 stanzas to use efi") },
4074     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4075        _("path for environment data"),
4076        _("path") },
4077   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4078      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4079   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3481  int main(int argc, const char ** argv) { Line 4086  int main(int argc, const char ** argv) {
4086   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4087      _("initrd image for the new kernel"), _("initrd-path") },      _("initrd image for the new kernel"), _("initrd-path") },
4088   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4089      _("auxilliary initrd image for things other than the new kernel"), _("initrd-path") },      _("auxiliary initrd image for things other than the new kernel"), _("initrd-path") },
4090   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4091      _("configure lilo bootloader") },      _("configure lilo bootloader") },
4092   { "make-default", 0, 0, &makeDefault, 0,   { "make-default", 0, 0, &makeDefault, 0,
# Line 3501  int main(int argc, const char ** argv) { Line 4106  int main(int argc, const char ** argv) {
4106   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
4107      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
4108        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
4109     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4110        _("make the given entry index the default entry"),
4111        _("entry-index") },
4112   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4113      _("configure silo bootloader") },      _("configure silo bootloader") },
4114   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 3522  int main(int argc, const char ** argv) { Line 4130  int main(int argc, const char ** argv) {
4130    
4131      signal(SIGSEGV, traceback);      signal(SIGSEGV, traceback);
4132    
4133        int i = 0;
4134        for (int j = 1; j < argc; j++)
4135     i += strlen(argv[j]) + 1;
4136        saved_command_line = malloc(i);
4137        if (!saved_command_line) {
4138     fprintf(stderr, "grubby: %m\n");
4139     exit(1);
4140        }
4141        saved_command_line[0] = '\0';
4142        for (int j = 1; j < argc; j++) {
4143     strcat(saved_command_line, argv[j]);
4144     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4145        }
4146    
4147      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4148      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
4149    
# Line 3565  int main(int argc, const char ** argv) { Line 4187  int main(int argc, const char ** argv) {
4187   return 1;   return 1;
4188      } else if (configureGrub2) {      } else if (configureGrub2) {
4189   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4190     if (envPath)
4191        cfi->envFile = envPath;
4192      } else if (configureLilo) {      } else if (configureLilo) {
4193   cfi = &liloConfigType;   cfi = &liloConfigType;
4194      } else if (configureGrub) {      } else if (configureGrub) {
# Line 3609  int main(int argc, const char ** argv) { Line 4233  int main(int argc, const char ** argv) {
4233      }      }
4234    
4235      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
4236    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
4237    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
4238        (defaultIndex >= 0))) {
4239   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
4240    "specified option"));    "specified option"));
4241   return 1;   return 1;
# Line 3652  int main(int argc, const char ** argv) { Line 4277  int main(int argc, const char ** argv) {
4277   makeDefault = 1;   makeDefault = 1;
4278   defaultKernel = NULL;   defaultKernel = NULL;
4279      }      }
4280        else if (defaultKernel && (defaultIndex >= 0)) {
4281     fprintf(stderr, _("grubby: --set-default and --set-default-index "
4282      "may not be used together\n"));
4283     return 1;
4284        }
4285    
4286      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
4287   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
# Line 3660  int main(int argc, const char ** argv) { Line 4290  int main(int argc, const char ** argv) {
4290      }      }
4291    
4292      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
4293   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
4294          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
4295     && (defaultIndex == -1)) {
4296   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
4297   return 1;   return 1;
4298      }      }
# Line 3688  int main(int argc, const char ** argv) { Line 4319  int main(int argc, const char ** argv) {
4319      }      }
4320    
4321      if (bootloaderProbe) {      if (bootloaderProbe) {
4322   int lrc = 0, grc = 0, gr2c = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4323   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4324    
4325   const char *grub2config = grub2FindConfig(&grub2ConfigType);   const char *grub2config = grub2FindConfig(&grub2ConfigType);
4326   if (grub2config) {   if (grub2config) {
# Line 3717  int main(int argc, const char ** argv) { Line 4348  int main(int argc, const char ** argv) {
4348   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4349   }   }
4350    
4351     if (!access(eliloConfigType.defaultConfig, F_OK)) {
4352        econfig = readConfig(eliloConfigType.defaultConfig,
4353     &eliloConfigType);
4354        if (!econfig)
4355     erc = 1;
4356        else
4357     erc = checkForElilo(econfig);
4358     }
4359    
4360   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4361      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4362      if (!lconfig)      if (!lconfig)
4363   erc = 1;   extrc = 1;
4364      else      else
4365   erc = checkForExtLinux(lconfig);   extrc = checkForExtLinux(lconfig);
4366   }   }
4367    
4368   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;  
4369     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4370        yconfig = readConfig(yabootConfigType.defaultConfig,
4371     &yabootConfigType);
4372        if (!yconfig)
4373     yrc = 1;
4374        else
4375     yrc = checkForYaboot(yconfig);
4376     }
4377    
4378     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4379     erc == 1)
4380        return 1;
4381    
4382   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4383   if (gr2c == 2) printf("grub2\n");   if (gr2c == 2) printf("grub2\n");
4384   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4385   if (erc == 2) printf("extlinux\n");   if (extrc == 2) printf("extlinux\n");
4386     if (yrc == 2) printf("yaboot\n");
4387     if (erc == 2) printf("elilo\n");
4388    
4389   return 0;   return 0;
4390      }      }
4391    
4392        if (grubConfig == NULL) {
4393     printf("Could not find bootloader configuration file.\n");
4394     exit(1);
4395        }
4396    
4397      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4398      if (!config) return 1;      if (!config) return 1;
4399    
# Line 3744  int main(int argc, const char ** argv) { Line 4403  int main(int argc, const char ** argv) {
4403          char * rootspec;          char * rootspec;
4404    
4405   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4406     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4407     cfi->defaultIsSaved)
4408        config->defaultImage = 0;
4409   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4410   if (!entry) return 0;   if (!entry) return 0;
4411   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4412    
4413   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
4414   if (!line) return 0;   if (!line) return 0;
4415    
4416          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 3762  int main(int argc, const char ** argv) { Line 4424  int main(int argc, const char ** argv) {
4424   struct singleEntry * entry;   struct singleEntry * entry;
4425    
4426   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4427     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4428     cfi->defaultIsSaved)
4429        config->defaultImage = 0;
4430   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4431   if (!entry) return 0;   if (!entry) return 0;
4432    
# Line 3784  int main(int argc, const char ** argv) { Line 4449  int main(int argc, const char ** argv) {
4449    
4450      } else if (displayDefaultIndex) {      } else if (displayDefaultIndex) {
4451          if (config->defaultImage == -1) return 0;          if (config->defaultImage == -1) return 0;
4452     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4453     cfi->defaultIsSaved)
4454        config->defaultImage = 0;
4455          printf("%i\n", config->defaultImage);          printf("%i\n", config->defaultImage);
4456            return 0;
4457    
4458      } else if (kernelInfo)      } else if (kernelInfo)
4459   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
# Line 3797  int main(int argc, const char ** argv) { Line 4466  int main(int argc, const char ** argv) {
4466      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4467      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4468      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4469      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4470      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4471      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4472                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;

Legend:
Removed from v.1849  
changed lines
  Added in v.2257