Magellan Linux

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

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

revision 1851 by niro, Mon Jul 2 13:09:47 2012 UTC revision 2995 by niro, Thu Jun 30 10:37:52 2016 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    #if defined(__aarch64__)
64    #define isEfiOnly 1
65    #else
66    #define isEfiOnly 0
67    #endif
68    
69    char *saved_command_line = NULL;
70    
71  /* comments get lumped in with indention */  /* comments get lumped in with indention */
72  struct lineElement {  struct lineElement {
73      char * item;      char * item;
# Line 82  enum lineType_e { Line 94  enum lineType_e {
94      LT_MENUENTRY    = 1 << 17,      LT_MENUENTRY    = 1 << 17,
95      LT_ENTRY_END    = 1 << 18,      LT_ENTRY_END    = 1 << 18,
96      LT_SET_VARIABLE = 1 << 19,      LT_SET_VARIABLE = 1 << 19,
97      LT_UNKNOWN      = 1 << 20,      LT_KERNEL_EFI   = 1 << 20,
98        LT_INITRD_EFI   = 1 << 21,
99        LT_KERNEL_16    = 1 << 22,
100        LT_INITRD_16    = 1 << 23,
101        LT_DEVTREE      = 1 << 24,
102        LT_UNKNOWN      = 1 << 25,
103  };  };
104    
105  struct singleLine {  struct singleLine {
# Line 111  struct singleEntry { Line 128  struct singleEntry {
128  #define NEED_ARGS    (1 << 3)  #define NEED_ARGS    (1 << 3)
129  #define NEED_MB      (1 << 4)  #define NEED_MB      (1 << 4)
130  #define NEED_END     (1 << 5)  #define NEED_END     (1 << 5)
131    #define NEED_DEVTREE (1 << 6)
132    
133  #define MAIN_DEFAULT    (1 << 0)  #define MAIN_DEFAULT    (1 << 0)
134  #define DEFAULT_SAVED       -2  #define DEFAULT_SAVED       -2
# Line 128  struct configFileInfo; Line 146  struct configFileInfo;
146  typedef const char *(*findConfigFunc)(struct configFileInfo *);  typedef const char *(*findConfigFunc)(struct configFileInfo *);
147  typedef const int (*writeLineFunc)(struct configFileInfo *,  typedef const int (*writeLineFunc)(struct configFileInfo *,
148   struct singleLine *line);   struct singleLine *line);
149    typedef char *(*getEnvFunc)(struct configFileInfo *, char *name);
150    typedef int (*setEnvFunc)(struct configFileInfo *, char *name, char *value);
151    
152  struct configFileInfo {  struct configFileInfo {
153      char * defaultConfig;      char * defaultConfig;
154      findConfigFunc findConfig;      findConfigFunc findConfig;
155      writeLineFunc writeLine;      writeLineFunc writeLine;
156        getEnvFunc getEnv;
157        setEnvFunc setEnv;
158      struct keywordTypes * keywords;      struct keywordTypes * keywords;
159        int caseInsensitive;
160      int defaultIsIndex;      int defaultIsIndex;
161      int defaultIsVariable;      int defaultIsVariable;
162      int defaultSupportSaved;      int defaultSupportSaved;
163        int defaultIsSaved;
164        int defaultIsUnquoted;
165      enum lineType_e entryStart;      enum lineType_e entryStart;
166      enum lineType_e entryEnd;      enum lineType_e entryEnd;
167      int needsBootPrefix;      int needsBootPrefix;
# Line 148  struct configFileInfo { Line 173  struct configFileInfo {
173      int mbInitRdIsModule;      int mbInitRdIsModule;
174      int mbConcatArgs;      int mbConcatArgs;
175      int mbAllowExtraInitRds;      int mbAllowExtraInitRds;
176        char *envFile;
177  };  };
178    
179  struct keywordTypes grubKeywords[] = {  struct keywordTypes grubKeywords[] = {
# Line 164  struct keywordTypes grubKeywords[] = { Line 190  struct keywordTypes grubKeywords[] = {
190    
191  const char *grubFindConfig(struct configFileInfo *cfi) {  const char *grubFindConfig(struct configFileInfo *cfi) {
192      static const char *configFiles[] = {      static const char *configFiles[] = {
  "/etc/grub.conf",  
193   "/boot/grub/grub.conf",   "/boot/grub/grub.conf",
194   "/boot/grub/menu.lst",   "/boot/grub/menu.lst",
195     "/etc/grub.conf",
196     "/boot/grub2/grub.cfg",
197     "/boot/grub2-efi/grub.cfg",
198   NULL   NULL
199      };      };
200      static int i = -1;      static int i = -1;
# Line 194  struct configFileInfo grubConfigType = { Line 222  struct configFileInfo grubConfigType = {
222      .mbHyperFirst = 1,      .mbHyperFirst = 1,
223      .mbInitRdIsModule = 1,      .mbInitRdIsModule = 1,
224      .mbAllowExtraInitRds = 1,      .mbAllowExtraInitRds = 1,
225        .titlePosition = 1,
226  };  };
227    
228  struct keywordTypes grub2Keywords[] = {  struct keywordTypes grub2Keywords[] = {
# Line 205  struct keywordTypes grub2Keywords[] = { Line 234  struct keywordTypes grub2Keywords[] = {
234      { "default",    LT_DEFAULT,     ' ' },      { "default",    LT_DEFAULT,     ' ' },
235      { "fallback",   LT_FALLBACK,    ' ' },      { "fallback",   LT_FALLBACK,    ' ' },
236      { "linux",      LT_KERNEL,      ' ' },      { "linux",      LT_KERNEL,      ' ' },
237        { "linuxefi",   LT_KERNEL_EFI,  ' ' },
238        { "linux16",    LT_KERNEL_16,   ' ' },
239      { "initrd",     LT_INITRD,      ' ', ' ' },      { "initrd",     LT_INITRD,      ' ', ' ' },
240        { "initrdefi",  LT_INITRD_EFI,  ' ', ' ' },
241        { "initrd16",   LT_INITRD_16,   ' ', ' ' },
242      { "module",     LT_MBMODULE,    ' ' },      { "module",     LT_MBMODULE,    ' ' },
243      { "kernel",     LT_HYPER,       ' ' },      { "kernel",     LT_HYPER,       ' ' },
244        { "devicetree", LT_DEVTREE,  ' ' },
245      { NULL, 0, 0 },      { NULL, 0, 0 },
246  };  };
247    
# Line 219  const char *grub2FindConfig(struct confi Line 253  const char *grub2FindConfig(struct confi
253      };      };
254      static int i = -1;      static int i = -1;
255      static const char *grub_cfg = "/boot/grub/grub.cfg";      static const char *grub_cfg = "/boot/grub/grub.cfg";
256        int rc = -1;
257    
258      if (i == -1) {      if (i == -1) {
259   for (i = 0; configFiles[i] != NULL; i++) {   for (i = 0; configFiles[i] != NULL; i++) {
260      dbgPrintf("Checking \"%s\": ", configFiles[i]);      dbgPrintf("Checking \"%s\": ", configFiles[i]);
261      if (!access(configFiles[i], R_OK)) {      if ((rc = access(configFiles[i], R_OK))) {
262     if (errno == EACCES) {
263        printf("Unable to access bootloader configuration file "
264           "\"%s\": %m\n", configFiles[i]);
265        exit(1);
266     }
267     continue;
268        } else {
269   dbgPrintf("found\n");   dbgPrintf("found\n");
270   return configFiles[i];   return configFiles[i];
271      }      }
# Line 242  const char *grub2FindConfig(struct confi Line 284  const char *grub2FindConfig(struct confi
284      return configFiles[i];      return configFiles[i];
285  }  }
286    
287    /* kind of hacky.  It'll give the first 1024 bytes, ish. */
288    static char *grub2GetEnv(struct configFileInfo *info, char *name)
289    {
290        static char buf[1025];
291        char *s = NULL;
292        char *ret = NULL;
293        char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv";
294        int rc = asprintf(&s, "grub-editenv %s list | grep '^%s='", envFile, name);
295    
296        if (rc < 0)
297     return NULL;
298    
299        FILE *f = popen(s, "r");
300        if (!f)
301     goto out;
302    
303        memset(buf, '\0', sizeof (buf));
304        ret = fgets(buf, 1024, f);
305        pclose(f);
306    
307        if (ret) {
308     ret += strlen(name) + 1;
309     ret[strlen(ret) - 1] = '\0';
310        }
311        dbgPrintf("grub2GetEnv(%s): %s\n", name, ret);
312    out:
313        free(s);
314        return ret;
315    }
316    
317    static int sPopCount(const char *s, const char *c)
318    {
319        int ret = 0;
320        if (!s)
321     return -1;
322        for (int i = 0; s[i] != '\0'; i++)
323     for (int j = 0; c[j] != '\0'; j++)
324        if (s[i] == c[j])
325     ret++;
326        return ret;
327    }
328    
329    static char *shellEscape(const char *s)
330    {
331        int l = strlen(s) + sPopCount(s, "'") * 2;
332    
333        char *ret = calloc(l+1, sizeof (*ret));
334        if (!ret)
335     return NULL;
336        for (int i = 0, j = 0; s[i] != '\0'; i++, j++) {
337     if (s[i] == '\'')
338        ret[j++] = '\\';
339     ret[j] = s[i];
340        }
341        return ret;
342    }
343    
344    static void unquote(char *s)
345    {
346        int l = strlen(s);
347    
348        if ((s[l-1] == '\'' && s[0] == '\'') || (s[l-1] == '"' && s[0] == '"')) {
349     memmove(s, s+1, l-2);
350     s[l-2] = '\0';
351        }
352    }
353    
354    static int grub2SetEnv(struct configFileInfo *info, char *name, char *value)
355    {
356        char *s = NULL;
357        int rc = 0;
358        char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv";
359    
360        unquote(value);
361        value = shellEscape(value);
362        if (!value)
363        return -1;
364    
365        rc = asprintf(&s, "grub-editenv %s set '%s=%s'", envFile, name, value);
366        free(value);
367        if (rc <0)
368     return -1;
369    
370        dbgPrintf("grub2SetEnv(%s): %s\n", name, s);
371        rc = system(s);
372        free(s);
373        return rc;
374    }
375    
376    /* this is a gigantic hack to avoid clobbering grub2 variables... */
377    static int is_special_grub2_variable(const char *name)
378    {
379        if (!strcmp(name,"\"${next_entry}\""))
380     return 1;
381        if (!strcmp(name,"\"${prev_saved_entry}\""))
382     return 1;
383        return 0;
384    }
385    
386  int sizeOfSingleLine(struct singleLine * line) {  int sizeOfSingleLine(struct singleLine * line) {
387    int count = 0;    int count = 0;
388    
# Line 271  static int isquote(char q) Line 412  static int isquote(char q)
412      return 0;      return 0;
413  }  }
414    
415    static int iskernel(enum lineType_e type) {
416        return (type == LT_KERNEL || type == LT_KERNEL_EFI || type == LT_KERNEL_16);
417    }
418    
419    static int isinitrd(enum lineType_e type) {
420        return (type == LT_INITRD || type == LT_INITRD_EFI || type == LT_INITRD_16);
421    }
422    
423  char *grub2ExtractTitle(struct singleLine * line) {  char *grub2ExtractTitle(struct singleLine * line) {
424      char * current;      char * current;
425      char * current_indent;      char * current_indent;
# Line 280  char *grub2ExtractTitle(struct singleLin Line 429  char *grub2ExtractTitle(struct singleLin
429    
430      /* bail out if line does not start with menuentry */      /* bail out if line does not start with menuentry */
431      if (strcmp(line->elements[0].item, "menuentry"))      if (strcmp(line->elements[0].item, "menuentry"))
432        return NULL;   return NULL;
433    
434      i = 1;      i = 1;
435      current = line->elements[i].item;      current = line->elements[i].item;
# Line 289  char *grub2ExtractTitle(struct singleLin Line 438  char *grub2ExtractTitle(struct singleLin
438      /* if second word is quoted, strip the quotes and return single word */      /* if second word is quoted, strip the quotes and return single word */
439      if (isquote(*current) && isquote(current[current_len - 1])) {      if (isquote(*current) && isquote(current[current_len - 1])) {
440   char *tmp;   char *tmp;
441    
442   tmp = strdup(current);   tmp = strdup(current+1);
443   *(tmp + current_len - 1) = '\0';   if (!tmp)
444   return ++tmp;      return NULL;
445     tmp[strlen(tmp)-1] = '\0';
446     return tmp;
447      }      }
448    
449      /* if no quotes, return second word verbatim */      /* if no quotes, return second word verbatim */
# Line 303  char *grub2ExtractTitle(struct singleLin Line 454  char *grub2ExtractTitle(struct singleLin
454       * whose last character is also quote (assuming it's the closing one) */       * whose last character is also quote (assuming it's the closing one) */
455      int resultMaxSize;      int resultMaxSize;
456      char * result;      char * result;
457            /* need to ensure that ' does not match " as we search */
458        char quote_char = *current;
459    
460      resultMaxSize = sizeOfSingleLine(line);      resultMaxSize = sizeOfSingleLine(line);
461      result = malloc(resultMaxSize);      result = malloc(resultMaxSize);
462      snprintf(result, resultMaxSize, "%s", ++current);      snprintf(result, resultMaxSize, "%s", ++current);
463        
464      i++;      i++;
465      for (; i < line->numElements; ++i) {      for (; i < line->numElements; ++i) {
466   current = line->elements[i].item;   current = line->elements[i].item;
# Line 316  char *grub2ExtractTitle(struct singleLin Line 469  char *grub2ExtractTitle(struct singleLin
469   current_indent_len = strlen(current_indent);   current_indent_len = strlen(current_indent);
470    
471   strncat(result, current_indent, current_indent_len);   strncat(result, current_indent, current_indent_len);
472   if (!isquote(current[current_len-1])) {   if (current[current_len-1] != quote_char) {
473      strncat(result, current, current_len);      strncat(result, current, current_len);
474   } else {   } else {
475      strncat(result, current, current_len - 1);      strncat(result, current, current_len - 1);
# Line 328  char *grub2ExtractTitle(struct singleLin Line 481  char *grub2ExtractTitle(struct singleLin
481    
482  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
483      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
484        .getEnv = grub2GetEnv,
485        .setEnv = grub2SetEnv,
486      .keywords = grub2Keywords,      .keywords = grub2Keywords,
487      .defaultIsIndex = 1,      .defaultIsIndex = 1,
488      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
# Line 432  struct keywordTypes extlinuxKeywords[] = Line 587  struct keywordTypes extlinuxKeywords[] =
587      { "initrd",    LT_INITRD,      ' ', ',' },      { "initrd",    LT_INITRD,      ' ', ',' },
588      { "append",    LT_KERNELARGS,  ' ' },      { "append",    LT_KERNELARGS,  ' ' },
589      { "prompt",     LT_UNKNOWN,     ' ' },      { "prompt",     LT_UNKNOWN,     ' ' },
590        { "fdt",        LT_DEVTREE,     ' ' },
591        { "fdtdir",     LT_DEVTREE,     ' ' },
592      { NULL,    0, 0 },      { NULL,    0, 0 },
593  };  };
594  int useextlinuxmenu;  int useextlinuxmenu;
# Line 442  struct configFileInfo eliloConfigType = Line 599  struct configFileInfo eliloConfigType =
599      .needsBootPrefix = 1,      .needsBootPrefix = 1,
600      .argsInQuotes = 1,      .argsInQuotes = 1,
601      .mbConcatArgs = 1,      .mbConcatArgs = 1,
602        .titlePosition = 1,
603  };  };
604    
605  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
# Line 450  struct configFileInfo liloConfigType = { Line 608  struct configFileInfo liloConfigType = {
608      .entryStart = LT_KERNEL,      .entryStart = LT_KERNEL,
609      .argsInQuotes = 1,      .argsInQuotes = 1,
610      .maxTitleLength = 15,      .maxTitleLength = 15,
611        .titlePosition = 1,
612  };  };
613    
614  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
# Line 460  struct configFileInfo yabootConfigType = Line 619  struct configFileInfo yabootConfigType =
619      .argsInQuotes = 1,      .argsInQuotes = 1,
620      .maxTitleLength = 15,      .maxTitleLength = 15,
621      .mbAllowExtraInitRds = 1,      .mbAllowExtraInitRds = 1,
622        .titlePosition = 1,
623  };  };
624    
625  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
# Line 469  struct configFileInfo siloConfigType = { Line 629  struct configFileInfo siloConfigType = {
629      .needsBootPrefix = 1,      .needsBootPrefix = 1,
630      .argsInQuotes = 1,      .argsInQuotes = 1,
631      .maxTitleLength = 15,      .maxTitleLength = 15,
632        .titlePosition = 1,
633  };  };
634    
635  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
# Line 482  struct configFileInfo ziplConfigType = { Line 643  struct configFileInfo ziplConfigType = {
643  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
644      .defaultConfig = "/boot/extlinux/extlinux.conf",      .defaultConfig = "/boot/extlinux/extlinux.conf",
645      .keywords = extlinuxKeywords,      .keywords = extlinuxKeywords,
646        .caseInsensitive = 1,
647      .entryStart = LT_TITLE,      .entryStart = LT_TITLE,
648      .needsBootPrefix = 1,      .needsBootPrefix = 1,
649      .maxTitleLength = 255,      .maxTitleLength = 255,
650      .mbAllowExtraInitRds = 1,      .mbAllowExtraInitRds = 1,
651        .defaultIsUnquoted = 1,
652        .titlePosition = 1,
653  };  };
654    
655  struct grubConfig {  struct grubConfig {
# Line 506  struct singleEntry * findEntryByIndex(st Line 670  struct singleEntry * findEntryByIndex(st
670  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
671       const char * path, const char * prefix,       const char * path, const char * prefix,
672       int * index);       int * index);
673    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
674          int * index);
675  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
676  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
677  struct singleLine * lineDup(struct singleLine * line);  struct singleLine * lineDup(struct singleLine * line);
# Line 570  static char * sdupprintf(const char *for Line 736  static char * sdupprintf(const char *for
736      return buf;      return buf;
737  }  }
738    
739    static enum lineType_e preferredLineType(enum lineType_e type,
740     struct configFileInfo *cfi) {
741        if (isEfi && cfi == &grub2ConfigType) {
742     switch (type) {
743     case LT_KERNEL:
744        return isEfiOnly ? LT_KERNEL : LT_KERNEL_EFI;
745     case LT_INITRD:
746        return isEfiOnly ? LT_INITRD : LT_INITRD_EFI;
747     default:
748        return type;
749     }
750    #if defined(__i386__) || defined(__x86_64__)
751        } else if (cfi == &grub2ConfigType) {
752     switch (type) {
753     case LT_KERNEL:
754        return LT_KERNEL_16;
755     case LT_INITRD:
756        return LT_INITRD_16;
757     default:
758        return type;
759     }
760    #endif
761        }
762        return type;
763    }
764    
765  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
766        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
767      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 795  static char * getuuidbydev(char *device)
795  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
796   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
797      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
798   if (!strcmp(keyword, kw->key))   if (cfi->caseInsensitive) {
799      return kw->type;      if (!strcasecmp(keyword, kw->key))
800                    return kw->type;
801     } else {
802        if (!strcmp(keyword, kw->key))
803            return kw->type;
804     }
805      }      }
806      return LT_UNKNOWN;      return LT_UNKNOWN;
807  }  }
# Line 641  static int isEntryStart(struct singleLin Line 838  static int isEntryStart(struct singleLin
838  }  }
839    
840  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
841  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct grubConfig *cfg, struct singleLine * line) {
842      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it */
843      char * title;      char * title = NULL;
844      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
845      title++;   char *tmp = line->elements[cfg->cfi->titlePosition].item;
846      *(title + strlen(title) - 1) = '\0';   if (cfg->cfi->titleBracketed) {
847        tmp++;
848        title = strdup(tmp);
849        *(title + strlen(title) - 1) = '\0';
850     } else {
851        title = strdup(tmp);
852     }
853        } else if (line->type == LT_MENUENTRY)
854     title = strdup(line->elements[1].item);
855        else
856     return NULL;
857      return title;      return title;
858  }  }
859    
# Line 726  static int lineWrite(FILE * out, struct Line 933  static int lineWrite(FILE * out, struct
933   /* Need to handle this, because we strip the quotes from   /* Need to handle this, because we strip the quotes from
934   * menuentry when read it. */   * menuentry when read it. */
935   if (line->type == LT_MENUENTRY && i == 1) {   if (line->type == LT_MENUENTRY && i == 1) {
936      if(!isquote(*line->elements[i].item))      if(!isquote(*line->elements[i].item)) {
937   fprintf(out, "\'%s\'", line->elements[i].item);   int substring = 0;
938      else   /* If the line contains nested quotes, we did not strip
939     * the "interna" quotes and we must use the right quotes
940     * again when writing the updated file. */
941     for (int j = i; j < line->numElements; j++) {
942        if (strchr(line->elements[i].item, '\'') != NULL) {
943           substring = 1;
944           fprintf(out, "\"%s\"", line->elements[i].item);
945           break;
946        }
947     }
948     if (!substring)
949        fprintf(out, "\'%s\'", line->elements[i].item);
950        } else {
951   fprintf(out, "%s", line->elements[i].item);   fprintf(out, "%s", line->elements[i].item);
952        }
953      fprintf(out, "%s", line->elements[i].indent);      fprintf(out, "%s", line->elements[i].indent);
954    
955      continue;      continue;
# Line 904  static int getNextLine(char ** bufPtr, s Line 1124  static int getNextLine(char ** bufPtr, s
1124      return 0;      return 0;
1125  }  }
1126    
1127    static int isnumber(const char *s)
1128    {
1129        int i;
1130        for (i = 0; s[i] != '\0'; i++)
1131     if (s[i] < '0' || s[i] > '9')
1132        return 0;
1133        return i;
1134    }
1135    
1136  static struct grubConfig * readConfig(const char * inName,  static struct grubConfig * readConfig(const char * inName,
1137        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
1138      int in;      int in;
# Line 918  static struct grubConfig * readConfig(co Line 1147  static struct grubConfig * readConfig(co
1147      int len;      int len;
1148      char * buf;      char * buf;
1149    
1150      if (!strcmp(inName, "-")) {      if (inName == NULL) {
1151            printf("Could not find bootloader configuration\n");
1152            exit(1);
1153        } else if (!strcmp(inName, "-")) {
1154   in = 0;   in = 0;
1155      } else {      } else {
1156   if ((in = open(inName, O_RDONLY)) < 0) {   if ((in = open(inName, O_RDONLY)) < 0) {
# Line 985  static struct grubConfig * readConfig(co Line 1217  static struct grubConfig * readConfig(co
1217      dbgPrintf("\n");      dbgPrintf("\n");
1218      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1219      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
1220      !strcmp(line->elements[1].item, kwType->key)) {      !strcmp(line->elements[1].item, kwType->key) &&
1221        !is_special_grub2_variable(line->elements[2].item)) {
1222   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1223   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1224   defaultLine = line;   defaultLine = line;
1225      }      }
  } else if (line->type == LT_DEFAULT && line->numElements == 2) {  
     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;  
     defaultLine = line;  
1226    
1227          } else if (line->type == LT_KERNEL) {          } else if (iskernel(line->type)) {
1228      /* if by some freak chance this is multiboot and the "module"      /* if by some freak chance this is multiboot and the "module"
1229       * lines came earlier in the template, make sure to use LT_HYPER       * lines came earlier in the template, make sure to use LT_HYPER
1230       * instead of LT_KERNEL now       * instead of LT_KERNEL now
1231       */       */
1232      if (entry->multiboot)      if (entry && entry->multiboot)
1233   line->type = LT_HYPER;   line->type = LT_HYPER;
1234    
1235          } else if (line->type == LT_MBMODULE) {          } else if (line->type == LT_MBMODULE) {
# Line 1011  static struct grubConfig * readConfig(co Line 1241  static struct grubConfig * readConfig(co
1241      for (struct singleLine *l = entry->lines; l; l = l->next) {      for (struct singleLine *l = entry->lines; l; l = l->next) {
1242   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1243      break;      break;
1244   else if (l->type == LT_KERNEL) {   else if (iskernel(l->type)) {
1245      l->type = LT_HYPER;      l->type = LT_HYPER;
1246      break;      break;
1247   }   }
# Line 1025  static struct grubConfig * readConfig(co Line 1255  static struct grubConfig * readConfig(co
1255      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);
1256      if (*end) cfg->fallbackImage = -1;      if (*end) cfg->fallbackImage = -1;
1257    
1258   } else if (line->type == LT_TITLE && line->numElements > 1) {   } else if ((line->type == LT_DEFAULT && cfi->defaultIsUnquoted) ||
1259      /* make the title a single argument (undoing our parsing) */                  (line->type == LT_TITLE && line->numElements > 1)) {
1260        /* make the title/default a single argument (undoing our parsing) */
1261      len = 0;      len = 0;
1262      for (int i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1263   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
# Line 1054  static struct grubConfig * readConfig(co Line 1285  static struct grubConfig * readConfig(co
1285      len = 0;      len = 0;
1286      char *extras;      char *extras;
1287      char *title;      char *title;
1288        /* initially unseen value */
1289        char quote_char = '\0';
1290    
1291      for (int i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1292   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
# Line 1070  static struct grubConfig * readConfig(co Line 1303  static struct grubConfig * readConfig(co
1303      for (int i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
1304   if (!strcmp(line->elements[i].item, "menuentry"))   if (!strcmp(line->elements[i].item, "menuentry"))
1305      continue;      continue;
1306   if (isquote(*line->elements[i].item))   if (isquote(*line->elements[i].item) && quote_char == '\0') {
1307        /* ensure we properly pair off quotes */
1308        quote_char = *line->elements[i].item;
1309      title = line->elements[i].item + 1;      title = line->elements[i].item + 1;
1310   else   } else {
1311      title = line->elements[i].item;      title = line->elements[i].item;
1312     }
1313    
1314   len = strlen(title);   len = strlen(title);
1315          if (isquote(title[len-1])) {          if (title[len-1] == quote_char) {
1316      strncat(buf, title,len-1);      strncat(buf, title,len-1);
1317      break;      break;
1318   } else {   } else {
# Line 1087  static struct grubConfig * readConfig(co Line 1323  static struct grubConfig * readConfig(co
1323    
1324      /* get extras */      /* get extras */
1325      int count = 0;      int count = 0;
1326        quote_char = '\0';
1327      for (int i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
1328   if (count >= 2) {   if (count >= 2) {
1329      strcat(extras, line->elements[i].item);      strcat(extras, line->elements[i].item);
# Line 1097  static struct grubConfig * readConfig(co Line 1334  static struct grubConfig * readConfig(co
1334      continue;      continue;
1335    
1336   /* count ' or ", there should be two in menuentry line. */   /* count ' or ", there should be two in menuentry line. */
1337   if (isquote(*line->elements[i].item))   if (isquote(*line->elements[i].item) && quote_char == '\0') {
1338        /* ensure we properly pair off quotes */
1339                quote_char = *line->elements[i].item;
1340      count++;      count++;
1341     }
1342    
1343   len = strlen(line->elements[i].item);   len = strlen(line->elements[i].item);
1344    
1345   if (isquote(line->elements[i].item[len -1]))   if (line->elements[i].item[len -1] == quote_char)
1346      count++;      count++;
1347    
1348   /* ok, we get the final ' or ", others are extras. */   /* ok, we get the final ' or ", others are extras. */
# Line 1133  static struct grubConfig * readConfig(co Line 1373  static struct grubConfig * readConfig(co
1373      }      }
1374   }   }
1375    
1376     if (line->type == LT_DEFAULT && line->numElements == 2) {
1377        cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1378        defaultLine = line;
1379     }
1380    
1381   /* If we find a generic config option which should live at the   /* If we find a generic config option which should live at the
1382     top of the file, move it there. Old versions of grubby were     top of the file, move it there. Old versions of grubby were
1383     probably responsible for putting new images in the wrong     probably responsible for putting new images in the wrong
# Line 1190  static struct grubConfig * readConfig(co Line 1435  static struct grubConfig * readConfig(co
1435          if (defaultLine->numElements > 2 &&          if (defaultLine->numElements > 2 &&
1436      cfi->defaultSupportSaved &&      cfi->defaultSupportSaved &&
1437      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1438      cfg->defaultImage = DEFAULT_SAVED_GRUB2;   cfg->cfi->defaultIsSaved = 1;
1439     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1440     if (cfg->cfi->getEnv) {
1441        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1442        if (defTitle) {
1443     int index = 0;
1444     if (isnumber(defTitle)) {
1445        index = atoi(defTitle);
1446        entry = findEntryByIndex(cfg, index);
1447     } else {
1448        entry = findEntryByTitle(cfg, defTitle, &index);
1449     }
1450     if (entry)
1451        cfg->defaultImage = index;
1452        }
1453     }
1454   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1455      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1456      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
# Line 1219  static struct grubConfig * readConfig(co Line 1479  static struct grubConfig * readConfig(co
1479                                  line->elements[1].item)) break;                                  line->elements[1].item)) break;
1480                  } else if (line) {                  } else if (line) {
1481                      if (!strcmp(defaultLine->elements[1].item,                      if (!strcmp(defaultLine->elements[1].item,
1482                                  extractTitle(line))) break;                                  extractTitle(cfg, line))) break;
1483                  }                  }
1484   i++;   i++;
1485   entry = NULL;   entry = NULL;
# Line 1231  static struct grubConfig * readConfig(co Line 1491  static struct grubConfig * readConfig(co
1491          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1492      }      }
1493   }   }
1494        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1495     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1496     if (defTitle) {
1497        int index = 0;
1498        if (isnumber(defTitle)) {
1499     index = atoi(defTitle);
1500     entry = findEntryByIndex(cfg, index);
1501        } else {
1502     entry = findEntryByTitle(cfg, defTitle, &index);
1503        }
1504        if (entry)
1505     cfg->defaultImage = index;
1506     }
1507      } else {      } else {
1508          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1509      }      }
# Line 1238  static struct grubConfig * readConfig(co Line 1511  static struct grubConfig * readConfig(co
1511      return cfg;      return cfg;
1512  }  }
1513    
1514  static void writeDefault(FILE * out, char * indent,  static void writeDefault(FILE * out, char * indent,
1515   char * separator, struct grubConfig * cfg) {   char * separator, struct grubConfig * cfg) {
1516      struct singleEntry * entry;      struct singleEntry * entry;
1517      struct singleLine * line;      struct singleLine * line;
1518      int i;      int i;
1519    
1520      if (!cfg->defaultImage && cfg->flags == GRUB_CONFIG_NO_DEFAULT) return;      if (!cfg->defaultImage && cfg->flags == GRUB_CONFIG_NO_DEFAULT)
1521     return;
1522    
1523      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1524   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1525      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1526   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1527      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1528        char *title;
1529        entry = findEntryByIndex(cfg, cfg->defaultImage);
1530        line = getLineByType(LT_MENUENTRY, entry->lines);
1531        if (!line)
1532     line = getLineByType(LT_TITLE, entry->lines);
1533        if (line) {
1534     title = extractTitle(cfg, line);
1535     if (title)
1536        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1537        }
1538     }
1539        } else if (cfg->defaultImage > -1) {
1540   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1541      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1542          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
1543   cfg->defaultImage);   cfg->defaultImage);
1544      } else {      } else {
1545   fprintf(out, "%sdefault%s%d\n", indent, separator,   fprintf(out, "%sdefault%s%d\n", indent, separator,
1546   cfg->defaultImage);   cfg->defaultImage);
1547      }      }
1548   } else {   } else {
1549      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1550    
1551      entry = cfg->entries;      entry = cfg->entries;
1552      while (entry && entry->skip) entry = entry->next;      while (entry && entry->skip)
1553     entry = entry->next;
1554    
1555      i = 0;      i = 0;
1556      while (entry && i < image) {      while (entry && i < image) {
1557   entry = entry->next;   entry = entry->next;
1558    
1559   while (entry && entry->skip) entry = entry->next;   while (entry && entry->skip)
1560        entry = entry->next;
1561   i++;   i++;
1562      }      }
1563    
1564      if (!entry) return;      if (!entry)
1565     return;
1566    
1567      line = getLineByType(LT_TITLE, entry->lines);      line = getLineByType(LT_TITLE, entry->lines);
1568    
1569      if (line && line->numElements >= 2)      if (line && line->numElements >= 2)
1570   fprintf(out, "%sdefault%s%s\n", indent, separator,   fprintf(out, "%sdefault%s%s\n", indent, separator,
1571   line->elements[1].item);   line->elements[1].item);
1572              else if (line && (line->numElements == 1) &&              else if (line && (line->numElements == 1) &&
1573                       cfg->cfi->titleBracketed) {                       cfg->cfi->titleBracketed) {
1574   fprintf(out, "%sdefault%s%s\n", indent, separator,   char *title = extractTitle(cfg, line);
1575                          extractTitle(line));   if (title) {
1576        fprintf(out, "%sdefault%s%s\n", indent, separator, title);
1577        free(title);
1578     }
1579              }              }
1580   }   }
1581      }      }
# Line 1310  static int writeConfig(struct grubConfig Line 1602  static int writeConfig(struct grubConfig
1602    
1603      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1604         directory to the dir of the symlink */         directory to the dir of the symlink */
1605              rc = chdir(dirname(outName));      char *dir = strdupa(outName);
1606        rc = chdir(dirname(dir));
1607      do {      do {
1608   buf = alloca(len + 1);   buf = alloca(len + 1);
1609   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1352  static int writeConfig(struct grubConfig Line 1645  static int writeConfig(struct grubConfig
1645      while (line) {      while (line) {
1646          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1647   line->numElements == 3 &&   line->numElements == 3 &&
1648   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1649     !is_special_grub2_variable(line->elements[2].item)) {
1650      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1651      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1652   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1497  static char *findDiskForRoot() Line 1791  static char *findDiskForRoot()
1791      return NULL;      return NULL;
1792  }  }
1793    
1794  void printEntry(struct singleEntry * entry) {  void printEntry(struct singleEntry * entry, FILE *f) {
1795      int i;      int i;
1796      struct singleLine * line;      struct singleLine * line;
1797    
1798      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1799   fprintf(stderr, "DBG: %s", line->indent);   log_message(f, "DBG: %s", line->indent);
1800   for (i = 0; i < line->numElements; i++) {   for (i = 0; i < line->numElements; i++) {
1801      /* Need to handle this, because we strip the quotes from      /* Need to handle this, because we strip the quotes from
1802       * menuentry when read it. */       * menuentry when read it. */
1803      if (line->type == LT_MENUENTRY && i == 1) {      if (line->type == LT_MENUENTRY && i == 1) {
1804   if(!isquote(*line->elements[i].item))   if(!isquote(*line->elements[i].item))
1805      fprintf(stderr, "\'%s\'", line->elements[i].item);      log_message(f, "\'%s\'", line->elements[i].item);
1806   else   else
1807      fprintf(stderr, "%s", line->elements[i].item);      log_message(f, "%s", line->elements[i].item);
1808   fprintf(stderr, "%s", line->elements[i].indent);   log_message(f, "%s", line->elements[i].indent);
1809    
1810   continue;   continue;
1811      }      }
1812            
1813      fprintf(stderr, "%s%s",      log_message(f, "%s%s",
1814      line->elements[i].item, line->elements[i].indent);      line->elements[i].item, line->elements[i].indent);
1815   }   }
1816   fprintf(stderr, "\n");   log_message(f, "\n");
1817      }      }
1818  }  }
1819    
1820  void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)  void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1821  {  {
1822      va_list argp;      static int once;
1823        va_list argp, argq;
1824    
1825      if (!debug)      va_start(argp, fmt);
1826    
1827        va_copy(argq, argp);
1828        if (!once) {
1829     log_time(NULL);
1830     log_message(NULL, "command line: %s\n", saved_command_line);
1831        }
1832        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1833        log_vmessage(NULL, fmt, argq);
1834    
1835        printEntry(entry, NULL);
1836        va_end(argq);
1837    
1838        if (!debug) {
1839     once = 1;
1840         va_end(argp);
1841   return;   return;
1842        }
1843    
1844      va_start(argp, fmt);      if (okay) {
1845     va_end(argp);
1846     return;
1847        }
1848    
1849        if (!once)
1850     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1851        once = 1;
1852      fprintf(stderr, "DBG: Image entry failed: ");      fprintf(stderr, "DBG: Image entry failed: ");
1853      vfprintf(stderr, fmt, argp);      vfprintf(stderr, fmt, argp);
1854      printEntry(entry);      printEntry(entry, stderr);
1855      va_end(argp);      va_end(argp);
1856  }  }
1857    
# Line 1560  int suitableImage(struct singleEntry * e Line 1878  int suitableImage(struct singleEntry * e
1878      char * rootdev;      char * rootdev;
1879    
1880      if (skipRemoved && entry->skip) {      if (skipRemoved && entry->skip) {
1881   notSuitablePrintf(entry, "marked to skip\n");   notSuitablePrintf(entry, 0, "marked to skip\n");
1882   return 0;   return 0;
1883      }      }
1884    
1885      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
1886      if (!line) {      if (!line) {
1887   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1888   return 0;   return 0;
1889      }      }
1890      if (line->numElements < 2) {      if (line->numElements < 2) {
1891   notSuitablePrintf(entry, "line has only %d elements\n",   notSuitablePrintf(entry, 0, "line has only %d elements\n",
1892      line->numElements);      line->numElements);
1893   return 0;   return 0;
1894      }      }
1895    
1896      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) {
1897        notSuitablePrintf(entry, 1, "\n");
1898        return 1;
1899        }
1900    
1901      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1902        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
# Line 1586  int suitableImage(struct singleEntry * e Line 1907  int suitableImage(struct singleEntry * e
1907      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1908              line->elements[1].item + rootspec_offset);              line->elements[1].item + rootspec_offset);
1909      if (access(fullName, R_OK)) {      if (access(fullName, R_OK)) {
1910   notSuitablePrintf(entry, "access to %s failed\n", fullName);   notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1911   return 0;   return 0;
1912      }      }
1913      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
# Line 1607  int suitableImage(struct singleEntry * e Line 1928  int suitableImage(struct singleEntry * e
1928    
1929              /* failed to find one */              /* failed to find one */
1930              if (!line) {              if (!line) {
1931   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1932   return 0;   return 0;
1933              }              }
1934    
# Line 1616  int suitableImage(struct singleEntry * e Line 1937  int suitableImage(struct singleEntry * e
1937      if (i < line->numElements)      if (i < line->numElements)
1938          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1939      else {      else {
1940   notSuitablePrintf(entry, "no root= entry found\n");   notSuitablePrintf(entry, 0, "no root= entry found\n");
1941   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1942          return 0;          return 0;
1943              }              }
# Line 1625  int suitableImage(struct singleEntry * e Line 1946  int suitableImage(struct singleEntry * e
1946    
1947      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1948      if (!getpathbyspec(dev)) {      if (!getpathbyspec(dev)) {
1949          notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);          notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1950          return 0;          return 0;
1951      } else      } else
1952   dev = getpathbyspec(dev);   dev = getpathbyspec(dev);
1953    
1954      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1955      if (!rootdev) {      if (!rootdev) {
1956          notSuitablePrintf(entry, "can't find root device\n");          notSuitablePrintf(entry, 0, "can't find root device\n");
1957   return 0;   return 0;
1958      }      }
1959    
1960      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1961          notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1962   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1963          free(rootdev);          free(rootdev);
1964          return 0;          return 0;
1965      }      }
1966    
1967      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1968          notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n",
1969   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1970   free(rootdev);   free(rootdev);
1971          return 0;          return 0;
1972      }      }
1973    
1974      free(rootdev);      free(rootdev);
1975        notSuitablePrintf(entry, 1, "\n");
1976    
1977      return 1;      return 1;
1978  }  }
# Line 1684  struct singleEntry * findEntryByPath(str Line 2006  struct singleEntry * findEntryByPath(str
2006   }   }
2007    
2008   indexVars[i + 1] = -1;   indexVars[i + 1] = -1;
2009    
2010   i = 0;   i = 0;
2011   if (index) {   if (index) {
2012      while (i < *index) i++;      while (i < *index) {
2013      if (indexVars[i] == -1) return NULL;   i++;
2014     if (indexVars[i] == -1) return NULL;
2015        }
2016   }   }
2017    
2018   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
2019   if (!entry) return NULL;   if (!entry) return NULL;
2020    
2021   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
2022   if (!line) return NULL;   if (!line) return NULL;
2023    
2024   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1743  struct singleEntry * findEntryByPath(str Line 2067  struct singleEntry * findEntryByPath(str
2067    
2068      /* check all the lines matching checkType */      /* check all the lines matching checkType */
2069      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
2070   line = getLineByType(entry->multiboot && checkType == LT_KERNEL ?   enum lineType_e ct = checkType;
2071       LT_KERNEL|LT_MBMODULE|LT_HYPER :   if (entry->multiboot && checkType == LT_KERNEL)
2072       checkType, line);      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER|LT_KERNEL_16;
2073   if (!line) break;  /* not found in this entry */   else if (checkType & LT_KERNEL)
2074        ct = checkType | LT_KERNEL_EFI | LT_KERNEL_16;
2075     line = getLineByType(ct, line);
2076     if (!line)
2077        break;  /* not found in this entry */
2078    
2079   if (line && line->type != LT_MENUENTRY &&   if (line && line->type != LT_MENUENTRY &&
2080   line->numElements >= 2) {   line->numElements >= 2) {
# Line 1765  struct singleEntry * findEntryByPath(str Line 2093  struct singleEntry * findEntryByPath(str
2093       * non-Linux boot entries (could find netbsd etc, though, which is       * non-Linux boot entries (could find netbsd etc, though, which is
2094       * unfortunate)       * unfortunate)
2095       */       */
2096      if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines))      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines))
2097   break; /* found 'im! */   break; /* found 'im! */
2098   }   }
2099    
# Line 1775  struct singleEntry * findEntryByPath(str Line 2103  struct singleEntry * findEntryByPath(str
2103      return entry;      return entry;
2104  }  }
2105    
2106    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2107          int * index) {
2108        struct singleEntry * entry;
2109        struct singleLine * line;
2110        int i;
2111        char * newtitle;
2112    
2113        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2114     if (index && i < *index)
2115        continue;
2116     line = getLineByType(LT_TITLE, entry->lines);
2117     if (!line)
2118        line = getLineByType(LT_MENUENTRY, entry->lines);
2119     if (!line)
2120        continue;
2121     newtitle = grub2ExtractTitle(line);
2122     if (!newtitle)
2123        continue;
2124     if (!strcmp(title, newtitle))
2125        break;
2126        }
2127    
2128        if (!entry)
2129     return NULL;
2130    
2131        if (index)
2132     *index = i;
2133        return entry;
2134    }
2135    
2136  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
2137      struct singleEntry * entry;      struct singleEntry * entry;
2138    
# Line 1797  struct singleEntry * findTemplate(struct Line 2155  struct singleEntry * findTemplate(struct
2155      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2156      int index;      int index;
2157    
2158      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2159     if (cfg->cfi->getEnv) {
2160        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2161        if (defTitle) {
2162     int index = 0;
2163     if (isnumber(defTitle)) {
2164        index = atoi(defTitle);
2165        entry = findEntryByIndex(cfg, index);
2166     } else {
2167        entry = findEntryByTitle(cfg, defTitle, &index);
2168     }
2169     if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2170        cfg->defaultImage = index;
2171        if (indexPtr)
2172     *indexPtr = index;
2173        return entry;
2174     }
2175        }
2176     }
2177        } else if (cfg->defaultImage > -1) {
2178   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2179   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2180      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 1867  void markRemovedImage(struct grubConfig Line 2244  void markRemovedImage(struct grubConfig
2244    
2245  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
2246       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
2247       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
2248      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
2249      int i, j;      int i, j;
2250    
2251      if (newIsDefault) {      if (newIsDefault) {
2252   config->defaultImage = 0;   config->defaultImage = 0;
2253   return;   return;
2254        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
2255     if (findEntryByIndex(config, index))
2256        config->defaultImage = index;
2257     else
2258        config->defaultImage = -1;
2259     return;
2260      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
2261   i = 0;   i = 0;
2262   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1945  void displayEntry(struct singleEntry * e Line 2328  void displayEntry(struct singleEntry * e
2328      struct singleLine * line;      struct singleLine * line;
2329      char * root = NULL;      char * root = NULL;
2330      int i;      int i;
2331        int j;
2332    
2333      printf("index=%d\n", index);      printf("index=%d\n", index);
2334    
2335      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
2336      if (!line) {      if (!line) {
2337          printf("non linux entry\n");          printf("non linux entry\n");
2338          return;          return;
# Line 2013  void displayEntry(struct singleEntry * e Line 2397  void displayEntry(struct singleEntry * e
2397   printf("root=%s\n", s);   printf("root=%s\n", s);
2398      }      }
2399    
2400      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
2401    
2402      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2403   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
# Line 2032  void displayEntry(struct singleEntry * e Line 2416  void displayEntry(struct singleEntry * e
2416      } else {      } else {
2417   char * title;   char * title;
2418   line = getLineByType(LT_MENUENTRY, entry->lines);   line = getLineByType(LT_MENUENTRY, entry->lines);
2419   title = grub2ExtractTitle(line);   if (line) {
2420   if (title)      title = grub2ExtractTitle(line);
2421      printf("title=%s\n", title);      if (title)
2422     printf("title=%s\n", title);
2423     }
2424        }
2425    
2426        for (j = 0, line = entry->lines; line; line = line->next) {
2427     if ((line->type & LT_MBMODULE) && line->numElements >= 2) {
2428        if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2429     printf("mbmodule%d=", j);
2430        else
2431     printf("mbmodule%d=%s", j, prefix);
2432    
2433        for (i = 1; i < line->numElements; i++)
2434     printf("%s%s", line->elements[i].item, line->elements[i].indent);
2435        printf("\n");
2436        j++;
2437     }
2438      }      }
2439  }  }
2440    
# Line 2414  struct singleLine * addLineTmpl(struct s Line 2814  struct singleLine * addLineTmpl(struct s
2814  {  {
2815      struct singleLine * newLine = lineDup(tmplLine);      struct singleLine * newLine = lineDup(tmplLine);
2816    
2817        if (isEfi && cfi == &grub2ConfigType) {
2818     enum lineType_e old = newLine->type;
2819     newLine->type = preferredLineType(newLine->type, cfi);
2820     if (old != newLine->type)
2821        newLine->elements[0].item = getKeyByType(newLine->type, cfi);
2822        }
2823    
2824      if (val) {      if (val) {
2825   /* override the inherited value with our own.   /* override the inherited value with our own.
2826   * 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 2830  struct singleLine * addLineTmpl(struct s
2830   insertElement(newLine, val, 1, cfi);   insertElement(newLine, val, 1, cfi);
2831    
2832   /* but try to keep the rootspec from the template... sigh */   /* but try to keep the rootspec from the template... sigh */
2833   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|LT_KERNEL_16|LT_INITRD_16)) {
2834      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2835      if (rootspec != NULL) {      if (rootspec != NULL) {
2836   free(newLine->elements[1].item);   free(newLine->elements[1].item);
# Line 2460  struct singleLine *  addLine(struct sing Line 2867  struct singleLine *  addLine(struct sing
2867      /* 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
2868       * stack since it calls addLineTmpl which will make copies.       * stack since it calls addLineTmpl which will make copies.
2869       */       */
   
2870      if (type == LT_TITLE && cfi->titleBracketed) {      if (type == LT_TITLE && cfi->titleBracketed) {
2871   /* we're doing a bracketed title (zipl) */   /* we're doing a bracketed title (zipl) */
2872   tmpl.type = type;   tmpl.type = type;
# Line 2794  int updateActualImage(struct grubConfig Line 3200  int updateActualImage(struct grubConfig
3200      firstElement = 2;      firstElement = 2;
3201    
3202   } else {   } else {
3203      line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3204      if (!line) {      if (!line) {
3205   /* no LT_KERNEL or LT_MBMODULE in this entry? */   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3206   continue;   continue;
# Line 2950  int updateImage(struct grubConfig * cfg, Line 3356  int updateImage(struct grubConfig * cfg,
3356      return rc;      return rc;
3357  }  }
3358    
3359    int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
3360     const char * image, const char * prefix, const char * initrd,
3361     const char * title) {
3362        struct singleEntry * entry;
3363        struct singleLine * line, * kernelLine, *endLine = NULL;
3364        int index = 0;
3365    
3366        if (!image) return 0;
3367    
3368        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3369            kernelLine = getLineByType(LT_MBMODULE, entry->lines);
3370            if (!kernelLine) continue;
3371    
3372     /* if title is supplied, the entry's title must match it. */
3373     if (title) {
3374        char *linetitle;
3375    
3376        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3377        if (!line)
3378     continue;
3379    
3380        linetitle = extractTitle(cfg, line);
3381        if (!linetitle)
3382     continue;
3383        if (strcmp(title, linetitle)) {
3384     free(linetitle);
3385     continue;
3386        }
3387        free(linetitle);
3388     }
3389    
3390            if (prefix) {
3391                int prefixLen = strlen(prefix);
3392                if (!strncmp(initrd, prefix, prefixLen))
3393                    initrd += prefixLen;
3394            }
3395     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3396     if (endLine)
3397        removeLine(entry, endLine);
3398            line = addLine(entry, cfg->cfi, preferredLineType(LT_MBMODULE,cfg->cfi),
3399     kernelLine->indent, initrd);
3400            if (!line)
3401        return 1;
3402     if (endLine) {
3403        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3404                if (!line)
3405     return 1;
3406     }
3407    
3408            break;
3409        }
3410    
3411        return 0;
3412    }
3413    
3414  int updateInitrd(struct grubConfig * cfg, const char * image,  int updateInitrd(struct grubConfig * cfg, const char * image,
3415                   const char * prefix, const char * initrd) {                   const char * prefix, const char * initrd, const char * title) {
3416      struct singleEntry * entry;      struct singleEntry * entry;
3417      struct singleLine * line, * kernelLine, *endLine = NULL;      struct singleLine * line, * kernelLine, *endLine = NULL;
3418      int index = 0;      int index = 0;
# Line 2959  int updateInitrd(struct grubConfig * cfg Line 3420  int updateInitrd(struct grubConfig * cfg
3420      if (!image) return 0;      if (!image) return 0;
3421    
3422      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3423          kernelLine = getLineByType(LT_KERNEL, entry->lines);          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3424          if (!kernelLine) continue;          if (!kernelLine) continue;
3425    
3426          line = getLineByType(LT_INITRD, entry->lines);   /* if title is supplied, the entry's title must match it. */
3427     if (title) {
3428        char *linetitle;
3429    
3430        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3431        if (!line)
3432     continue;
3433    
3434        linetitle = extractTitle(cfg, line);
3435        if (!linetitle)
3436     continue;
3437        if (strcmp(title, linetitle)) {
3438     free(linetitle);
3439     continue;
3440        }
3441        free(linetitle);
3442     }
3443    
3444            line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
3445          if (line)          if (line)
3446              removeLine(entry, line);              removeLine(entry, line);
3447          if (prefix) {          if (prefix) {
# Line 2973  int updateInitrd(struct grubConfig * cfg Line 3452  int updateInitrd(struct grubConfig * cfg
3452   endLine = getLineByType(LT_ENTRY_END, entry->lines);   endLine = getLineByType(LT_ENTRY_END, entry->lines);
3453   if (endLine)   if (endLine)
3454      removeLine(entry, endLine);      removeLine(entry, endLine);
3455          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);   enum lineType_e lt;
3456     switch(kernelLine->type) {
3457        case LT_KERNEL:
3458            lt = LT_INITRD;
3459     break;
3460        case LT_KERNEL_EFI:
3461            lt = LT_INITRD_EFI;
3462     break;
3463        case LT_KERNEL_16:
3464            lt = LT_INITRD_16;
3465     break;
3466        default:
3467            lt = preferredLineType(LT_INITRD, cfg->cfi);
3468     }
3469            line = addLine(entry, cfg->cfi, lt, kernelLine->indent, initrd);
3470          if (!line)          if (!line)
3471      return 1;      return 1;
3472   if (endLine) {   if (endLine) {
# Line 3246  int checkForExtLinux(struct grubConfig * Line 3739  int checkForExtLinux(struct grubConfig *
3739      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3740  }  }
3741    
3742    int checkForYaboot(struct grubConfig * config) {
3743        /*
3744         * This is a simplistic check that we consider good enough for own puporses
3745         *
3746         * If we were to properly check if yaboot is *installed* we'd need to:
3747         * 1) get the system boot device (LT_BOOT)
3748         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3749         *    the content on the boot device
3750         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3751         * 4) check again if binary and boot device contents match
3752         */
3753        if (!access("/etc/yaboot.conf", R_OK))
3754     return 2;
3755    
3756        return 1;
3757    }
3758    
3759    int checkForElilo(struct grubConfig * config) {
3760        if (!access("/etc/elilo.conf", R_OK))
3761     return 2;
3762    
3763        return 1;
3764    }
3765    
3766  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3767      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3768    
# Line 3308  int addNewKernel(struct grubConfig * con Line 3825  int addNewKernel(struct grubConfig * con
3825   const char * newKernelPath, const char * newKernelTitle,   const char * newKernelPath, const char * newKernelTitle,
3826   const char * newKernelArgs, const char * newKernelInitrd,   const char * newKernelArgs, const char * newKernelInitrd,
3827   const char ** extraInitrds, int extraInitrdCount,   const char ** extraInitrds, int extraInitrdCount,
3828                   const char * newMBKernel, const char * newMBKernelArgs) {                   const char * newMBKernel, const char * newMBKernelArgs,
3829     const char * newDevTreePath) {
3830      struct singleEntry * new;      struct singleEntry * new;
3831      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;
3832      int needs;      int needs;
# Line 3349  int addNewKernel(struct grubConfig * con Line 3867  int addNewKernel(struct grubConfig * con
3867          needs |= NEED_MB;          needs |= NEED_MB;
3868          new->multiboot = 1;          new->multiboot = 1;
3869      }      }
3870        if (newDevTreePath && getKeywordByType(LT_DEVTREE, config->cfi))
3871     needs |= NEED_DEVTREE;
3872    
3873      if (template) {      if (template) {
3874   for (masterLine = template->lines;   for (masterLine = template->lines;
# Line 3362  int addNewKernel(struct grubConfig * con Line 3882  int addNewKernel(struct grubConfig * con
3882      while (*chptr && isspace(*chptr)) chptr++;      while (*chptr && isspace(*chptr)) chptr++;
3883      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3884    
3885      if (tmplLine->type == LT_KERNEL &&      if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) {
     tmplLine->numElements >= 2) {  
3886   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
3887      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
3888       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 3440  int addNewKernel(struct grubConfig * con Line 3959  int addNewKernel(struct grubConfig * con
3959      /* template is multi but new is not,      /* template is multi but new is not,
3960       * insert the kernel in the first module slot       * insert the kernel in the first module slot
3961       */       */
3962      tmplLine->type = LT_KERNEL;      tmplLine->type = preferredLineType(LT_KERNEL, config->cfi);
3963      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3964      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3965   strdup(getKeywordByType(LT_KERNEL, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3966     config->cfi)->key);
3967      newLine = addLineTmpl(new, tmplLine, newLine,      newLine = addLineTmpl(new, tmplLine, newLine,
3968    newKernelPath + strlen(prefix), config->cfi);    newKernelPath + strlen(prefix),
3969      config->cfi);
3970      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
3971   } else if (needs & NEED_INITRD) {   } else if (needs & NEED_INITRD) {
3972      char *initrdVal;      char *initrdVal;
3973      /* template is multi but new is not,      /* template is multi but new is not,
3974       * insert the initrd in the second module slot       * insert the initrd in the second module slot
3975       */       */
3976      tmplLine->type = LT_INITRD;      tmplLine->type = preferredLineType(LT_INITRD, config->cfi);
3977      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3978      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3979   strdup(getKeywordByType(LT_INITRD, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3980     config->cfi)->key);
3981      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3982      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3983      free(initrdVal);      free(initrdVal);
3984      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3985   }   }
3986    
3987      } else if (tmplLine->type == LT_INITRD &&      } else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) {
        tmplLine->numElements >= 2) {  
3988   if (needs & NEED_INITRD &&   if (needs & NEED_INITRD &&
3989      new->multiboot && !template->multiboot &&      new->multiboot && !template->multiboot &&
3990      config->cfi->mbInitRdIsModule) {      config->cfi->mbInitRdIsModule) {
# Line 3517  int addNewKernel(struct grubConfig * con Line 4038  int addNewKernel(struct grubConfig * con
4038      static const char *prefix = "'Loading ";      static const char *prefix = "'Loading ";
4039      if (tmplLine->numElements > 1 &&      if (tmplLine->numElements > 1 &&
4040      strstr(tmplLine->elements[1].item, prefix) &&      strstr(tmplLine->elements[1].item, prefix) &&
4041      masterLine->next && masterLine->next->type == LT_KERNEL) {      masterLine->next &&
4042        iskernel(masterLine->next->type)) {
4043   char *newTitle = malloc(strlen(prefix) +   char *newTitle = malloc(strlen(prefix) +
4044   strlen(newKernelTitle) + 2);   strlen(newKernelTitle) + 2);
4045    
# Line 3532  int addNewKernel(struct grubConfig * con Line 4054  int addNewKernel(struct grubConfig * con
4054   newLine = addLineTmpl(new, tmplLine, newLine, NULL,   newLine = addLineTmpl(new, tmplLine, newLine, NULL,
4055   config->cfi);   config->cfi);
4056      }      }
4057        } else if (tmplLine->type == LT_DEVTREE &&
4058           tmplLine->numElements == 2 && newDevTreePath) {
4059            newLine = addLineTmpl(new, tmplLine, newLine,
4060          newDevTreePath + strlen(prefix),
4061          config->cfi);
4062     needs &= ~NEED_DEVTREE;
4063        } else if (tmplLine->type == LT_ENTRY_END && needs & NEED_DEVTREE) {
4064     const char *ndtp = newDevTreePath;
4065     if (!strncmp(newDevTreePath, prefix, strlen(prefix)))
4066        ndtp += strlen(prefix);
4067     newLine = addLine(new, config->cfi, LT_DEVTREE,
4068      config->secondaryIndent,
4069      ndtp);
4070     needs &= ~NEED_DEVTREE;
4071     newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
4072      } else {      } else {
4073   /* pass through other lines from the template */   /* pass through other lines from the template */
4074   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
# Line 3544  int addNewKernel(struct grubConfig * con Line 4081  int addNewKernel(struct grubConfig * con
4081   */   */
4082   switch (config->cfi->entryStart) {   switch (config->cfi->entryStart) {
4083      case LT_KERNEL:      case LT_KERNEL:
4084        case LT_KERNEL_EFI:
4085        case LT_KERNEL_16:
4086   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
4087      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
4088   } else {   } else {
4089      newLine = addLine(new, config->cfi, LT_KERNEL,      newLine = addLine(new, config->cfi,
4090              preferredLineType(LT_KERNEL, config->cfi),
4091        config->primaryIndent,        config->primaryIndent,
4092        newKernelPath + strlen(prefix));        newKernelPath + strlen(prefix));
4093      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
# Line 3604  int addNewKernel(struct grubConfig * con Line 4144  int addNewKernel(struct grubConfig * con
4144   }   }
4145      }      }
4146    
4147        struct singleLine *endLine = NULL;
4148        endLine = getLineByType(LT_ENTRY_END, new->lines);
4149        if (endLine) {
4150        removeLine(new, endLine);
4151        needs |= NEED_END;
4152        }
4153    
4154      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
4155       * weren't present in the template, or in the case of no template,       * weren't present in the template, or in the case of no template,
4156       * all the lines following the entryStart.       * all the lines following the entryStart.
# Line 3623  int addNewKernel(struct grubConfig * con Line 4170  int addNewKernel(struct grubConfig * con
4170      if (needs & NEED_KERNEL) {      if (needs & NEED_KERNEL) {
4171   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
4172    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
4173        config->cfi)) ?        config->cfi))
4174    LT_MBMODULE : LT_KERNEL,     ? LT_MBMODULE
4175     : preferredLineType(LT_KERNEL, config->cfi),
4176    config->secondaryIndent,    config->secondaryIndent,
4177    newKernelPath + strlen(prefix));    newKernelPath + strlen(prefix));
4178   needs &= ~NEED_KERNEL;   needs &= ~NEED_KERNEL;
# Line 3640  int addNewKernel(struct grubConfig * con Line 4188  int addNewKernel(struct grubConfig * con
4188   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);
4189   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
4190    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
4191        config->cfi)) ?        config->cfi))
4192    LT_MBMODULE : LT_INITRD,     ? LT_MBMODULE
4193       : preferredLineType(LT_INITRD, config->cfi),
4194    config->secondaryIndent,    config->secondaryIndent,
4195    initrdVal);    initrdVal);
4196   free(initrdVal);   free(initrdVal);
4197   needs &= ~NEED_INITRD;   needs &= ~NEED_INITRD;
4198      }      }
4199        if (needs & NEED_DEVTREE) {
4200     newLine = addLine(new, config->cfi, LT_DEVTREE,
4201      config->secondaryIndent,
4202      newDevTreePath);
4203     needs &= ~NEED_DEVTREE;
4204        }
4205    
4206        /* NEEDS_END must be last on bootloaders that need it... */
4207      if (needs & NEED_END) {      if (needs & NEED_END) {
4208   newLine = addLine(new, config->cfi, LT_ENTRY_END,   newLine = addLine(new, config->cfi, LT_ENTRY_END,
4209   config->secondaryIndent, NULL);   config->secondaryIndent, NULL);
# Line 3664  int addNewKernel(struct grubConfig * con Line 4221  int addNewKernel(struct grubConfig * con
4221      return 0;      return 0;
4222  }  }
4223    
 static void traceback(int signum)  
 {  
     void *array[40];  
     size_t size;  
   
     signal(SIGSEGV, SIG_DFL);  
     memset(array, '\0', sizeof (array));  
     size = backtrace(array, 40);  
   
     fprintf(stderr, "grubby recieved SIGSEGV!  Backtrace (%ld):\n",  
             (unsigned long)size);  
     backtrace_symbols_fd(array, size, STDERR_FILENO);  
     exit(1);  
 }  
   
