Magellan Linux

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

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

revision 1853 by niro, Mon Jul 2 13:12:07 2012 UTC revision 2258 by niro, Mon Oct 21 14:02:25 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 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 904  static int getNextLine(char ** bufPtr, s Line 1066  static int getNextLine(char ** bufPtr, s
1066      return 0;      return 0;
1067  }  }
1068    
1069    static int isnumber(const char *s)
1070    {
1071        int i;
1072        for (i = 0; s[i] != '\0'; i++)
1073     if (s[i] < '0' || s[i] > '9')
1074        return 0;
1075        return i;
1076    }
1077    
1078  static struct grubConfig * readConfig(const char * inName,  static struct grubConfig * readConfig(const char * inName,
1079        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
1080      int in;      int in;
# Line 918  static struct grubConfig * readConfig(co Line 1089  static struct grubConfig * readConfig(co
1089      int len;      int len;
1090      char * buf;      char * buf;
1091    
1092      if (!strcmp(inName, "-")) {      if (inName == NULL) {
1093            printf("Could not find bootloader configuration\n");
1094            exit(1);
1095        } else if (!strcmp(inName, "-")) {
1096   in = 0;   in = 0;
1097      } else {      } else {
1098   if ((in = open(inName, O_RDONLY)) < 0) {   if ((in = open(inName, O_RDONLY)) < 0) {
# Line 985  static struct grubConfig * readConfig(co Line 1159  static struct grubConfig * readConfig(co
1159      dbgPrintf("\n");      dbgPrintf("\n");
1160      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1161      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
1162      !strcmp(line->elements[1].item, kwType->key)) {      !strcmp(line->elements[1].item, kwType->key) &&
1163        !is_special_grub2_variable(line->elements[2].item)) {
1164   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1165   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1166   defaultLine = line;   defaultLine = line;
# Line 994  static struct grubConfig * readConfig(co Line 1169  static struct grubConfig * readConfig(co
1169      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1170      defaultLine = line;      defaultLine = line;
1171    
1172          } else if (line->type == LT_KERNEL) {          } else if (iskernel(line->type)) {
1173      /* if by some freak chance this is multiboot and the "module"      /* if by some freak chance this is multiboot and the "module"
1174       * lines came earlier in the template, make sure to use LT_HYPER       * lines came earlier in the template, make sure to use LT_HYPER
1175       * instead of LT_KERNEL now       * instead of LT_KERNEL now
# Line 1011  static struct grubConfig * readConfig(co Line 1186  static struct grubConfig * readConfig(co
1186      for (struct singleLine *l = entry->lines; l; l = l->next) {      for (struct singleLine *l = entry->lines; l; l = l->next) {
1187   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1188      break;      break;
1189   else if (l->type == LT_KERNEL) {   else if (iskernel(l->type)) {
1190      l->type = LT_HYPER;      l->type = LT_HYPER;
1191      break;      break;
1192   }   }
# Line 1190  static struct grubConfig * readConfig(co Line 1365  static struct grubConfig * readConfig(co
1365          if (defaultLine->numElements > 2 &&          if (defaultLine->numElements > 2 &&
1366      cfi->defaultSupportSaved &&      cfi->defaultSupportSaved &&
1367      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1368      cfg->defaultImage = DEFAULT_SAVED_GRUB2;   cfg->cfi->defaultIsSaved = 1;
1369     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1370     if (cfg->cfi->getEnv) {
1371        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1372        if (defTitle) {
1373     int index = 0;
1374     if (isnumber(defTitle)) {
1375        index = atoi(defTitle);
1376        entry = findEntryByIndex(cfg, index);
1377     } else {
1378        entry = findEntryByTitle(cfg, defTitle, &index);
1379     }
1380     if (entry)
1381        cfg->defaultImage = index;
1382        }
1383     }
1384   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1385      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1386      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
# Line 1231  static struct grubConfig * readConfig(co Line 1421  static struct grubConfig * readConfig(co
1421          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1422      }      }
1423   }   }
1424        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1425     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1426     if (defTitle) {
1427        int index = 0;
1428        if (isnumber(defTitle)) {
1429     index = atoi(defTitle);
1430     entry = findEntryByIndex(cfg, index);
1431        } else {
1432     entry = findEntryByTitle(cfg, defTitle, &index);
1433        }
1434        if (entry)
1435     cfg->defaultImage = index;
1436     }
1437      } else {      } else {
1438          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1439      }      }
# Line 1248  static void writeDefault(FILE * out, cha Line 1451  static void writeDefault(FILE * out, cha
1451    
1452      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1453   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1454      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1455   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1456      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1457        char *title;
1458        entry = findEntryByIndex(cfg, cfg->defaultImage);
1459        line = getLineByType(LT_MENUENTRY, entry->lines);
1460        if (!line)
1461     line = getLineByType(LT_TITLE, entry->lines);
1462        if (line) {
1463     title = extractTitle(line);
1464     if (title)
1465        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1466        }
1467     }
1468        } else if (cfg->defaultImage > -1) {
1469   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1470      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1471          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 1310  static int writeConfig(struct grubConfig Line 1525  static int writeConfig(struct grubConfig
1525    
1526      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1527         directory to the dir of the symlink */         directory to the dir of the symlink */
1528              rc = chdir(dirname(outName));      char *dir = strdupa(outName);
1529        rc = chdir(dirname(dir));
1530      do {      do {
1531   buf = alloca(len + 1);   buf = alloca(len + 1);
1532   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1352  static int writeConfig(struct grubConfig Line 1568  static int writeConfig(struct grubConfig
1568      while (line) {      while (line) {
1569          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1570   line->numElements == 3 &&   line->numElements == 3 &&
1571   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1572     !is_special_grub2_variable(line->elements[2].item)) {
1573      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1574      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1575   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1497  static char *findDiskForRoot() Line 1714  static char *findDiskForRoot()
1714      return NULL;      return NULL;
1715  }  }
1716    
1717  void printEntry(struct singleEntry * entry) {  void printEntry(struct singleEntry * entry, FILE *f) {
1718      int i;      int i;
1719      struct singleLine * line;      struct singleLine * line;
1720    
1721      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1722   fprintf(stderr, "DBG: %s", line->indent);   log_message(f, "DBG: %s", line->indent);
1723   for (i = 0; i < line->numElements; i++) {   for (i = 0; i < line->numElements; i++) {
1724      /* Need to handle this, because we strip the quotes from      /* Need to handle this, because we strip the quotes from
1725       * menuentry when read it. */       * menuentry when read it. */
1726      if (line->type == LT_MENUENTRY && i == 1) {      if (line->type == LT_MENUENTRY && i == 1) {
1727   if(!isquote(*line->elements[i].item))   if(!isquote(*line->elements[i].item))
1728      fprintf(stderr, "\'%s\'", line->elements[i].item);      log_message(f, "\'%s\'", line->elements[i].item);
1729   else   else
1730      fprintf(stderr, "%s", line->elements[i].item);      log_message(f, "%s", line->elements[i].item);
1731   fprintf(stderr, "%s", line->elements[i].indent);   log_message(f, "%s", line->elements[i].indent);
1732    
1733   continue;   continue;
1734      }      }
1735            
1736      fprintf(stderr, "%s%s",      log_message(f, "%s%s",
1737      line->elements[i].item, line->elements[i].indent);      line->elements[i].item, line->elements[i].indent);
1738   }   }
1739   fprintf(stderr, "\n");   log_message(f, "\n");
1740      }      }
1741  }  }
1742    
1743  void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)  void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1744  {  {
1745      va_list argp;      static int once;
1746        va_list argp, argq;
1747    
1748        va_start(argp, fmt);
1749    
1750        va_copy(argq, argp);
1751        if (!once) {
1752     log_time(NULL);
1753     log_message(NULL, "command line: %s\n", saved_command_line);
1754        }
1755        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1756        log_vmessage(NULL, fmt, argq);
1757    
1758        printEntry(entry, NULL);
1759        va_end(argq);
1760    
1761      if (!debug)      if (!debug) {
1762     once = 1;
1763         va_end(argp);
1764   return;   return;
1765        }
1766    
1767      va_start(argp, fmt);      if (okay) {
1768     va_end(argp);
1769     return;
1770        }
1771    
1772        if (!once)
1773     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1774        once = 1;
1775      fprintf(stderr, "DBG: Image entry failed: ");      fprintf(stderr, "DBG: Image entry failed: ");
1776      vfprintf(stderr, fmt, argp);      vfprintf(stderr, fmt, argp);
1777      printEntry(entry);      printEntry(entry, stderr);
1778      va_end(argp);      va_end(argp);
1779  }  }
1780    
# Line 1560  int suitableImage(struct singleEntry * e Line 1801  int suitableImage(struct singleEntry * e
1801      char * rootdev;      char * rootdev;
1802    
1803      if (skipRemoved && entry->skip) {      if (skipRemoved && entry->skip) {
1804   notSuitablePrintf(entry, "marked to skip\n");   notSuitablePrintf(entry, 0, "marked to skip\n");
1805   return 0;   return 0;
1806      }      }
1807    
1808      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1809      if (!line) {      if (!line) {
1810   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1811   return 0;   return 0;
1812      }      }
1813      if (line->numElements < 2) {      if (line->numElements < 2) {
1814   notSuitablePrintf(entry, "line has only %d elements\n",   notSuitablePrintf(entry, 0, "line has only %d elements\n",
1815      line->numElements);      line->numElements);
1816   return 0;   return 0;
1817      }      }
1818    
1819      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) {
1820        notSuitablePrintf(entry, 1, "\n");
1821        return 1;
1822        }
1823    
1824      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1825        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
# Line 1586  int suitableImage(struct singleEntry * e Line 1830  int suitableImage(struct singleEntry * e
1830      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1831              line->elements[1].item + rootspec_offset);              line->elements[1].item + rootspec_offset);
1832      if (access(fullName, R_OK)) {      if (access(fullName, R_OK)) {
1833   notSuitablePrintf(entry, "access to %s failed\n", fullName);   notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1834   return 0;   return 0;
1835      }      }
1836      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
# Line 1607  int suitableImage(struct singleEntry * e Line 1851  int suitableImage(struct singleEntry * e
1851    
1852              /* failed to find one */              /* failed to find one */
1853              if (!line) {              if (!line) {
1854   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1855   return 0;   return 0;
1856              }              }
1857    
# Line 1616  int suitableImage(struct singleEntry * e Line 1860  int suitableImage(struct singleEntry * e
1860      if (i < line->numElements)      if (i < line->numElements)
1861          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1862      else {      else {
1863   notSuitablePrintf(entry, "no root= entry found\n");   notSuitablePrintf(entry, 0, "no root= entry found\n");
1864   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1865          return 0;          return 0;
1866              }              }
# Line 1625  int suitableImage(struct singleEntry * e Line 1869  int suitableImage(struct singleEntry * e
1869    
1870      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1871      if (!getpathbyspec(dev)) {      if (!getpathbyspec(dev)) {
1872          notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);          notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1873          return 0;          return 0;
1874      } else      } else
1875   dev = getpathbyspec(dev);   dev = getpathbyspec(dev);
1876    
1877      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1878      if (!rootdev) {      if (!rootdev) {
1879          notSuitablePrintf(entry, "can't find root device\n");          notSuitablePrintf(entry, 0, "can't find root device\n");
1880   return 0;   return 0;
1881      }      }
1882    
1883      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1884          notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1885   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1886          free(rootdev);          free(rootdev);
1887          return 0;          return 0;
1888      }      }
1889    
1890      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1891          notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n",
1892   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1893   free(rootdev);   free(rootdev);
1894          return 0;          return 0;
1895      }      }
1896    
1897      free(rootdev);      free(rootdev);
1898        notSuitablePrintf(entry, 1, "\n");
1899    
1900      return 1;      return 1;
1901  }  }
# Line 1694  struct singleEntry * findEntryByPath(str Line 1939  struct singleEntry * findEntryByPath(str
1939   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
1940   if (!entry) return NULL;   if (!entry) return NULL;
1941    
1942   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1943   if (!line) return NULL;   if (!line) return NULL;
1944    
1945   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1743  struct singleEntry * findEntryByPath(str Line 1988  struct singleEntry * findEntryByPath(str
1988    
1989      /* check all the lines matching checkType */      /* check all the lines matching checkType */
1990      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1991   line = getLineByType(entry->multiboot && checkType == LT_KERNEL ?   enum lineType_e ct = checkType;
1992       LT_KERNEL|LT_MBMODULE|LT_HYPER :   if (entry->multiboot && checkType == LT_KERNEL)
1993       checkType, line);      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER;
1994   if (!line) break;  /* not found in this entry */   else if (checkType & LT_KERNEL)
1995        ct = checkType | LT_KERNEL_EFI;
1996     line = getLineByType(ct, line);
1997     if (!line)
1998        break;  /* not found in this entry */
1999    
2000   if (line && line->type != LT_MENUENTRY &&   if (line && line->type != LT_MENUENTRY &&
2001   line->numElements >= 2) {   line->numElements >= 2) {
# Line 1765  struct singleEntry * findEntryByPath(str Line 2014  struct singleEntry * findEntryByPath(str
2014       * non-Linux boot entries (could find netbsd etc, though, which is       * non-Linux boot entries (could find netbsd etc, though, which is
2015       * unfortunate)       * unfortunate)
2016       */       */
2017      if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines))      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines))
2018   break; /* found 'im! */   break; /* found 'im! */
2019   }   }
2020    
# Line 1775  struct singleEntry * findEntryByPath(str Line 2024  struct singleEntry * findEntryByPath(str
2024      return entry;      return entry;
2025  }  }
2026    
2027    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2028          int * index) {
2029        struct singleEntry * entry;
2030        struct singleLine * line;
2031        int i;
2032        char * newtitle;
2033    
2034        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2035     if (index && i < *index)
2036        continue;
2037     line = getLineByType(LT_TITLE, entry->lines);
2038     if (!line)
2039        line = getLineByType(LT_MENUENTRY, entry->lines);
2040     if (!line)
2041        continue;
2042     newtitle = grub2ExtractTitle(line);
2043     if (!newtitle)
2044        continue;
2045     if (!strcmp(title, newtitle))
2046        break;
2047        }
2048    
2049        if (!entry)
2050     return NULL;
2051    
2052        if (index)
2053     *index = i;
2054        return entry;
2055    }
2056    
2057  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
2058      struct singleEntry * entry;      struct singleEntry * entry;
2059    
# Line 1797  struct singleEntry * findTemplate(struct Line 2076  struct singleEntry * findTemplate(struct
2076      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2077      int index;      int index;
2078    
2079      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2080     if (cfg->cfi->getEnv) {
2081        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2082        if (defTitle) {
2083     int index = 0;
2084     if (isnumber(defTitle)) {
2085        index = atoi(defTitle);
2086        entry = findEntryByIndex(cfg, index);
2087     } else {
2088        entry = findEntryByTitle(cfg, defTitle, &index);
2089     }
2090     if (entry)
2091        cfg->defaultImage = index;
2092        }
2093     }
2094        } else if (cfg->defaultImage > -1) {
2095   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2096   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2097      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 1867  void markRemovedImage(struct grubConfig Line 2161  void markRemovedImage(struct grubConfig
2161    
2162  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
2163       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
2164       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
2165      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
2166      int i, j;      int i, j;
2167    
2168      if (newIsDefault) {      if (newIsDefault) {
2169   config->defaultImage = 0;   config->defaultImage = 0;
2170   return;   return;
2171        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
2172     if (findEntryByIndex(config, index))
2173        config->defaultImage = index;
2174     else
2175        config->defaultImage = -1;
2176     return;
2177      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
2178   i = 0;   i = 0;
2179   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1948  void displayEntry(struct singleEntry * e Line 2248  void displayEntry(struct singleEntry * e
2248    
2249      printf("index=%d\n", index);      printf("index=%d\n", index);
2250    
2251      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
2252      if (!line) {      if (!line) {
2253          printf("non linux entry\n");          printf("non linux entry\n");
2254          return;          return;
# Line 2013  void displayEntry(struct singleEntry * e Line 2313  void displayEntry(struct singleEntry * e
2313   printf("root=%s\n", s);   printf("root=%s\n", s);
2314      }      }
2315    
2316      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);
2317    
2318      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2319   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
# Line 2414  struct singleLine * addLineTmpl(struct s Line 2714  struct singleLine * addLineTmpl(struct s
2714  {  {
2715      struct singleLine * newLine = lineDup(tmplLine);      struct singleLine * newLine = lineDup(tmplLine);
2716    
2717        if (isEfi && cfi == &grub2ConfigType) {
2718     enum lineType_e old = newLine->type;
2719     newLine->type = preferredLineType(newLine->type, cfi);
2720     if (old != newLine->type)
2721        newLine->elements[0].item = getKeyByType(newLine->type, cfi);
2722        }
2723    
2724      if (val) {      if (val) {
2725   /* override the inherited value with our own.   /* override the inherited value with our own.
2726   * 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 2423  struct singleLine * addLineTmpl(struct s Line 2730  struct singleLine * addLineTmpl(struct s
2730   insertElement(newLine, val, 1, cfi);   insertElement(newLine, val, 1, cfi);
2731    
2732   /* but try to keep the rootspec from the template... sigh */   /* but try to keep the rootspec from the template... sigh */
2733   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)) {
2734      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2735      if (rootspec != NULL) {      if (rootspec != NULL) {
2736   free(newLine->elements[1].item);   free(newLine->elements[1].item);
# Line 2460  struct singleLine *  addLine(struct sing Line 2767  struct singleLine *  addLine(struct sing
2767      /* 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
2768       * stack since it calls addLineTmpl which will make copies.       * stack since it calls addLineTmpl which will make copies.
2769       */       */
   
2770      if (type == LT_TITLE && cfi->titleBracketed) {      if (type == LT_TITLE && cfi->titleBracketed) {
2771   /* we're doing a bracketed title (zipl) */   /* we're doing a bracketed title (zipl) */
2772   tmpl.type = type;   tmpl.type = type;
# Line 2794  int updateActualImage(struct grubConfig Line 3100  int updateActualImage(struct grubConfig
3100      firstElement = 2;      firstElement = 2;
3101    
3102   } else {   } else {
3103      line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines);
3104      if (!line) {      if (!line) {
3105   /* no LT_KERNEL or LT_MBMODULE in this entry? */   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3106   continue;   continue;
# Line 2959  int updateInitrd(struct grubConfig * cfg Line 3265  int updateInitrd(struct grubConfig * cfg
3265      if (!image) return 0;      if (!image) return 0;
3266    
3267      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3268          kernelLine = getLineByType(LT_KERNEL, entry->lines);          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines);
3269          if (!kernelLine) continue;          if (!kernelLine) continue;
3270    
3271          line = getLineByType(LT_INITRD, entry->lines);          line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);
3272          if (line)          if (line)
3273              removeLine(entry, line);              removeLine(entry, line);
3274          if (prefix) {          if (prefix) {
# Line 2973  int updateInitrd(struct grubConfig * cfg Line 3279  int updateInitrd(struct grubConfig * cfg
3279   endLine = getLineByType(LT_ENTRY_END, entry->lines);   endLine = getLineByType(LT_ENTRY_END, entry->lines);
3280   if (endLine)   if (endLine)
3281      removeLine(entry, endLine);      removeLine(entry, endLine);
3282          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);          line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi),
3283     kernelLine->indent, initrd);
3284          if (!line)          if (!line)
3285      return 1;      return 1;
3286   if (endLine) {   if (endLine) {
# Line 3263  int checkForYaboot(struct grubConfig * c Line 3570  int checkForYaboot(struct grubConfig * c
3570      return 1;      return 1;
3571  }  }
3572    
3573    int checkForElilo(struct grubConfig * config) {
3574        if (!access("/etc/elilo.conf", R_OK))
3575     return 2;
3576    
3577        return 1;
3578    }
3579    
3580  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3581      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3582    
# Line 3379  int addNewKernel(struct grubConfig * con Line 3693  int addNewKernel(struct grubConfig * con
3693      while (*chptr && isspace(*chptr)) chptr++;      while (*chptr && isspace(*chptr)) chptr++;
3694      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3695    
3696      if (tmplLine->type == LT_KERNEL &&      if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) {
     tmplLine->numElements >= 2) {  
3697   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
3698      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
3699       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 3457  int addNewKernel(struct grubConfig * con Line 3770  int addNewKernel(struct grubConfig * con
3770      /* template is multi but new is not,      /* template is multi but new is not,
3771       * insert the kernel in the first module slot       * insert the kernel in the first module slot
3772       */       */
3773      tmplLine->type = LT_KERNEL;      tmplLine->type = preferredLineType(LT_KERNEL, config->cfi);
3774      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3775      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3776   strdup(getKeywordByType(LT_KERNEL, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3777     config->cfi)->key);
3778      newLine = addLineTmpl(new, tmplLine, newLine,      newLine = addLineTmpl(new, tmplLine, newLine,
3779    newKernelPath + strlen(prefix), config->cfi);    newKernelPath + strlen(prefix),
3780      config->cfi);
3781      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
3782   } else if (needs & NEED_INITRD) {   } else if (needs & NEED_INITRD) {
3783      char *initrdVal;      char *initrdVal;
3784      /* template is multi but new is not,      /* template is multi but new is not,
3785       * insert the initrd in the second module slot       * insert the initrd in the second module slot
3786       */       */
3787      tmplLine->type = LT_INITRD;      tmplLine->type = preferredLineType(LT_INITRD, config->cfi);
3788      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3789      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3790   strdup(getKeywordByType(LT_INITRD, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3791     config->cfi)->key);
3792      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3793      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3794      free(initrdVal);      free(initrdVal);
3795      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3796   }   }
3797    
3798      } else if (tmplLine->type == LT_INITRD &&      } else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) {
        tmplLine->numElements >= 2) {  
3799   if (needs & NEED_INITRD &&   if (needs & NEED_INITRD &&
3800      new->multiboot && !template->multiboot &&      new->multiboot && !template->multiboot &&
3801      config->cfi->mbInitRdIsModule) {      config->cfi->mbInitRdIsModule) {
# Line 3534  int addNewKernel(struct grubConfig * con Line 3849  int addNewKernel(struct grubConfig * con
3849      static const char *prefix = "'Loading ";      static const char *prefix = "'Loading ";
3850      if (tmplLine->numElements > 1 &&      if (tmplLine->numElements > 1 &&
3851      strstr(tmplLine->elements[1].item, prefix) &&      strstr(tmplLine->elements[1].item, prefix) &&
3852      masterLine->next && masterLine->next->type == LT_KERNEL) {      masterLine->next &&
3853        iskernel(masterLine->next->type)) {
3854   char *newTitle = malloc(strlen(prefix) +   char *newTitle = malloc(strlen(prefix) +
3855   strlen(newKernelTitle) + 2);   strlen(newKernelTitle) + 2);
3856    
# Line 3561  int addNewKernel(struct grubConfig * con Line 3877  int addNewKernel(struct grubConfig * con
3877   */   */
3878   switch (config->cfi->entryStart) {   switch (config->cfi->entryStart) {
3879      case LT_KERNEL:      case LT_KERNEL:
3880        case LT_KERNEL_EFI:
3881   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
3882      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
3883   } else {   } else {
3884      newLine = addLine(new, config->cfi, LT_KERNEL,      newLine = addLine(new, config->cfi,
3885              preferredLineType(LT_KERNEL, config->cfi),
3886        config->primaryIndent,        config->primaryIndent,
3887        newKernelPath + strlen(prefix));        newKernelPath + strlen(prefix));
3888      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
# Line 3640  int addNewKernel(struct grubConfig * con Line 3958  int addNewKernel(struct grubConfig * con
3958      if (needs & NEED_KERNEL) {      if (needs & NEED_KERNEL) {
3959   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
3960    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
3961        config->cfi)) ?        config->cfi))
3962    LT_MBMODULE : LT_KERNEL,     ? LT_MBMODULE
3963     : preferredLineType(LT_KERNEL, config->cfi),
3964    config->secondaryIndent,    config->secondaryIndent,
3965    newKernelPath + strlen(prefix));    newKernelPath + strlen(prefix));
3966   needs &= ~NEED_KERNEL;   needs &= ~NEED_KERNEL;
# Line 3657  int addNewKernel(struct grubConfig * con Line 3976  int addNewKernel(struct grubConfig * con
3976   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);
3977   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
3978    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
3979        config->cfi)) ?        config->cfi))
3980    LT_MBMODULE : LT_INITRD,     ? LT_MBMODULE
3981       : preferredLineType(LT_INITRD, config->cfi),
3982    config->secondaryIndent,    config->secondaryIndent,
3983    initrdVal);    initrdVal);
3984   free(initrdVal);   free(initrdVal);
# Line 3690  static void traceback(int signum) Line 4010  static void traceback(int signum)
4010      memset(array, '\0', sizeof (array));      memset(array, '\0', sizeof (array));
4011      size = backtrace(array, 40);      size = backtrace(array, 40);
4012    
4013      fprintf(stderr, "grubby recieved SIGSEGV!  Backtrace (%ld):\n",      fprintf(stderr, "grubby received SIGSEGV!  Backtrace (%ld):\n",
4014              (unsigned long)size);              (unsigned long)size);
4015      backtrace_symbols_fd(array, size, STDERR_FILENO);      backtrace_symbols_fd(array, size, STDERR_FILENO);
4016      exit(1);      exit(1);
# Line 3725  int main(int argc, const char ** argv) { Line 4045  int main(int argc, const char ** argv) {
4045      char * removeArgs = NULL;      char * removeArgs = NULL;
4046      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4047      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4048        char * envPath = NULL;
4049      const char * chptr = NULL;      const char * chptr = NULL;
4050      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4051      struct grubConfig * config;      struct grubConfig * config;
# Line 3733  int main(int argc, const char ** argv) { Line 4054  int main(int argc, const char ** argv) {
4054      int displayDefault = 0;      int displayDefault = 0;
4055      int displayDefaultIndex = 0;      int displayDefaultIndex = 0;
4056      int displayDefaultTitle = 0;      int displayDefaultTitle = 0;
4057        int defaultIndex = -1;
4058      struct poptOption options[] = {      struct poptOption options[] = {
4059   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
4060      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 3750  int main(int argc, const char ** argv) { Line 4072  int main(int argc, const char ** argv) {
4072   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4073      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
4074      _("bootfs") },      _("bootfs") },
4075  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4076   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
4077      _("check which bootloader is installed on boot sector") },      _("check which bootloader is installed on boot sector") },
4078  #endif  #endif
# Line 3773  int main(int argc, const char ** argv) { Line 4095  int main(int argc, const char ** argv) {
4095      _("display the title of the default kernel") },      _("display the title of the default kernel") },
4096   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4097      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4098     { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4099        _("force grub2 stanzas to use efi") },
4100     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4101        _("path for environment data"),
4102        _("path") },
4103   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4104      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4105   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3785  int main(int argc, const char ** argv) { Line 4112  int main(int argc, const char ** argv) {
4112   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4113      _("initrd image for the new kernel"), _("initrd-path") },      _("initrd image for the new kernel"), _("initrd-path") },
4114   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4115      _("auxilliary initrd image for things other than the new kernel"), _("initrd-path") },      _("auxiliary initrd image for things other than the new kernel"), _("initrd-path") },
4116   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4117      _("configure lilo bootloader") },      _("configure lilo bootloader") },
4118   { "make-default", 0, 0, &makeDefault, 0,   { "make-default", 0, 0, &makeDefault, 0,
# Line 3805  int main(int argc, const char ** argv) { Line 4132  int main(int argc, const char ** argv) {
4132   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
4133      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
4134        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
4135     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4136        _("make the given entry index the default entry"),
4137        _("entry-index") },
4138   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4139      _("configure silo bootloader") },      _("configure silo bootloader") },
4140   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 3826  int main(int argc, const char ** argv) { Line 4156  int main(int argc, const char ** argv) {
4156    
4157      signal(SIGSEGV, traceback);      signal(SIGSEGV, traceback);
4158    
4159        int i = 0;
4160        for (int j = 1; j < argc; j++)
4161     i += strlen(argv[j]) + 1;
4162        saved_command_line = malloc(i);
4163        if (!saved_command_line) {
4164     fprintf(stderr, "grubby: %m\n");
4165     exit(1);
4166        }
4167        saved_command_line[0] = '\0';
4168        for (int j = 1; j < argc; j++) {
4169     strcat(saved_command_line, argv[j]);
4170     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4171        }
4172    
4173      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4174      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
4175    
# Line 3869  int main(int argc, const char ** argv) { Line 4213  int main(int argc, const char ** argv) {
4213   return 1;   return 1;
4214      } else if (configureGrub2) {      } else if (configureGrub2) {
4215   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4216     if (envPath)
4217        cfi->envFile = envPath;
4218      } else if (configureLilo) {      } else if (configureLilo) {
4219   cfi = &liloConfigType;   cfi = &liloConfigType;
4220      } else if (configureGrub) {      } else if (configureGrub) {
# Line 3913  int main(int argc, const char ** argv) { Line 4259  int main(int argc, const char ** argv) {
4259      }      }
4260    
4261      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
4262    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
4263    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
4264        (defaultIndex >= 0))) {
4265   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
4266    "specified option"));    "specified option"));
4267   return 1;   return 1;
# Line 3956  int main(int argc, const char ** argv) { Line 4303  int main(int argc, const char ** argv) {
4303   makeDefault = 1;   makeDefault = 1;
4304   defaultKernel = NULL;   defaultKernel = NULL;
4305      }      }
4306        else if (defaultKernel && (defaultIndex >= 0)) {
4307     fprintf(stderr, _("grubby: --set-default and --set-default-index "
4308      "may not be used together\n"));
4309     return 1;
4310        }
4311    
4312      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
4313   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
# Line 3964  int main(int argc, const char ** argv) { Line 4316  int main(int argc, const char ** argv) {
4316      }      }
4317    
4318      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
4319   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
4320          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
4321     && (defaultIndex == -1)) {
4322   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
4323   return 1;   return 1;
4324      }      }
# Line 3992  int main(int argc, const char ** argv) { Line 4345  int main(int argc, const char ** argv) {
4345      }      }
4346    
4347      if (bootloaderProbe) {      if (bootloaderProbe) {
4348   int lrc = 0, grc = 0, gr2c = 0, erc = 0, yrc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4349   struct grubConfig * lconfig, * gconfig, * yconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4350    
4351   const char *grub2config = grub2FindConfig(&grub2ConfigType);   const char *grub2config = grub2FindConfig(&grub2ConfigType);
4352   if (grub2config) {   if (grub2config) {
# Line 4021  int main(int argc, const char ** argv) { Line 4374  int main(int argc, const char ** argv) {
4374   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4375   }   }
4376    
4377     if (!access(eliloConfigType.defaultConfig, F_OK)) {
4378        econfig = readConfig(eliloConfigType.defaultConfig,
4379     &eliloConfigType);
4380        if (!econfig)
4381     erc = 1;
4382        else
4383     erc = checkForElilo(econfig);
4384     }
4385    
4386   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4387      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4388      if (!lconfig)      if (!lconfig)
4389   erc = 1;   extrc = 1;
4390      else      else
4391   erc = checkForExtLinux(lconfig);   extrc = checkForExtLinux(lconfig);
4392   }   }
4393    
4394    
# Line 4036  int main(int argc, const char ** argv) { Line 4398  int main(int argc, const char ** argv) {
4398      if (!yconfig)      if (!yconfig)
4399   yrc = 1;   yrc = 1;
4400      else      else
4401        yrc = checkForYaboot(lconfig);   yrc = checkForYaboot(yconfig);
4402   }   }
4403    
4404   if (lrc == 1 || grc == 1 || gr2c == 1 || yrc == 1) return 1;   if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4405     erc == 1)
4406        return 1;
4407    
4408   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4409   if (gr2c == 2) printf("grub2\n");   if (gr2c == 2) printf("grub2\n");
4410   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4411   if (erc == 2) printf("extlinux\n");   if (extrc == 2) printf("extlinux\n");
4412   if (yrc == 2) printf("yaboot\n");   if (yrc == 2) printf("yaboot\n");
4413     if (erc == 2) printf("elilo\n");
4414    
4415   return 0;   return 0;
4416      }      }
4417    
4418        if (grubConfig == NULL) {
4419     printf("Could not find bootloader configuration file.\n");
4420     exit(1);
4421        }
4422    
4423      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4424      if (!config) return 1;      if (!config) return 1;
4425    
# Line 4059  int main(int argc, const char ** argv) { Line 4429  int main(int argc, const char ** argv) {
4429          char * rootspec;          char * rootspec;
4430    
4431   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4432     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4433     cfi->defaultIsSaved)
4434        config->defaultImage = 0;
4435   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4436   if (!entry) return 0;   if (!entry) return 0;
4437   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4438    
4439   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
4440   if (!line) return 0;   if (!line) return 0;
4441    
4442          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 4077  int main(int argc, const char ** argv) { Line 4450  int main(int argc, const char ** argv) {
4450   struct singleEntry * entry;   struct singleEntry * entry;
4451    
4452   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4453     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4454     cfi->defaultIsSaved)
4455        config->defaultImage = 0;
4456   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4457   if (!entry) return 0;   if (!entry) return 0;
4458    
# Line 4099  int main(int argc, const char ** argv) { Line 4475  int main(int argc, const char ** argv) {
4475    
4476      } else if (displayDefaultIndex) {      } else if (displayDefaultIndex) {
4477          if (config->defaultImage == -1) return 0;          if (config->defaultImage == -1) return 0;
4478     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4479     cfi->defaultIsSaved)
4480        config->defaultImage = 0;
4481          printf("%i\n", config->defaultImage);          printf("%i\n", config->defaultImage);
4482            return 0;
4483    
4484      } else if (kernelInfo)      } else if (kernelInfo)
4485   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
# Line 4112  int main(int argc, const char ** argv) { Line 4492  int main(int argc, const char ** argv) {
4492      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4493      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4494      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4495      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4496      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4497      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4498                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;

Legend:
Removed from v.1853  
changed lines
  Added in v.2258