Magellan Linux

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

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

revision 1868 by niro, Mon Jul 2 13:22:30 2012 UTC revision 2682 by niro, Wed Jul 16 09:49:24 2014 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 167  const char *grubFindConfig(struct config Line 182  const char *grubFindConfig(struct config
182   "/boot/grub/grub.conf",   "/boot/grub/grub.conf",
183   "/boot/grub/menu.lst",   "/boot/grub/menu.lst",
184   "/etc/grub.conf",   "/etc/grub.conf",
185     "/boot/grub2/grub.cfg",
186     "/boot/grub2-efi/grub.cfg",
187   NULL   NULL
188      };      };
189      static int i = -1;      static int i = -1;
# Line 205  struct keywordTypes grub2Keywords[] = { Line 222  struct keywordTypes grub2Keywords[] = {
222      { "default",    LT_DEFAULT,     ' ' },      { "default",    LT_DEFAULT,     ' ' },
223      { "fallback",   LT_FALLBACK,    ' ' },      { "fallback",   LT_FALLBACK,    ' ' },
224      { "linux",      LT_KERNEL,      ' ' },      { "linux",      LT_KERNEL,      ' ' },
225        { "linuxefi",   LT_KERNEL_EFI,  ' ' },
226      { "initrd",     LT_INITRD,      ' ', ' ' },      { "initrd",     LT_INITRD,      ' ', ' ' },
227        { "initrdefi",  LT_INITRD_EFI,  ' ', ' ' },
228      { "module",     LT_MBMODULE,    ' ' },      { "module",     LT_MBMODULE,    ' ' },
229      { "kernel",     LT_HYPER,       ' ' },      { "kernel",     LT_HYPER,       ' ' },
230      { NULL, 0, 0 },      { NULL, 0, 0 },
# Line 219  const char *grub2FindConfig(struct confi Line 238  const char *grub2FindConfig(struct confi
238      };      };
239      static int i = -1;      static int i = -1;
240      static const char *grub_cfg = "/boot/grub/grub.cfg";      static const char *grub_cfg = "/boot/grub/grub.cfg";
241        int rc = -1;
242    
243      if (i == -1) {      if (i == -1) {
244   for (i = 0; configFiles[i] != NULL; i++) {   for (i = 0; configFiles[i] != NULL; i++) {
245      dbgPrintf("Checking \"%s\": ", configFiles[i]);      dbgPrintf("Checking \"%s\": ", configFiles[i]);
246      if (!access(configFiles[i], R_OK)) {      if ((rc = access(configFiles[i], R_OK))) {
247     if (errno == EACCES) {
248        printf("Unable to access bootloader configuration file "
249           "\"%s\": %m\n", configFiles[i]);
250        exit(1);
251     }
252     continue;
253        } else {
254   dbgPrintf("found\n");   dbgPrintf("found\n");
255   return configFiles[i];   return configFiles[i];
256      }      }
# Line 242  const char *grub2FindConfig(struct confi Line 269  const char *grub2FindConfig(struct confi
269      return configFiles[i];      return configFiles[i];
270  }  }
271    
272    /* kind of hacky.  It'll give the first 1024 bytes, ish. */
273    static char *grub2GetEnv(struct configFileInfo *info, char *name)
274    {
275        static char buf[1025];
276        char *s = NULL;
277        char *ret = NULL;
278        char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv";
279        int rc = asprintf(&s, "grub-editenv %s list | grep '^%s='", envFile, name);
280    
281        if (rc < 0)
282     return NULL;
283    
284        FILE *f = popen(s, "r");
285        if (!f)
286     goto out;
287    
288        memset(buf, '\0', sizeof (buf));
289        ret = fgets(buf, 1024, f);
290        pclose(f);
291    
292        if (ret) {
293     ret += strlen(name) + 1;
294     ret[strlen(ret) - 1] = '\0';
295        }
296        dbgPrintf("grub2GetEnv(%s): %s\n", name, ret);
297    out:
298        free(s);
299        return ret;
300    }
301    
302    static int sPopCount(const char *s, const char *c)
303    {
304        int ret = 0;
305        if (!s)
306     return -1;
307        for (int i = 0; s[i] != '\0'; i++)
308     for (int j = 0; c[j] != '\0'; j++)
309        if (s[i] == c[j])
310     ret++;
311        return ret;
312    }
313    
314    static char *shellEscape(const char *s)
315    {
316        int l = strlen(s) + sPopCount(s, "'") * 2;
317    
318        char *ret = calloc(l+1, sizeof (*ret));
319        if (!ret)
320     return NULL;
321        for (int i = 0, j = 0; s[i] != '\0'; i++, j++) {
322     if (s[i] == '\'')
323        ret[j++] = '\\';
324     ret[j] = s[i];
325        }
326        return ret;
327    }
328    
329    static void unquote(char *s)
330    {
331        int l = strlen(s);
332    
333        if ((s[l-1] == '\'' && s[0] == '\'') || (s[l-1] == '"' && s[0] == '"')) {
334     memmove(s, s+1, l-2);
335     s[l-2] = '\0';
336        }
337    }
338    
339    static int grub2SetEnv(struct configFileInfo *info, char *name, char *value)
340    {
341        char *s = NULL;
342        int rc = 0;
343        char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv";
344    
345        unquote(value);
346        value = shellEscape(value);
347        if (!value)
348        return -1;
349    
350        rc = asprintf(&s, "grub-editenv %s set '%s=%s'", envFile, name, value);
351        free(value);
352        if (rc <0)
353     return -1;
354    
355        dbgPrintf("grub2SetEnv(%s): %s\n", name, s);
356        rc = system(s);
357        free(s);
358        return rc;
359    }
360    
361    /* this is a gigantic hack to avoid clobbering grub2 variables... */
362    static int is_special_grub2_variable(const char *name)
363    {
364        if (!strcmp(name,"\"${next_entry}\""))
365     return 1;
366        if (!strcmp(name,"\"${prev_saved_entry}\""))
367     return 1;
368        return 0;
369    }
370    
371  int sizeOfSingleLine(struct singleLine * line) {  int sizeOfSingleLine(struct singleLine * line) {
372    int count = 0;    int count = 0;
373    
# Line 271  static int isquote(char q) Line 397  static int isquote(char q)
397      return 0;      return 0;
398  }  }
399    
400    static int iskernel(enum lineType_e type) {
401        return (type == LT_KERNEL || type == LT_KERNEL_EFI);
402    }
403    
404    static int isinitrd(enum lineType_e type) {
405        return (type == LT_INITRD || type == LT_INITRD_EFI);
406    }
407    
408  char *grub2ExtractTitle(struct singleLine * line) {  char *grub2ExtractTitle(struct singleLine * line) {
409      char * current;      char * current;
410      char * current_indent;      char * current_indent;
# Line 328  char *grub2ExtractTitle(struct singleLin Line 462  char *grub2ExtractTitle(struct singleLin
462    
463  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
464      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
465        .getEnv = grub2GetEnv,
466        .setEnv = grub2SetEnv,
467      .keywords = grub2Keywords,      .keywords = grub2Keywords,
468      .defaultIsIndex = 1,      .defaultIsIndex = 1,
469      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
# Line 482  struct configFileInfo ziplConfigType = { Line 618  struct configFileInfo ziplConfigType = {
618  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
619      .defaultConfig = "/boot/extlinux/extlinux.conf",      .defaultConfig = "/boot/extlinux/extlinux.conf",
620      .keywords = extlinuxKeywords,      .keywords = extlinuxKeywords,
621        .caseInsensitive = 1,
622      .entryStart = LT_TITLE,      .entryStart = LT_TITLE,
623      .needsBootPrefix = 1,      .needsBootPrefix = 1,
624      .maxTitleLength = 255,      .maxTitleLength = 255,
# Line 506  struct singleEntry * findEntryByIndex(st Line 643  struct singleEntry * findEntryByIndex(st
643  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
644       const char * path, const char * prefix,       const char * path, const char * prefix,
645       int * index);       int * index);
646    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
647          int * index);
648  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
649  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
650  struct singleLine * lineDup(struct singleLine * line);  struct singleLine * lineDup(struct singleLine * line);
# Line 570  static char * sdupprintf(const char *for Line 709  static char * sdupprintf(const char *for
709      return buf;      return buf;
710  }  }
711    
712    static enum lineType_e preferredLineType(enum lineType_e type,
713     struct configFileInfo *cfi) {
714        if (isEfi && cfi == &grub2ConfigType) {
715     switch (type) {
716     case LT_KERNEL:
717        return LT_KERNEL_EFI;
718     case LT_INITRD:
719        return LT_INITRD_EFI;
720     default:
721        return type;
722     }
723        }
724        return type;
725    }
726    
727  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
728        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
729      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 757  static char * getuuidbydev(char *device)
757  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
758   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
759      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
760   if (!strcmp(keyword, kw->key))   if (cfi->caseInsensitive) {
761      return kw->type;      if (!strcasecmp(keyword, kw->key))
762                    return kw->type;
763     } else {
764        if (!strcmp(keyword, kw->key))
765            return kw->type;
766     }
767      }      }
768      return LT_UNKNOWN;      return LT_UNKNOWN;
769  }  }
# Line 643  static int isEntryStart(struct singleLin Line 802  static int isEntryStart(struct singleLin
802  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
803  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct singleLine * line) {
804      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it (leaks a byte) */
805      char * title;      char * title = NULL;
806      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
807      title++;   title = strdup(line->elements[0].item);
808      *(title + strlen(title) - 1) = '\0';   title++;
809     *(title + strlen(title) - 1) = '\0';
810        } else if (line->type == LT_MENUENTRY)
811     title = strdup(line->elements[1].item);
812        else
813     return NULL;
814      return title;      return title;
815  }  }
816    
# Line 904  static int getNextLine(char ** bufPtr, s Line 1068  static int getNextLine(char ** bufPtr, s
1068      return 0;      return 0;
1069  }  }
1070    
1071    static int isnumber(const char *s)
1072    {
1073        int i;
1074        for (i = 0; s[i] != '\0'; i++)
1075     if (s[i] < '0' || s[i] > '9')
1076        return 0;
1077        return i;
1078    }
1079    
1080  static struct grubConfig * readConfig(const char * inName,  static struct grubConfig * readConfig(const char * inName,
1081        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
1082      int in;      int in;
# Line 918  static struct grubConfig * readConfig(co Line 1091  static struct grubConfig * readConfig(co
1091      int len;      int len;
1092      char * buf;      char * buf;
1093    
1094      if (!strcmp(inName, "-")) {      if (inName == NULL) {
1095            printf("Could not find bootloader configuration\n");
1096            exit(1);
1097        } else if (!strcmp(inName, "-")) {
1098   in = 0;   in = 0;
1099      } else {      } else {
1100   if ((in = open(inName, O_RDONLY)) < 0) {   if ((in = open(inName, O_RDONLY)) < 0) {
# Line 985  static struct grubConfig * readConfig(co Line 1161  static struct grubConfig * readConfig(co
1161      dbgPrintf("\n");      dbgPrintf("\n");
1162      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1163      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
1164      !strcmp(line->elements[1].item, kwType->key)) {      !strcmp(line->elements[1].item, kwType->key) &&
1165        !is_special_grub2_variable(line->elements[2].item)) {
1166   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1167   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1168   defaultLine = line;   defaultLine = line;
# Line 994  static struct grubConfig * readConfig(co Line 1171  static struct grubConfig * readConfig(co
1171      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1172      defaultLine = line;      defaultLine = line;
1173    
1174          } else if (line->type == LT_KERNEL) {          } else if (iskernel(line->type)) {
1175      /* if by some freak chance this is multiboot and the "module"      /* if by some freak chance this is multiboot and the "module"
1176       * lines came earlier in the template, make sure to use LT_HYPER       * lines came earlier in the template, make sure to use LT_HYPER
1177       * instead of LT_KERNEL now       * instead of LT_KERNEL now
# Line 1011  static struct grubConfig * readConfig(co Line 1188  static struct grubConfig * readConfig(co
1188      for (struct singleLine *l = entry->lines; l; l = l->next) {      for (struct singleLine *l = entry->lines; l; l = l->next) {
1189   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1190      break;      break;
1191   else if (l->type == LT_KERNEL) {   else if (iskernel(l->type)) {
1192      l->type = LT_HYPER;      l->type = LT_HYPER;
1193      break;      break;
1194   }   }
# Line 1190  static struct grubConfig * readConfig(co Line 1367  static struct grubConfig * readConfig(co
1367          if (defaultLine->numElements > 2 &&          if (defaultLine->numElements > 2 &&
1368      cfi->defaultSupportSaved &&      cfi->defaultSupportSaved &&
1369      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1370      cfg->defaultImage = DEFAULT_SAVED_GRUB2;   cfg->cfi->defaultIsSaved = 1;
1371     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1372     if (cfg->cfi->getEnv) {
1373        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1374        if (defTitle) {
1375     int index = 0;
1376     if (isnumber(defTitle)) {
1377        index = atoi(defTitle);
1378        entry = findEntryByIndex(cfg, index);
1379     } else {
1380        entry = findEntryByTitle(cfg, defTitle, &index);
1381     }
1382     if (entry)
1383        cfg->defaultImage = index;
1384        }
1385     }
1386   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1387      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1388      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
# Line 1231  static struct grubConfig * readConfig(co Line 1423  static struct grubConfig * readConfig(co
1423          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1424      }      }
1425   }   }
1426        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1427     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1428     if (defTitle) {
1429        int index = 0;
1430        if (isnumber(defTitle)) {
1431     index = atoi(defTitle);
1432     entry = findEntryByIndex(cfg, index);
1433        } else {
1434     entry = findEntryByTitle(cfg, defTitle, &index);
1435        }
1436        if (entry)
1437     cfg->defaultImage = index;
1438     }
1439      } else {      } else {
1440          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1441      }      }
# Line 1248  static void writeDefault(FILE * out, cha Line 1453  static void writeDefault(FILE * out, cha
1453    
1454      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1455   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1456      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1457   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1458      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1459        char *title;
1460        entry = findEntryByIndex(cfg, cfg->defaultImage);
1461        line = getLineByType(LT_MENUENTRY, entry->lines);
1462        if (!line)
1463     line = getLineByType(LT_TITLE, entry->lines);
1464        if (line) {
1465     title = extractTitle(line);
1466     if (title)
1467        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1468        }
1469     }
1470        } else if (cfg->defaultImage > -1) {
1471   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1472      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1473          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 1311  static int writeConfig(struct grubConfig Line 1528  static int writeConfig(struct grubConfig
1528      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1529         directory to the dir of the symlink */         directory to the dir of the symlink */
1530      char *dir = strdupa(outName);      char *dir = strdupa(outName);
1531              rc = chdir(dirname(dir));      rc = chdir(dirname(dir));
     free(dir);  
1532      do {      do {
1533   buf = alloca(len + 1);   buf = alloca(len + 1);
1534   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1354  static int writeConfig(struct grubConfig Line 1570  static int writeConfig(struct grubConfig
1570      while (line) {      while (line) {
1571          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1572   line->numElements == 3 &&   line->numElements == 3 &&
1573   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1574     !is_special_grub2_variable(line->elements[2].item)) {
1575      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1576      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1577   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1499  static char *findDiskForRoot() Line 1716  static char *findDiskForRoot()
1716      return NULL;      return NULL;
1717  }  }
1718    
1719  void printEntry(struct singleEntry * entry) {  void printEntry(struct singleEntry * entry, FILE *f) {
1720      int i;      int i;
1721      struct singleLine * line;      struct singleLine * line;
1722    
1723      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1724   fprintf(stderr, "DBG: %s", line->indent);   log_message(f, "DBG: %s", line->indent);
1725   for (i = 0; i < line->numElements; i++) {   for (i = 0; i < line->numElements; i++) {
1726      /* Need to handle this, because we strip the quotes from      /* Need to handle this, because we strip the quotes from
1727       * menuentry when read it. */       * menuentry when read it. */
1728      if (line->type == LT_MENUENTRY && i == 1) {      if (line->type == LT_MENUENTRY && i == 1) {
1729   if(!isquote(*line->elements[i].item))   if(!isquote(*line->elements[i].item))
1730      fprintf(stderr, "\'%s\'", line->elements[i].item);      log_message(f, "\'%s\'", line->elements[i].item);
1731   else   else
1732      fprintf(stderr, "%s", line->elements[i].item);      log_message(f, "%s", line->elements[i].item);
1733   fprintf(stderr, "%s", line->elements[i].indent);   log_message(f, "%s", line->elements[i].indent);
1734    
1735   continue;   continue;
1736      }      }
1737            
1738      fprintf(stderr, "%s%s",      log_message(f, "%s%s",
1739      line->elements[i].item, line->elements[i].indent);      line->elements[i].item, line->elements[i].indent);
1740   }   }
1741   fprintf(stderr, "\n");   log_message(f, "\n");
1742      }      }
1743  }  }
1744    
1745  void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)  void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1746  {  {
1747      va_list argp;      static int once;
1748        va_list argp, argq;
1749    
1750        va_start(argp, fmt);
1751    
1752        va_copy(argq, argp);
1753        if (!once) {
1754     log_time(NULL);
1755     log_message(NULL, "command line: %s\n", saved_command_line);
1756        }
1757        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1758        log_vmessage(NULL, fmt, argq);
1759    
1760        printEntry(entry, NULL);
1761        va_end(argq);
1762    
1763      if (!debug)      if (!debug) {
1764     once = 1;
1765         va_end(argp);
1766   return;   return;
1767        }
1768    
1769      va_start(argp, fmt);      if (okay) {
1770     va_end(argp);
1771     return;
1772        }
1773    
1774        if (!once)
1775     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1776        once = 1;
1777      fprintf(stderr, "DBG: Image entry failed: ");      fprintf(stderr, "DBG: Image entry failed: ");
1778      vfprintf(stderr, fmt, argp);      vfprintf(stderr, fmt, argp);
1779      printEntry(entry);      printEntry(entry, stderr);
1780      va_end(argp);      va_end(argp);
1781  }  }
1782    
# Line 1562  int suitableImage(struct singleEntry * e Line 1803  int suitableImage(struct singleEntry * e
1803      char * rootdev;      char * rootdev;
1804    
1805      if (skipRemoved && entry->skip) {      if (skipRemoved && entry->skip) {
1806   notSuitablePrintf(entry, "marked to skip\n");   notSuitablePrintf(entry, 0, "marked to skip\n");
1807   return 0;   return 0;
1808      }      }
1809    
1810      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1811      if (!line) {      if (!line) {
1812   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1813   return 0;   return 0;
1814      }      }
1815      if (line->numElements < 2) {      if (line->numElements < 2) {
1816   notSuitablePrintf(entry, "line has only %d elements\n",   notSuitablePrintf(entry, 0, "line has only %d elements\n",
1817      line->numElements);      line->numElements);
1818   return 0;   return 0;
1819      }      }
1820    
1821      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) {
1822        notSuitablePrintf(entry, 1, "\n");
1823        return 1;
1824        }
1825    
1826      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1827        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
# Line 1588  int suitableImage(struct singleEntry * e Line 1832  int suitableImage(struct singleEntry * e
1832      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1833              line->elements[1].item + rootspec_offset);              line->elements[1].item + rootspec_offset);
1834      if (access(fullName, R_OK)) {      if (access(fullName, R_OK)) {
1835   notSuitablePrintf(entry, "access to %s failed\n", fullName);   notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1836   return 0;   return 0;
1837      }      }
1838      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
# Line 1609  int suitableImage(struct singleEntry * e Line 1853  int suitableImage(struct singleEntry * e
1853    
1854              /* failed to find one */              /* failed to find one */
1855              if (!line) {              if (!line) {
1856   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1857   return 0;   return 0;
1858              }              }
1859    
# Line 1618  int suitableImage(struct singleEntry * e Line 1862  int suitableImage(struct singleEntry * e
1862      if (i < line->numElements)      if (i < line->numElements)
1863          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1864      else {      else {
1865   notSuitablePrintf(entry, "no root= entry found\n");   notSuitablePrintf(entry, 0, "no root= entry found\n");
1866   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1867          return 0;          return 0;
1868              }              }
# Line 1627  int suitableImage(struct singleEntry * e Line 1871  int suitableImage(struct singleEntry * e
1871    
1872      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1873      if (!getpathbyspec(dev)) {      if (!getpathbyspec(dev)) {
1874          notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);          notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1875          return 0;          return 0;
1876      } else      } else
1877   dev = getpathbyspec(dev);   dev = getpathbyspec(dev);
1878    
1879      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1880      if (!rootdev) {      if (!rootdev) {
1881          notSuitablePrintf(entry, "can't find root device\n");          notSuitablePrintf(entry, 0, "can't find root device\n");
1882   return 0;   return 0;
1883      }      }
1884    
1885      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1886          notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1887   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1888          free(rootdev);          free(rootdev);
1889          return 0;          return 0;
1890      }      }
1891    
1892      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1893          notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n",
1894   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1895   free(rootdev);   free(rootdev);
1896          return 0;          return 0;
1897      }      }
1898    
1899      free(rootdev);      free(rootdev);
1900        notSuitablePrintf(entry, 1, "\n");
1901    
1902      return 1;      return 1;
1903  }  }
# Line 1696  struct singleEntry * findEntryByPath(str Line 1941  struct singleEntry * findEntryByPath(str
1941   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
1942   if (!entry) return NULL;   if (!entry) return NULL;
1943    
1944   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1945   if (!line) return NULL;   if (!line) return NULL;
1946    
1947   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1745  struct singleEntry * findEntryByPath(str Line 1990  struct singleEntry * findEntryByPath(str
1990    
1991      /* check all the lines matching checkType */      /* check all the lines matching checkType */
1992      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1993   line = getLineByType(entry->multiboot && checkType == LT_KERNEL ?   enum lineType_e ct = checkType;
1994       LT_KERNEL|LT_MBMODULE|LT_HYPER :   if (entry->multiboot && checkType == LT_KERNEL)
1995       checkType, line);      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER;
1996   if (!line) break;  /* not found in this entry */   else if (checkType & LT_KERNEL)
1997        ct = checkType | LT_KERNEL_EFI;
1998     line = getLineByType(ct, line);
1999     if (!line)
2000        break;  /* not found in this entry */
2001    
2002   if (line && line->type != LT_MENUENTRY &&   if (line && line->type != LT_MENUENTRY &&
2003   line->numElements >= 2) {   line->numElements >= 2) {
# Line 1767  struct singleEntry * findEntryByPath(str Line 2016  struct singleEntry * findEntryByPath(str
2016       * non-Linux boot entries (could find netbsd etc, though, which is       * non-Linux boot entries (could find netbsd etc, though, which is
2017       * unfortunate)       * unfortunate)
2018       */       */
2019      if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines))      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines))
2020   break; /* found 'im! */   break; /* found 'im! */
2021   }   }
2022    
# Line 1777  struct singleEntry * findEntryByPath(str Line 2026  struct singleEntry * findEntryByPath(str
2026      return entry;      return entry;
2027  }  }
2028    
2029    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2030          int * index) {
2031        struct singleEntry * entry;
2032        struct singleLine * line;
2033        int i;
2034        char * newtitle;
2035    
2036        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2037     if (index && i < *index)
2038        continue;
2039     line = getLineByType(LT_TITLE, entry->lines);
2040     if (!line)
2041        line = getLineByType(LT_MENUENTRY, entry->lines);
2042     if (!line)
2043        continue;
2044     newtitle = grub2ExtractTitle(line);
2045     if (!newtitle)
2046        continue;
2047     if (!strcmp(title, newtitle))
2048        break;
2049        }
2050    
2051        if (!entry)
2052     return NULL;
2053    
2054        if (index)
2055     *index = i;
2056        return entry;
2057    }
2058    
2059  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
2060      struct singleEntry * entry;      struct singleEntry * entry;
2061    
# Line 1799  struct singleEntry * findTemplate(struct Line 2078  struct singleEntry * findTemplate(struct
2078      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2079      int index;      int index;
2080    
2081      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2082     if (cfg->cfi->getEnv) {
2083        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2084        if (defTitle) {
2085     int index = 0;
2086     if (isnumber(defTitle)) {
2087        index = atoi(defTitle);
2088        entry = findEntryByIndex(cfg, index);
2089     } else {
2090        entry = findEntryByTitle(cfg, defTitle, &index);
2091     }
2092     if (entry)
2093        cfg->defaultImage = index;
2094        }
2095     }
2096        } else if (cfg->defaultImage > -1) {
2097   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2098   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2099      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 1956  void displayEntry(struct singleEntry * e Line 2250  void displayEntry(struct singleEntry * e
2250    
2251      printf("index=%d\n", index);      printf("index=%d\n", index);
2252    
2253      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
2254      if (!line) {      if (!line) {
2255          printf("non linux entry\n");          printf("non linux entry\n");
2256          return;          return;
# Line 2021  void displayEntry(struct singleEntry * e Line 2315  void displayEntry(struct singleEntry * e
2315   printf("root=%s\n", s);   printf("root=%s\n", s);
2316      }      }
2317    
2318      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);
2319    
2320      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2321   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
# Line 2422  struct singleLine * addLineTmpl(struct s Line 2716  struct singleLine * addLineTmpl(struct s
2716  {  {
2717      struct singleLine * newLine = lineDup(tmplLine);      struct singleLine * newLine = lineDup(tmplLine);
2718    
2719        if (isEfi && cfi == &grub2ConfigType) {
2720     enum lineType_e old = newLine->type;
2721     newLine->type = preferredLineType(newLine->type, cfi);
2722     if (old != newLine->type)
2723        newLine->elements[0].item = getKeyByType(newLine->type, cfi);
2724        }
2725    
2726      if (val) {      if (val) {
2727   /* override the inherited value with our own.   /* override the inherited value with our own.
2728   * 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 2431  struct singleLine * addLineTmpl(struct s Line 2732  struct singleLine * addLineTmpl(struct s
2732   insertElement(newLine, val, 1, cfi);   insertElement(newLine, val, 1, cfi);
2733    
2734   /* but try to keep the rootspec from the template... sigh */   /* but try to keep the rootspec from the template... sigh */
2735   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)) {
2736      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2737      if (rootspec != NULL) {      if (rootspec != NULL) {
2738   free(newLine->elements[1].item);   free(newLine->elements[1].item);
# Line 2468  struct singleLine *  addLine(struct sing Line 2769  struct singleLine *  addLine(struct sing
2769      /* 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
2770       * stack since it calls addLineTmpl which will make copies.       * stack since it calls addLineTmpl which will make copies.
2771       */       */
   
2772      if (type == LT_TITLE && cfi->titleBracketed) {      if (type == LT_TITLE && cfi->titleBracketed) {
2773   /* we're doing a bracketed title (zipl) */   /* we're doing a bracketed title (zipl) */
2774   tmpl.type = type;   tmpl.type = type;
# Line 2802  int updateActualImage(struct grubConfig Line 3102  int updateActualImage(struct grubConfig
3102      firstElement = 2;      firstElement = 2;
3103    
3104   } else {   } else {
3105      line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines);
3106      if (!line) {      if (!line) {
3107   /* no LT_KERNEL or LT_MBMODULE in this entry? */   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3108   continue;   continue;
# Line 2958  int updateImage(struct grubConfig * cfg, Line 3258  int updateImage(struct grubConfig * cfg,
3258      return rc;      return rc;
3259  }  }
3260    
3261    int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
3262     const char * image, const char * prefix, const char * initrd) {
3263        struct singleEntry * entry;
3264        struct singleLine * line, * kernelLine, *endLine = NULL;
3265        int index = 0;
3266    
3267        if (!image) return 0;
3268    
3269        for (; (entry = findEntryByPath(cfg, newMBKernel, prefix, &index)); index++) {
3270            kernelLine = getLineByType(LT_MBMODULE, entry->lines);
3271            if (!kernelLine) continue;
3272    
3273            if (prefix) {
3274                int prefixLen = strlen(prefix);
3275                if (!strncmp(initrd, prefix, prefixLen))
3276                    initrd += prefixLen;
3277            }
3278     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3279     if (endLine)
3280        removeLine(entry, endLine);
3281            line = addLine(entry, cfg->cfi, preferredLineType(LT_MBMODULE,cfg->cfi),
3282     kernelLine->indent, initrd);
3283            if (!line)
3284        return 1;
3285     if (endLine) {
3286        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3287                if (!line)
3288     return 1;
3289     }
3290    
3291            break;
3292        }
3293    
3294        return 0;
3295    }
3296    
3297  int updateInitrd(struct grubConfig * cfg, const char * image,  int updateInitrd(struct grubConfig * cfg, const char * image,
3298                   const char * prefix, const char * initrd) {                   const char * prefix, const char * initrd) {
3299      struct singleEntry * entry;      struct singleEntry * entry;
# Line 2967  int updateInitrd(struct grubConfig * cfg Line 3303  int updateInitrd(struct grubConfig * cfg
3303      if (!image) return 0;      if (!image) return 0;
3304    
3305      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3306          kernelLine = getLineByType(LT_KERNEL, entry->lines);          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines);
3307          if (!kernelLine) continue;          if (!kernelLine) continue;
3308    
3309          line = getLineByType(LT_INITRD, entry->lines);          line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);
3310          if (line)          if (line)
3311              removeLine(entry, line);              removeLine(entry, line);
3312          if (prefix) {          if (prefix) {
# Line 2981  int updateInitrd(struct grubConfig * cfg Line 3317  int updateInitrd(struct grubConfig * cfg
3317   endLine = getLineByType(LT_ENTRY_END, entry->lines);   endLine = getLineByType(LT_ENTRY_END, entry->lines);
3318   if (endLine)   if (endLine)
3319      removeLine(entry, endLine);      removeLine(entry, endLine);
3320          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);          line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi),
3321     kernelLine->indent, initrd);
3322          if (!line)          if (!line)
3323      return 1;      return 1;
3324   if (endLine) {   if (endLine) {
# Line 3394  int addNewKernel(struct grubConfig * con Line 3731  int addNewKernel(struct grubConfig * con
3731      while (*chptr && isspace(*chptr)) chptr++;      while (*chptr && isspace(*chptr)) chptr++;
3732      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3733    
3734      if (tmplLine->type == LT_KERNEL &&      if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) {
     tmplLine->numElements >= 2) {  
3735   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
3736      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
3737       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 3472  int addNewKernel(struct grubConfig * con Line 3808  int addNewKernel(struct grubConfig * con
3808      /* template is multi but new is not,      /* template is multi but new is not,
3809       * insert the kernel in the first module slot       * insert the kernel in the first module slot
3810       */       */
3811      tmplLine->type = LT_KERNEL;      tmplLine->type = preferredLineType(LT_KERNEL, config->cfi);
3812      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3813      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3814   strdup(getKeywordByType(LT_KERNEL, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3815     config->cfi)->key);
3816      newLine = addLineTmpl(new, tmplLine, newLine,      newLine = addLineTmpl(new, tmplLine, newLine,
3817    newKernelPath + strlen(prefix), config->cfi);    newKernelPath + strlen(prefix),
3818      config->cfi);
3819      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
3820   } else if (needs & NEED_INITRD) {   } else if (needs & NEED_INITRD) {
3821      char *initrdVal;      char *initrdVal;
3822      /* template is multi but new is not,      /* template is multi but new is not,
3823       * insert the initrd in the second module slot       * insert the initrd in the second module slot
3824       */       */
3825      tmplLine->type = LT_INITRD;      tmplLine->type = preferredLineType(LT_INITRD, config->cfi);
3826      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3827      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3828   strdup(getKeywordByType(LT_INITRD, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3829     config->cfi)->key);
3830      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3831      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3832      free(initrdVal);      free(initrdVal);
3833      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3834   }   }
3835    
3836      } else if (tmplLine->type == LT_INITRD &&      } else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) {
        tmplLine->numElements >= 2) {  
3837   if (needs & NEED_INITRD &&   if (needs & NEED_INITRD &&
3838      new->multiboot && !template->multiboot &&      new->multiboot && !template->multiboot &&
3839      config->cfi->mbInitRdIsModule) {      config->cfi->mbInitRdIsModule) {
# Line 3549  int addNewKernel(struct grubConfig * con Line 3887  int addNewKernel(struct grubConfig * con
3887      static const char *prefix = "'Loading ";      static const char *prefix = "'Loading ";
3888      if (tmplLine->numElements > 1 &&      if (tmplLine->numElements > 1 &&
3889      strstr(tmplLine->elements[1].item, prefix) &&      strstr(tmplLine->elements[1].item, prefix) &&
3890      masterLine->next && masterLine->next->type == LT_KERNEL) {      masterLine->next &&
3891        iskernel(masterLine->next->type)) {
3892   char *newTitle = malloc(strlen(prefix) +   char *newTitle = malloc(strlen(prefix) +
3893   strlen(newKernelTitle) + 2);   strlen(newKernelTitle) + 2);
3894    
# Line 3576  int addNewKernel(struct grubConfig * con Line 3915  int addNewKernel(struct grubConfig * con
3915   */   */
3916   switch (config->cfi->entryStart) {   switch (config->cfi->entryStart) {
3917      case LT_KERNEL:      case LT_KERNEL:
3918        case LT_KERNEL_EFI:
3919   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
3920      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
3921   } else {   } else {
3922      newLine = addLine(new, config->cfi, LT_KERNEL,      newLine = addLine(new, config->cfi,
3923              preferredLineType(LT_KERNEL, config->cfi),
3924        config->primaryIndent,        config->primaryIndent,
3925        newKernelPath + strlen(prefix));        newKernelPath + strlen(prefix));
3926      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
# Line 3655  int addNewKernel(struct grubConfig * con Line 3996  int addNewKernel(struct grubConfig * con
3996      if (needs & NEED_KERNEL) {      if (needs & NEED_KERNEL) {
3997   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
3998    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
3999        config->cfi)) ?        config->cfi))
4000    LT_MBMODULE : LT_KERNEL,     ? LT_MBMODULE
4001     : preferredLineType(LT_KERNEL, config->cfi),
4002    config->secondaryIndent,    config->secondaryIndent,
4003    newKernelPath + strlen(prefix));    newKernelPath + strlen(prefix));
4004   needs &= ~NEED_KERNEL;   needs &= ~NEED_KERNEL;
# Line 3672  int addNewKernel(struct grubConfig * con Line 4014  int addNewKernel(struct grubConfig * con
4014   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);
4015   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
4016    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
4017        config->cfi)) ?        config->cfi))
4018    LT_MBMODULE : LT_INITRD,     ? LT_MBMODULE
4019       : preferredLineType(LT_INITRD, config->cfi),
4020    config->secondaryIndent,    config->secondaryIndent,
4021    initrdVal);    initrdVal);
4022   free(initrdVal);   free(initrdVal);
# Line 3705  static void traceback(int signum) Line 4048  static void traceback(int signum)
4048      memset(array, '\0', sizeof (array));      memset(array, '\0', sizeof (array));
4049      size = backtrace(array, 40);      size = backtrace(array, 40);
4050    
4051      fprintf(stderr, "grubby recieved SIGSEGV!  Backtrace (%ld):\n",      fprintf(stderr, "grubby received SIGSEGV!  Backtrace (%ld):\n",
4052              (unsigned long)size);              (unsigned long)size);
4053      backtrace_symbols_fd(array, size, STDERR_FILENO);      backtrace_symbols_fd(array, size, STDERR_FILENO);
4054      exit(1);      exit(1);
# Line 3740  int main(int argc, const char ** argv) { Line 4083  int main(int argc, const char ** argv) {
4083      char * removeArgs = NULL;      char * removeArgs = NULL;
4084      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4085      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4086        char * envPath = NULL;
4087      const char * chptr = NULL;      const char * chptr = NULL;
4088      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4089      struct grubConfig * config;      struct grubConfig * config;
# Line 3789  int main(int argc, const char ** argv) { Line 4133  int main(int argc, const char ** argv) {
4133      _("display the title of the default kernel") },      _("display the title of the default kernel") },
4134   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4135      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4136     { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4137        _("force grub2 stanzas to use efi") },
4138     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4139        _("path for environment data"),
4140        _("path") },
4141   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4142      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4143   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3801  int main(int argc, const char ** argv) { Line 4150  int main(int argc, const char ** argv) {
4150   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4151      _("initrd image for the new kernel"), _("initrd-path") },      _("initrd image for the new kernel"), _("initrd-path") },
4152   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4153      _("auxilliary initrd image for things other than the new kernel"), _("initrd-path") },      _("auxiliary initrd image for things other than the new kernel"), _("initrd-path") },
4154   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4155      _("configure lilo bootloader") },      _("configure lilo bootloader") },
4156   { "make-default", 0, 0, &makeDefault, 0,   { "make-default", 0, 0, &makeDefault, 0,
# Line 3845  int main(int argc, const char ** argv) { Line 4194  int main(int argc, const char ** argv) {
4194    
4195      signal(SIGSEGV, traceback);      signal(SIGSEGV, traceback);
4196    
4197        int i = 0;
4198        for (int j = 1; j < argc; j++)
4199     i += strlen(argv[j]) + 1;
4200        saved_command_line = malloc(i);
4201        if (!saved_command_line) {
4202     fprintf(stderr, "grubby: %m\n");
4203     exit(1);
4204        }
4205        saved_command_line[0] = '\0';
4206        for (int j = 1; j < argc; j++) {
4207     strcat(saved_command_line, argv[j]);
4208     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4209        }
4210    
4211      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4212      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
4213    
# Line 3888  int main(int argc, const char ** argv) { Line 4251  int main(int argc, const char ** argv) {
4251   return 1;   return 1;
4252      } else if (configureGrub2) {      } else if (configureGrub2) {
4253   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4254     if (envPath)
4255        cfi->envFile = envPath;
4256      } else if (configureLilo) {      } else if (configureLilo) {
4257   cfi = &liloConfigType;   cfi = &liloConfigType;
4258      } else if (configureGrub) {      } else if (configureGrub) {
# Line 4088  int main(int argc, const char ** argv) { Line 4453  int main(int argc, const char ** argv) {
4453   return 0;   return 0;
4454      }      }
4455    
4456        if (grubConfig == NULL) {
4457     printf("Could not find bootloader configuration file.\n");
4458     exit(1);
4459        }
4460    
4461      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4462      if (!config) return 1;      if (!config) return 1;
4463    
# Line 4097  int main(int argc, const char ** argv) { Line 4467  int main(int argc, const char ** argv) {
4467          char * rootspec;          char * rootspec;
4468    
4469   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4470     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4471     cfi->defaultIsSaved)
4472        config->defaultImage = 0;
4473   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4474   if (!entry) return 0;   if (!entry) return 0;
4475   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4476    
4477   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
4478   if (!line) return 0;   if (!line) return 0;
4479    
4480          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 4115  int main(int argc, const char ** argv) { Line 4488  int main(int argc, const char ** argv) {
4488   struct singleEntry * entry;   struct singleEntry * entry;
4489    
4490   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4491     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4492     cfi->defaultIsSaved)
4493        config->defaultImage = 0;
4494   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4495   if (!entry) return 0;   if (!entry) return 0;
4496    
# Line 4137  int main(int argc, const char ** argv) { Line 4513  int main(int argc, const char ** argv) {
4513    
4514      } else if (displayDefaultIndex) {      } else if (displayDefaultIndex) {
4515          if (config->defaultImage == -1) return 0;          if (config->defaultImage == -1) return 0;
4516     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4517     cfi->defaultIsSaved)
4518        config->defaultImage = 0;
4519          printf("%i\n", config->defaultImage);          printf("%i\n", config->defaultImage);
4520            return 0;
4521    
4522      } else if (kernelInfo)      } else if (kernelInfo)
4523   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
# Line 4155  int main(int argc, const char ** argv) { Line 4535  int main(int argc, const char ** argv) {
4535      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4536                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
4537      if (updateKernelPath && newKernelInitrd) {      if (updateKernelPath && newKernelInitrd) {
4538              if (updateInitrd(config, updateKernelPath, bootPrefix,      if (newMBKernel) {
4539                               newKernelInitrd)) return 1;      if (addMBInitrd(config, newMBKernel, updateKernelPath,
4540     bootPrefix, newKernelInitrd))
4541        return 1;
4542        } else {
4543        if (updateInitrd(config, updateKernelPath, bootPrefix,
4544     newKernelInitrd))
4545     return 1;
4546        }
4547      }      }
4548      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
4549                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,

Legend:
Removed from v.1868  
changed lines
  Added in v.2682