4224  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
4225      poptContext optCon;      poptContext optCon;
4226      const char * grubConfig = NULL;      const char * grubConfig = NULL;
# Line 3698  int main(int argc, const char ** argv) { Line 4240  int main(int argc, const char ** argv) {
4240      char * newKernelArgs = NULL;      char * newKernelArgs = NULL;
4241      char * newKernelInitrd = NULL;      char * newKernelInitrd = NULL;
4242      char * newKernelTitle = NULL;      char * newKernelTitle = NULL;
4243      char * newKernelVersion = NULL;      char * newDevTreePath = NULL;
4244      char * newMBKernel = NULL;      char * newMBKernel = NULL;
4245      char * newMBKernelArgs = NULL;      char * newMBKernelArgs = NULL;
4246      char * removeMBKernelArgs = NULL;      char * removeMBKernelArgs = NULL;
# Line 3708  int main(int argc, const char ** argv) { Line 4250  int main(int argc, const char ** argv) {
4250      char * removeArgs = NULL;      char * removeArgs = NULL;
4251      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4252      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4253        char * envPath = NULL;
4254      const char * chptr = NULL;      const char * chptr = NULL;
4255      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4256      struct grubConfig * config;      struct grubConfig * config;
# Line 3716  int main(int argc, const char ** argv) { Line 4259  int main(int argc, const char ** argv) {
4259      int displayDefault = 0;      int displayDefault = 0;
4260      int displayDefaultIndex = 0;      int displayDefaultIndex = 0;
4261      int displayDefaultTitle = 0;      int displayDefaultTitle = 0;
4262        int defaultIndex = -1;
4263      struct poptOption options[] = {      struct poptOption options[] = {
4264   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
4265      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 3733  int main(int argc, const char ** argv) { Line 4277  int main(int argc, const char ** argv) {
4277   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4278      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
4279      _("bootfs") },      _("bootfs") },
4280  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4281   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
4282      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
4283  #endif  #endif
4284   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
4285      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 3754  int main(int argc, const char ** argv) { Line 4298  int main(int argc, const char ** argv) {
4298      _("display the index of the default kernel") },      _("display the index of the default kernel") },
4299   { "default-title", 0, 0, &displayDefaultTitle, 0,   { "default-title", 0, 0, &displayDefaultTitle, 0,
4300      _("display the title of the default kernel") },      _("display the title of the default kernel") },
4301     { "devtree", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4302        _("device tree file for new stanza"), _("dtb-path") },
4303     { "devtreedir", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4304        _("device tree directory for new stanza"), _("dtb-path") },
4305   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4306      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4307     { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4308        _("force grub2 stanzas to use efi") },
4309     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4310        _("path for environment data"),
4311        _("path") },
4312   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4313      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4314   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3768  int main(int argc, const char ** argv) { Line 4321  int main(int argc, const char ** argv) {
4321   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4322      _("initrd image for the new kernel"), _("initrd-path") },      _("initrd image for the new kernel"), _("initrd-path") },
4323   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4324      _("auxilliary initrd image for things other than the new kernel"), _("initrd-path") },      _("auxiliary initrd image for things other than the new kernel"), _("initrd-path") },
4325   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4326      _("configure lilo bootloader") },      _("configure lilo bootloader") },
4327   { "make-default", 0, 0, &makeDefault, 0,   { "make-default", 0, 0, &makeDefault, 0,
# Line 3788  int main(int argc, const char ** argv) { Line 4341  int main(int argc, const char ** argv) {
4341   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
4342      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
4343        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
4344     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4345        _("make the given entry index the default entry"),
4346        _("entry-index") },
4347   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4348      _("configure silo bootloader") },      _("configure silo bootloader") },
4349   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 3807  int main(int argc, const char ** argv) { Line 4363  int main(int argc, const char ** argv) {
4363    
4364      useextlinuxmenu=0;      useextlinuxmenu=0;
4365    
4366      signal(SIGSEGV, traceback);      int i = 0;
4367        for (int j = 1; j < argc; j++)
4368     i += strlen(argv[j]) + 1;
4369        saved_command_line = malloc(i);
4370        if (!saved_command_line) {
4371     fprintf(stderr, "grubby: %m\n");
4372     exit(1);
4373        }
4374        saved_command_line[0] = '\0';
4375        for (int j = 1; j < argc; j++) {
4376     strcat(saved_command_line, argv[j]);
4377     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4378        }
4379    
4380      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4381      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
# Line 3852  int main(int argc, const char ** argv) { Line 4420  int main(int argc, const char ** argv) {
4420   return 1;   return 1;
4421      } else if (configureGrub2) {      } else if (configureGrub2) {
4422   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4423     if (envPath)
4424        cfi->envFile = envPath;
4425      } else if (configureLilo) {      } else if (configureLilo) {
4426   cfi = &liloConfigType;   cfi = &liloConfigType;
4427      } else if (configureGrub) {      } else if (configureGrub) {
# Line 3870  int main(int argc, const char ** argv) { Line 4440  int main(int argc, const char ** argv) {
4440      }      }
4441    
4442      if (!cfi) {      if (!cfi) {
4443          if (grub2FindConfig(&grub2ConfigType))          if (grub2FindConfig(&grub2ConfigType)) {
4444      cfi = &grub2ConfigType;      cfi = &grub2ConfigType;
4445   else      if (envPath)
4446     cfi->envFile = envPath;
4447            } else
4448        #ifdef __ia64__        #ifdef __ia64__
4449      cfi = &eliloConfigType;      cfi = &eliloConfigType;
4450        #elif __powerpc__        #elif __powerpc__
# Line 3895  int main(int argc, const char ** argv) { Line 4467  int main(int argc, const char ** argv) {
4467      grubConfig = cfi->defaultConfig;      grubConfig = cfi->defaultConfig;
4468      }      }
4469    
4470      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo ||
4471    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
4472    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
4473        (defaultIndex >= 0))) {
4474   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
4475    "specified option"));    "specified option"));
4476   return 1;   return 1;
4477      }      }
4478    
4479      if ((displayDefault || kernelInfo) && (newKernelVersion || newKernelPath ||      if ((displayDefault || kernelInfo) && (newKernelPath ||
4480     removeKernelPath)) {     removeKernelPath)) {
4481   fprintf(stderr, _("grubby: --default-kernel and --info may not "   fprintf(stderr, _("grubby: --default-kernel and --info may not "
4482    "be used when adding or removing kernels\n"));    "be used when adding or removing kernels\n"));
# Line 3913  int main(int argc, const char ** argv) { Line 4486  int main(int argc, const char ** argv) {
4486      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
4487   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
4488   return 1;   return 1;
4489      } else if (!newKernelPath && (newKernelTitle  || copyDefault ||      } else if (!newKernelPath && (copyDefault ||
4490    (newKernelInitrd && !updateKernelPath)||    (newKernelInitrd && !updateKernelPath)||
4491    makeDefault || extraInitrdCount > 0)) {    makeDefault || extraInitrdCount > 0)) {
4492   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
# Line 3939  int main(int argc, const char ** argv) { Line 4512  int main(int argc, const char ** argv) {
4512   makeDefault = 1;   makeDefault = 1;
4513   defaultKernel = NULL;   defaultKernel = NULL;
4514      }      }
4515        else if (defaultKernel && (defaultIndex >= 0)) {
4516     fprintf(stderr, _("grubby: --set-default and --set-default-index "
4517      "may not be used together\n"));
4518     return 1;
4519        }
4520    
4521      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
4522   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
# Line 3947  int main(int argc, const char ** argv) { Line 4525  int main(int argc, const char ** argv) {
4525      }      }
4526    
4527      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
4528   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
4529          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
4530     && (defaultIndex == -1)) {
4531   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
4532   return 1;   return 1;
4533      }      }
# Line 3975  int main(int argc, const char ** argv) { Line 4554  int main(int argc, const char ** argv) {
4554      }      }
4555    
4556      if (bootloaderProbe) {      if (bootloaderProbe) {
4557   int lrc = 0, grc = 0, gr2c = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4558   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4559    
4560   const char *grub2config = grub2FindConfig(&grub2ConfigType);   const char *grub2config = grub2FindConfig(&grub2ConfigType);
4561   if (grub2config) {   if (grub2config) {
# Line 4004  int main(int argc, const char ** argv) { Line 4583  int main(int argc, const char ** argv) {
4583   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4584   }   }
4585    
4586     if (!access(eliloConfigType.defaultConfig, F_OK)) {
4587        econfig = readConfig(eliloConfigType.defaultConfig,
4588     &eliloConfigType);
4589        if (!econfig)
4590     erc = 1;
4591        else
4592     erc = checkForElilo(econfig);
4593     }
4594    
4595   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4596      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4597      if (!lconfig)      if (!lconfig)
4598   erc = 1;   extrc = 1;
4599      else      else
4600   erc = checkForExtLinux(lconfig);   extrc = checkForExtLinux(lconfig);
4601   }   }
4602    
4603   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;  
4604     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4605        yconfig = readConfig(yabootConfigType.defaultConfig,
4606     &yabootConfigType);
4607        if (!yconfig)
4608     yrc = 1;
4609        else
4610     yrc = checkForYaboot(yconfig);
4611     }
4612    
4613     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4614     erc == 1)
4615        return 1;
4616    
4617   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4618   if (gr2c == 2) printf("grub2\n");   if (gr2c == 2) printf("grub2\n");
4619   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4620   if (erc == 2) printf("extlinux\n");   if (extrc == 2) printf("extlinux\n");
4621     if (yrc == 2) printf("yaboot\n");
4622     if (erc == 2) printf("elilo\n");
4623    
4624   return 0;   return 0;
4625      }      }
4626    
4627        if (grubConfig == NULL) {
4628     printf("Could not find bootloader configuration file.\n");
4629     exit(1);
4630        }
4631    
4632      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4633      if (!config) return 1;      if (!config) return 1;
4634    
# Line 4031  int main(int argc, const char ** argv) { Line 4638  int main(int argc, const char ** argv) {
4638          char * rootspec;          char * rootspec;
4639    
4640   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4641     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4642     cfi->defaultIsSaved)
4643        config->defaultImage = 0;
4644   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4645   if (!entry) return 0;   if (!entry) return 0;
4646   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4647    
4648   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
4649   if (!line) return 0;   if (!line) return 0;
4650    
4651          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 4048  int main(int argc, const char ** argv) { Line 4658  int main(int argc, const char ** argv) {
4658   struct singleLine * line;   struct singleLine * line;
4659   struct singleEntry * entry;   struct singleEntry * entry;
4660    
4661   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1)
4662        return 0;
4663     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4664     cfi->defaultIsSaved)
4665        config->defaultImage = 0;
4666   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4667   if (!entry) return 0;   if (!entry)
4668     return 0;
4669    
4670   if (!configureGrub2) {   if (!configureGrub2) {
4671    line = getLineByType(LT_TITLE, entry->lines);      char *title;
4672    if (!line) return 0;      line = getLineByType(LT_TITLE, entry->lines);
4673    printf("%s\n", line->elements[1].item);      if (!line)
4674     return 0;
4675        title = extractTitle(config, line);
4676        if (!title)
4677     return 0;
4678        printf("%s\n", title);
4679        free(title);
4680   } else {   } else {
4681    char * title;      char * title;
4682    
4683    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
4684    line = getLineByType(LT_MENUENTRY, entry->lines);      line = getLineByType(LT_MENUENTRY, entry->lines);
4685    if (!line) return 0;      if (!line)
4686    title = grub2ExtractTitle(line);   return 0;
4687    if (title)      title = grub2ExtractTitle(line);
4688      printf("%s\n", title);      if (title)
4689     printf("%s\n", title);
4690   }   }
4691   return 0;   return 0;
4692    
4693      } else if (displayDefaultIndex) {      } else if (displayDefaultIndex) {
4694          if (config->defaultImage == -1) return 0;          if (config->defaultImage == -1) return 0;
4695     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4696     cfi->defaultIsSaved)
4697        config->defaultImage = 0;
4698          printf("%i\n", config->defaultImage);          printf("%i\n", config->defaultImage);
4699            return 0;
4700    
4701      } else if (kernelInfo)      } else if (kernelInfo)
4702   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
# Line 4084  int main(int argc, const char ** argv) { Line 4709  int main(int argc, const char ** argv) {
4709      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4710      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4711      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4712      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4713      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4714      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4715                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
4716      if (updateKernelPath && newKernelInitrd) {      if (updateKernelPath && newKernelInitrd) {
4717              if (updateInitrd(config, updateKernelPath, bootPrefix,      if (newMBKernel) {
4718                               newKernelInitrd)) return 1;      if (addMBInitrd(config, newMBKernel, updateKernelPath,
4719     bootPrefix, newKernelInitrd,
4720     newKernelTitle))
4721        return 1;
4722        } else {
4723        if (updateInitrd(config, updateKernelPath, bootPrefix,
4724     newKernelInitrd, newKernelTitle))
4725     return 1;
4726        }
4727      }      }
4728      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
4729                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
4730                       (const char **)extraInitrds, extraInitrdCount,                       (const char **)extraInitrds, extraInitrdCount,
4731                       newMBKernel, newMBKernelArgs)) return 1;                       newMBKernel, newMBKernelArgs, newDevTreePath)) return 1;
4732            
4733    
4734      if (numEntries(config) == 0) {      if (numEntries(config) == 0) {

Legend:
Removed from v.1851  
changed lines
  Added in v.2995