Magellan Linux

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

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

revision 1854 by niro, Mon Jul 2 13:12:32 2012 UTC revision 2252 by niro, Mon Oct 21 13:59:08 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 grub2SetEnv(struct configFileInfo *info, char *name, char *value)
301    {
302        char *s = NULL;
303        int rc = 0;
304        char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";
305    
306        rc = asprintf(&s, "grub2-editenv %s set '%s=%s'", envFile, name, value);
307        if (rc <0)
308     return -1;
309    
310        dbgPrintf("grub2SetEnv(%s): %s\n", name, s);
311        rc = system(s);
312        free(s);
313        return rc;
314    }
315    
316    /* this is a gigantic hack to avoid clobbering grub2 variables... */
317    static int is_special_grub2_variable(const char *name)
318    {
319        if (!strcmp(name,"\"${next_entry}\""))
320     return 1;
321        if (!strcmp(name,"\"${prev_saved_entry}\""))
322     return 1;
323        return 0;
324    }
325    
326  int sizeOfSingleLine(struct singleLine * line) {  int sizeOfSingleLine(struct singleLine * line) {
327    int count = 0;    int count = 0;
328    
# Line 271  static int isquote(char q) Line 352  static int isquote(char q)
352      return 0;      return 0;
353  }  }
354    
355    static int iskernel(enum lineType_e type) {
356        return (type == LT_KERNEL || type == LT_KERNEL_EFI);
357    }
358    
359    static int isinitrd(enum lineType_e type) {
360        return (type == LT_INITRD || type == LT_INITRD_EFI);
361    }
362    
363  char *grub2ExtractTitle(struct singleLine * line) {  char *grub2ExtractTitle(struct singleLine * line) {
364      char * current;      char * current;
365      char * current_indent;      char * current_indent;
# Line 328  char *grub2ExtractTitle(struct singleLin Line 417  char *grub2ExtractTitle(struct singleLin
417    
418  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
419      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
420        .getEnv = grub2GetEnv,
421        .setEnv = grub2SetEnv,
422      .keywords = grub2Keywords,      .keywords = grub2Keywords,
423      .defaultIsIndex = 1,      .defaultIsIndex = 1,
424      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
# Line 482  struct configFileInfo ziplConfigType = { Line 573  struct configFileInfo ziplConfigType = {
573  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
574      .defaultConfig = "/boot/extlinux/extlinux.conf",      .defaultConfig = "/boot/extlinux/extlinux.conf",
575      .keywords = extlinuxKeywords,      .keywords = extlinuxKeywords,
576        .caseInsensitive = 1,
577      .entryStart = LT_TITLE,      .entryStart = LT_TITLE,
578      .needsBootPrefix = 1,      .needsBootPrefix = 1,
579      .maxTitleLength = 255,      .maxTitleLength = 255,
# Line 506  struct singleEntry * findEntryByIndex(st Line 598  struct singleEntry * findEntryByIndex(st
598  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
599       const char * path, const char * prefix,       const char * path, const char * prefix,
600       int * index);       int * index);
601    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
602          int * index);
603  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
604  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
605  struct singleLine * lineDup(struct singleLine * line);  struct singleLine * lineDup(struct singleLine * line);
# Line 570  static char * sdupprintf(const char *for Line 664  static char * sdupprintf(const char *for
664      return buf;      return buf;
665  }  }
666    
667    static enum lineType_e preferredLineType(enum lineType_e type,
668     struct configFileInfo *cfi) {
669        if (isEfi && cfi == &grub2ConfigType) {
670     switch (type) {
671     case LT_KERNEL:
672        return LT_KERNEL_EFI;
673     case LT_INITRD:
674        return LT_INITRD_EFI;
675     default:
676        return type;
677     }
678        }
679        return type;
680    }
681    
682  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
683        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
684      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 712  static char * getuuidbydev(char *device)
712  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
713   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
714      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
715   if (!strcmp(keyword, kw->key))   if (cfi->caseInsensitive) {
716      return kw->type;      if (!strcasecmp(keyword, kw->key))
717                    return kw->type;
718     } else {
719        if (!strcmp(keyword, kw->key))
720            return kw->type;
721     }
722      }      }
723      return LT_UNKNOWN;      return LT_UNKNOWN;
724  }  }
# Line 643  static int isEntryStart(struct singleLin Line 757  static int isEntryStart(struct singleLin
757  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
758  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct singleLine * line) {
759      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it (leaks a byte) */
760      char * title;      char * title = NULL;
761      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
762      title++;   title = strdup(line->elements[0].item);
763      *(title + strlen(title) - 1) = '\0';   title++;
764     *(title + strlen(title) - 1) = '\0';
765        } else if (line->type == LT_MENUENTRY)
766     title = strdup(line->elements[1].item);
767        else
768     return NULL;
769      return title;      return title;
770  }  }
771    
# Line 918  static struct grubConfig * readConfig(co Line 1037  static struct grubConfig * readConfig(co
1037      int len;      int len;
1038      char * buf;      char * buf;
1039    
1040      if (!strcmp(inName, "-")) {      if (inName == NULL) {
1041            printf("Could not find bootloader configuration\n");
1042            exit(1);
1043        } else if (!strcmp(inName, "-")) {
1044   in = 0;   in = 0;
1045      } else {      } else {
1046   if ((in = open(inName, O_RDONLY)) < 0) {   if ((in = open(inName, O_RDONLY)) < 0) {
# Line 985  static struct grubConfig * readConfig(co Line 1107  static struct grubConfig * readConfig(co
1107      dbgPrintf("\n");      dbgPrintf("\n");
1108      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1109      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
1110      !strcmp(line->elements[1].item, kwType->key)) {      !strcmp(line->elements[1].item, kwType->key) &&
1111        !is_special_grub2_variable(line->elements[2].item)) {
1112   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1113   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1114   defaultLine = line;   defaultLine = line;
# Line 994  static struct grubConfig * readConfig(co Line 1117  static struct grubConfig * readConfig(co
1117      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1118      defaultLine = line;      defaultLine = line;
1119    
1120          } else if (line->type == LT_KERNEL) {          } else if (iskernel(line->type)) {
1121      /* if by some freak chance this is multiboot and the "module"      /* if by some freak chance this is multiboot and the "module"
1122       * lines came earlier in the template, make sure to use LT_HYPER       * lines came earlier in the template, make sure to use LT_HYPER
1123       * instead of LT_KERNEL now       * instead of LT_KERNEL now
# Line 1011  static struct grubConfig * readConfig(co Line 1134  static struct grubConfig * readConfig(co
1134      for (struct singleLine *l = entry->lines; l; l = l->next) {      for (struct singleLine *l = entry->lines; l; l = l->next) {
1135   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1136      break;      break;
1137   else if (l->type == LT_KERNEL) {   else if (iskernel(l->type)) {
1138      l->type = LT_HYPER;      l->type = LT_HYPER;
1139      break;      break;
1140   }   }
# Line 1190  static struct grubConfig * readConfig(co Line 1313  static struct grubConfig * readConfig(co
1313          if (defaultLine->numElements > 2 &&          if (defaultLine->numElements > 2 &&
1314      cfi->defaultSupportSaved &&      cfi->defaultSupportSaved &&
1315      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1316      cfg->defaultImage = DEFAULT_SAVED_GRUB2;   cfg->cfi->defaultIsSaved = 1;
1317     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1318     if (cfg->cfi->getEnv) {
1319        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1320        if (defTitle) {
1321     int index = 0;
1322     entry = findEntryByTitle(cfg, defTitle, &index);
1323     if (entry)
1324        cfg->defaultImage = index;
1325        }
1326     }
1327   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1328      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1329      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
# Line 1231  static struct grubConfig * readConfig(co Line 1364  static struct grubConfig * readConfig(co
1364          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1365      }      }
1366   }   }
1367        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1368     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1369     if (defTitle) {
1370        int index = 0;
1371        entry = findEntryByTitle(cfg, defTitle, &index);
1372        if (entry)
1373     cfg->defaultImage = index;
1374     }
1375      } else {      } else {
1376          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1377      }      }
# Line 1248  static void writeDefault(FILE * out, cha Line 1389  static void writeDefault(FILE * out, cha
1389    
1390      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1391   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1392      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1393   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1394      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1395        char *title;
1396        entry = findEntryByIndex(cfg, cfg->defaultImage);
1397        line = getLineByType(LT_MENUENTRY, entry->lines);
1398        if (!line)
1399     line = getLineByType(LT_TITLE, entry->lines);
1400        if (line) {
1401     title = extractTitle(line);
1402     if (title)
1403        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1404        }
1405     }
1406        } else if (cfg->defaultImage > -1) {
1407   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1408      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1409          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 1310  static int writeConfig(struct grubConfig Line 1463  static int writeConfig(struct grubConfig
1463    
1464      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1465         directory to the dir of the symlink */         directory to the dir of the symlink */
1466              rc = chdir(dirname(outName));      char *dir = strdupa(outName);
1467        rc = chdir(dirname(dir));
1468      do {      do {
1469   buf = alloca(len + 1);   buf = alloca(len + 1);
1470   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1352  static int writeConfig(struct grubConfig Line 1506  static int writeConfig(struct grubConfig
1506      while (line) {      while (line) {
1507          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1508   line->numElements == 3 &&   line->numElements == 3 &&
1509   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1510     !is_special_grub2_variable(line->elements[2].item)) {
1511      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1512      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1513   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1497  static char *findDiskForRoot() Line 1652  static char *findDiskForRoot()
1652      return NULL;      return NULL;
1653  }  }
1654    
1655  void printEntry(struct singleEntry * entry) {  void printEntry(struct singleEntry * entry, FILE *f) {
1656      int i;      int i;
1657      struct singleLine * line;      struct singleLine * line;
1658    
1659      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1660   fprintf(stderr, "DBG: %s", line->indent);   log_message(f, "DBG: %s", line->indent);
1661   for (i = 0; i < line->numElements; i++) {   for (i = 0; i < line->numElements; i++) {
1662      /* Need to handle this, because we strip the quotes from      /* Need to handle this, because we strip the quotes from
1663       * menuentry when read it. */       * menuentry when read it. */
1664      if (line->type == LT_MENUENTRY && i == 1) {      if (line->type == LT_MENUENTRY && i == 1) {
1665   if(!isquote(*line->elements[i].item))   if(!isquote(*line->elements[i].item))
1666      fprintf(stderr, "\'%s\'", line->elements[i].item);      log_message(f, "\'%s\'", line->elements[i].item);
1667   else   else
1668      fprintf(stderr, "%s", line->elements[i].item);      log_message(f, "%s", line->elements[i].item);
1669   fprintf(stderr, "%s", line->elements[i].indent);   log_message(f, "%s", line->elements[i].indent);
1670    
1671   continue;   continue;
1672      }      }
1673            
1674      fprintf(stderr, "%s%s",      log_message(f, "%s%s",
1675      line->elements[i].item, line->elements[i].indent);      line->elements[i].item, line->elements[i].indent);
1676   }   }
1677   fprintf(stderr, "\n");   log_message(f, "\n");
1678      }      }
1679  }  }
1680    
1681  void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)  void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1682  {  {
1683      va_list argp;      static int once;
1684        va_list argp, argq;
1685    
1686      if (!debug)      va_start(argp, fmt);
1687    
1688        va_copy(argq, argp);
1689        if (!once) {
1690     log_time(NULL);
1691     log_message(NULL, "command line: %s\n", saved_command_line);
1692        }
1693        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1694        log_vmessage(NULL, fmt, argq);
1695    
1696        printEntry(entry, NULL);
1697        va_end(argq);
1698    
1699        if (!debug) {
1700     once = 1;
1701         va_end(argp);
1702   return;   return;
1703        }
1704    
1705      va_start(argp, fmt);      if (okay) {
1706     va_end(argp);
1707     return;
1708        }
1709    
1710        if (!once)
1711     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1712        once = 1;
1713      fprintf(stderr, "DBG: Image entry failed: ");      fprintf(stderr, "DBG: Image entry failed: ");
1714      vfprintf(stderr, fmt, argp);      vfprintf(stderr, fmt, argp);
1715      printEntry(entry);      printEntry(entry, stderr);
1716      va_end(argp);      va_end(argp);
1717  }  }
1718    
# Line 1560  int suitableImage(struct singleEntry * e Line 1739  int suitableImage(struct singleEntry * e
1739      char * rootdev;      char * rootdev;
1740    
1741      if (skipRemoved && entry->skip) {      if (skipRemoved && entry->skip) {
1742   notSuitablePrintf(entry, "marked to skip\n");   notSuitablePrintf(entry, 0, "marked to skip\n");
1743   return 0;   return 0;
1744      }      }
1745    
1746      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1747      if (!line) {      if (!line) {
1748   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1749   return 0;   return 0;
1750      }      }
1751      if (line->numElements < 2) {      if (line->numElements < 2) {
1752   notSuitablePrintf(entry, "line has only %d elements\n",   notSuitablePrintf(entry, 0, "line has only %d elements\n",
1753      line->numElements);      line->numElements);
1754   return 0;   return 0;
1755      }      }
1756    
1757      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) {
1758        notSuitablePrintf(entry, 1, "\n");
1759        return 1;
1760        }
1761    
1762      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1763        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
# Line 1586  int suitableImage(struct singleEntry * e Line 1768  int suitableImage(struct singleEntry * e
1768      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1769              line->elements[1].item + rootspec_offset);              line->elements[1].item + rootspec_offset);
1770      if (access(fullName, R_OK)) {      if (access(fullName, R_OK)) {
1771   notSuitablePrintf(entry, "access to %s failed\n", fullName);   notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1772   return 0;   return 0;
1773      }      }
1774      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
# Line 1607  int suitableImage(struct singleEntry * e Line 1789  int suitableImage(struct singleEntry * e
1789    
1790              /* failed to find one */              /* failed to find one */
1791              if (!line) {              if (!line) {
1792   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1793   return 0;   return 0;
1794              }              }
1795    
# Line 1616  int suitableImage(struct singleEntry * e Line 1798  int suitableImage(struct singleEntry * e
1798      if (i < line->numElements)      if (i < line->numElements)
1799          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1800      else {      else {
1801   notSuitablePrintf(entry, "no root= entry found\n");   notSuitablePrintf(entry, 0, "no root= entry found\n");
1802   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1803          return 0;          return 0;
1804              }              }
# Line 1625  int suitableImage(struct singleEntry * e Line 1807  int suitableImage(struct singleEntry * e
1807    
1808      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1809      if (!getpathbyspec(dev)) {      if (!getpathbyspec(dev)) {
1810          notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);          notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1811          return 0;          return 0;
1812      } else      } else
1813   dev = getpathbyspec(dev);   dev = getpathbyspec(dev);
1814    
1815      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1816      if (!rootdev) {      if (!rootdev) {
1817          notSuitablePrintf(entry, "can't find root device\n");          notSuitablePrintf(entry, 0, "can't find root device\n");
1818   return 0;   return 0;
1819      }      }
1820    
1821      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1822          notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1823   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1824          free(rootdev);          free(rootdev);
1825          return 0;          return 0;
1826      }      }
1827    
1828      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1829          notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n",
1830   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1831   free(rootdev);   free(rootdev);
1832          return 0;          return 0;
1833      }      }
1834    
1835      free(rootdev);      free(rootdev);
1836        notSuitablePrintf(entry, 1, "\n");
1837    
1838      return 1;      return 1;
1839  }  }
# Line 1694  struct singleEntry * findEntryByPath(str Line 1877  struct singleEntry * findEntryByPath(str
1877   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
1878   if (!entry) return NULL;   if (!entry) return NULL;
1879    
1880   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1881   if (!line) return NULL;   if (!line) return NULL;
1882    
1883   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1743  struct singleEntry * findEntryByPath(str Line 1926  struct singleEntry * findEntryByPath(str
1926    
1927      /* check all the lines matching checkType */      /* check all the lines matching checkType */
1928      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1929   line = getLineByType(entry->multiboot && checkType == LT_KERNEL ?   enum lineType_e ct = checkType;
1930       LT_KERNEL|LT_MBMODULE|LT_HYPER :   if (entry->multiboot && checkType == LT_KERNEL)
1931       checkType, line);      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER;
1932   if (!line) break;  /* not found in this entry */   else if (checkType & LT_KERNEL)
1933        ct = checkType | LT_KERNEL_EFI;
1934     line = getLineByType(ct, line);
1935     if (!line)
1936        break;  /* not found in this entry */
1937    
1938   if (line && line->type != LT_MENUENTRY &&   if (line && line->type != LT_MENUENTRY &&
1939   line->numElements >= 2) {   line->numElements >= 2) {
# Line 1765  struct singleEntry * findEntryByPath(str Line 1952  struct singleEntry * findEntryByPath(str
1952       * non-Linux boot entries (could find netbsd etc, though, which is       * non-Linux boot entries (could find netbsd etc, though, which is
1953       * unfortunate)       * unfortunate)
1954       */       */
1955      if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines))      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines))
1956   break; /* found 'im! */   break; /* found 'im! */
1957   }   }
1958    
# Line 1775  struct singleEntry * findEntryByPath(str Line 1962  struct singleEntry * findEntryByPath(str
1962      return entry;      return entry;
1963  }  }
1964    
1965    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
1966          int * index) {
1967        struct singleEntry * entry;
1968        struct singleLine * line;
1969        int i;
1970        char * newtitle;
1971    
1972        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
1973     if (index && i < *index)
1974        continue;
1975     line = getLineByType(LT_TITLE, entry->lines);
1976     if (!line)
1977        line = getLineByType(LT_MENUENTRY, entry->lines);
1978     if (!line)
1979        continue;
1980     newtitle = grub2ExtractTitle(line);
1981     if (!newtitle)
1982        continue;
1983     if (!strcmp(title, newtitle))
1984        break;
1985        }
1986    
1987        if (!entry)
1988     return NULL;
1989    
1990        if (index)
1991     *index = i;
1992        return entry;
1993    }
1994    
1995  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
1996      struct singleEntry * entry;      struct singleEntry * entry;
1997    
# Line 1797  struct singleEntry * findTemplate(struct Line 2014  struct singleEntry * findTemplate(struct
2014      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2015      int index;      int index;
2016    
2017      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2018     if (cfg->cfi->getEnv) {
2019        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2020        if (defTitle) {
2021     int index = 0;
2022     entry = findEntryByTitle(cfg, defTitle, &index);
2023        }
2024     }
2025        } else if (cfg->defaultImage > -1) {
2026   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2027   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2028      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 1867  void markRemovedImage(struct grubConfig Line 2092  void markRemovedImage(struct grubConfig
2092    
2093  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
2094       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
2095       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
2096      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
2097      int i, j;      int i, j;
2098    
2099      if (newIsDefault) {      if (newIsDefault) {
2100   config->defaultImage = 0;   config->defaultImage = 0;
2101   return;   return;
2102        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
2103     if (findEntryByIndex(config, index))
2104        config->defaultImage = index;
2105     else
2106        config->defaultImage = -1;
2107     return;
2108      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
2109   i = 0;   i = 0;
2110   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1948  void displayEntry(struct singleEntry * e Line 2179  void displayEntry(struct singleEntry * e
2179    
2180      printf("index=%d\n", index);      printf("index=%d\n", index);
2181    
2182      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
2183      if (!line) {      if (!line) {
2184          printf("non linux entry\n");          printf("non linux entry\n");
2185          return;          return;
# Line 2013  void displayEntry(struct singleEntry * e Line 2244  void displayEntry(struct singleEntry * e
2244   printf("root=%s\n", s);   printf("root=%s\n", s);
2245      }      }
2246    
2247      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);
2248    
2249      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2250   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 2645  struct singleLine * addLineTmpl(struct s
2645  {  {
2646      struct singleLine * newLine = lineDup(tmplLine);      struct singleLine * newLine = lineDup(tmplLine);
2647    
2648        if (isEfi && cfi == &grub2ConfigType) {
2649     enum lineType_e old = newLine->type;
2650     newLine->type = preferredLineType(newLine->type, cfi);
2651     if (old != newLine->type)
2652        newLine->elements[0].item = getKeyByType(newLine->type, cfi);
2653        }
2654    
2655      if (val) {      if (val) {
2656   /* override the inherited value with our own.   /* override the inherited value with our own.
2657   * 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 2661  struct singleLine * addLineTmpl(struct s
2661   insertElement(newLine, val, 1, cfi);   insertElement(newLine, val, 1, cfi);
2662    
2663   /* but try to keep the rootspec from the template... sigh */   /* but try to keep the rootspec from the template... sigh */
2664   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)) {
2665      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2666      if (rootspec != NULL) {      if (rootspec != NULL) {
2667   free(newLine->elements[1].item);   free(newLine->elements[1].item);
# Line 2460  struct singleLine *  addLine(struct sing Line 2698  struct singleLine *  addLine(struct sing
2698      /* 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
2699       * stack since it calls addLineTmpl which will make copies.       * stack since it calls addLineTmpl which will make copies.
2700       */       */
   
2701      if (type == LT_TITLE && cfi->titleBracketed) {      if (type == LT_TITLE && cfi->titleBracketed) {
2702   /* we're doing a bracketed title (zipl) */   /* we're doing a bracketed title (zipl) */
2703   tmpl.type = type;   tmpl.type = type;
# Line 2794  int updateActualImage(struct grubConfig Line 3031  int updateActualImage(struct grubConfig
3031      firstElement = 2;      firstElement = 2;
3032    
3033   } else {   } else {
3034      line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines);
3035      if (!line) {      if (!line) {
3036   /* no LT_KERNEL or LT_MBMODULE in this entry? */   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3037   continue;   continue;
# Line 2959  int updateInitrd(struct grubConfig * cfg Line 3196  int updateInitrd(struct grubConfig * cfg
3196      if (!image) return 0;      if (!image) return 0;
3197    
3198      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3199          kernelLine = getLineByType(LT_KERNEL, entry->lines);          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines);
3200          if (!kernelLine) continue;          if (!kernelLine) continue;
3201    
3202          line = getLineByType(LT_INITRD, entry->lines);          line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);
3203          if (line)          if (line)
3204              removeLine(entry, line);              removeLine(entry, line);
3205          if (prefix) {          if (prefix) {
# Line 2973  int updateInitrd(struct grubConfig * cfg Line 3210  int updateInitrd(struct grubConfig * cfg
3210   endLine = getLineByType(LT_ENTRY_END, entry->lines);   endLine = getLineByType(LT_ENTRY_END, entry->lines);
3211   if (endLine)   if (endLine)
3212      removeLine(entry, endLine);      removeLine(entry, endLine);
3213          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);          line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi),
3214     kernelLine->indent, initrd);
3215          if (!line)          if (!line)
3216      return 1;      return 1;
3217   if (endLine) {   if (endLine) {
# Line 3386  int addNewKernel(struct grubConfig * con Line 3624  int addNewKernel(struct grubConfig * con
3624      while (*chptr && isspace(*chptr)) chptr++;      while (*chptr && isspace(*chptr)) chptr++;
3625      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3626    
3627      if (tmplLine->type == LT_KERNEL &&      if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) {
     tmplLine->numElements >= 2) {  
3628   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
3629      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
3630       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 3464  int addNewKernel(struct grubConfig * con Line 3701  int addNewKernel(struct grubConfig * con
3701      /* template is multi but new is not,      /* template is multi but new is not,
3702       * insert the kernel in the first module slot       * insert the kernel in the first module slot
3703       */       */
3704      tmplLine->type = LT_KERNEL;      tmplLine->type = preferredLineType(LT_KERNEL, config->cfi);
3705      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3706      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3707   strdup(getKeywordByType(LT_KERNEL, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3708     config->cfi)->key);
3709      newLine = addLineTmpl(new, tmplLine, newLine,      newLine = addLineTmpl(new, tmplLine, newLine,
3710    newKernelPath + strlen(prefix), config->cfi);    newKernelPath + strlen(prefix),
3711      config->cfi);
3712      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
3713   } else if (needs & NEED_INITRD) {   } else if (needs & NEED_INITRD) {
3714      char *initrdVal;      char *initrdVal;
3715      /* template is multi but new is not,      /* template is multi but new is not,
3716       * insert the initrd in the second module slot       * insert the initrd in the second module slot
3717       */       */
3718      tmplLine->type = LT_INITRD;      tmplLine->type = preferredLineType(LT_INITRD, config->cfi);
3719      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3720      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3721   strdup(getKeywordByType(LT_INITRD, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3722     config->cfi)->key);
3723      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3724      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3725      free(initrdVal);      free(initrdVal);
3726      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3727   }   }
3728    
3729      } else if (tmplLine->type == LT_INITRD &&      } else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) {
        tmplLine->numElements >= 2) {  
3730   if (needs & NEED_INITRD &&   if (needs & NEED_INITRD &&
3731      new->multiboot && !template->multiboot &&      new->multiboot && !template->multiboot &&
3732      config->cfi->mbInitRdIsModule) {      config->cfi->mbInitRdIsModule) {
# Line 3541  int addNewKernel(struct grubConfig * con Line 3780  int addNewKernel(struct grubConfig * con
3780      static const char *prefix = "'Loading ";      static const char *prefix = "'Loading ";
3781      if (tmplLine->numElements > 1 &&      if (tmplLine->numElements > 1 &&
3782      strstr(tmplLine->elements[1].item, prefix) &&      strstr(tmplLine->elements[1].item, prefix) &&
3783      masterLine->next && masterLine->next->type == LT_KERNEL) {      masterLine->next &&
3784        iskernel(masterLine->next->type)) {
3785   char *newTitle = malloc(strlen(prefix) +   char *newTitle = malloc(strlen(prefix) +
3786   strlen(newKernelTitle) + 2);   strlen(newKernelTitle) + 2);
3787    
# Line 3568  int addNewKernel(struct grubConfig * con Line 3808  int addNewKernel(struct grubConfig * con
3808   */   */
3809   switch (config->cfi->entryStart) {   switch (config->cfi->entryStart) {
3810      case LT_KERNEL:      case LT_KERNEL:
3811        case LT_KERNEL_EFI:
3812   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
3813      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
3814   } else {   } else {
3815      newLine = addLine(new, config->cfi, LT_KERNEL,      newLine = addLine(new, config->cfi,
3816              preferredLineType(LT_KERNEL, config->cfi),
3817        config->primaryIndent,        config->primaryIndent,
3818        newKernelPath + strlen(prefix));        newKernelPath + strlen(prefix));
3819      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
# Line 3647  int addNewKernel(struct grubConfig * con Line 3889  int addNewKernel(struct grubConfig * con
3889      if (needs & NEED_KERNEL) {      if (needs & NEED_KERNEL) {
3890   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
3891    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
3892        config->cfi)) ?        config->cfi))
3893    LT_MBMODULE : LT_KERNEL,     ? LT_MBMODULE
3894     : preferredLineType(LT_KERNEL, config->cfi),
3895    config->secondaryIndent,    config->secondaryIndent,
3896    newKernelPath + strlen(prefix));    newKernelPath + strlen(prefix));
3897   needs &= ~NEED_KERNEL;   needs &= ~NEED_KERNEL;
# Line 3664  int addNewKernel(struct grubConfig * con Line 3907  int addNewKernel(struct grubConfig * con
3907   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);
3908   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
3909    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
3910        config->cfi)) ?        config->cfi))
3911    LT_MBMODULE : LT_INITRD,     ? LT_MBMODULE
3912       : preferredLineType(LT_INITRD, config->cfi),
3913    config->secondaryIndent,    config->secondaryIndent,
3914    initrdVal);    initrdVal);
3915   free(initrdVal);   free(initrdVal);
# Line 3697  static void traceback(int signum) Line 3941  static void traceback(int signum)
3941      memset(array, '\0', sizeof (array));      memset(array, '\0', sizeof (array));
3942      size = backtrace(array, 40);      size = backtrace(array, 40);
3943    
3944      fprintf(stderr, "grubby recieved SIGSEGV!  Backtrace (%ld):\n",      fprintf(stderr, "grubby received SIGSEGV!  Backtrace (%ld):\n",
3945              (unsigned long)size);              (unsigned long)size);
3946      backtrace_symbols_fd(array, size, STDERR_FILENO);      backtrace_symbols_fd(array, size, STDERR_FILENO);
3947      exit(1);      exit(1);
# Line 3732  int main(int argc, const char ** argv) { Line 3976  int main(int argc, const char ** argv) {
3976      char * removeArgs = NULL;      char * removeArgs = NULL;
3977      char * kernelInfo = NULL;      char * kernelInfo = NULL;
3978      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
3979        char * envPath = NULL;
3980      const char * chptr = NULL;      const char * chptr = NULL;
3981      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
3982      struct grubConfig * config;      struct grubConfig * config;
# Line 3740  int main(int argc, const char ** argv) { Line 3985  int main(int argc, const char ** argv) {
3985      int displayDefault = 0;      int displayDefault = 0;
3986      int displayDefaultIndex = 0;      int displayDefaultIndex = 0;
3987      int displayDefaultTitle = 0;      int displayDefaultTitle = 0;
3988        int defaultIndex = -1;
3989      struct poptOption options[] = {      struct poptOption options[] = {
3990   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
3991      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 3780  int main(int argc, const char ** argv) { Line 4026  int main(int argc, const char ** argv) {
4026      _("display the title of the default kernel") },      _("display the title of the default kernel") },
4027   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4028      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4029     { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4030        _("force grub2 stanzas to use efi") },
4031     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4032        _("path for environment data"),
4033        _("path") },
4034   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4035      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4036   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3792  int main(int argc, const char ** argv) { Line 4043  int main(int argc, const char ** argv) {
4043   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4044      _("initrd image for the new kernel"), _("initrd-path") },      _("initrd image for the new kernel"), _("initrd-path") },
4045   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4046      _("auxilliary initrd image for things other than the new kernel"), _("initrd-path") },      _("auxiliary initrd image for things other than the new kernel"), _("initrd-path") },
4047   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4048      _("configure lilo bootloader") },      _("configure lilo bootloader") },
4049   { "make-default", 0, 0, &makeDefault, 0,   { "make-default", 0, 0, &makeDefault, 0,
# Line 3812  int main(int argc, const char ** argv) { Line 4063  int main(int argc, const char ** argv) {
4063   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
4064      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
4065        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
4066     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4067        _("make the given entry index the default entry"),
4068        _("entry-index") },
4069   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4070      _("configure silo bootloader") },      _("configure silo bootloader") },
4071   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 3833  int main(int argc, const char ** argv) { Line 4087  int main(int argc, const char ** argv) {
4087    
4088      signal(SIGSEGV, traceback);      signal(SIGSEGV, traceback);
4089    
4090        int i = 0;
4091        for (int j = 1; j < argc; j++)
4092     i += strlen(argv[j]) + 1;
4093        saved_command_line = malloc(i);
4094        if (!saved_command_line) {
4095     fprintf(stderr, "grubby: %m\n");
4096     exit(1);
4097        }
4098        saved_command_line[0] = '\0';
4099        for (int j = 1; j < argc; j++) {
4100     strcat(saved_command_line, argv[j]);
4101     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4102        }
4103    
4104      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4105      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
4106    
# Line 3876  int main(int argc, const char ** argv) { Line 4144  int main(int argc, const char ** argv) {
4144   return 1;   return 1;
4145      } else if (configureGrub2) {      } else if (configureGrub2) {
4146   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4147     if (envPath)
4148        cfi->envFile = envPath;
4149      } else if (configureLilo) {      } else if (configureLilo) {
4150   cfi = &liloConfigType;   cfi = &liloConfigType;
4151      } else if (configureGrub) {      } else if (configureGrub) {
# Line 3920  int main(int argc, const char ** argv) { Line 4190  int main(int argc, const char ** argv) {
4190      }      }
4191    
4192      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
4193    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
4194    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
4195        (defaultIndex >= 0))) {
4196   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
4197    "specified option"));    "specified option"));
4198   return 1;   return 1;
# Line 3963  int main(int argc, const char ** argv) { Line 4234  int main(int argc, const char ** argv) {
4234   makeDefault = 1;   makeDefault = 1;
4235   defaultKernel = NULL;   defaultKernel = NULL;
4236      }      }
4237        else if (defaultKernel && (defaultIndex >= 0)) {
4238     fprintf(stderr, _("grubby: --set-default and --set-default-index "
4239      "may not be used together\n"));
4240     return 1;
4241        }
4242    
4243      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
4244   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
# Line 3971  int main(int argc, const char ** argv) { Line 4247  int main(int argc, const char ** argv) {
4247      }      }
4248    
4249      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
4250   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
4251          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
4252     && (defaultIndex == -1)) {
4253   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
4254   return 1;   return 1;
4255      }      }
# Line 4052  int main(int argc, const char ** argv) { Line 4329  int main(int argc, const char ** argv) {
4329      if (!yconfig)      if (!yconfig)
4330   yrc = 1;   yrc = 1;
4331      else      else
4332        yrc = checkForYaboot(lconfig);   yrc = checkForYaboot(yconfig);
4333   }   }
4334    
4335   if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||   if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4336      erc == 1)   erc == 1)
4337      return 1;      return 1;
4338    
4339   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
# Line 4069  int main(int argc, const char ** argv) { Line 4346  int main(int argc, const char ** argv) {
4346   return 0;   return 0;
4347      }      }
4348    
4349        if (grubConfig == NULL) {
4350     printf("Could not find bootloader configuration file.\n");
4351     exit(1);
4352        }
4353    
4354      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4355      if (!config) return 1;      if (!config) return 1;
4356    
# Line 4082  int main(int argc, const char ** argv) { Line 4364  int main(int argc, const char ** argv) {
4364   if (!entry) return 0;   if (!entry) return 0;
4365   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4366    
4367   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
4368   if (!line) return 0;   if (!line) return 0;
4369    
4370          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 4119  int main(int argc, const char ** argv) { Line 4401  int main(int argc, const char ** argv) {
4401      } else if (displayDefaultIndex) {      } else if (displayDefaultIndex) {
4402          if (config->defaultImage == -1) return 0;          if (config->defaultImage == -1) return 0;
4403          printf("%i\n", config->defaultImage);          printf("%i\n", config->defaultImage);
4404            return 0;
4405    
4406      } else if (kernelInfo)      } else if (kernelInfo)
4407   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
# Line 4131  int main(int argc, const char ** argv) { Line 4414  int main(int argc, const char ** argv) {
4414      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4415      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4416      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4417      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4418      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4419      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4420                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;

Legend:
Removed from v.1854  
changed lines
  Added in v.2252