Magellan Linux

Diff of /trunk/grubby/grubby.c

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

revision 2058 by niro, Wed Feb 20 14:06:30 2013 UTC revision 2992 by niro, Thu Jun 30 10:35:14 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 58  int debug = 0; /* Currently just for tem Line 60  int debug = 0; /* Currently just for tem
60    
61  int isEfi = 0;  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 86  enum lineType_e { Line 96  enum lineType_e {
96      LT_SET_VARIABLE = 1 << 19,      LT_SET_VARIABLE = 1 << 19,
97      LT_KERNEL_EFI   = 1 << 20,      LT_KERNEL_EFI   = 1 << 20,
98      LT_INITRD_EFI   = 1 << 21,      LT_INITRD_EFI   = 1 << 21,
99      LT_UNKNOWN      = 1 << 22,      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 115  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 132  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;      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 153  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 172  const char *grubFindConfig(struct config Line 193  const char *grubFindConfig(struct config
193   "/boot/grub/grub.conf",   "/boot/grub/grub.conf",
194   "/boot/grub/menu.lst",   "/boot/grub/menu.lst",
195   "/etc/grub.conf",   "/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 199  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 211  struct keywordTypes grub2Keywords[] = { Line 235  struct keywordTypes grub2Keywords[] = {
235      { "fallback",   LT_FALLBACK,    ' ' },      { "fallback",   LT_FALLBACK,    ' ' },
236      { "linux",      LT_KERNEL,      ' ' },      { "linux",      LT_KERNEL,      ' ' },
237      { "linuxefi",   LT_KERNEL_EFI,  ' ' },      { "linuxefi",   LT_KERNEL_EFI,  ' ' },
238        { "linux16",    LT_KERNEL_16,   ' ' },
239      { "initrd",     LT_INITRD,      ' ', ' ' },      { "initrd",     LT_INITRD,      ' ', ' ' },
240      { "initrdefi",  LT_INITRD_EFI,  ' ', ' ' },      { "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 226  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 249  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 279  static int isquote(char q) Line 413  static int isquote(char q)
413  }  }
414    
415  static int iskernel(enum lineType_e type) {  static int iskernel(enum lineType_e type) {
416      return (type == LT_KERNEL || type == LT_KERNEL_EFI);      return (type == LT_KERNEL || type == LT_KERNEL_EFI || type == LT_KERNEL_16);
417  }  }
418    
419  static int isinitrd(enum lineType_e type) {  static int isinitrd(enum lineType_e type) {
420      return (type == LT_INITRD || type == LT_INITRD_EFI);      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) {
# Line 295  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 304  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 318  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 331  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 343  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 447  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 457  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 465  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 475  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 484  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 502  struct configFileInfo extlinuxConfigType Line 648  struct configFileInfo extlinuxConfigType
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 522  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 591  static enum lineType_e preferredLineType Line 741  static enum lineType_e preferredLineType
741      if (isEfi && cfi == &grub2ConfigType) {      if (isEfi && cfi == &grub2ConfigType) {
742   switch (type) {   switch (type) {
743   case LT_KERNEL:   case LT_KERNEL:
744      return LT_KERNEL_EFI;      return isEfiOnly ? LT_KERNEL : LT_KERNEL_EFI;
745   case LT_INITRD:   case LT_INITRD:
746      return LT_INITRD_EFI;      return isEfiOnly ? LT_INITRD : LT_INITRD_EFI;
747   default:   default:
748      return type;      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;      return type;
763  }  }
# Line 677  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 762  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 940  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 954  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 1021  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 (iskernel(line->type)) {          } 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 1061  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 1090  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 1106  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 1123  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 1133  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 1169  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 1226  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 1255  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 1267  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 1284  static void writeDefault(FILE * out, cha Line 1521  static void writeDefault(FILE * out, cha
1521    
1522      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1523   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1524      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1525   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1526      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1527        char *title;
1528        entry = findEntryByIndex(cfg, cfg->defaultImage);
1529        line = getLineByType(LT_MENUENTRY, entry->lines);
1530        if (!line)
1531     line = getLineByType(LT_TITLE, entry->lines);
1532        if (line) {
1533     title = extractTitle(cfg, line);
1534     if (title)
1535        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1536        }
1537     }
1538        } else if (cfg->defaultImage > -1) {
1539   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1540      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1541          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 1319  static void writeDefault(FILE * out, cha Line 1568  static void writeDefault(FILE * out, cha
1568              else if (line && (line->numElements == 1) &&              else if (line && (line->numElements == 1) &&
1569                       cfg->cfi->titleBracketed) {                       cfg->cfi->titleBracketed) {
1570   fprintf(out, "%sdefault%s%s\n", indent, separator,   fprintf(out, "%sdefault%s%s\n", indent, separator,
1571                          extractTitle(line));                          extractTitle(cfg, line));
1572              }              }
1573   }   }
1574      }      }
# Line 1389  static int writeConfig(struct grubConfig Line 1638  static int writeConfig(struct grubConfig
1638      while (line) {      while (line) {
1639          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1640   line->numElements == 3 &&   line->numElements == 3 &&
1641   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1642     !is_special_grub2_variable(line->elements[2].item)) {
1643      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1644      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1645   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1534  static char *findDiskForRoot() Line 1784  static char *findDiskForRoot()
1784      return NULL;      return NULL;
1785  }  }
1786    
1787  void printEntry(struct singleEntry * entry) {  void printEntry(struct singleEntry * entry, FILE *f) {
1788      int i;      int i;
1789      struct singleLine * line;      struct singleLine * line;
1790    
1791      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1792   fprintf(stderr, "DBG: %s", line->indent);   log_message(f, "DBG: %s", line->indent);
1793   for (i = 0; i < line->numElements; i++) {   for (i = 0; i < line->numElements; i++) {
1794      /* Need to handle this, because we strip the quotes from      /* Need to handle this, because we strip the quotes from
1795       * menuentry when read it. */       * menuentry when read it. */
1796      if (line->type == LT_MENUENTRY && i == 1) {      if (line->type == LT_MENUENTRY && i == 1) {
1797   if(!isquote(*line->elements[i].item))   if(!isquote(*line->elements[i].item))
1798      fprintf(stderr, "\'%s\'", line->elements[i].item);      log_message(f, "\'%s\'", line->elements[i].item);
1799   else   else
1800      fprintf(stderr, "%s", line->elements[i].item);      log_message(f, "%s", line->elements[i].item);
1801   fprintf(stderr, "%s", line->elements[i].indent);   log_message(f, "%s", line->elements[i].indent);
1802    
1803   continue;   continue;
1804      }      }
1805            
1806      fprintf(stderr, "%s%s",      log_message(f, "%s%s",
1807      line->elements[i].item, line->elements[i].indent);      line->elements[i].item, line->elements[i].indent);
1808   }   }
1809   fprintf(stderr, "\n");   log_message(f, "\n");
1810      }      }
1811  }  }
1812    
1813  void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)  void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1814  {  {
1815      va_list argp;      static int once;
1816        va_list argp, argq;
1817    
1818        va_start(argp, fmt);
1819    
1820      if (!debug)      va_copy(argq, argp);
1821        if (!once) {
1822     log_time(NULL);
1823     log_message(NULL, "command line: %s\n", saved_command_line);
1824        }
1825        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1826        log_vmessage(NULL, fmt, argq);
1827    
1828        printEntry(entry, NULL);
1829        va_end(argq);
1830    
1831        if (!debug) {
1832     once = 1;
1833         va_end(argp);
1834   return;   return;
1835        }
1836    
1837      va_start(argp, fmt);      if (okay) {
1838     va_end(argp);
1839     return;
1840        }
1841    
1842        if (!once)
1843     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1844        once = 1;
1845      fprintf(stderr, "DBG: Image entry failed: ");      fprintf(stderr, "DBG: Image entry failed: ");
1846      vfprintf(stderr, fmt, argp);      vfprintf(stderr, fmt, argp);
1847      printEntry(entry);      printEntry(entry, stderr);
1848      va_end(argp);      va_end(argp);
1849  }  }
1850    
# Line 1597  int suitableImage(struct singleEntry * e Line 1871  int suitableImage(struct singleEntry * e
1871      char * rootdev;      char * rootdev;
1872    
1873      if (skipRemoved && entry->skip) {      if (skipRemoved && entry->skip) {
1874   notSuitablePrintf(entry, "marked to skip\n");   notSuitablePrintf(entry, 0, "marked to skip\n");
1875   return 0;   return 0;
1876      }      }
1877    
1878      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
1879      if (!line) {      if (!line) {
1880   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1881   return 0;   return 0;
1882      }      }
1883      if (line->numElements < 2) {      if (line->numElements < 2) {
1884   notSuitablePrintf(entry, "line has only %d elements\n",   notSuitablePrintf(entry, 0, "line has only %d elements\n",
1885      line->numElements);      line->numElements);
1886   return 0;   return 0;
1887      }      }
1888    
1889      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) {
1890        notSuitablePrintf(entry, 1, "\n");
1891        return 1;
1892        }
1893    
1894      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1895        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
# Line 1623  int suitableImage(struct singleEntry * e Line 1900  int suitableImage(struct singleEntry * e
1900      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1901              line->elements[1].item + rootspec_offset);              line->elements[1].item + rootspec_offset);
1902      if (access(fullName, R_OK)) {      if (access(fullName, R_OK)) {
1903   notSuitablePrintf(entry, "access to %s failed\n", fullName);   notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1904   return 0;   return 0;
1905      }      }
1906      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
# Line 1644  int suitableImage(struct singleEntry * e Line 1921  int suitableImage(struct singleEntry * e
1921    
1922              /* failed to find one */              /* failed to find one */
1923              if (!line) {              if (!line) {
1924   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1925   return 0;   return 0;
1926              }              }
1927    
# Line 1653  int suitableImage(struct singleEntry * e Line 1930  int suitableImage(struct singleEntry * e
1930      if (i < line->numElements)      if (i < line->numElements)
1931          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1932      else {      else {
1933   notSuitablePrintf(entry, "no root= entry found\n");   notSuitablePrintf(entry, 0, "no root= entry found\n");
1934   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1935          return 0;          return 0;
1936              }              }
# Line 1662  int suitableImage(struct singleEntry * e Line 1939  int suitableImage(struct singleEntry * e
1939    
1940      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1941      if (!getpathbyspec(dev)) {      if (!getpathbyspec(dev)) {
1942          notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);          notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1943          return 0;          return 0;
1944      } else      } else
1945   dev = getpathbyspec(dev);   dev = getpathbyspec(dev);
1946    
1947      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1948      if (!rootdev) {      if (!rootdev) {
1949          notSuitablePrintf(entry, "can't find root device\n");          notSuitablePrintf(entry, 0, "can't find root device\n");
1950   return 0;   return 0;
1951      }      }
1952    
1953      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1954          notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1955   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1956          free(rootdev);          free(rootdev);
1957          return 0;          return 0;
1958      }      }
1959    
1960      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1961          notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid mismatch: 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      free(rootdev);      free(rootdev);
1968        notSuitablePrintf(entry, 1, "\n");
1969    
1970      return 1;      return 1;
1971  }  }
# Line 1721  struct singleEntry * findEntryByPath(str Line 1999  struct singleEntry * findEntryByPath(str
1999   }   }
2000    
2001   indexVars[i + 1] = -1;   indexVars[i + 1] = -1;
2002    
2003   i = 0;   i = 0;
2004   if (index) {   if (index) {
2005      while (i < *index) i++;      while (i < *index) {
2006      if (indexVars[i] == -1) return NULL;   i++;
2007     if (indexVars[i] == -1) return NULL;
2008        }
2009   }   }
2010    
2011   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
2012   if (!entry) return NULL;   if (!entry) return NULL;
2013    
2014   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
2015   if (!line) return NULL;   if (!line) return NULL;
2016    
2017   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1782  struct singleEntry * findEntryByPath(str Line 2062  struct singleEntry * findEntryByPath(str
2062      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
2063   enum lineType_e ct = checkType;   enum lineType_e ct = checkType;
2064   if (entry->multiboot && checkType == LT_KERNEL)   if (entry->multiboot && checkType == LT_KERNEL)
2065      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER;      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER|LT_KERNEL_16;
2066   else if (checkType & LT_KERNEL)   else if (checkType & LT_KERNEL)
2067      ct = checkType | LT_KERNEL_EFI;      ct = checkType | LT_KERNEL_EFI | LT_KERNEL_16;
2068   line = getLineByType(ct, line);   line = getLineByType(ct, line);
2069   if (!line)   if (!line)
2070      break;  /* not found in this entry */      break;  /* not found in this entry */
# Line 1806  struct singleEntry * findEntryByPath(str Line 2086  struct singleEntry * findEntryByPath(str
2086       * non-Linux boot entries (could find netbsd etc, though, which is       * non-Linux boot entries (could find netbsd etc, though, which is
2087       * unfortunate)       * unfortunate)
2088       */       */
2089      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines))      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines))
2090   break; /* found 'im! */   break; /* found 'im! */
2091   }   }
2092    
# Line 1816  struct singleEntry * findEntryByPath(str Line 2096  struct singleEntry * findEntryByPath(str
2096      return entry;      return entry;
2097  }  }
2098    
2099    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2100          int * index) {
2101        struct singleEntry * entry;
2102        struct singleLine * line;
2103        int i;
2104        char * newtitle;
2105    
2106        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2107     if (index && i < *index)
2108        continue;
2109     line = getLineByType(LT_TITLE, entry->lines);
2110     if (!line)
2111        line = getLineByType(LT_MENUENTRY, entry->lines);
2112     if (!line)
2113        continue;
2114     newtitle = grub2ExtractTitle(line);
2115     if (!newtitle)
2116        continue;
2117     if (!strcmp(title, newtitle))
2118        break;
2119        }
2120    
2121        if (!entry)
2122     return NULL;
2123    
2124        if (index)
2125     *index = i;
2126        return entry;
2127    }
2128    
2129  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
2130      struct singleEntry * entry;      struct singleEntry * entry;
2131    
# Line 1838  struct singleEntry * findTemplate(struct Line 2148  struct singleEntry * findTemplate(struct
2148      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2149      int index;      int index;
2150    
2151      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2152     if (cfg->cfi->getEnv) {
2153        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2154        if (defTitle) {
2155     int index = 0;
2156     if (isnumber(defTitle)) {
2157        index = atoi(defTitle);
2158        entry = findEntryByIndex(cfg, index);
2159     } else {
2160        entry = findEntryByTitle(cfg, defTitle, &index);
2161     }
2162     if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2163        cfg->defaultImage = index;
2164        if (indexPtr)
2165     *indexPtr = index;
2166        return entry;
2167     }
2168        }
2169     }
2170        } else if (cfg->defaultImage > -1) {
2171   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2172   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2173      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 1992  void displayEntry(struct singleEntry * e Line 2321  void displayEntry(struct singleEntry * e
2321      struct singleLine * line;      struct singleLine * line;
2322      char * root = NULL;      char * root = NULL;
2323      int i;      int i;
2324        int j;
2325    
2326      printf("index=%d\n", index);      printf("index=%d\n", index);
2327    
2328      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
2329      if (!line) {      if (!line) {
2330          printf("non linux entry\n");          printf("non linux entry\n");
2331          return;          return;
# Line 2060  void displayEntry(struct singleEntry * e Line 2390  void displayEntry(struct singleEntry * e
2390   printf("root=%s\n", s);   printf("root=%s\n", s);
2391      }      }
2392    
2393      line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);      line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
2394    
2395      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2396   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
# Line 2079  void displayEntry(struct singleEntry * e Line 2409  void displayEntry(struct singleEntry * e
2409      } else {      } else {
2410   char * title;   char * title;
2411   line = getLineByType(LT_MENUENTRY, entry->lines);   line = getLineByType(LT_MENUENTRY, entry->lines);
2412   title = grub2ExtractTitle(line);   if (line) {
2413   if (title)      title = grub2ExtractTitle(line);
2414      printf("title=%s\n", title);      if (title)
2415     printf("title=%s\n", title);
2416     }
2417        }
2418    
2419        for (j = 0, line = entry->lines; line; line = line->next) {
2420     if ((line->type & LT_MBMODULE) && line->numElements >= 2) {
2421        if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2422     printf("mbmodule%d=", j);
2423        else
2424     printf("mbmodule%d=%s", j, prefix);
2425    
2426        for (i = 1; i < line->numElements; i++)
2427     printf("%s%s", line->elements[i].item, line->elements[i].indent);
2428        printf("\n");
2429        j++;
2430     }
2431      }      }
2432  }  }
2433    
# Line 2477  struct singleLine * addLineTmpl(struct s Line 2823  struct singleLine * addLineTmpl(struct s
2823   insertElement(newLine, val, 1, cfi);   insertElement(newLine, val, 1, cfi);
2824    
2825   /* but try to keep the rootspec from the template... sigh */   /* but try to keep the rootspec from the template... sigh */
2826   if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI)) {   if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI|LT_KERNEL_16|LT_INITRD_16)) {
2827      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2828      if (rootspec != NULL) {      if (rootspec != NULL) {
2829   free(newLine->elements[1].item);   free(newLine->elements[1].item);
# Line 2847  int updateActualImage(struct grubConfig Line 3193  int updateActualImage(struct grubConfig
3193      firstElement = 2;      firstElement = 2;
3194    
3195   } else {   } else {
3196      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines);      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3197      if (!line) {      if (!line) {
3198   /* no LT_KERNEL or LT_MBMODULE in this entry? */   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3199   continue;   continue;
# Line 3003  int updateImage(struct grubConfig * cfg, Line 3349  int updateImage(struct grubConfig * cfg,
3349      return rc;      return rc;
3350  }  }
3351    
3352    int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
3353     const char * image, const char * prefix, const char * initrd,
3354     const char * title) {
3355        struct singleEntry * entry;
3356        struct singleLine * line, * kernelLine, *endLine = NULL;
3357        int index = 0;
3358    
3359        if (!image) return 0;
3360    
3361        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3362            kernelLine = getLineByType(LT_MBMODULE, entry->lines);
3363            if (!kernelLine) continue;
3364    
3365     /* if title is supplied, the entry's title must match it. */
3366     if (title) {
3367        char *linetitle;
3368    
3369        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3370        if (!line)
3371     continue;
3372    
3373        linetitle = extractTitle(cfg, line);
3374        if (!linetitle)
3375     continue;
3376        if (strcmp(title, linetitle)) {
3377     free(linetitle);
3378     continue;
3379        }
3380        free(linetitle);
3381     }
3382    
3383            if (prefix) {
3384                int prefixLen = strlen(prefix);
3385                if (!strncmp(initrd, prefix, prefixLen))
3386                    initrd += prefixLen;
3387            }
3388     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3389     if (endLine)
3390        removeLine(entry, endLine);
3391            line = addLine(entry, cfg->cfi, preferredLineType(LT_MBMODULE,cfg->cfi),
3392     kernelLine->indent, initrd);
3393            if (!line)
3394        return 1;
3395     if (endLine) {
3396        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3397                if (!line)
3398     return 1;
3399     }
3400    
3401            break;
3402        }
3403    
3404        return 0;
3405    }
3406    
3407  int updateInitrd(struct grubConfig * cfg, const char * image,  int updateInitrd(struct grubConfig * cfg, const char * image,
3408                   const char * prefix, const char * initrd) {                   const char * prefix, const char * initrd, const char * title) {
3409      struct singleEntry * entry;      struct singleEntry * entry;
3410      struct singleLine * line, * kernelLine, *endLine = NULL;      struct singleLine * line, * kernelLine, *endLine = NULL;
3411      int index = 0;      int index = 0;
# Line 3012  int updateInitrd(struct grubConfig * cfg Line 3413  int updateInitrd(struct grubConfig * cfg
3413      if (!image) return 0;      if (!image) return 0;
3414    
3415      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3416          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines);          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3417          if (!kernelLine) continue;          if (!kernelLine) continue;
3418    
3419          line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);   /* if title is supplied, the entry's title must match it. */
3420     if (title) {
3421        char *linetitle;
3422    
3423        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3424        if (!line)
3425     continue;
3426    
3427        linetitle = extractTitle(cfg, line);
3428        if (!linetitle)
3429     continue;
3430        if (strcmp(title, linetitle)) {
3431     free(linetitle);
3432     continue;
3433        }
3434        free(linetitle);
3435     }
3436    
3437            line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
3438          if (line)          if (line)
3439              removeLine(entry, line);              removeLine(entry, line);
3440          if (prefix) {          if (prefix) {
# Line 3026  int updateInitrd(struct grubConfig * cfg Line 3445  int updateInitrd(struct grubConfig * cfg
3445   endLine = getLineByType(LT_ENTRY_END, entry->lines);   endLine = getLineByType(LT_ENTRY_END, entry->lines);
3446   if (endLine)   if (endLine)
3447      removeLine(entry, endLine);      removeLine(entry, endLine);
3448          line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi),   enum lineType_e lt;
3449   kernelLine->indent, initrd);   switch(kernelLine->type) {
3450        case LT_KERNEL:
3451            lt = LT_INITRD;
3452     break;
3453        case LT_KERNEL_EFI:
3454            lt = LT_INITRD_EFI;
3455     break;
3456        case LT_KERNEL_16:
3457            lt = LT_INITRD_16;
3458     break;
3459        default:
3460            lt = preferredLineType(LT_INITRD, cfg->cfi);
3461     }
3462            line = addLine(entry, cfg->cfi, lt, kernelLine->indent, initrd);
3463          if (!line)          if (!line)
3464      return 1;      return 1;
3465   if (endLine) {   if (endLine) {
# Line 3386  int addNewKernel(struct grubConfig * con Line 3818  int addNewKernel(struct grubConfig * con
3818   const char * newKernelPath, const char * newKernelTitle,   const char * newKernelPath, const char * newKernelTitle,
3819   const char * newKernelArgs, const char * newKernelInitrd,   const char * newKernelArgs, const char * newKernelInitrd,
3820   const char ** extraInitrds, int extraInitrdCount,   const char ** extraInitrds, int extraInitrdCount,
3821                   const char * newMBKernel, const char * newMBKernelArgs) {                   const char * newMBKernel, const char * newMBKernelArgs,
3822     const char * newDevTreePath) {
3823      struct singleEntry * new;      struct singleEntry * new;
3824      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;
3825      int needs;      int needs;
# Line 3427  int addNewKernel(struct grubConfig * con Line 3860  int addNewKernel(struct grubConfig * con
3860          needs |= NEED_MB;          needs |= NEED_MB;
3861          new->multiboot = 1;          new->multiboot = 1;
3862      }      }
3863        if (newDevTreePath && getKeywordByType(LT_DEVTREE, config->cfi))
3864     needs |= NEED_DEVTREE;
3865    
3866      if (template) {      if (template) {
3867   for (masterLine = template->lines;   for (masterLine = template->lines;
# Line 3612  int addNewKernel(struct grubConfig * con Line 4047  int addNewKernel(struct grubConfig * con
4047   newLine = addLineTmpl(new, tmplLine, newLine, NULL,   newLine = addLineTmpl(new, tmplLine, newLine, NULL,
4048   config->cfi);   config->cfi);
4049      }      }
4050        } else if (tmplLine->type == LT_DEVTREE &&
4051           tmplLine->numElements == 2 && newDevTreePath) {
4052            newLine = addLineTmpl(new, tmplLine, newLine,
4053          newDevTreePath + strlen(prefix),
4054          config->cfi);
4055     needs &= ~NEED_DEVTREE;
4056        } else if (tmplLine->type == LT_ENTRY_END && needs & NEED_DEVTREE) {
4057     const char *ndtp = newDevTreePath;
4058     if (!strncmp(newDevTreePath, prefix, strlen(prefix)))
4059        ndtp += strlen(prefix);
4060     newLine = addLine(new, config->cfi, LT_DEVTREE,
4061      config->secondaryIndent,
4062      ndtp);
4063     needs &= ~NEED_DEVTREE;
4064     newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
4065      } else {      } else {
4066   /* pass through other lines from the template */   /* pass through other lines from the template */
4067   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
# Line 3625  int addNewKernel(struct grubConfig * con Line 4075  int addNewKernel(struct grubConfig * con
4075   switch (config->cfi->entryStart) {   switch (config->cfi->entryStart) {
4076      case LT_KERNEL:      case LT_KERNEL:
4077      case LT_KERNEL_EFI:      case LT_KERNEL_EFI:
4078        case LT_KERNEL_16:
4079   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
4080      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
4081   } else {   } else {
# Line 3686  int addNewKernel(struct grubConfig * con Line 4137  int addNewKernel(struct grubConfig * con
4137   }   }
4138      }      }
4139    
4140        struct singleLine *endLine = NULL;
4141        endLine = getLineByType(LT_ENTRY_END, new->lines);
4142        if (endLine) {
4143        removeLine(new, endLine);
4144        needs |= NEED_END;
4145        }
4146    
4147      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
4148       * 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,
4149       * all the lines following the entryStart.       * all the lines following the entryStart.
# Line 3731  int addNewKernel(struct grubConfig * con Line 4189  int addNewKernel(struct grubConfig * con
4189   free(initrdVal);   free(initrdVal);
4190   needs &= ~NEED_INITRD;   needs &= ~NEED_INITRD;
4191      }      }
4192        if (needs & NEED_DEVTREE) {
4193     newLine = addLine(new, config->cfi, LT_DEVTREE,
4194      config->secondaryIndent,
4195      newDevTreePath);
4196     needs &= ~NEED_DEVTREE;
4197        }
4198    
4199        /* NEEDS_END must be last on bootloaders that need it... */
4200      if (needs & NEED_END) {      if (needs & NEED_END) {
4201   newLine = addLine(new, config->cfi, LT_ENTRY_END,   newLine = addLine(new, config->cfi, LT_ENTRY_END,
4202   config->secondaryIndent, NULL);   config->secondaryIndent, NULL);
# Line 3748  int addNewKernel(struct grubConfig * con Line 4214  int addNewKernel(struct grubConfig * con
4214      return 0;      return 0;
4215  }  }
4216    
 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 received SIGSEGV!  Backtrace (%ld):\n",  
             (unsigned long)size);  
     backtrace_symbols_fd(array, size, STDERR_FILENO);  
     exit(1);  
 }  
   
