Magellan Linux

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

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

revision 1748 by niro, Sat Feb 18 01:07:15 2012 UTC revision 2989 by niro, Thu Jun 30 10:32:12 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) {
   int i;  
387    int count = 0;    int count = 0;
388    
389    for (i = 0; i < line->numElements; i++) {    for (int i = 0; i < line->numElements; i++) {
390      int indentSize = 0;      int indentSize = 0;
391    
392      count = count + strlen(line->elements[i].item);      count = count + strlen(line->elements[i].item);
# Line 265  int sizeOfSingleLine(struct singleLine * Line 405  int sizeOfSingleLine(struct singleLine *
405    return count;    return count;
406  }  }
407    
408    static int isquote(char q)
409    {
410        if (q == '\'' || q == '\"')
411     return 1;
412        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 281  char *grub2ExtractTitle(struct singleLin Line 436  char *grub2ExtractTitle(struct singleLin
436      current_len = strlen(current);      current_len = strlen(current);
437    
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 ((*current == '\'') && (*(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);
443        *(tmp + current_len - 1) = '\0';   *(tmp + current_len - 1) = '\0';
444        return ++tmp;   return ++tmp;
445      }      }
446    
447      /* if no quotes, return second word verbatim */      /* if no quotes, return second word verbatim */
448      if (*current != '\'') {      if (!isquote(*current))
449        return current;   return current;
     }  
450    
451      /* second element start with a quote, so we have to find the element      /* second element start with a quote, so we have to find the element
452       * whose last character is also quote (assuming it's the closing one) */       * whose last character is also quote (assuming it's the closing one) */
453      if (*current == '\'') {      int resultMaxSize;
454        int resultMaxSize;      char * result;
455        char * result;      /* need to ensure that ' does not match " as we search */
456        char quote_char = *current;
457        resultMaxSize = sizeOfSingleLine(line);      
458        result = malloc(resultMaxSize);      resultMaxSize = sizeOfSingleLine(line);
459        snprintf(result, resultMaxSize, "%s", ++current);      result = malloc(resultMaxSize);
460        snprintf(result, resultMaxSize, "%s", ++current);
461        i++;      
462        for (; i < line->numElements; ++i) {      i++;
463        for (; i < line->numElements; ++i) {
464   current = line->elements[i].item;   current = line->elements[i].item;
465   current_len = strlen(current);   current_len = strlen(current);
466   current_indent = line->elements[i].indent;   current_indent = line->elements[i].indent;
467   current_indent_len = strlen(current_indent);   current_indent_len = strlen(current_indent);
468    
469   strncat(result, current_indent, current_indent_len);   strncat(result, current_indent, current_indent_len);
470   if (*(current + current_len - 1) != '\'') {   if (current[current_len-1] != quote_char) {
471    strncat(result, current, current_len);      strncat(result, current, current_len);
472   } else {   } else {
473    strncat(result, current, current_len - 1);      strncat(result, current, current_len - 1);
474    break;      break;
475   }   }
       }  
       return result;  
476      }      }
477        return result;
     return NULL;  
478  }  }
479    
480  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
481      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
482        .getEnv = grub2GetEnv,
483        .setEnv = grub2SetEnv,
484      .keywords = grub2Keywords,      .keywords = grub2Keywords,
485      .defaultIsIndex = 1,      .defaultIsIndex = 1,
486      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
# Line 431  struct keywordTypes extlinuxKeywords[] = Line 585  struct keywordTypes extlinuxKeywords[] =
585      { "initrd",    LT_INITRD,      ' ', ',' },      { "initrd",    LT_INITRD,      ' ', ',' },
586      { "append",    LT_KERNELARGS,  ' ' },      { "append",    LT_KERNELARGS,  ' ' },
587      { "prompt",     LT_UNKNOWN,     ' ' },      { "prompt",     LT_UNKNOWN,     ' ' },
588        { "fdt",        LT_DEVTREE,     ' ' },
589        { "fdtdir",     LT_DEVTREE,     ' ' },
590      { NULL,    0, 0 },      { NULL,    0, 0 },
591  };  };
592  int useextlinuxmenu;  int useextlinuxmenu;
# Line 441  struct configFileInfo eliloConfigType = Line 597  struct configFileInfo eliloConfigType =
597      .needsBootPrefix = 1,      .needsBootPrefix = 1,
598      .argsInQuotes = 1,      .argsInQuotes = 1,
599      .mbConcatArgs = 1,      .mbConcatArgs = 1,
600        .titlePosition = 1,
601  };  };
602    
603  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
# Line 449  struct configFileInfo liloConfigType = { Line 606  struct configFileInfo liloConfigType = {
606      .entryStart = LT_KERNEL,      .entryStart = LT_KERNEL,
607      .argsInQuotes = 1,      .argsInQuotes = 1,
608      .maxTitleLength = 15,      .maxTitleLength = 15,
609        .titlePosition = 1,
610  };  };
611    
612  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
# Line 459  struct configFileInfo yabootConfigType = Line 617  struct configFileInfo yabootConfigType =
617      .argsInQuotes = 1,      .argsInQuotes = 1,
618      .maxTitleLength = 15,      .maxTitleLength = 15,
619      .mbAllowExtraInitRds = 1,      .mbAllowExtraInitRds = 1,
620        .titlePosition = 1,
621  };  };
622    
623  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
# Line 468  struct configFileInfo siloConfigType = { Line 627  struct configFileInfo siloConfigType = {
627      .needsBootPrefix = 1,      .needsBootPrefix = 1,
628      .argsInQuotes = 1,      .argsInQuotes = 1,
629      .maxTitleLength = 15,      .maxTitleLength = 15,
630        .titlePosition = 1,
631  };  };
632    
633  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
# Line 481  struct configFileInfo ziplConfigType = { Line 641  struct configFileInfo ziplConfigType = {
641  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
642      .defaultConfig = "/boot/extlinux/extlinux.conf",      .defaultConfig = "/boot/extlinux/extlinux.conf",
643      .keywords = extlinuxKeywords,      .keywords = extlinuxKeywords,
644        .caseInsensitive = 1,
645      .entryStart = LT_TITLE,      .entryStart = LT_TITLE,
646      .needsBootPrefix = 1,      .needsBootPrefix = 1,
647      .maxTitleLength = 255,      .maxTitleLength = 255,
648      .mbAllowExtraInitRds = 1,      .mbAllowExtraInitRds = 1,
649        .defaultIsUnquoted = 1,
650        .titlePosition = 1,
651  };  };
652    
653  struct grubConfig {  struct grubConfig {
# Line 505  struct singleEntry * findEntryByIndex(st Line 668  struct singleEntry * findEntryByIndex(st
668  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
669       const char * path, const char * prefix,       const char * path, const char * prefix,
670       int * index);       int * index);
671    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
672          int * index);
673  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
674  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
675  struct singleLine * lineDup(struct singleLine * line);  struct singleLine * lineDup(struct singleLine * line);
# Line 569  static char * sdupprintf(const char *for Line 734  static char * sdupprintf(const char *for
734      return buf;      return buf;
735  }  }
736    
737    static enum lineType_e preferredLineType(enum lineType_e type,
738     struct configFileInfo *cfi) {
739        if (isEfi && cfi == &grub2ConfigType) {
740     switch (type) {
741     case LT_KERNEL:
742        return isEfiOnly ? LT_KERNEL : LT_KERNEL_EFI;
743     case LT_INITRD:
744        return isEfiOnly ? LT_INITRD : LT_INITRD_EFI;
745     default:
746        return type;
747     }
748    #if defined(__i386__) || defined(__x86_64__)
749        } else if (cfi == &grub2ConfigType) {
750     switch (type) {
751     case LT_KERNEL:
752        return LT_KERNEL_16;
753     case LT_INITRD:
754        return LT_INITRD_16;
755     default:
756        return type;
757     }
758    #endif
759        }
760        return type;
761    }
762    
763  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
764        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
765      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
     for (kw = cfi->keywords; kw->key; kw++) {  
766   if (kw->type == type)   if (kw->type == type)
767      return kw;      return kw;
768      }      }
# Line 602  static char * getuuidbydev(char *device) Line 792  static char * getuuidbydev(char *device)
792    
793  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
794   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
795      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
796      for (kw = cfi->keywords; kw->key; kw++) {   if (cfi->caseInsensitive) {
797   if (!strcmp(keyword, kw->key))      if (!strcasecmp(keyword, kw->key))
798      return kw->type;                  return kw->type;
799     } else {
800        if (!strcmp(keyword, kw->key))
801            return kw->type;
802     }
803      }      }
804      return LT_UNKNOWN;      return LT_UNKNOWN;
805  }  }
# Line 642  static int isEntryStart(struct singleLin Line 836  static int isEntryStart(struct singleLin
836  }  }
837    
838  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
839  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct grubConfig *cfg, struct singleLine * line) {
840      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it */
841      char * title;      char * title = NULL;
842      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
843      title++;   char *tmp = line->elements[cfg->cfi->titlePosition].item;
844      *(title + strlen(title) - 1) = '\0';   if (cfg->cfi->titleBracketed) {
845        tmp++;
846        title = strdup(tmp);
847        *(title + strlen(title) - 1) = '\0';
848     } else {
849        title = strdup(tmp);
850     }
851        } else if (line->type == LT_MENUENTRY)
852     title = strdup(line->elements[1].item);
853        else
854     return NULL;
855      return title;      return title;
856  }  }
857    
# Line 690  static void lineInit(struct singleLine * Line 894  static void lineInit(struct singleLine *
894  }  }
895    
896  struct singleLine * lineDup(struct singleLine * line) {  struct singleLine * lineDup(struct singleLine * line) {
     int i;  
897      struct singleLine * newLine = malloc(sizeof(*newLine));      struct singleLine * newLine = malloc(sizeof(*newLine));
898    
899      newLine->indent = strdup(line->indent);      newLine->indent = strdup(line->indent);
# Line 700  struct singleLine * lineDup(struct singl Line 903  struct singleLine * lineDup(struct singl
903      newLine->elements = malloc(sizeof(*newLine->elements) *      newLine->elements = malloc(sizeof(*newLine->elements) *
904         newLine->numElements);         newLine->numElements);
905    
906      for (i = 0; i < newLine->numElements; i++) {      for (int i = 0; i < newLine->numElements; i++) {
907   newLine->elements[i].indent = strdup(line->elements[i].indent);   newLine->elements[i].indent = strdup(line->elements[i].indent);
908   newLine->elements[i].item = strdup(line->elements[i].item);   newLine->elements[i].item = strdup(line->elements[i].item);
909      }      }
# Line 709  struct singleLine * lineDup(struct singl Line 912  struct singleLine * lineDup(struct singl
912  }  }
913    
914  static void lineFree(struct singleLine * line) {  static void lineFree(struct singleLine * line) {
     int i;  
   
915      if (line->indent) free(line->indent);      if (line->indent) free(line->indent);
916    
917      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
918   free(line->elements[i].item);   free(line->elements[i].item);
919   free(line->elements[i].indent);   free(line->elements[i].indent);
920      }      }
# Line 724  static void lineFree(struct singleLine * Line 925  static void lineFree(struct singleLine *
925    
926  static int lineWrite(FILE * out, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine * line,
927       struct configFileInfo * cfi) {       struct configFileInfo * cfi) {
     int i;  
   
928      if (fprintf(out, "%s", line->indent) == -1) return -1;      if (fprintf(out, "%s", line->indent) == -1) return -1;
929    
930      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
931     /* Need to handle this, because we strip the quotes from
932     * menuentry when read it. */
933     if (line->type == LT_MENUENTRY && i == 1) {
934        if(!isquote(*line->elements[i].item)) {
935     int substring = 0;
936     /* If the line contains nested quotes, we did not strip
937     * the "interna" quotes and we must use the right quotes
938     * again when writing the updated file. */
939     for (int j = i; j < line->numElements; j++) {
940        if (strchr(line->elements[i].item, '\'') != NULL) {
941           substring = 1;
942           fprintf(out, "\"%s\"", line->elements[i].item);
943           break;
944        }
945     }
946     if (!substring)
947        fprintf(out, "\'%s\'", line->elements[i].item);
948        } else {
949     fprintf(out, "%s", line->elements[i].item);
950        }
951        fprintf(out, "%s", line->elements[i].indent);
952    
953        continue;
954     }
955    
956   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)
957      if (fputc('"', out) == EOF) return -1;      if (fputc('"', out) == EOF) return -1;
958    
# Line 822  static int getNextLine(char ** bufPtr, s Line 1046  static int getNextLine(char ** bufPtr, s
1046      if (*line->elements[0].item == '#') {      if (*line->elements[0].item == '#') {
1047   char * fullLine;   char * fullLine;
1048   int len;   int len;
  int i;  
1049    
1050   len = strlen(line->indent);   len = strlen(line->indent);
1051   for (i = 0; i < line->numElements; i++)   for (int i = 0; i < line->numElements; i++)
1052      len += strlen(line->elements[i].item) +      len += strlen(line->elements[i].item) +
1053     strlen(line->elements[i].indent);     strlen(line->elements[i].indent);
1054    
# Line 834  static int getNextLine(char ** bufPtr, s Line 1057  static int getNextLine(char ** bufPtr, s
1057   free(line->indent);   free(line->indent);
1058   line->indent = fullLine;   line->indent = fullLine;
1059    
1060   for (i = 0; i < line->numElements; i++) {   for (int i = 0; i < line->numElements; i++) {
1061      strcat(fullLine, line->elements[i].item);      strcat(fullLine, line->elements[i].item);
1062      strcat(fullLine, line->elements[i].indent);      strcat(fullLine, line->elements[i].indent);
1063      free(line->elements[i].item);      free(line->elements[i].item);
# Line 853  static int getNextLine(char ** bufPtr, s Line 1076  static int getNextLine(char ** bufPtr, s
1076   * elements up more   * elements up more
1077   */   */
1078   if (!isspace(kw->separatorChar)) {   if (!isspace(kw->separatorChar)) {
     int i;  
1079      char indent[2] = "";      char indent[2] = "";
1080      indent[0] = kw->separatorChar;      indent[0] = kw->separatorChar;
1081      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1082   char *p;   char *p;
  int j;  
1083   int numNewElements;   int numNewElements;
1084    
1085   numNewElements = 0;   numNewElements = 0;
# Line 874  static int getNextLine(char ** bufPtr, s Line 1095  static int getNextLine(char ** bufPtr, s
1095      sizeof(*line->elements) * elementsAlloced);      sizeof(*line->elements) * elementsAlloced);
1096   }   }
1097    
1098   for (j = line->numElements; j > i; j--) {   for (int j = line->numElements; j > i; j--) {
1099   line->elements[j + numNewElements] = line->elements[j];   line->elements[j + numNewElements] = line->elements[j];
1100   }   }
1101   line->numElements += numNewElements;   line->numElements += numNewElements;
# Line 887  static int getNextLine(char ** bufPtr, s Line 1108  static int getNextLine(char ** bufPtr, s
1108   break;   break;
1109   }   }
1110    
1111   free(line->elements[i].indent);   line->elements[i + 1].indent = line->elements[i].indent;
1112   line->elements[i].indent = strdup(indent);   line->elements[i].indent = strdup(indent);
1113   *p++ = '\0';   *p++ = '\0';
1114   i++;   i++;
1115   line->elements[i].item = strdup(p);   line->elements[i].item = strdup(p);
  line->elements[i].indent = strdup("");  
  p = line->elements[i].item;  