4217  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
4218      poptContext optCon;      poptContext optCon;
4219      const char * grubConfig = NULL;      const char * grubConfig = NULL;
# Line 3782  int main(int argc, const char ** argv) { Line 4233  int main(int argc, const char ** argv) {
4233      char * newKernelArgs = NULL;      char * newKernelArgs = NULL;
4234      char * newKernelInitrd = NULL;      char * newKernelInitrd = NULL;
4235      char * newKernelTitle = NULL;      char * newKernelTitle = NULL;
4236      char * newKernelVersion = NULL;      char * newDevTreePath = NULL;
4237      char * newMBKernel = NULL;      char * newMBKernel = NULL;
4238      char * newMBKernelArgs = NULL;      char * newMBKernelArgs = NULL;
4239      char * removeMBKernelArgs = NULL;      char * removeMBKernelArgs = NULL;
# Line 3792  int main(int argc, const char ** argv) { Line 4243  int main(int argc, const char ** argv) {
4243      char * removeArgs = NULL;      char * removeArgs = NULL;
4244      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4245      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4246        char * envPath = NULL;
4247      const char * chptr = NULL;      const char * chptr = NULL;
4248      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4249      struct grubConfig * config;      struct grubConfig * config;
# Line 3839  int main(int argc, const char ** argv) { Line 4291  int main(int argc, const char ** argv) {
4291      _("display the index of the default kernel") },      _("display the index of the default kernel") },
4292   { "default-title", 0, 0, &displayDefaultTitle, 0,   { "default-title", 0, 0, &displayDefaultTitle, 0,
4293      _("display the title of the default kernel") },      _("display the title of the default kernel") },
4294     { "devtree", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4295        _("device tree file for new stanza"), _("dtb-path") },
4296     { "devtreedir", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4297        _("device tree directory for new stanza"), _("dtb-path") },
4298   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4299      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4300   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4301      _("force grub2 stanzas to use efi") },      _("force grub2 stanzas to use efi") },
4302     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4303        _("path for environment data"),
4304        _("path") },
4305   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4306      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4307   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3897  int main(int argc, const char ** argv) { Line 4356  int main(int argc, const char ** argv) {
4356    
4357      useextlinuxmenu=0;      useextlinuxmenu=0;
4358    
4359      signal(SIGSEGV, traceback);      int i = 0;
4360        for (int j = 1; j < argc; j++)
4361     i += strlen(argv[j]) + 1;
4362        saved_command_line = malloc(i);
4363        if (!saved_command_line) {
4364     fprintf(stderr, "grubby: %m\n");
4365     exit(1);
4366        }
4367        saved_command_line[0] = '\0';
4368        for (int j = 1; j < argc; j++) {
4369     strcat(saved_command_line, argv[j]);
4370     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4371        }
4372    
4373      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4374      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
# Line 3942  int main(int argc, const char ** argv) { Line 4413  int main(int argc, const char ** argv) {
4413   return 1;   return 1;
4414      } else if (configureGrub2) {      } else if (configureGrub2) {
4415   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4416     if (envPath)
4417        cfi->envFile = envPath;
4418      } else if (configureLilo) {      } else if (configureLilo) {
4419   cfi = &liloConfigType;   cfi = &liloConfigType;
4420      } else if (configureGrub) {      } else if (configureGrub) {
# Line 3960  int main(int argc, const char ** argv) { Line 4433  int main(int argc, const char ** argv) {
4433      }      }
4434    
4435      if (!cfi) {      if (!cfi) {
4436          if (grub2FindConfig(&grub2ConfigType))          if (grub2FindConfig(&grub2ConfigType)) {
4437      cfi = &grub2ConfigType;      cfi = &grub2ConfigType;
4438   else      if (envPath)
4439     cfi->envFile = envPath;
4440            } else
4441        #ifdef __ia64__        #ifdef __ia64__
4442      cfi = &eliloConfigType;      cfi = &eliloConfigType;
4443        #elif __powerpc__        #elif __powerpc__
# Line 3985  int main(int argc, const char ** argv) { Line 4460  int main(int argc, const char ** argv) {
4460      grubConfig = cfi->defaultConfig;      grubConfig = cfi->defaultConfig;
4461      }      }
4462    
4463      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo ||
4464      newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
4465      defaultKernel || displayDefaultIndex || displayDefaultTitle ||      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
4466      (defaultIndex >= 0))) {      (defaultIndex >= 0))) {
# Line 3994  int main(int argc, const char ** argv) { Line 4469  int main(int argc, const char ** argv) {
4469   return 1;   return 1;
4470      }      }
4471    
4472      if ((displayDefault || kernelInfo) && (newKernelVersion || newKernelPath ||      if ((displayDefault || kernelInfo) && (newKernelPath ||
4473     removeKernelPath)) {     removeKernelPath)) {
4474   fprintf(stderr, _("grubby: --default-kernel and --info may not "   fprintf(stderr, _("grubby: --default-kernel and --info may not "
4475    "be used when adding or removing kernels\n"));    "be used when adding or removing kernels\n"));
# Line 4004  int main(int argc, const char ** argv) { Line 4479  int main(int argc, const char ** argv) {
4479      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
4480   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
4481   return 1;   return 1;
4482      } else if (!newKernelPath && (newKernelTitle  || copyDefault ||      } else if (!newKernelPath && (copyDefault ||
4483    (newKernelInitrd && !updateKernelPath)||    (newKernelInitrd && !updateKernelPath)||
4484    makeDefault || extraInitrdCount > 0)) {    makeDefault || extraInitrdCount > 0)) {
4485   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
# Line 4142  int main(int argc, const char ** argv) { Line 4617  int main(int argc, const char ** argv) {
4617   return 0;   return 0;
4618      }      }
4619    
4620        if (grubConfig == NULL) {
4621     printf("Could not find bootloader configuration file.\n");
4622     exit(1);
4623        }
4624    
4625      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4626      if (!config) return 1;      if (!config) return 1;
4627    
# Line 4151  int main(int argc, const char ** argv) { Line 4631  int main(int argc, const char ** argv) {
4631          char * rootspec;          char * rootspec;
4632    
4633   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4634     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4635     cfi->defaultIsSaved)
4636        config->defaultImage = 0;
4637   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4638   if (!entry) return 0;   if (!entry) return 0;
4639   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4640    
4641   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
4642   if (!line) return 0;   if (!line) return 0;
4643    
4644          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 4168  int main(int argc, const char ** argv) { Line 4651  int main(int argc, const char ** argv) {
4651   struct singleLine * line;   struct singleLine * line;
4652   struct singleEntry * entry;   struct singleEntry * entry;
4653    
4654   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1)
4655        return 0;
4656     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4657     cfi->defaultIsSaved)
4658        config->defaultImage = 0;
4659   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4660   if (!entry) return 0;   if (!entry)
4661     return 0;
4662    
4663   if (!configureGrub2) {   if (!configureGrub2) {
4664    line = getLineByType(LT_TITLE, entry->lines);      char *title;
4665    if (!line) return 0;      line = getLineByType(LT_TITLE, entry->lines);
4666    printf("%s\n", line->elements[1].item);      if (!line)
4667     return 0;
4668        title = extractTitle(config, line);
4669        if (!title)
4670     return 0;
4671        printf("%s\n", title);
4672        free(title);
4673   } else {   } else {
4674    char * title;      char * title;
4675    
4676    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
4677    line = getLineByType(LT_MENUENTRY, entry->lines);      line = getLineByType(LT_MENUENTRY, entry->lines);
4678    if (!line) return 0;      if (!line)
4679    title = grub2ExtractTitle(line);   return 0;
4680    if (title)      title = grub2ExtractTitle(line);
4681      printf("%s\n", title);      if (title)
4682     printf("%s\n", title);
4683   }   }
4684   return 0;   return 0;
4685    
4686      } else if (displayDefaultIndex) {      } else if (displayDefaultIndex) {
4687          if (config->defaultImage == -1) return 0;          if (config->defaultImage == -1) return 0;
4688     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4689     cfi->defaultIsSaved)
4690        config->defaultImage = 0;
4691          printf("%i\n", config->defaultImage);          printf("%i\n", config->defaultImage);
4692            return 0;
4693    
4694      } else if (kernelInfo)      } else if (kernelInfo)
4695   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
# Line 4209  int main(int argc, const char ** argv) { Line 4707  int main(int argc, const char ** argv) {
4707      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4708                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
4709      if (updateKernelPath && newKernelInitrd) {      if (updateKernelPath && newKernelInitrd) {
4710              if (updateInitrd(config, updateKernelPath, bootPrefix,      if (newMBKernel) {
4711                               newKernelInitrd)) return 1;      if (addMBInitrd(config, newMBKernel, updateKernelPath,
4712     bootPrefix, newKernelInitrd,
4713     newKernelTitle))
4714        return 1;
4715        } else {
4716        if (updateInitrd(config, updateKernelPath, bootPrefix,
4717     newKernelInitrd, newKernelTitle))
4718     return 1;
4719        }
4720      }      }
4721      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
4722                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
4723                       (const char **)extraInitrds, extraInitrdCount,                       (const char **)extraInitrds, extraInitrdCount,
4724                       newMBKernel, newMBKernelArgs)) return 1;                       newMBKernel, newMBKernelArgs, newDevTreePath)) return 1;
4725            
4726    
4727      if (numEntries(config) == 0) {      if (numEntries(config) == 0) {

Legend:
Removed from v.2058  
changed lines
  Added in v.2992