1116   }   }
1117      }      }
1118   }   }
# Line 903  static int getNextLine(char ** bufPtr, s Line 1122  static int getNextLine(char ** bufPtr, s
1122      return 0;      return 0;
1123  }  }
1124    
1125    static int isnumber(const char *s)
1126    {
1127        int i;
1128        for (i = 0; s[i] != '\0'; i++)
1129     if (s[i] < '0' || s[i] > '9')
1130        return 0;
1131        return i;
1132    }
1133    
1134  static struct grubConfig * readConfig(const char * inName,  static struct grubConfig * readConfig(const char * inName,
1135        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
1136      int in;      int in;
# Line 914  static struct grubConfig * readConfig(co Line 1142  static struct grubConfig * readConfig(co
1142      struct singleLine * last = NULL, * line, * defaultLine = NULL;      struct singleLine * last = NULL, * line, * defaultLine = NULL;
1143      char * end;      char * end;
1144      struct singleEntry * entry = NULL;      struct singleEntry * entry = NULL;
1145      int i, len;      int len;
1146      char * buf;      char * buf;
1147    
1148      if (!strcmp(inName, "-")) {      if (inName == NULL) {
1149            printf("Could not find bootloader configuration\n");
1150            exit(1);
1151        } else if (!strcmp(inName, "-")) {
1152   in = 0;   in = 0;
1153      } else {      } else {
1154   if ((in = open(inName, O_RDONLY)) < 0) {   if ((in = open(inName, O_RDONLY)) < 0) {
# Line 977  static struct grubConfig * readConfig(co Line 1208  static struct grubConfig * readConfig(co
1208   }   }
1209    
1210   if (line->type == LT_SET_VARIABLE) {   if (line->type == LT_SET_VARIABLE) {
     int i;  
1211      dbgPrintf("found 'set' command (%d elements): ", line->numElements);      dbgPrintf("found 'set' command (%d elements): ", line->numElements);
1212      dbgPrintf("%s", line->indent);      dbgPrintf("%s", line->indent);
1213      for (i = 0; i < line->numElements; i++)      for (int i = 0; i < line->numElements; i++)
1214   dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item);   dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);
1215      dbgPrintf("\n");      dbgPrintf("\n");
1216      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1217      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
1218      !strcmp(line->elements[1].item, kwType->key)) {      !strcmp(line->elements[1].item, kwType->key) &&
1219        !is_special_grub2_variable(line->elements[2].item)) {
1220   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1221   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1222   defaultLine = line;   defaultLine = line;
1223      }      }
  } else if (line->type == LT_DEFAULT && line->numElements == 2) {  
     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;  
     defaultLine = line;  
1224    
1225          } else if (line->type == LT_KERNEL) {          } else if (iskernel(line->type)) {
1226      /* if by some freak chance this is multiboot and the "module"      /* if by some freak chance this is multiboot and the "module"
1227       * lines came earlier in the template, make sure to use LT_HYPER       * lines came earlier in the template, make sure to use LT_HYPER
1228       * instead of LT_KERNEL now       * instead of LT_KERNEL now
1229       */       */
1230      if (entry->multiboot)      if (entry && entry->multiboot)
1231   line->type = LT_HYPER;   line->type = LT_HYPER;
1232    
1233          } else if (line->type == LT_MBMODULE) {          } else if (line->type == LT_MBMODULE) {
# Line 1008  static struct grubConfig * readConfig(co Line 1236  static struct grubConfig * readConfig(co
1236       * This only applies to grub, but that's the only place we       * This only applies to grub, but that's the only place we
1237       * should find LT_MBMODULE lines anyway.       * should find LT_MBMODULE lines anyway.
1238       */       */
1239      struct singleLine * l;      for (struct singleLine *l = entry->lines; l; l = l->next) {
     for (l = entry->lines; l; l = l->next) {  
1240   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1241      break;      break;
1242   else if (l->type == LT_KERNEL) {   else if (iskernel(l->type)) {
1243      l->type = LT_HYPER;      l->type = LT_HYPER;
1244      break;      break;
1245   }   }
# Line 1026  static struct grubConfig * readConfig(co Line 1253  static struct grubConfig * readConfig(co
1253      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);
1254      if (*end) cfg->fallbackImage = -1;      if (*end) cfg->fallbackImage = -1;
1255    
1256   } else if (line->type == LT_TITLE && line->numElements > 1) {   } else if ((line->type == LT_DEFAULT && cfi->defaultIsUnquoted) ||
1257      /* make the title a single argument (undoing our parsing) */                  (line->type == LT_TITLE && line->numElements > 1)) {
1258        /* make the title/default a single argument (undoing our parsing) */
1259      len = 0;      len = 0;
1260      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1261   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
1262   len += strlen(line->elements[i].indent);   len += strlen(line->elements[i].indent);
1263      }      }
1264      buf = malloc(len + 1);      buf = malloc(len + 1);
1265      *buf = '\0';      *buf = '\0';
1266    
1267      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1268   strcat(buf, line->elements[i].item);   strcat(buf, line->elements[i].item);
1269   free(line->elements[i].item);   free(line->elements[i].item);
1270    
# Line 1050  static struct grubConfig * readConfig(co Line 1278  static struct grubConfig * readConfig(co
1278      line->elements[line->numElements - 1].indent;      line->elements[line->numElements - 1].indent;
1279      line->elements[1].item = buf;      line->elements[1].item = buf;
1280      line->numElements = 2;      line->numElements = 2;
1281     } else if (line->type == LT_MENUENTRY && line->numElements > 3) {
1282        /* let --remove-kernel="TITLE=what" work */
1283        len = 0;
1284        char *extras;
1285        char *title;
1286        /* initially unseen value */
1287        char quote_char = '\0';
1288    
1289        for (int i = 1; i < line->numElements; i++) {
1290     len += strlen(line->elements[i].item);
1291     len += strlen(line->elements[i].indent);
1292        }
1293        buf = malloc(len + 1);
1294        *buf = '\0';
1295    
1296        /* allocate mem for extra flags. */
1297        extras = malloc(len + 1);
1298        *extras = '\0';
1299    
1300        /* get title. */
1301        for (int i = 0; i < line->numElements; i++) {
1302     if (!strcmp(line->elements[i].item, "menuentry"))
1303        continue;
1304     if (isquote(*line->elements[i].item) && quote_char == '\0') {
1305        /* ensure we properly pair off quotes */
1306        quote_char = *line->elements[i].item;
1307        title = line->elements[i].item + 1;
1308     } else {
1309        title = line->elements[i].item;
1310     }
1311    
1312     len = strlen(title);
1313            if (title[len-1] == quote_char) {
1314        strncat(buf, title,len-1);
1315        break;
1316     } else {
1317        strcat(buf, title);
1318        strcat(buf, line->elements[i].indent);
1319     }
1320        }
1321    
1322        /* get extras */
1323        int count = 0;
1324        quote_char = '\0';
1325        for (int i = 0; i < line->numElements; i++) {
1326     if (count >= 2) {
1327        strcat(extras, line->elements[i].item);
1328        strcat(extras, line->elements[i].indent);
1329     }
1330    
1331     if (!strcmp(line->elements[i].item, "menuentry"))
1332        continue;
1333    
1334     /* count ' or ", there should be two in menuentry line. */
1335     if (isquote(*line->elements[i].item) && quote_char == '\0') {
1336        /* ensure we properly pair off quotes */
1337                quote_char = *line->elements[i].item;
1338        count++;
1339     }
1340    
1341     len = strlen(line->elements[i].item);
1342    
1343     if (line->elements[i].item[len -1] == quote_char)
1344        count++;
1345    
1346     /* ok, we get the final ' or ", others are extras. */
1347                }
1348        line->elements[1].indent =
1349     line->elements[line->numElements - 2].indent;
1350        line->elements[1].item = buf;
1351        line->elements[2].indent =
1352     line->elements[line->numElements - 2].indent;
1353        line->elements[2].item = extras;
1354        line->numElements = 3;
1355   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {
1356      /* Strip off any " which may be present; they'll be put back      /* Strip off any " which may be present; they'll be put back
1357         on write. This is one of the few (the only?) places that grubby         on write. This is one of the few (the only?) places that grubby
# Line 1059  static struct grubConfig * readConfig(co Line 1360  static struct grubConfig * readConfig(co
1360      if (line->numElements >= 2) {      if (line->numElements >= 2) {
1361   int last, len;   int last, len;
1362    
1363   if (*line->elements[1].item == '"')   if (isquote(*line->elements[1].item))
1364      memmove(line->elements[1].item, line->elements[1].item + 1,      memmove(line->elements[1].item, line->elements[1].item + 1,
1365      strlen(line->elements[1].item + 1) + 1);      strlen(line->elements[1].item + 1) + 1);
1366    
1367   last = line->numElements - 1;   last = line->numElements - 1;
1368   len = strlen(line->elements[last].item) - 1;   len = strlen(line->elements[last].item) - 1;
1369   if (line->elements[last].item[len] == '"')   if (isquote(line->elements[last].item[len]))
1370      line->elements[last].item[len] = '\0';      line->elements[last].item[len] = '\0';
1371      }      }
1372   }   }
1373    
1374     if (line->type == LT_DEFAULT && line->numElements == 2) {
1375        cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1376        defaultLine = line;
1377     }
1378    
1379   /* If we find a generic config option which should live at the   /* If we find a generic config option which should live at the
1380     top of the file, move it there. Old versions of grubby were     top of the file, move it there. Old versions of grubby were
1381     probably responsible for putting new images in the wrong     probably responsible for putting new images in the wrong
# Line 1127  static struct grubConfig * readConfig(co Line 1433  static struct grubConfig * readConfig(co
1433          if (defaultLine->numElements > 2 &&          if (defaultLine->numElements > 2 &&
1434      cfi->defaultSupportSaved &&      cfi->defaultSupportSaved &&
1435      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1436      cfg->defaultImage = DEFAULT_SAVED_GRUB2;   cfg->cfi->defaultIsSaved = 1;
1437     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1438     if (cfg->cfi->getEnv) {
1439        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1440        if (defTitle) {
1441     int index = 0;
1442     if (isnumber(defTitle)) {
1443        index = atoi(defTitle);
1444        entry = findEntryByIndex(cfg, index);
1445     } else {
1446        entry = findEntryByTitle(cfg, defTitle, &index);
1447     }
1448     if (entry)
1449        cfg->defaultImage = index;
1450        }
1451     }
1452   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1453      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1454      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
# Line 1145  static struct grubConfig * readConfig(co Line 1466  static struct grubConfig * readConfig(co
1466      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);
1467      if (*end) cfg->defaultImage = -1;      if (*end) cfg->defaultImage = -1;
1468   } else if (defaultLine->numElements >= 2) {   } else if (defaultLine->numElements >= 2) {
1469      i = 0;      int i = 0;
1470      while ((entry = findEntryByIndex(cfg, i))) {      while ((entry = findEntryByIndex(cfg, i))) {
1471   for (line = entry->lines; line; line = line->next)   for (line = entry->lines; line; line = line->next)
1472      if (line->type == LT_TITLE) break;      if (line->type == LT_TITLE) break;
# Line 1156  static struct grubConfig * readConfig(co Line 1477  static struct grubConfig * readConfig(co
1477                                  line->elements[1].item)) break;                                  line->elements[1].item)) break;
1478                  } else if (line) {                  } else if (line) {
1479                      if (!strcmp(defaultLine->elements[1].item,                      if (!strcmp(defaultLine->elements[1].item,
1480                                  extractTitle(line))) break;                                  extractTitle(cfg, line))) break;
1481                  }                  }
1482   i++;   i++;
1483   entry = NULL;   entry = NULL;
# Line 1168  static struct grubConfig * readConfig(co Line 1489  static struct grubConfig * readConfig(co
1489          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1490      }      }
1491   }   }
1492        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1493     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1494     if (defTitle) {
1495        int index = 0;
1496        if (isnumber(defTitle)) {
1497     index = atoi(defTitle);
1498     entry = findEntryByIndex(cfg, index);
1499        } else {
1500     entry = findEntryByTitle(cfg, defTitle, &index);
1501        }
1502        if (entry)
1503     cfg->defaultImage = index;
1504     }
1505      } else {      } else {
1506          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1507      }      }
# Line 1185  static void writeDefault(FILE * out, cha Line 1519  static void writeDefault(FILE * out, cha
1519    
1520      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1521   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1522      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1523   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1524      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1525        char *title;
1526        entry = findEntryByIndex(cfg, cfg->defaultImage);
1527        line = getLineByType(LT_MENUENTRY, entry->lines);
1528        if (!line)
1529     line = getLineByType(LT_TITLE, entry->lines);
1530        if (line) {
1531     title = extractTitle(cfg, line);
1532     if (title)
1533        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1534        }
1535     }
1536        } else if (cfg->defaultImage > -1) {
1537   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1538      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1539          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 1220  static void writeDefault(FILE * out, cha Line 1566  static void writeDefault(FILE * out, cha
1566              else if (line && (line->numElements == 1) &&              else if (line && (line->numElements == 1) &&
1567                       cfg->cfi->titleBracketed) {                       cfg->cfi->titleBracketed) {
1568   fprintf(out, "%sdefault%s%s\n", indent, separator,   fprintf(out, "%sdefault%s%s\n", indent, separator,
1569                          extractTitle(line));                          extractTitle(cfg, line));
1570              }              }
1571   }   }
1572      }      }
# Line 1247  static int writeConfig(struct grubConfig Line 1593  static int writeConfig(struct grubConfig
1593    
1594      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1595         directory to the dir of the symlink */         directory to the dir of the symlink */
1596              rc = chdir(dirname(strdupa(outName)));      char *dir = strdupa(outName);
1597        rc = chdir(dirname(dir));
1598      do {      do {
1599   buf = alloca(len + 1);   buf = alloca(len + 1);
1600   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1289  static int writeConfig(struct grubConfig Line 1636  static int writeConfig(struct grubConfig
1636      while (line) {      while (line) {
1637          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1638   line->numElements == 3 &&   line->numElements == 3 &&
1639   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1640     !is_special_grub2_variable(line->elements[2].item)) {
1641      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1642      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1643   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1385  static char *findDiskForRoot() Line 1733  static char *findDiskForRoot()
1733      buf[rc] = '\0';      buf[rc] = '\0';
1734      chptr = buf;      chptr = buf;
1735    
1736        char *foundanswer = NULL;
1737    
1738      while (chptr && chptr != buf+rc) {      while (chptr && chptr != buf+rc) {
1739          devname = chptr;          devname = chptr;
1740    
# Line 1412  static char *findDiskForRoot() Line 1762  static char *findDiskForRoot()
1762           * for '/' obviously.           * for '/' obviously.
1763           */           */
1764          if (*(++chptr) == '/' && *(++chptr) == ' ') {          if (*(++chptr) == '/' && *(++chptr) == ' ') {
1765              /*              /* remember the last / entry in mtab */
1766               * Move back 2, which is the first space after the device name, set             foundanswer = devname;
              * it to \0 so strdup will just get the devicename.  
              */  
             chptr -= 2;  
             *chptr = '\0';  
             return strdup(devname);  
1767          }          }
1768    
1769          /* Next line */          /* Next line */
# Line 1427  static char *findDiskForRoot() Line 1772  static char *findDiskForRoot()
1772              chptr++;              chptr++;
1773      }      }
1774    
1775        /* Return the last / entry found */
1776        if (foundanswer) {
1777            chptr = strchr(foundanswer, ' ');
1778            *chptr = '\0';
1779            return strdup(foundanswer);
1780        }
1781    
1782      return NULL;      return NULL;
1783  }  }
1784    
1785  void printEntry(struct singleEntry * entry) {  void printEntry(struct singleEntry * entry, FILE *f) {
1786      int i;      int i;
1787      struct singleLine * line;      struct singleLine * line;
1788    
1789      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1790   fprintf(stderr, "DBG: %s", line->indent);   log_message(f, "DBG: %s", line->indent);
1791   for (i = 0; i < line->numElements; i++) {   for (i = 0; i < line->numElements; i++) {
1792   fprintf(stderr, "%s%s",      /* Need to handle this, because we strip the quotes from
1793         * menuentry when read it. */
1794        if (line->type == LT_MENUENTRY && i == 1) {
1795     if(!isquote(*line->elements[i].item))
1796        log_message(f, "\'%s\'", line->elements[i].item);
1797     else
1798        log_message(f, "%s", line->elements[i].item);
1799     log_message(f, "%s", line->elements[i].indent);
1800    
1801     continue;
1802        }
1803        
1804        log_message(f, "%s%s",
1805      line->elements[i].item, line->elements[i].indent);      line->elements[i].item, line->elements[i].indent);
1806   }   }
1807   fprintf(stderr, "\n");   log_message(f, "\n");
1808      }      }
1809  }  }
1810    
1811  void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)  void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1812  {  {
1813      va_list argp;      static int once;
1814        va_list argp, argq;
1815    
1816        va_start(argp, fmt);
1817    
1818      if (!debug)      va_copy(argq, argp);
1819        if (!once) {
1820     log_time(NULL);
1821     log_message(NULL, "command line: %s\n", saved_command_line);
1822        }
1823        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1824        log_vmessage(NULL, fmt, argq);
1825    
1826        printEntry(entry, NULL);
1827        va_end(argq);
1828    
1829        if (!debug) {
1830     once = 1;
1831         va_end(argp);
1832   return;   return;
1833        }
1834    
1835      va_start(argp, fmt);      if (okay) {
1836     va_end(argp);
1837     return;
1838        }
1839    
1840        if (!once)
1841     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1842        once = 1;
1843      fprintf(stderr, "DBG: Image entry failed: ");      fprintf(stderr, "DBG: Image entry failed: ");
1844      vfprintf(stderr, fmt, argp);      vfprintf(stderr, fmt, argp);
1845      printEntry(entry);      printEntry(entry, stderr);
1846      va_end(argp);      va_end(argp);
1847  }  }
1848    
# Line 1464  static int endswith(const char *s, char Line 1852  static int endswith(const char *s, char
1852  {  {
1853   int slen;   int slen;
1854    
1855   if (!s && !s[0])   if (!s || !s[0])
1856   return 0;   return 0;
1857   slen = strlen(s) - 1;   slen = strlen(s) - 1;
1858    
# Line 1481  int suitableImage(struct singleEntry * e Line 1869  int suitableImage(struct singleEntry * e
1869      char * rootdev;      char * rootdev;
1870    
1871      if (skipRemoved && entry->skip) {      if (skipRemoved && entry->skip) {
1872   notSuitablePrintf(entry, "marked to skip\n");   notSuitablePrintf(entry, 0, "marked to skip\n");
1873   return 0;   return 0;
1874      }      }
1875    
1876      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
1877      if (!line) {      if (!line) {
1878   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1879   return 0;   return 0;
1880      }      }
1881      if (line->numElements < 2) {      if (line->numElements < 2) {
1882   notSuitablePrintf(entry, "line has only %d elements\n",   notSuitablePrintf(entry, 0, "line has only %d elements\n",
1883      line->numElements);      line->numElements);
1884   return 0;   return 0;
1885      }      }
1886    
1887      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) {
1888        notSuitablePrintf(entry, 1, "\n");
1889        return 1;
1890        }
1891    
1892      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1893        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
# Line 1507  int suitableImage(struct singleEntry * e Line 1898  int suitableImage(struct singleEntry * e
1898      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1899              line->elements[1].item + rootspec_offset);              line->elements[1].item + rootspec_offset);
1900      if (access(fullName, R_OK)) {      if (access(fullName, R_OK)) {
1901   notSuitablePrintf(entry, "access to %s failed\n", fullName);   notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1902   return 0;   return 0;
1903      }      }
1904      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
# Line 1528  int suitableImage(struct singleEntry * e Line 1919  int suitableImage(struct singleEntry * e
1919    
1920              /* failed to find one */              /* failed to find one */
1921              if (!line) {              if (!line) {
1922   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1923   return 0;   return 0;
1924              }              }
1925    
# Line 1537  int suitableImage(struct singleEntry * e Line 1928  int suitableImage(struct singleEntry * e
1928      if (i < line->numElements)      if (i < line->numElements)
1929          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1930      else {      else {
1931   notSuitablePrintf(entry, "no root= entry found\n");   notSuitablePrintf(entry, 0, "no root= entry found\n");
1932   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1933          return 0;          return 0;
1934              }              }
# Line 1546  int suitableImage(struct singleEntry * e Line 1937  int suitableImage(struct singleEntry * e
1937    
1938      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1939      if (!getpathbyspec(dev)) {      if (!getpathbyspec(dev)) {
1940          notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);          notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1941          return 0;          return 0;
1942      } else      } else
1943   dev = getpathbyspec(dev);   dev = getpathbyspec(dev);
1944    
1945      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1946      if (!rootdev) {      if (!rootdev) {
1947          notSuitablePrintf(entry, "can't find root device\n");          notSuitablePrintf(entry, 0, "can't find root device\n");
1948   return 0;   return 0;
1949      }      }
1950    
1951      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1952          notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1953   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1954          free(rootdev);          free(rootdev);
1955          return 0;          return 0;
1956      }      }
1957    
1958      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1959          notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n",
1960   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1961   free(rootdev);   free(rootdev);
1962          return 0;          return 0;
1963      }      }
1964    
1965      free(rootdev);      free(rootdev);
1966        notSuitablePrintf(entry, 1, "\n");
1967    
1968      return 1;      return 1;
1969  }  }
# Line 1605  struct singleEntry * findEntryByPath(str Line 1997  struct singleEntry * findEntryByPath(str
1997   }   }
1998    
1999   indexVars[i + 1] = -1;   indexVars[i + 1] = -1;
2000    
2001   i = 0;   i = 0;
2002   if (index) {   if (index) {
2003      while (i < *index) i++;      while (i < *index) {
2004      if (indexVars[i] == -1) return NULL;   i++;
2005     if (indexVars[i] == -1) return NULL;
2006        }
2007   }   }
2008    
2009   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
2010   if (!entry) return NULL;   if (!entry) return NULL;
2011    
2012   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
2013   if (!line) return NULL;   if (!line) return NULL;
2014    
2015   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1653  struct singleEntry * findEntryByPath(str Line 2047  struct singleEntry * findEntryByPath(str
2047    
2048   if (!strncmp(kernel, "TITLE=", 6)) {   if (!strncmp(kernel, "TITLE=", 6)) {
2049      prefix = "";      prefix = "";
2050      checkType = LT_TITLE;      checkType = LT_TITLE|LT_MENUENTRY;
2051      kernel += 6;      kernel += 6;
2052   }   }
2053    
# Line 1664  struct singleEntry * findEntryByPath(str Line 2058  struct singleEntry * findEntryByPath(str
2058    
2059      /* check all the lines matching checkType */      /* check all the lines matching checkType */
2060      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
2061   line = getLineByType(entry->multiboot && checkType == LT_KERNEL ?   enum lineType_e ct = checkType;
2062       LT_KERNEL|LT_MBMODULE|LT_HYPER :   if (entry->multiboot && checkType == LT_KERNEL)
2063       checkType, line);      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER|LT_KERNEL_16;
2064   if (!line) break;  /* not found in this entry */   else if (checkType & LT_KERNEL)
2065        ct = checkType | LT_KERNEL_EFI | LT_KERNEL_16;
2066     line = getLineByType(ct, line);
2067     if (!line)
2068        break;  /* not found in this entry */
2069    
2070   if (line && line->numElements >= 2) {   if (line && line->type != LT_MENUENTRY &&
2071     line->numElements >= 2) {
2072      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
2073      if (!strcmp(line->elements[1].item +      if (!strcmp(line->elements[1].item +
2074   ((rootspec != NULL) ? strlen(rootspec) : 0),   ((rootspec != NULL) ? strlen(rootspec) : 0),
2075   kernel + strlen(prefix)))   kernel + strlen(prefix)))
2076   break;   break;
2077   }   }
2078     if(line->type == LT_MENUENTRY &&
2079     !strcmp(line->elements[1].item, kernel))
2080        break;
2081      }      }
2082    
2083      /* make sure this entry has a kernel identifier; this skips      /* make sure this entry has a kernel identifier; this skips
2084       * non-Linux boot entries (could find netbsd etc, though, which is       * non-Linux boot entries (could find netbsd etc, though, which is
2085       * unfortunate)       * unfortunate)
2086       */       */
2087      if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines))      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines))
2088   break; /* found 'im! */   break; /* found 'im! */
2089   }   }
2090    
# Line 1692  struct singleEntry * findEntryByPath(str Line 2094  struct singleEntry * findEntryByPath(str
2094      return entry;      return entry;
2095  }  }
2096    
2097    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2098          int * index) {
2099        struct singleEntry * entry;
2100        struct singleLine * line;
2101        int i;
2102        char * newtitle;
2103    
2104        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2105     if (index && i < *index)
2106        continue;
2107     line = getLineByType(LT_TITLE, entry->lines);
2108     if (!line)
2109        line = getLineByType(LT_MENUENTRY, entry->lines);
2110     if (!line)
2111        continue;
2112     newtitle = grub2ExtractTitle(line);
2113     if (!newtitle)
2114        continue;
2115     if (!strcmp(title, newtitle))
2116        break;
2117        }
2118    
2119        if (!entry)
2120     return NULL;
2121    
2122        if (index)
2123     *index = i;
2124        return entry;
2125    }
2126    
2127  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
2128      struct singleEntry * entry;      struct singleEntry * entry;
2129    
# Line 1714  struct singleEntry * findTemplate(struct Line 2146  struct singleEntry * findTemplate(struct
2146      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2147      int index;      int index;
2148    
2149      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2150     if (cfg->cfi->getEnv) {
2151        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2152        if (defTitle) {
2153     int index = 0;
2154     if (isnumber(defTitle)) {
2155        index = atoi(defTitle);
2156        entry = findEntryByIndex(cfg, index);
2157     } else {
2158        entry = findEntryByTitle(cfg, defTitle, &index);
2159     }
2160     if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2161        cfg->defaultImage = index;
2162        if (indexPtr)
2163     *indexPtr = index;
2164        return entry;
2165     }
2166        }
2167     }
2168        } else if (cfg->defaultImage > -1) {
2169   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2170   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2171      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 1767  void markRemovedImage(struct grubConfig Line 2218  void markRemovedImage(struct grubConfig
2218        const char * prefix) {        const char * prefix) {
2219      struct singleEntry * entry;      struct singleEntry * entry;
2220    
2221      if (!image) return;      if (!image)
2222     return;
2223    
2224        /* check and see if we're removing the default image */
2225        if (isdigit(*image)) {
2226     entry = findEntryByPath(cfg, image, prefix, NULL);
2227     if(entry)
2228        entry->skip = 1;
2229     return;
2230        }
2231    
2232      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
2233   entry->skip = 1;   entry->skip = 1;
# Line 1775  void markRemovedImage(struct grubConfig Line 2235  void markRemovedImage(struct grubConfig
2235    
2236  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
2237       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
2238       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
2239      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
2240      int i, j;      int i, j;
2241    
2242      if (newIsDefault) {      if (newIsDefault) {
2243   config->defaultImage = 0;   config->defaultImage = 0;
2244   return;   return;
2245        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
2246     if (findEntryByIndex(config, index))
2247        config->defaultImage = index;
2248     else
2249        config->defaultImage = -1;
2250     return;
2251      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
2252   i = 0;   i = 0;
2253   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1853  void displayEntry(struct singleEntry * e Line 2319  void displayEntry(struct singleEntry * e
2319      struct singleLine * line;      struct singleLine * line;
2320      char * root = NULL;      char * root = NULL;
2321      int i;      int i;
2322        int j;
2323    
2324      printf("index=%d\n", index);      printf("index=%d\n", index);
2325    
2326      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
2327      if (!line) {      if (!line) {
2328          printf("non linux entry\n");          printf("non linux entry\n");
2329          return;          return;
2330      }      }
2331    
2332      printf("kernel=%s%s\n", prefix, line->elements[1].item);      if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2333     printf("kernel=%s\n", line->elements[1].item);
2334        else
2335     printf("kernel=%s%s\n", prefix, line->elements[1].item);
2336    
2337      if (line->numElements >= 3) {      if (line->numElements >= 3) {
2338   printf("args=\"");   printf("args=\"");
# Line 1918  void displayEntry(struct singleEntry * e Line 2388  void displayEntry(struct singleEntry * e
2388   printf("root=%s\n", s);   printf("root=%s\n", s);
2389      }      }
2390    
2391      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
2392    
2393      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2394   printf("initrd=%s", prefix);   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2395        printf("initrd=");
2396     else
2397        printf("initrd=%s", prefix);
2398    
2399   for (i = 1; i < line->numElements; i++)   for (i = 1; i < line->numElements; i++)
2400      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2401   printf("\n");   printf("\n");
2402      }      }
2403    
2404        line = getLineByType(LT_TITLE, entry->lines);
2405        if (line) {
2406     printf("title=%s\n", line->elements[1].item);
2407        } else {
2408     char * title;
2409     line = getLineByType(LT_MENUENTRY, entry->lines);
2410     if (line) {
2411        title = grub2ExtractTitle(line);
2412        if (title)
2413     printf("title=%s\n", title);
2414     }
2415        }
2416    
2417        for (j = 0, line = entry->lines; line; line = line->next) {
2418     if ((line->type & LT_MBMODULE) && line->numElements >= 2) {
2419        if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2420     printf("mbmodule%d=", j);
2421        else
2422     printf("mbmodule%d=%s", j, prefix);
2423    
2424        for (i = 1; i < line->numElements; i++)
2425     printf("%s%s", line->elements[i].item, line->elements[i].indent);
2426        printf("\n");
2427        j++;
2428     }
2429        }
2430    }
2431    
2432    int isSuseSystem(void) {
2433        const char * path;
2434        const static char default_path[] = "/etc/SuSE-release";
2435    
2436        if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL)
2437     path = default_path;
2438    
2439        if (!access(path, R_OK))
2440     return 1;
2441        return 0;
2442    }
2443    
2444    int isSuseGrubConf(const char * path) {
2445        FILE * grubConf;
2446        char * line = NULL;
2447        size_t len = 0, res = 0;
2448    
2449        grubConf = fopen(path, "r");
2450        if (!grubConf) {
2451            dbgPrintf("Could not open SuSE configuration file '%s'\n", path);
2452     return 0;
2453        }
2454    
2455        while ((res = getline(&line, &len, grubConf)) != -1) {
2456     if (!strncmp(line, "setup", 5)) {
2457        fclose(grubConf);
2458        free(line);
2459        return 1;
2460     }
2461        }
2462    
2463        dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n",
2464          path);
2465    
2466        fclose(grubConf);
2467        free(line);
2468        return 0;
2469    }
2470    
2471    int suseGrubConfGetLba(const char * path, int * lbaPtr) {
2472        FILE * grubConf;
2473        char * line = NULL;
2474        size_t res = 0, len = 0;
2475    
2476        if (!path) return 1;
2477        if (!lbaPtr) return 1;
2478    
2479        grubConf = fopen(path, "r");
2480        if (!grubConf) return 1;
2481    
2482        while ((res = getline(&line, &len, grubConf)) != -1) {
2483     if (line[res - 1] == '\n')
2484        line[res - 1] = '\0';
2485     else if (len > res)
2486        line[res] = '\0';
2487     else {
2488        line = realloc(line, res + 1);
2489        line[res] = '\0';
2490     }
2491    
2492     if (!strncmp(line, "setup", 5)) {
2493        if (strstr(line, "--force-lba")) {
2494            *lbaPtr = 1;
2495        } else {
2496            *lbaPtr = 0;
2497        }
2498        dbgPrintf("lba: %i\n", *lbaPtr);
2499        break;
2500     }
2501        }
2502    
2503        free(line);
2504        fclose(grubConf);
2505        return 0;
2506    }
2507    
2508    int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) {
2509        FILE * grubConf;
2510        char * line = NULL;
2511        size_t res = 0, len = 0;
2512        char * lastParamPtr = NULL;
2513        char * secLastParamPtr = NULL;
2514        char installDeviceNumber = '\0';
2515        char * bounds = NULL;
2516    
2517        if (!path) return 1;
2518        if (!devicePtr) return 1;
2519    
2520        grubConf = fopen(path, "r");
2521        if (!grubConf) return 1;
2522    
2523        while ((res = getline(&line, &len, grubConf)) != -1) {
2524     if (strncmp(line, "setup", 5))
2525        continue;
2526    
2527     if (line[res - 1] == '\n')
2528        line[res - 1] = '\0';
2529     else if (len > res)
2530        line[res] = '\0';
2531     else {
2532        line = realloc(line, res + 1);
2533        line[res] = '\0';
2534     }
2535    
2536     lastParamPtr = bounds = line + res;
2537    
2538     /* Last parameter in grub may be an optional IMAGE_DEVICE */
2539     while (!isspace(*lastParamPtr))
2540        lastParamPtr--;
2541     lastParamPtr++;
2542    
2543     secLastParamPtr = lastParamPtr - 2;
2544     dbgPrintf("lastParamPtr: %s\n", lastParamPtr);
2545    
2546     if (lastParamPtr + 3 > bounds) {
2547        dbgPrintf("lastParamPtr going over boundary");
2548        fclose(grubConf);
2549        free(line);
2550        return 1;
2551     }
2552     if (!strncmp(lastParamPtr, "(hd", 3))
2553        lastParamPtr += 3;
2554     dbgPrintf("lastParamPtr: %c\n", *lastParamPtr);
2555    
2556     /*
2557     * Second last parameter will decide wether last parameter is
2558     * an IMAGE_DEVICE or INSTALL_DEVICE
2559     */
2560     while (!isspace(*secLastParamPtr))
2561        secLastParamPtr--;
2562     secLastParamPtr++;
2563    
2564     if (secLastParamPtr + 3 > bounds) {
2565        dbgPrintf("secLastParamPtr going over boundary");
2566        fclose(grubConf);
2567        free(line);
2568        return 1;
2569     }
2570     dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr);
2571     if (!strncmp(secLastParamPtr, "(hd", 3)) {
2572        secLastParamPtr += 3;
2573        dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr);
2574        installDeviceNumber = *secLastParamPtr;
2575     } else {
2576        installDeviceNumber = *lastParamPtr;
2577     }
2578    
2579     *devicePtr = malloc(6);
2580     snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber);
2581     dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber);
2582     fclose(grubConf);
2583     free(line);
2584     return 0;
2585        }
2586    
2587        free(line);
2588        fclose(grubConf);
2589        return 1;
2590    }
2591    
2592    int grubGetBootFromDeviceMap(const char * device,
2593         char ** bootPtr) {
2594        FILE * deviceMap;
2595        char * line = NULL;
2596        size_t res = 0, len = 0;
2597        char * devicePtr;
2598        char * bounds = NULL;
2599        const char * path;
2600        const static char default_path[] = "/boot/grub/device.map";
2601    
2602        if (!device) return 1;
2603        if (!bootPtr) return 1;
2604    
2605        if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL)
2606     path = default_path;
2607    
2608        dbgPrintf("opening grub device.map file from: %s\n", path);
2609        deviceMap = fopen(path, "r");
2610        if (!deviceMap)
2611     return 1;
2612    
2613        while ((res = getline(&line, &len, deviceMap)) != -1) {
2614            if (!strncmp(line, "#", 1))
2615        continue;
2616    
2617     if (line[res - 1] == '\n')
2618        line[res - 1] = '\0';
2619     else if (len > res)
2620        line[res] = '\0';
2621     else {
2622        line = realloc(line, res + 1);
2623        line[res] = '\0';
2624     }
2625    
2626     devicePtr = line;
2627     bounds = line + res;
2628    
2629     while ((isspace(*line) && ((devicePtr + 1) <= bounds)))
2630        devicePtr++;
2631     dbgPrintf("device: %s\n", devicePtr);
2632    
2633     if (!strncmp(devicePtr, device, strlen(device))) {
2634        devicePtr += strlen(device);
2635        while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds))
2636            devicePtr++;
2637    
2638        *bootPtr = strdup(devicePtr);
2639        break;
2640     }
2641        }
2642    
2643        free(line);
2644        fclose(deviceMap);
2645        return 0;
2646    }
2647    
2648    int suseGrubConfGetBoot(const char * path, char ** bootPtr) {
2649        char * grubDevice;
2650    
2651        if (suseGrubConfGetInstallDevice(path, &grubDevice))
2652     dbgPrintf("error looking for grub installation device\n");
2653        else
2654     dbgPrintf("grubby installation device: %s\n", grubDevice);
2655    
2656        if (grubGetBootFromDeviceMap(grubDevice, bootPtr))
2657     dbgPrintf("error looking for grub boot device\n");
2658        else
2659     dbgPrintf("grubby boot device: %s\n", *bootPtr);
2660    
2661        free(grubDevice);
2662        return 0;
2663    }
2664    
2665    int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) {
2666        /*
2667         * This SuSE grub configuration file at this location is not your average
2668         * grub configuration file, but instead the grub commands used to setup
2669         * grub on that system.
2670         */
2671        const char * path;
2672        const static char default_path[] = "/etc/grub.conf";
2673    
2674        if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL)
2675     path = default_path;
2676    
2677        if (!isSuseGrubConf(path)) return 1;
2678    
2679        if (lbaPtr) {
2680            *lbaPtr = 0;
2681            if (suseGrubConfGetLba(path, lbaPtr))
2682                return 1;
2683        }
2684    
2685        if (bootPtr) {
2686            *bootPtr = NULL;
2687            suseGrubConfGetBoot(path, bootPtr);
2688        }
2689    
2690        return 0;
2691  }  }
2692    
2693  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 1976  int parseSysconfigGrub(int * lbaPtr, cha Line 2738  int parseSysconfigGrub(int * lbaPtr, cha
2738  }  }
2739    
2740  void dumpSysconfigGrub(void) {  void dumpSysconfigGrub(void) {
2741      char * boot;      char * boot = NULL;
2742      int lba;      int lba;
2743    
2744      if (!parseSysconfigGrub(&lba, &boot)) {      if (isSuseSystem()) {
2745   if (lba) printf("lba\n");          if (parseSuseGrubConf(&lba, &boot)) {
2746   if (boot) printf("boot=%s\n", boot);      free(boot);
2747        return;
2748     }
2749        } else {
2750            if (parseSysconfigGrub(&lba, &boot)) {
2751        free(boot);
2752        return;
2753     }
2754        }
2755    
2756        if (lba) printf("lba\n");
2757        if (boot) {
2758     printf("boot=%s\n", boot);
2759     free(boot);
2760      }      }
2761  }  }
2762    
# Line 2030  struct singleLine * addLineTmpl(struct s Line 2805  struct singleLine * addLineTmpl(struct s
2805  {  {
2806      struct singleLine * newLine = lineDup(tmplLine);      struct singleLine * newLine = lineDup(tmplLine);
2807    
2808        if (isEfi && cfi == &grub2ConfigType) {
2809     enum lineType_e old = newLine->type;
2810     newLine->type = preferredLineType(newLine->type, cfi);
2811     if (old != newLine->type)
2812        newLine->elements[0].item = getKeyByType(newLine->type, cfi);
2813        }
2814    
2815      if (val) {      if (val) {
2816   /* override the inherited value with our own.   /* override the inherited value with our own.
2817   * 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 2039  struct singleLine * addLineTmpl(struct s Line 2821  struct singleLine * addLineTmpl(struct s
2821   insertElement(newLine, val, 1, cfi);   insertElement(newLine, val, 1, cfi);
2822    
2823   /* but try to keep the rootspec from the template... sigh */   /* but try to keep the rootspec from the template... sigh */
2824   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)) {
2825      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2826      if (rootspec != NULL) {      if (rootspec != NULL) {
2827   free(newLine->elements[1].item);   free(newLine->elements[1].item);
# Line 2076  struct singleLine *  addLine(struct sing Line 2858  struct singleLine *  addLine(struct sing
2858      /* 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
2859       * stack since it calls addLineTmpl which will make copies.       * stack since it calls addLineTmpl which will make copies.
2860       */       */
   
2861      if (type == LT_TITLE && cfi->titleBracketed) {      if (type == LT_TITLE && cfi->titleBracketed) {
2862   /* we're doing a bracketed title (zipl) */   /* we're doing a bracketed title (zipl) */
2863   tmpl.type = type;   tmpl.type = type;
# Line 2180  void removeLine(struct singleEntry * ent Line 2961  void removeLine(struct singleEntry * ent
2961      free(line);      free(line);
2962  }  }
2963    
 static int isquote(char q)  
 {  
     if (q == '\'' || q == '\"')  
  return 1;  
     return 0;  
 }  
   
2964  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2965  {  {
2966      struct singleLine newLine = {      struct singleLine newLine = {
# Line 2417  int updateActualImage(struct grubConfig Line 3191  int updateActualImage(struct grubConfig
3191      firstElement = 2;      firstElement = 2;
3192    
3193   } else {   } else {
3194      line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3195      if (!line) {      if (!line) {
3196   /* no LT_KERNEL or LT_MBMODULE in this entry? */   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3197   continue;   continue;
# Line 2573  int updateImage(struct grubConfig * cfg, Line 3347  int updateImage(struct grubConfig * cfg,
3347      return rc;      return rc;
3348  }  }
3349    
3350    int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
3351     const char * image, const char * prefix, const char * initrd,
3352     const char * title) {
3353        struct singleEntry * entry;
3354        struct singleLine * line, * kernelLine, *endLine = NULL;
3355        int index = 0;
3356    
3357        if (!image) return 0;
3358    
3359        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3360            kernelLine = getLineByType(LT_MBMODULE, entry->lines);
3361            if (!kernelLine) continue;
3362    
3363     /* if title is supplied, the entry's title must match it. */
3364     if (title) {
3365        char *linetitle;
3366    
3367        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3368        if (!line)
3369     continue;
3370    
3371        linetitle = extractTitle(cfg, line);
3372        if (!linetitle)
3373     continue;
3374        if (strcmp(title, linetitle)) {
3375     free(linetitle);
3376     continue;
3377        }
3378        free(linetitle);
3379     }
3380    
3381            if (prefix) {
3382                int prefixLen = strlen(prefix);
3383                if (!strncmp(initrd, prefix, prefixLen))
3384                    initrd += prefixLen;
3385            }
3386     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3387     if (endLine)
3388        removeLine(entry, endLine);
3389            line = addLine(entry, cfg->cfi, preferredLineType(LT_MBMODULE,cfg->cfi),
3390     kernelLine->indent, initrd);
3391            if (!line)
3392        return 1;
3393     if (endLine) {
3394        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3395                if (!line)
3396     return 1;
3397     }
3398    
3399            break;
3400        }
3401    
3402        return 0;
3403    }
3404    
3405  int updateInitrd(struct grubConfig * cfg, const char * image,  int updateInitrd(struct grubConfig * cfg, const char * image,
3406                   const char * prefix, const char * initrd) {                   const char * prefix, const char * initrd, const char * title) {
3407      struct singleEntry * entry;      struct singleEntry * entry;
3408      struct singleLine * line, * kernelLine, *endLine = NULL;      struct singleLine * line, * kernelLine, *endLine = NULL;
3409      int index = 0;      int index = 0;
# Line 2582  int updateInitrd(struct grubConfig * cfg Line 3411  int updateInitrd(struct grubConfig * cfg
3411      if (!image) return 0;      if (!image) return 0;
3412    
3413      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3414          kernelLine = getLineByType(LT_KERNEL, entry->lines);          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3415          if (!kernelLine) continue;          if (!kernelLine) continue;
3416    
3417          line = getLineByType(LT_INITRD, entry->lines);   /* if title is supplied, the entry's title must match it. */
3418     if (title) {
3419        char *linetitle;
3420    
3421        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3422        if (!line)
3423     continue;
3424    
3425        linetitle = extractTitle(cfg, line);
3426        if (!linetitle)
3427     continue;
3428        if (strcmp(title, linetitle)) {
3429     free(linetitle);
3430     continue;
3431        }
3432        free(linetitle);
3433     }
3434    
3435            line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
3436          if (line)          if (line)
3437              removeLine(entry, line);              removeLine(entry, line);
3438          if (prefix) {          if (prefix) {
# Line 2596  int updateInitrd(struct grubConfig * cfg Line 3443  int updateInitrd(struct grubConfig * cfg
3443   endLine = getLineByType(LT_ENTRY_END, entry->lines);   endLine = getLineByType(LT_ENTRY_END, entry->lines);
3444   if (endLine)   if (endLine)
3445      removeLine(entry, endLine);      removeLine(entry, endLine);
3446          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);   enum lineType_e lt;
3447     switch(kernelLine->type) {
3448        case LT_KERNEL:
3449            lt = LT_INITRD;
3450     break;
3451        case LT_KERNEL_EFI:
3452            lt = LT_INITRD_EFI;
3453     break;
3454        case LT_KERNEL_16:
3455            lt = LT_INITRD_16;
3456     break;
3457        default:
3458            lt = preferredLineType(LT_INITRD, cfg->cfi);
3459     }
3460            line = addLine(entry, cfg->cfi, lt, kernelLine->indent, initrd);
3461          if (!line)          if (!line)
3462      return 1;      return 1;
3463   if (endLine) {   if (endLine) {
# Line 2802  int checkForGrub(struct grubConfig * con Line 3663  int checkForGrub(struct grubConfig * con
3663      int fd;      int fd;
3664      unsigned char bootSect[512];      unsigned char bootSect[512];
3665      char * boot;      char * boot;
3666        int onSuse = isSuseSystem();
3667    
3668      if (parseSysconfigGrub(NULL, &boot))  
3669   return 0;      if (onSuse) {
3670     if (parseSuseGrubConf(NULL, &boot))
3671        return 0;
3672        } else {
3673     if (parseSysconfigGrub(NULL, &boot))
3674        return 0;
3675        }
3676    
3677      /* assume grub is not installed -- not an error condition */      /* assume grub is not installed -- not an error condition */
3678      if (!boot)      if (!boot)
# Line 2823  int checkForGrub(struct grubConfig * con Line 3691  int checkForGrub(struct grubConfig * con
3691      }      }
3692      close(fd);      close(fd);
3693    
3694        /* The more elaborate checks do not work on SuSE. The checks done
3695         * seem to be reasonble (at least for now), so just return success
3696         */
3697        if (onSuse)
3698     return 2;
3699    
3700      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3701  }  }
3702    
# Line 2856  int checkForExtLinux(struct grubConfig * Line 3730  int checkForExtLinux(struct grubConfig *
3730      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3731  }  }
3732    
3733    int checkForYaboot(struct grubConfig * config) {
3734        /*
3735         * This is a simplistic check that we consider good enough for own puporses
3736         *
3737         * If we were to properly check if yaboot is *installed* we'd need to:
3738         * 1) get the system boot device (LT_BOOT)
3739         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3740         *    the content on the boot device
3741         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3742         * 4) check again if binary and boot device contents match
3743         */
3744        if (!access("/etc/yaboot.conf", R_OK))
3745     return 2;
3746    
3747        return 1;
3748    }
3749    
3750    int checkForElilo(struct grubConfig * config) {
3751        if (!access("/etc/elilo.conf", R_OK))
3752     return 2;
3753    
3754        return 1;
3755    }
3756    
3757  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3758      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3759    
# Line 2870  static char * getRootSpecifier(char * st Line 3768  static char * getRootSpecifier(char * st
3768  static char * getInitrdVal(struct grubConfig * config,  static char * getInitrdVal(struct grubConfig * config,
3769     const char * prefix, struct singleLine *tmplLine,     const char * prefix, struct singleLine *tmplLine,
3770     const char * newKernelInitrd,     const char * newKernelInitrd,
3771     char ** extraInitrds, int extraInitrdCount)     const char ** extraInitrds, int extraInitrdCount)
3772  {  {
3773      char *initrdVal, *end;      char *initrdVal, *end;
3774      int i;      int i;
# Line 2915  static char * getInitrdVal(struct grubCo Line 3813  static char * getInitrdVal(struct grubCo
3813    
3814  int addNewKernel(struct grubConfig * config, struct singleEntry * template,  int addNewKernel(struct grubConfig * config, struct singleEntry * template,
3815           const char * prefix,           const char * prefix,
3816   char * newKernelPath, char * newKernelTitle,   const char * newKernelPath, const char * newKernelTitle,
3817   char * newKernelArgs, char * newKernelInitrd,   const char * newKernelArgs, const char * newKernelInitrd,
3818   char ** extraInitrds, int extraInitrdCount,   const char ** extraInitrds, int extraInitrdCount,
3819                   char * newMBKernel, char * newMBKernelArgs) {                   const char * newMBKernel, const char * newMBKernelArgs,
3820     const char * newDevTreePath) {
3821      struct singleEntry * new;      struct singleEntry * new;
3822      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;
3823      int needs;      int needs;
# Line 2959  int addNewKernel(struct grubConfig * con Line 3858  int addNewKernel(struct grubConfig * con
3858          needs |= NEED_MB;          needs |= NEED_MB;
3859          new->multiboot = 1;          new->multiboot = 1;
3860      }      }
3861        if (newDevTreePath && getKeywordByType(LT_DEVTREE, config->cfi))
3862     needs |= NEED_DEVTREE;
3863    
3864      if (template) {      if (template) {
3865   for (masterLine = template->lines;   for (masterLine = template->lines;
# Line 2972  int addNewKernel(struct grubConfig * con Line 3873  int addNewKernel(struct grubConfig * con
3873      while (*chptr && isspace(*chptr)) chptr++;      while (*chptr && isspace(*chptr)) chptr++;
3874      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3875    
3876      if (tmplLine->type == LT_KERNEL &&      if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) {
     tmplLine->numElements >= 2) {  
3877   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
3878      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
3879       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 3050  int addNewKernel(struct grubConfig * con Line 3950  int addNewKernel(struct grubConfig * con
3950      /* template is multi but new is not,      /* template is multi but new is not,
3951       * insert the kernel in the first module slot       * insert the kernel in the first module slot
3952       */       */
3953      tmplLine->type = LT_KERNEL;      tmplLine->type = preferredLineType(LT_KERNEL, config->cfi);
3954      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3955      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3956   strdup(getKeywordByType(LT_KERNEL, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3957     config->cfi)->key);
3958      newLine = addLineTmpl(new, tmplLine, newLine,      newLine = addLineTmpl(new, tmplLine, newLine,
3959    newKernelPath + strlen(prefix), config->cfi);    newKernelPath + strlen(prefix),
3960      config->cfi);
3961      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
3962   } else if (needs & NEED_INITRD) {   } else if (needs & NEED_INITRD) {
3963      char *initrdVal;      char *initrdVal;
3964      /* template is multi but new is not,      /* template is multi but new is not,
3965       * insert the initrd in the second module slot       * insert the initrd in the second module slot
3966       */       */
3967      tmplLine->type = LT_INITRD;      tmplLine->type = preferredLineType(LT_INITRD, config->cfi);
3968      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3969      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3970   strdup(getKeywordByType(LT_INITRD, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3971     config->cfi)->key);
3972      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3973      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3974      free(initrdVal);      free(initrdVal);
3975      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3976   }   }
3977    
3978      } else if (tmplLine->type == LT_INITRD &&      } else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) {
        tmplLine->numElements >= 2) {  
3979   if (needs & NEED_INITRD &&   if (needs & NEED_INITRD &&
3980      new->multiboot && !template->multiboot &&      new->multiboot && !template->multiboot &&
3981      config->cfi->mbInitRdIsModule) {      config->cfi->mbInitRdIsModule) {
# Line 3127  int addNewKernel(struct grubConfig * con Line 4029  int addNewKernel(struct grubConfig * con
4029      static const char *prefix = "'Loading ";      static const char *prefix = "'Loading ";
4030      if (tmplLine->numElements > 1 &&      if (tmplLine->numElements > 1 &&
4031      strstr(tmplLine->elements[1].item, prefix) &&      strstr(tmplLine->elements[1].item, prefix) &&
4032      masterLine->next && masterLine->next->type == LT_KERNEL) {      masterLine->next &&
4033        iskernel(masterLine->next->type)) {
4034   char *newTitle = malloc(strlen(prefix) +   char *newTitle = malloc(strlen(prefix) +
4035   strlen(newKernelTitle) + 2);   strlen(newKernelTitle) + 2);
4036    
# Line 3142  int addNewKernel(struct grubConfig * con Line 4045  int addNewKernel(struct grubConfig * con
4045   newLine = addLineTmpl(new, tmplLine, newLine, NULL,   newLine = addLineTmpl(new, tmplLine, newLine, NULL,
4046   config->cfi);   config->cfi);
4047      }      }
4048        } else if (tmplLine->type == LT_DEVTREE &&
4049           tmplLine->numElements == 2 && newDevTreePath) {
4050            newLine = addLineTmpl(new, tmplLine, newLine,
4051          newDevTreePath + strlen(prefix),
4052          config->cfi);
4053     needs &= ~NEED_DEVTREE;
4054        } else if (tmplLine->type == LT_ENTRY_END && needs & NEED_DEVTREE) {
4055     const char *ndtp = newDevTreePath;
4056     if (!strncmp(newDevTreePath, prefix, strlen(prefix)))
4057        ndtp += strlen(prefix);
4058     newLine = addLine(new, config->cfi, LT_DEVTREE,
4059      config->secondaryIndent,
4060      ndtp);
4061     needs &= ~NEED_DEVTREE;
4062     newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
4063      } else {      } else {
4064   /* pass through other lines from the template */   /* pass through other lines from the template */
4065   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
# Line 3154  int addNewKernel(struct grubConfig * con Line 4072  int addNewKernel(struct grubConfig * con
4072   */   */
4073   switch (config->cfi->entryStart) {   switch (config->cfi->entryStart) {
4074      case LT_KERNEL:      case LT_KERNEL:
4075        case LT_KERNEL_EFI:
4076        case LT_KERNEL_16:
4077   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
4078      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
4079   } else {   } else {
4080      newLine = addLine(new, config->cfi, LT_KERNEL,      newLine = addLine(new, config->cfi,
4081              preferredLineType(LT_KERNEL, config->cfi),
4082        config->primaryIndent,        config->primaryIndent,
4083        newKernelPath + strlen(prefix));        newKernelPath + strlen(prefix));
4084      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
# Line 3214  int addNewKernel(struct grubConfig * con Line 4135  int addNewKernel(struct grubConfig * con
4135   }   }
4136      }      }
4137    
4138        struct singleLine *endLine = NULL;
4139        endLine = getLineByType(LT_ENTRY_END, new->lines);
4140        if (endLine) {
4141        removeLine(new, endLine);
4142        needs |= NEED_END;
4143        }
4144    
4145      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
4146       * 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,
4147       * all the lines following the entryStart.       * all the lines following the entryStart.
# Line 3233  int addNewKernel(struct grubConfig * con Line 4161  int addNewKernel(struct grubConfig * con
4161      if (needs & NEED_KERNEL) {      if (needs & NEED_KERNEL) {
4162   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
4163    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
4164        config->cfi)) ?        config->cfi))
4165    LT_MBMODULE : LT_KERNEL,     ? LT_MBMODULE
4166     : preferredLineType(LT_KERNEL, config->cfi),
4167    config->secondaryIndent,    config->secondaryIndent,
4168    newKernelPath + strlen(prefix));    newKernelPath + strlen(prefix));
4169   needs &= ~NEED_KERNEL;   needs &= ~NEED_KERNEL;
# Line 3250  int addNewKernel(struct grubConfig * con Line 4179  int addNewKernel(struct grubConfig * con
4179   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);
4180   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
4181    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
4182        config->cfi)) ?        config->cfi))
4183    LT_MBMODULE : LT_INITRD,     ? LT_MBMODULE
4184       : preferredLineType(LT_INITRD, config->cfi),
4185    config->secondaryIndent,    config->secondaryIndent,
4186    initrdVal);    initrdVal);
4187   free(initrdVal);   free(initrdVal);
4188   needs &= ~NEED_INITRD;   needs &= ~NEED_INITRD;
4189      }      }
4190        if (needs & NEED_DEVTREE) {
4191     newLine = addLine(new, config->cfi, LT_DEVTREE,
4192      config->secondaryIndent,
4193      newDevTreePath);
4194     needs &= ~NEED_DEVTREE;
4195        }
4196    
4197        /* NEEDS_END must be last on bootloaders that need it... */
4198      if (needs & NEED_END) {      if (needs & NEED_END) {
4199   newLine = addLine(new, config->cfi, LT_ENTRY_END,   newLine = addLine(new, config->cfi, LT_ENTRY_END,
4200   config->secondaryIndent, NULL);   config->secondaryIndent, NULL);
# Line 3283  static void traceback(int signum) Line 4221  static void traceback(int signum)
4221      memset(array, '\0', sizeof (array));      memset(array, '\0', sizeof (array));
4222      size = backtrace(array, 40);      size = backtrace(array, 40);
4223    
4224      fprintf(stderr, "grubby recieved SIGSEGV!  Backtrace (%ld):\n",      fprintf(stderr, "grubby received SIGSEGV!  Backtrace (%ld):\n",
4225              (unsigned long)size);              (unsigned long)size);
4226      backtrace_symbols_fd(array, size, STDERR_FILENO);      backtrace_symbols_fd(array, size, STDERR_FILENO);
4227      exit(1);      exit(1);
# Line 3308  int main(int argc, const char ** argv) { Line 4246  int main(int argc, const char ** argv) {
4246      char * newKernelArgs = NULL;      char * newKernelArgs = NULL;
4247      char * newKernelInitrd = NULL;      char * newKernelInitrd = NULL;
4248      char * newKernelTitle = NULL;      char * newKernelTitle = NULL;
4249      char * newKernelVersion = NULL;      char * newDevTreePath = NULL;
4250      char * newMBKernel = NULL;      char * newMBKernel = NULL;
4251      char * newMBKernelArgs = NULL;      char * newMBKernelArgs = NULL;
4252      char * removeMBKernelArgs = NULL;      char * removeMBKernelArgs = NULL;
# Line 3318  int main(int argc, const char ** argv) { Line 4256  int main(int argc, const char ** argv) {
4256      char * removeArgs = NULL;      char * removeArgs = NULL;
4257      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4258      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4259        char * envPath = NULL;
4260      const char * chptr = NULL;      const char * chptr = NULL;
4261      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4262      struct grubConfig * config;      struct grubConfig * config;
# Line 3326  int main(int argc, const char ** argv) { Line 4265  int main(int argc, const char ** argv) {
4265      int displayDefault = 0;      int displayDefault = 0;
4266      int displayDefaultIndex = 0;      int displayDefaultIndex = 0;
4267      int displayDefaultTitle = 0;      int displayDefaultTitle = 0;
4268        int defaultIndex = -1;
4269      struct poptOption options[] = {      struct poptOption options[] = {
4270   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
4271      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 3343  int main(int argc, const char ** argv) { Line 4283  int main(int argc, const char ** argv) {
4283   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4284      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
4285      _("bootfs") },      _("bootfs") },
4286  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4287   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
4288      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
4289  #endif  #endif
4290   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
4291      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 3364  int main(int argc, const char ** argv) { Line 4304  int main(int argc, const char ** argv) {
4304      _("display the index of the default kernel") },      _("display the index of the default kernel") },
4305   { "default-title", 0, 0, &displayDefaultTitle, 0,   { "default-title", 0, 0, &displayDefaultTitle, 0,
4306      _("display the title of the default kernel") },      _("display the title of the default kernel") },
4307     { "devtree", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4308        _("device tree file for new stanza"), _("dtb-path") },
4309     { "devtreedir", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4310        _("device tree directory for new stanza"), _("dtb-path") },
4311   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4312      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4313     { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4314        _("force grub2 stanzas to use efi") },
4315     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4316        _("path for environment data"),
4317        _("path") },
4318   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4319      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4320   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3378  int main(int argc, const char ** argv) { Line 4327  int main(int argc, const char ** argv) {
4327   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4328      _("initrd image for the new kernel"), _("initrd-path") },      _("initrd image for the new kernel"), _("initrd-path") },
4329   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4330      _("auxilliary initrd image for things other than the new kernel"), _("initrd-path") },      _("auxiliary initrd image for things other than the new kernel"), _("initrd-path") },
4331   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4332      _("configure lilo bootloader") },      _("configure lilo bootloader") },
4333   { "make-default", 0, 0, &makeDefault, 0,   { "make-default", 0, 0, &makeDefault, 0,
# Line 3398  int main(int argc, const char ** argv) { Line 4347  int main(int argc, const char ** argv) {
4347   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
4348      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
4349        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
4350     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4351        _("make the given entry index the default entry"),
4352        _("entry-index") },
4353   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4354      _("configure silo bootloader") },      _("configure silo bootloader") },
4355   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 3419  int main(int argc, const char ** argv) { Line 4371  int main(int argc, const char ** argv) {
4371    
4372      signal(SIGSEGV, traceback);      signal(SIGSEGV, traceback);
4373    
4374        int i = 0;
4375        for (int j = 1; j < argc; j++)
4376     i += strlen(argv[j]) + 1;
4377        saved_command_line = malloc(i);
4378        if (!saved_command_line) {
4379     fprintf(stderr, "grubby: %m\n");
4380     exit(1);
4381        }
4382        saved_command_line[0] = '\0';
4383        for (int j = 1; j < argc; j++) {
4384     strcat(saved_command_line, argv[j]);
4385     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4386        }
4387    
4388      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4389      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
4390    
# Line 3462  int main(int argc, const char ** argv) { Line 4428  int main(int argc, const char ** argv) {
4428   return 1;   return 1;
4429      } else if (configureGrub2) {      } else if (configureGrub2) {
4430   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4431     if (envPath)
4432        cfi->envFile = envPath;
4433      } else if (configureLilo) {      } else if (configureLilo) {
4434   cfi = &liloConfigType;   cfi = &liloConfigType;
4435      } else if (configureGrub) {      } else if (configureGrub) {
# Line 3480  int main(int argc, const char ** argv) { Line 4448  int main(int argc, const char ** argv) {
4448      }      }
4449    
4450      if (!cfi) {      if (!cfi) {
4451            if (grub2FindConfig(&grub2ConfigType)) {
4452        cfi = &grub2ConfigType;
4453        if (envPath)
4454     cfi->envFile = envPath;
4455            } else
4456        #ifdef __ia64__        #ifdef __ia64__
4457   cfi = &eliloConfigType;      cfi = &eliloConfigType;
4458        #elif __powerpc__        #elif __powerpc__
4459   cfi = &yabootConfigType;      cfi = &yabootConfigType;
4460        #elif __sparc__        #elif __sparc__
4461          cfi = &siloConfigType;              cfi = &siloConfigType;
4462        #elif __s390__        #elif __s390__
4463          cfi = &ziplConfigType;              cfi = &ziplConfigType;
4464        #elif __s390x__        #elif __s390x__
4465          cfi = &ziplConfigtype;              cfi = &ziplConfigtype;
4466        #else        #else
         if (grub2FindConfig(&grub2ConfigType))  
     cfi = &grub2ConfigType;  
  else  
4467      cfi = &grubConfigType;      cfi = &grubConfigType;
4468        #endif        #endif
4469      }      }
# Line 3505  int main(int argc, const char ** argv) { Line 4475  int main(int argc, const char ** argv) {
4475      grubConfig = cfi->defaultConfig;      grubConfig = cfi->defaultConfig;
4476      }      }
4477    
4478      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo ||
4479    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
4480    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
4481        (defaultIndex >= 0))) {
4482   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
4483    "specified option"));    "specified option"));
4484   return 1;   return 1;
4485      }      }
4486    
4487      if ((displayDefault || kernelInfo) && (newKernelVersion || newKernelPath ||      if ((displayDefault || kernelInfo) && (newKernelPath ||
4488     removeKernelPath)) {     removeKernelPath)) {
4489   fprintf(stderr, _("grubby: --default-kernel and --info may not "   fprintf(stderr, _("grubby: --default-kernel and --info may not "
4490    "be used when adding or removing kernels\n"));    "be used when adding or removing kernels\n"));
# Line 3523  int main(int argc, const char ** argv) { Line 4494  int main(int argc, const char ** argv) {
4494      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
4495   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
4496   return 1;   return 1;
4497      } else if (!newKernelPath && (newKernelTitle  || copyDefault ||      } else if (!newKernelPath && (copyDefault ||
4498    (newKernelInitrd && !updateKernelPath)||    (newKernelInitrd && !updateKernelPath)||
4499    makeDefault || extraInitrdCount > 0)) {    makeDefault || extraInitrdCount > 0)) {
4500   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
# Line 3549  int main(int argc, const char ** argv) { Line 4520  int main(int argc, const char ** argv) {
4520   makeDefault = 1;   makeDefault = 1;
4521   defaultKernel = NULL;   defaultKernel = NULL;
4522      }      }
4523        else if (defaultKernel && (defaultIndex >= 0)) {
4524     fprintf(stderr, _("grubby: --set-default and --set-default-index "
4525      "may not be used together\n"));
4526     return 1;
4527        }
4528    
4529      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
4530   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
# Line 3557  int main(int argc, const char ** argv) { Line 4533  int main(int argc, const char ** argv) {
4533      }      }
4534    
4535      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
4536   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
4537          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
4538     && (defaultIndex == -1)) {
4539   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
4540   return 1;   return 1;
4541      }      }
# Line 3585  int main(int argc, const char ** argv) { Line 4562  int main(int argc, const char ** argv) {
4562      }      }
4563    
4564      if (bootloaderProbe) {      if (bootloaderProbe) {
4565   int lrc = 0, grc = 0, gr2c = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4566   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4567    
4568   const char *grub2config = grub2FindConfig(&grub2ConfigType);   const char *grub2config = grub2FindConfig(&grub2ConfigType);
4569   if (grub2config) {   if (grub2config) {
# Line 3614  int main(int argc, const char ** argv) { Line 4591  int main(int argc, const char ** argv) {
4591   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4592   }   }
4593    
4594     if (!access(eliloConfigType.defaultConfig, F_OK)) {
4595        econfig = readConfig(eliloConfigType.defaultConfig,
4596     &eliloConfigType);
4597        if (!econfig)
4598     erc = 1;
4599        else
4600     erc = checkForElilo(econfig);
4601     }
4602    
4603   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4604      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4605      if (!lconfig)      if (!lconfig)
4606   erc = 1;   extrc = 1;
4607      else      else
4608   erc = checkForExtLinux(lconfig);   extrc = checkForExtLinux(lconfig);
4609   }   }
4610    
4611   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;  
4612     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4613        yconfig = readConfig(yabootConfigType.defaultConfig,
4614     &yabootConfigType);
4615        if (!yconfig)
4616     yrc = 1;
4617        else
4618     yrc = checkForYaboot(yconfig);
4619     }
4620    
4621     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4622     erc == 1)
4623        return 1;
4624    
4625   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4626   if (gr2c == 2) printf("grub2\n");   if (gr2c == 2) printf("grub2\n");
4627   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4628   if (erc == 2) printf("extlinux\n");   if (extrc == 2) printf("extlinux\n");
4629     if (yrc == 2) printf("yaboot\n");
4630     if (erc == 2) printf("elilo\n");
4631    
4632   return 0;   return 0;
4633      }      }
4634    
4635        if (grubConfig == NULL) {
4636     printf("Could not find bootloader configuration file.\n");
4637     exit(1);
4638        }
4639    
4640      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4641      if (!config) return 1;      if (!config) return 1;
4642    
# Line 3641  int main(int argc, const char ** argv) { Line 4646  int main(int argc, const char ** argv) {
4646          char * rootspec;          char * rootspec;
4647    
4648   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4649     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4650     cfi->defaultIsSaved)
4651        config->defaultImage = 0;
4652   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4653   if (!entry) return 0;   if (!entry) return 0;
4654   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4655    
4656   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
4657   if (!line) return 0;   if (!line) return 0;
4658    
4659          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 3659  int main(int argc, const char ** argv) { Line 4667  int main(int argc, const char ** argv) {
4667   struct singleEntry * entry;   struct singleEntry * entry;
4668    
4669   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4670     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4671     cfi->defaultIsSaved)
4672        config->defaultImage = 0;
4673   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4674   if (!entry) return 0;   if (!entry) return 0;
4675    
# Line 3681  int main(int argc, const char ** argv) { Line 4692  int main(int argc, const char ** argv) {
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 3694  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                       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.1748  
changed lines
  Added in v.2989