Magellan Linux

Diff of /trunk/grubby/grubby.c

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

trunk/mkinitrd-magellan/grubby/grubby.c revision 914 by niro, Wed Oct 28 00:16:16 2009 UTC trunk/grubby/grubby.c revision 2983 by niro, Thu Jun 30 10:27:31 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
42  #define DEBUG 0  #define DEBUG 0
43    #endif
44    
45  #if DEBUG  #if DEBUG
46  #define dbgPrintf(format, args...) fprintf(stderr, format , ## args)  #define dbgPrintf(format, args...) fprintf(stderr, format , ## args)
# Line 44  Line 48 
48  #define dbgPrintf(format, args...)  #define dbgPrintf(format, args...)
49  #endif  #endif
50    
51    int debug = 0; /* Currently just for template debugging */
52    
53  #define _(A) (A)  #define _(A) (A)
54    
55  #define MAX_EXTRA_INITRDS  16 /* code segment checked by --bootloader-probe */  #define MAX_EXTRA_INITRDS  16 /* code segment checked by --bootloader-probe */
56  #define CODE_SEG_SIZE  128 /* code segment checked by --bootloader-probe */  #define CODE_SEG_SIZE  128 /* code segment checked by --bootloader-probe */
57    
58    #define NOOP_OPCODE 0x90
59    #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 56  struct lineElement { Line 75  struct lineElement {
75  };  };
76    
77  enum lineType_e {  enum lineType_e {
78      LT_WHITESPACE = 1 << 0,      LT_WHITESPACE   = 1 << 0,
79      LT_TITLE      = 1 << 1,      LT_TITLE        = 1 << 1,
80      LT_KERNEL     = 1 << 2,      LT_KERNEL       = 1 << 2,
81      LT_INITRD     = 1 << 3,      LT_INITRD       = 1 << 3,
82      LT_HYPER      = 1 << 4,      LT_HYPER        = 1 << 4,
83      LT_DEFAULT    = 1 << 5,      LT_DEFAULT      = 1 << 5,
84      LT_MBMODULE   = 1 << 6,      LT_MBMODULE     = 1 << 6,
85      LT_ROOT       = 1 << 7,      LT_ROOT         = 1 << 7,
86      LT_FALLBACK   = 1 << 8,      LT_FALLBACK     = 1 << 8,
87      LT_KERNELARGS = 1 << 9,      LT_KERNELARGS   = 1 << 9,
88      LT_BOOT       = 1 << 10,      LT_BOOT         = 1 << 10,
89      LT_BOOTROOT   = 1 << 11,      LT_BOOTROOT     = 1 << 11,
90      LT_LBA        = 1 << 12,      LT_LBA          = 1 << 12,
91      LT_OTHER      = 1 << 13,      LT_OTHER        = 1 << 13,
92      LT_GENERIC    = 1 << 14,      LT_GENERIC      = 1 << 14,
93      LT_UNKNOWN    = 1 << 15,      LT_ECHO    = 1 << 16,
94        LT_MENUENTRY    = 1 << 17,
95        LT_ENTRY_END    = 1 << 18,
96        LT_SET_VARIABLE = 1 << 19,
97        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 99  struct singleEntry { Line 127  struct singleEntry {
127  #define NEED_TITLE   (1 << 2)  #define NEED_TITLE   (1 << 2)
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)
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
135    #define DEFAULT_SAVED_GRUB2 -3
136    
137  struct keywordTypes {  struct keywordTypes {
138      char * key;      char * key;
# Line 110  struct keywordTypes { Line 141  struct keywordTypes {
141      char separatorChar;      char separatorChar;
142  };  };
143    
144    struct configFileInfo;
145    
146    typedef const char *(*findConfigFunc)(struct configFileInfo *);
147    typedef const int (*writeLineFunc)(struct configFileInfo *,
148     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;
155        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;
162      int defaultSupportSaved;      int defaultSupportSaved;
163      enum lineType_e entrySeparator;      int defaultIsSaved;
164        int defaultIsUnquoted;
165        enum lineType_e entryStart;
166        enum lineType_e entryEnd;
167      int needsBootPrefix;      int needsBootPrefix;
168      int argsInQuotes;      int argsInQuotes;
169      int maxTitleLength;      int maxTitleLength;
170      int titleBracketed;      int titleBracketed;
171        int titlePosition;
172      int mbHyperFirst;      int mbHyperFirst;
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 138  struct keywordTypes grubKeywords[] = { Line 188  struct keywordTypes grubKeywords[] = {
188      { NULL,    0, 0 },      { NULL,    0, 0 },
189  };  };
190    
191    const char *grubFindConfig(struct configFileInfo *cfi) {
192        static const char *configFiles[] = {
193     "/boot/grub/grub.conf",
194     "/boot/grub/menu.lst",
195     "/etc/grub.conf",
196     "/boot/grub2/grub.cfg",
197     "/boot/grub2-efi/grub.cfg",
198     NULL
199        };
200        static int i = -1;
201    
202        if (i == -1) {
203     for (i = 0; configFiles[i] != NULL; i++) {
204        dbgPrintf("Checking \"%s\": ", configFiles[i]);
205        if (!access(configFiles[i], R_OK)) {
206     dbgPrintf("found\n");
207     return configFiles[i];
208        }
209        dbgPrintf("not found\n");
210     }
211        }
212        return configFiles[i];
213    }
214    
215  struct configFileInfo grubConfigType = {  struct configFileInfo grubConfigType = {
216      "/etc/grub.conf",    /* defaultConfig */      .findConfig = grubFindConfig,
217      grubKeywords,    /* keywords */      .keywords = grubKeywords,
218      1,    /* defaultIsIndex */      .defaultIsIndex = 1,
219      1,    /* defaultSupportSaved */      .defaultSupportSaved = 1,
220      LT_TITLE,    /* entrySeparator */      .entryStart = LT_TITLE,
221      1,    /* needsBootPrefix */      .needsBootPrefix = 1,
222      0,    /* argsInQuotes */      .mbHyperFirst = 1,
223      0,    /* maxTitleLength */      .mbInitRdIsModule = 1,
224      0,                                      /* titleBracketed */      .mbAllowExtraInitRds = 1,
225      1,                                      /* mbHyperFirst */      .titlePosition = 1,
226      1,                                      /* mbInitRdIsModule */  };
227      0,                                      /* mbConcatArgs */  
228      1,                                      /* mbAllowExtraInitRds */  struct keywordTypes grub2Keywords[] = {
229        { "menuentry",  LT_MENUENTRY,   ' ' },
230        { "}",          LT_ENTRY_END,   ' ' },
231        { "echo",       LT_ECHO,        ' ' },
232        { "set",        LT_SET_VARIABLE,' ', '=' },
233        { "root",       LT_BOOTROOT,    ' ' },
234        { "default",    LT_DEFAULT,     ' ' },
235        { "fallback",   LT_FALLBACK,    ' ' },
236        { "linux",      LT_KERNEL,      ' ' },
237        { "linuxefi",   LT_KERNEL_EFI,  ' ' },
238        { "linux16",    LT_KERNEL_16,   ' ' },
239        { "initrd",     LT_INITRD,      ' ', ' ' },
240        { "initrdefi",  LT_INITRD_EFI,  ' ', ' ' },
241        { "initrd16",   LT_INITRD_16,   ' ', ' ' },
242        { "module",     LT_MBMODULE,    ' ' },
243        { "kernel",     LT_HYPER,       ' ' },
244        { "devicetree", LT_DEVTREE,  ' ' },
245        { NULL, 0, 0 },
246    };
247    
248    const char *grub2FindConfig(struct configFileInfo *cfi) {
249        static const char *configFiles[] = {
250     "/boot/grub/grub-efi.cfg",
251     "/boot/grub/grub.cfg",
252     NULL
253        };
254        static int i = -1;
255        static const char *grub_cfg = "/boot/grub/grub.cfg";
256        int rc = -1;
257    
258        if (i == -1) {
259     for (i = 0; configFiles[i] != NULL; i++) {
260        dbgPrintf("Checking \"%s\": ", configFiles[i]);
261        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");
270     return configFiles[i];
271        }
272     }
273        }
274    
275        /* Ubuntu renames grub2 to grub, so check for the grub.d directory
276         * that isn't in grub1, and if it exists, return the config file path
277         * that they use. */
278        if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) {
279     dbgPrintf("found\n");
280     return grub_cfg;
281        }
282    
283        dbgPrintf("not found\n");
284        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) {
387      int count = 0;
388    
389      for (int i = 0; i < line->numElements; i++) {
390        int indentSize = 0;
391    
392        count = count + strlen(line->elements[i].item);
393    
394        indentSize = strlen(line->elements[i].indent);
395        if (indentSize > 0)
396          count = count + indentSize;
397        else
398          /* be extra safe and add room for whitespaces */
399          count = count + 1;
400      }
401    
402      /* room for trailing terminator */
403      count = count + 1;
404    
405      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) {
424        char * current;
425        char * current_indent;
426        int current_len;
427        int current_indent_len;
428        int i;
429    
430        /* bail out if line does not start with menuentry */
431        if (strcmp(line->elements[0].item, "menuentry"))
432          return NULL;
433    
434        i = 1;
435        current = line->elements[i].item;
436        current_len = strlen(current);
437    
438        /* if second word is quoted, strip the quotes and return single word */
439        if (isquote(*current) && isquote(current[current_len - 1])) {
440     char *tmp;
441    
442     tmp = strdup(current);
443     *(tmp + current_len - 1) = '\0';
444     return ++tmp;
445        }
446    
447        /* if no quotes, return second word verbatim */
448        if (!isquote(*current))
449     return current;
450    
451        /* 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) */
453        int resultMaxSize;
454        char * result;
455        
456        resultMaxSize = sizeOfSingleLine(line);
457        result = malloc(resultMaxSize);
458        snprintf(result, resultMaxSize, "%s", ++current);
459        
460        i++;
461        for (; i < line->numElements; ++i) {
462     current = line->elements[i].item;
463     current_len = strlen(current);
464     current_indent = line->elements[i].indent;
465     current_indent_len = strlen(current_indent);
466    
467     strncat(result, current_indent, current_indent_len);
468     if (!isquote(current[current_len-1])) {
469        strncat(result, current, current_len);
470     } else {
471        strncat(result, current, current_len - 1);
472        break;
473     }
474        }
475        return result;
476    }
477    
478    struct configFileInfo grub2ConfigType = {
479        .findConfig = grub2FindConfig,
480        .getEnv = grub2GetEnv,
481        .setEnv = grub2SetEnv,
482        .keywords = grub2Keywords,
483        .defaultIsIndex = 1,
484        .defaultSupportSaved = 1,
485        .defaultIsVariable = 1,
486        .entryStart = LT_MENUENTRY,
487        .entryEnd = LT_ENTRY_END,
488        .titlePosition = 1,
489        .needsBootPrefix = 1,
490        .mbHyperFirst = 1,
491        .mbInitRdIsModule = 1,
492        .mbAllowExtraInitRds = 1,
493  };  };
494    
495  struct keywordTypes yabootKeywords[] = {  struct keywordTypes yabootKeywords[] = {
# Line 245  struct keywordTypes extlinuxKeywords[] = Line 583  struct keywordTypes extlinuxKeywords[] =
583      { "initrd",    LT_INITRD,      ' ', ',' },      { "initrd",    LT_INITRD,      ' ', ',' },
584      { "append",    LT_KERNELARGS,  ' ' },      { "append",    LT_KERNELARGS,  ' ' },
585      { "prompt",     LT_UNKNOWN,     ' ' },      { "prompt",     LT_UNKNOWN,     ' ' },
586        { "fdt",        LT_DEVTREE,     ' ' },
587        { "fdtdir",     LT_DEVTREE,     ' ' },
588      { NULL,    0, 0 },      { NULL,    0, 0 },
589  };  };
590  int useextlinuxmenu;  int useextlinuxmenu;
591  struct configFileInfo eliloConfigType = {  struct configFileInfo eliloConfigType = {
592      "/boot/efi/EFI/redhat/elilo.conf",    /* defaultConfig */      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",
593      eliloKeywords,    /* keywords */      .keywords = eliloKeywords,
594      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
595      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
596      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
597      1,                    /* needsBootPrefix */      .mbConcatArgs = 1,
598      1,    /* argsInQuotes */      .titlePosition = 1,
     0,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     1,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
599  };  };
600    
601  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
602      "/etc/lilo.conf",    /* defaultConfig */      .defaultConfig = "/etc/lilo.conf",
603      liloKeywords,    /* keywords */      .keywords = liloKeywords,
604      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
605      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
606      LT_KERNEL,    /* entrySeparator */      .maxTitleLength = 15,
607      0,    /* needsBootPrefix */      .titlePosition = 1,
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
608  };  };
609    
610  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
611      "/etc/yaboot.conf",    /* defaultConfig */      .defaultConfig = "/etc/yaboot.conf",
612      yabootKeywords,    /* keywords */      .keywords = yabootKeywords,
613      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
614      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
615      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
616      1,    /* needsBootPrefix */      .maxTitleLength = 15,
617      1,    /* argsInQuotes */      .mbAllowExtraInitRds = 1,
618      15,    /* maxTitleLength */      .titlePosition = 1,
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
619  };  };
620    
621  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
622      "/etc/silo.conf",    /* defaultConfig */      .defaultConfig = "/etc/silo.conf",
623      siloKeywords,    /* keywords */      .keywords = siloKeywords,
624      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
625      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
626      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
627      1,    /* needsBootPrefix */      .maxTitleLength = 15,
628      1,    /* argsInQuotes */      .titlePosition = 1,
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
629  };  };
630    
631  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
632      "/etc/zipl.conf",    /* defaultConfig */      .defaultConfig = "/etc/zipl.conf",
633      ziplKeywords,    /* keywords */      .keywords = ziplKeywords,
634      0,    /* defaultIsIndex */      .entryStart = LT_TITLE,
635      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
636      LT_TITLE,    /* entrySeparator */      .titleBracketed = 1,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     1,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
637  };  };
638    
639  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
640      "/boot/extlinux/extlinux.conf",         /* defaultConfig */      .defaultConfig = "/boot/extlinux/extlinux.conf",
641      extlinuxKeywords,                       /* keywords */      .keywords = extlinuxKeywords,
642      0,                                      /* defaultIsIndex */      .caseInsensitive = 1,
643      0,                                      /* defaultSupportSaved */      .entryStart = LT_TITLE,
644      LT_TITLE,                               /* entrySeparator */      .needsBootPrefix = 1,
645      1,                                      /* needsBootPrefix */      .maxTitleLength = 255,
646      0,                                      /* argsInQuotes */      .mbAllowExtraInitRds = 1,
647      255,                                    /* maxTitleLength */      .defaultIsUnquoted = 1,
648      0,                                      /* titleBracketed */      .titlePosition = 1,
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
649  };  };
650    
651  struct grubConfig {  struct grubConfig {
# Line 356  struct grubConfig { Line 660  struct grubConfig {
660      struct configFileInfo * cfi;      struct configFileInfo * cfi;
661  };  };
662    
663    blkid_cache blkid;
664    
665  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);
666  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
667       const char * path, const char * prefix,       const char * path, const char * prefix,
668       int * index);       int * index);
669    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
670          int * index);
671  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
672  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
673  struct singleLine * lineDup(struct singleLine * line);  struct singleLine * lineDup(struct singleLine * line);
# Line 369  static int lineWrite(FILE * out, struct Line 677  static int lineWrite(FILE * out, struct
677  static int getNextLine(char ** bufPtr, struct singleLine * line,  static int getNextLine(char ** bufPtr, struct singleLine * line,
678         struct configFileInfo * cfi);         struct configFileInfo * cfi);
679  static char * getRootSpecifier(char * str);  static char * getRootSpecifier(char * str);
680    static void requote(struct singleLine *line, struct configFileInfo * cfi);
681  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
682    const char * item, int insertHere,    const char * item, int insertHere,
683    struct configFileInfo * cfi);    struct configFileInfo * cfi);
# Line 423  static char * sdupprintf(const char *for Line 732  static char * sdupprintf(const char *for
732      return buf;      return buf;
733  }  }
734    
735    static enum lineType_e preferredLineType(enum lineType_e type,
736     struct configFileInfo *cfi) {
737        if (isEfi && cfi == &grub2ConfigType) {
738     switch (type) {
739     case LT_KERNEL:
740        return isEfiOnly ? LT_KERNEL : LT_KERNEL_EFI;
741     case LT_INITRD:
742        return isEfiOnly ? LT_INITRD : LT_INITRD_EFI;
743     default:
744        return type;
745     }
746    #if defined(__i386__) || defined(__x86_64__)
747        } else if (cfi == &grub2ConfigType) {
748     switch (type) {
749     case LT_KERNEL:
750        return LT_KERNEL_16;
751     case LT_INITRD:
752        return LT_INITRD_16;
753     default:
754        return type;
755     }
756    #endif
757        }
758        return type;
759    }
760    
761  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
762        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
763      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
     for (kw = cfi->keywords; kw->key; kw++) {  
764   if (kw->type == type)   if (kw->type == type)
765      return kw;      return kw;
766      }      }
767      return NULL;      return NULL;
768  }  }
769    
770  static char * getpathbyspec(char *device) {  static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) {
771      static blkid_cache blkid;      struct keywordTypes *kt = getKeywordByType(type, cfi);
772        if (kt)
773     return kt->key;
774        return "unknown";
775    }
776    
777    static char * getpathbyspec(char *device) {
778      if (!blkid)      if (!blkid)
779          blkid_get_cache(&blkid, NULL);          blkid_get_cache(&blkid, NULL);
780    
781      return blkid_get_devname(blkid, device, NULL);      return blkid_get_devname(blkid, device, NULL);
782  }  }
783    
784    static char * getuuidbydev(char *device) {
785        if (!blkid)
786     blkid_get_cache(&blkid, NULL);
787    
788        return blkid_get_tag_value(blkid, "UUID", device);
789    }
790    
791  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
792   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
793      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
794      for (kw = cfi->keywords; kw->key; kw++) {   if (cfi->caseInsensitive) {
795   if (!strcmp(keyword, kw->key))      if (!strcasecmp(keyword, kw->key))
796      return kw->type;                  return kw->type;
797     } else {
798        if (!strcmp(keyword, kw->key))
799            return kw->type;
800     }
801      }      }
802      return LT_UNKNOWN;      return LT_UNKNOWN;
803  }  }
# Line 477  static int isBracketedTitle(struct singl Line 827  static int isBracketedTitle(struct singl
827      return 0;      return 0;
828  }  }
829    
830  static int isEntrySeparator(struct singleLine * line,  static int isEntryStart(struct singleLine * line,
831                              struct configFileInfo * cfi) {                              struct configFileInfo * cfi) {
832      return line->type == cfi->entrySeparator || line->type == LT_OTHER ||      return line->type == cfi->entryStart || line->type == LT_OTHER ||
833   (cfi->titleBracketed && isBracketedTitle(line));   (cfi->titleBracketed && isBracketedTitle(line));
834  }  }
835    
836  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
837  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct grubConfig *cfg, struct singleLine * line) {
838      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it */
839      char * title;      char * title = NULL;
840      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
841      title++;   char *tmp = line->elements[cfg->cfi->titlePosition].item;
842      *(title + strlen(title) - 1) = '\0';   if (cfg->cfi->titleBracketed) {
843        tmp++;
844        title = strdup(tmp);
845        *(title + strlen(title) - 1) = '\0';
846     } else {
847        title = strdup(tmp);
848     }
849        } else if (line->type == LT_MENUENTRY)
850     title = strdup(line->elements[1].item);
851        else
852     return NULL;
853      return title;      return title;
854  }  }
855    
# Line 532  static void lineInit(struct singleLine * Line 892  static void lineInit(struct singleLine *
892  }  }
893    
894  struct singleLine * lineDup(struct singleLine * line) {  struct singleLine * lineDup(struct singleLine * line) {
     int i;  
895      struct singleLine * newLine = malloc(sizeof(*newLine));      struct singleLine * newLine = malloc(sizeof(*newLine));
896    
897      newLine->indent = strdup(line->indent);      newLine->indent = strdup(line->indent);
# Line 542  struct singleLine * lineDup(struct singl Line 901  struct singleLine * lineDup(struct singl
901      newLine->elements = malloc(sizeof(*newLine->elements) *      newLine->elements = malloc(sizeof(*newLine->elements) *
902         newLine->numElements);         newLine->numElements);
903    
904      for (i = 0; i < newLine->numElements; i++) {      for (int i = 0; i < newLine->numElements; i++) {
905   newLine->elements[i].indent = strdup(line->elements[i].indent);   newLine->elements[i].indent = strdup(line->elements[i].indent);
906   newLine->elements[i].item = strdup(line->elements[i].item);   newLine->elements[i].item = strdup(line->elements[i].item);
907      }      }
# Line 551  struct singleLine * lineDup(struct singl Line 910  struct singleLine * lineDup(struct singl
910  }  }
911    
912  static void lineFree(struct singleLine * line) {  static void lineFree(struct singleLine * line) {
     int i;  
   
913      if (line->indent) free(line->indent);      if (line->indent) free(line->indent);
914    
915      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
916   free(line->elements[i].item);   free(line->elements[i].item);
917   free(line->elements[i].indent);   free(line->elements[i].indent);
918      }      }
# Line 566  static void lineFree(struct singleLine * Line 923  static void lineFree(struct singleLine *
923    
924  static int lineWrite(FILE * out, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine * line,
925       struct configFileInfo * cfi) {       struct configFileInfo * cfi) {
     int i;  
   
926      if (fprintf(out, "%s", line->indent) == -1) return -1;      if (fprintf(out, "%s", line->indent) == -1) return -1;
927    
928      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
929     /* Need to handle this, because we strip the quotes from
930     * menuentry when read it. */
931     if (line->type == LT_MENUENTRY && i == 1) {
932        if(!isquote(*line->elements[i].item))
933     fprintf(out, "\'%s\'", line->elements[i].item);
934        else
935     fprintf(out, "%s", line->elements[i].item);
936        fprintf(out, "%s", line->elements[i].indent);
937    
938        continue;
939     }
940    
941   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)
942      if (fputc('"', out) == EOF) return -1;      if (fputc('"', out) == EOF) return -1;
943    
# Line 664  static int getNextLine(char ** bufPtr, s Line 1031  static int getNextLine(char ** bufPtr, s
1031      if (*line->elements[0].item == '#') {      if (*line->elements[0].item == '#') {
1032   char * fullLine;   char * fullLine;
1033   int len;   int len;
  int i;  
1034    
1035   len = strlen(line->indent);   len = strlen(line->indent);
1036   for (i = 0; i < line->numElements; i++)   for (int i = 0; i < line->numElements; i++)
1037      len += strlen(line->elements[i].item) +      len += strlen(line->elements[i].item) +
1038     strlen(line->elements[i].indent);     strlen(line->elements[i].indent);
1039    
# Line 676  static int getNextLine(char ** bufPtr, s Line 1042  static int getNextLine(char ** bufPtr, s
1042   free(line->indent);   free(line->indent);
1043   line->indent = fullLine;   line->indent = fullLine;
1044    
1045   for (i = 0; i < line->numElements; i++) {   for (int i = 0; i < line->numElements; i++) {
1046      strcat(fullLine, line->elements[i].item);      strcat(fullLine, line->elements[i].item);
1047      strcat(fullLine, line->elements[i].indent);      strcat(fullLine, line->elements[i].indent);
1048      free(line->elements[i].item);      free(line->elements[i].item);
# Line 695  static int getNextLine(char ** bufPtr, s Line 1061  static int getNextLine(char ** bufPtr, s
1061   * elements up more   * elements up more
1062   */   */
1063   if (!isspace(kw->separatorChar)) {   if (!isspace(kw->separatorChar)) {
     int i;  
1064      char indent[2] = "";      char indent[2] = "";
1065      indent[0] = kw->separatorChar;      indent[0] = kw->separatorChar;
1066      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1067   char *p;   char *p;
  int j;  
1068   int numNewElements;   int numNewElements;
1069    
1070   numNewElements = 0;   numNewElements = 0;
# Line 716  static int getNextLine(char ** bufPtr, s Line 1080  static int getNextLine(char ** bufPtr, s
1080      sizeof(*line->elements) * elementsAlloced);      sizeof(*line->elements) * elementsAlloced);
1081   }   }
1082    
1083   for (j = line->numElements; j > i; j--) {   for (int j = line->numElements; j > i; j--) {
1084   line->elements[j + numNewElements] = line->elements[j];   line->elements[j + numNewElements] = line->elements[j];
1085   }   }
1086   line->numElements += numNewElements;   line->numElements += numNewElements;
# Line 729  static int getNextLine(char ** bufPtr, s Line 1093  static int getNextLine(char ** bufPtr, s
1093   break;   break;
1094   }   }
1095    
1096   free(line->elements[i].indent);   line->elements[i + 1].indent = line->elements[i].indent;
1097   line->elements[i].indent = strdup(indent);   line->elements[i].indent = strdup(indent);
1098   *p++ = '\0';   *p++ = '\0';
1099   i++;   i++;
1100   line->elements[i].item = strdup(p);   line->elements[i].item = strdup(p);
  line->elements[i].indent = strdup("");  
  p = line->elements[i].item;  
1101   }   }
1102      }      }
1103   }   }
# Line 745  static int getNextLine(char ** bufPtr, s Line 1107  static int getNextLine(char ** bufPtr, s
1107      return 0;      return 0;
1108  }  }
1109    
1110    static int isnumber(const char *s)
1111    {
1112        int i;
1113        for (i = 0; s[i] != '\0'; i++)
1114     if (s[i] < '0' || s[i] > '9')
1115        return 0;
1116        return i;
1117    }
1118    
1119  static struct grubConfig * readConfig(const char * inName,  static struct grubConfig * readConfig(const char * inName,
1120        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
1121      int in;      int in;
# Line 756  static struct grubConfig * readConfig(co Line 1127  static struct grubConfig * readConfig(co
1127      struct singleLine * last = NULL, * line, * defaultLine = NULL;      struct singleLine * last = NULL, * line, * defaultLine = NULL;
1128      char * end;      char * end;
1129      struct singleEntry * entry = NULL;      struct singleEntry * entry = NULL;
1130      int i, len;      int len;
1131      char * buf;      char * buf;
1132    
1133      if (!strcmp(inName, "-")) {      if (inName == NULL) {
1134            printf("Could not find bootloader configuration\n");
1135            exit(1);
1136        } else if (!strcmp(inName, "-")) {
1137   in = 0;   in = 0;
1138      } else {      } else {
1139   if ((in = open(inName, O_RDONLY)) < 0) {   if ((in = open(inName, O_RDONLY)) < 0) {
# Line 802  static struct grubConfig * readConfig(co Line 1176  static struct grubConfig * readConfig(co
1176      cfg->secondaryIndent = strdup(line->indent);      cfg->secondaryIndent = strdup(line->indent);
1177   }   }
1178    
1179   if (isEntrySeparator(line, cfi)) {   if (isEntryStart(line, cfi) || (cfg->entries && !sawEntry)) {
1180      sawEntry = 1;      sawEntry = 1;
1181      if (!entry) {      if (!entry) {
1182   cfg->entries = malloc(sizeof(*entry));   cfg->entries = malloc(sizeof(*entry));
# Line 818  static struct grubConfig * readConfig(co Line 1192  static struct grubConfig * readConfig(co
1192      entry->next = NULL;      entry->next = NULL;
1193   }   }
1194    
1195   if (line->type == LT_DEFAULT && line->numElements == 2) {   if (line->type == LT_SET_VARIABLE) {
1196      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      dbgPrintf("found 'set' command (%d elements): ", line->numElements);
1197      defaultLine = line;      dbgPrintf("%s", line->indent);
1198        for (int i = 0; i < line->numElements; i++)
1199     dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);
1200        dbgPrintf("\n");
1201        struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1202        if (kwType && line->numElements == 3 &&
1203        !strcmp(line->elements[1].item, kwType->key) &&
1204        !is_special_grub2_variable(line->elements[2].item)) {
1205     dbgPrintf("Line sets default config\n");
1206     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1207     defaultLine = line;
1208        }
1209    
1210          } else if (line->type == LT_KERNEL) {          } else if (iskernel(line->type)) {
1211      /* if by some freak chance this is multiboot and the "module"      /* if by some freak chance this is multiboot and the "module"
1212       * lines came earlier in the template, make sure to use LT_HYPER       * lines came earlier in the template, make sure to use LT_HYPER
1213       * instead of LT_KERNEL now       * instead of LT_KERNEL now
1214       */       */
1215      if (entry->multiboot)      if (entry && entry->multiboot)
1216   line->type = LT_HYPER;   line->type = LT_HYPER;
1217    
1218          } else if (line->type == LT_MBMODULE) {          } else if (line->type == LT_MBMODULE) {
# Line 836  static struct grubConfig * readConfig(co Line 1221  static struct grubConfig * readConfig(co
1221       * This only applies to grub, but that's the only place we       * This only applies to grub, but that's the only place we
1222       * should find LT_MBMODULE lines anyway.       * should find LT_MBMODULE lines anyway.
1223       */       */
1224      struct singleLine * l;      for (struct singleLine *l = entry->lines; l; l = l->next) {
     for (l = entry->lines; l; l = l->next) {  
1225   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1226      break;      break;
1227   else if (l->type == LT_KERNEL) {   else if (iskernel(l->type)) {
1228      l->type = LT_HYPER;      l->type = LT_HYPER;
1229      break;      break;
1230   }   }
# Line 854  static struct grubConfig * readConfig(co Line 1238  static struct grubConfig * readConfig(co
1238      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);
1239      if (*end) cfg->fallbackImage = -1;      if (*end) cfg->fallbackImage = -1;
1240    
1241   } else if (line->type == LT_TITLE && line->numElements > 1) {   } else if ((line->type == LT_DEFAULT && cfi->defaultIsUnquoted) ||
1242      /* make the title a single argument (undoing our parsing) */                  (line->type == LT_TITLE && line->numElements > 1)) {
1243        /* make the title/default a single argument (undoing our parsing) */
1244      len = 0;      len = 0;
1245      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1246   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
1247   len += strlen(line->elements[i].indent);   len += strlen(line->elements[i].indent);
1248      }      }
1249      buf = malloc(len + 1);      buf = malloc(len + 1);
1250      *buf = '\0';      *buf = '\0';
1251    
1252      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1253   strcat(buf, line->elements[i].item);   strcat(buf, line->elements[i].item);
1254   free(line->elements[i].item);   free(line->elements[i].item);
1255    
# Line 878  static struct grubConfig * readConfig(co Line 1263  static struct grubConfig * readConfig(co
1263      line->elements[line->numElements - 1].indent;      line->elements[line->numElements - 1].indent;
1264      line->elements[1].item = buf;      line->elements[1].item = buf;
1265      line->numElements = 2;      line->numElements = 2;
1266     } else if (line->type == LT_MENUENTRY && line->numElements > 3) {
1267        /* let --remove-kernel="TITLE=what" work */
1268        len = 0;
1269        char *extras;
1270        char *title;
1271    
1272        for (int i = 1; i < line->numElements; i++) {
1273     len += strlen(line->elements[i].item);
1274     len += strlen(line->elements[i].indent);
1275        }
1276        buf = malloc(len + 1);
1277        *buf = '\0';
1278    
1279        /* allocate mem for extra flags. */
1280        extras = malloc(len + 1);
1281        *extras = '\0';
1282    
1283        /* get title. */
1284        for (int i = 0; i < line->numElements; i++) {
1285     if (!strcmp(line->elements[i].item, "menuentry"))
1286        continue;
1287     if (isquote(*line->elements[i].item))
1288        title = line->elements[i].item + 1;
1289     else
1290        title = line->elements[i].item;
1291    
1292     len = strlen(title);
1293            if (isquote(title[len-1])) {
1294        strncat(buf, title,len-1);
1295        break;
1296     } else {
1297        strcat(buf, title);
1298        strcat(buf, line->elements[i].indent);
1299     }
1300        }
1301    
1302        /* get extras */
1303        int count = 0;
1304        for (int i = 0; i < line->numElements; i++) {
1305     if (count >= 2) {
1306        strcat(extras, line->elements[i].item);
1307        strcat(extras, line->elements[i].indent);
1308     }
1309    
1310     if (!strcmp(line->elements[i].item, "menuentry"))
1311        continue;
1312    
1313     /* count ' or ", there should be two in menuentry line. */
1314     if (isquote(*line->elements[i].item))
1315        count++;
1316    
1317     len = strlen(line->elements[i].item);
1318    
1319     if (isquote(line->elements[i].item[len -1]))
1320        count++;
1321    
1322     /* ok, we get the final ' or ", others are extras. */
1323                }
1324        line->elements[1].indent =
1325     line->elements[line->numElements - 2].indent;
1326        line->elements[1].item = buf;
1327        line->elements[2].indent =
1328     line->elements[line->numElements - 2].indent;
1329        line->elements[2].item = extras;
1330        line->numElements = 3;
1331   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {
1332      /* Strip off any " which may be present; they'll be put back      /* Strip off any " which may be present; they'll be put back
1333         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 887  static struct grubConfig * readConfig(co Line 1336  static struct grubConfig * readConfig(co
1336      if (line->numElements >= 2) {      if (line->numElements >= 2) {
1337   int last, len;   int last, len;
1338    
1339   if (*line->elements[1].item == '"')   if (isquote(*line->elements[1].item))
1340      memmove(line->elements[1].item, line->elements[1].item + 1,      memmove(line->elements[1].item, line->elements[1].item + 1,
1341      strlen(line->elements[1].item + 1) + 1);      strlen(line->elements[1].item + 1) + 1);
1342    
1343   last = line->numElements - 1;   last = line->numElements - 1;
1344   len = strlen(line->elements[last].item) - 1;   len = strlen(line->elements[last].item) - 1;
1345   if (line->elements[last].item[len] == '"')   if (isquote(line->elements[last].item[len]))
1346      line->elements[last].item[len] = '\0';      line->elements[last].item[len] = '\0';
1347      }      }
1348   }   }
1349    
1350     if (line->type == LT_DEFAULT && line->numElements == 2) {
1351        cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1352        defaultLine = line;
1353     }
1354    
1355   /* If we find a generic config option which should live at the   /* If we find a generic config option which should live at the
1356     top of the file, move it there. Old versions of grubby were     top of the file, move it there. Old versions of grubby were
1357     probably responsible for putting new images in the wrong     probably responsible for putting new images in the wrong
# Line 931  static struct grubConfig * readConfig(co Line 1385  static struct grubConfig * readConfig(co
1385   entry->lines = line;   entry->lines = line;
1386      else      else
1387   last->next = line;   last->next = line;
1388      dbgPrintf("readConfig added %d to %p\n", line->type, entry);      dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry);
1389    
1390        /* we could have seen this outside of an entry... if so, we
1391         * ignore it like any other line we don't grok */
1392        if (line->type == LT_ENTRY_END && sawEntry)
1393     sawEntry = 0;
1394   } else {   } else {
1395      if (!cfg->theLines)      if (!cfg->theLines)
1396   cfg->theLines = line;   cfg->theLines = line;
1397      else      else
1398   last->next = line;   last->next = line;
1399      dbgPrintf("readConfig added %d to cfg\n", line->type);      dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi));
1400   }   }
1401    
1402   last = line;   last = line;
# Line 945  static struct grubConfig * readConfig(co Line 1404  static struct grubConfig * readConfig(co
1404    
1405      free(incoming);      free(incoming);
1406    
1407        dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1408      if (defaultLine) {      if (defaultLine) {
1409   if (cfi->defaultSupportSaved &&          if (defaultLine->numElements > 2 &&
1410        cfi->defaultSupportSaved &&
1411        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1412     cfg->cfi->defaultIsSaved = 1;
1413     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1414     if (cfg->cfi->getEnv) {
1415        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1416        if (defTitle) {
1417     int index = 0;
1418     if (isnumber(defTitle)) {
1419        index = atoi(defTitle);
1420        entry = findEntryByIndex(cfg, index);
1421     } else {
1422        entry = findEntryByTitle(cfg, defTitle, &index);
1423     }
1424     if (entry)
1425        cfg->defaultImage = index;
1426        }
1427     }
1428     } else if (cfi->defaultIsVariable) {
1429        char *value = defaultLine->elements[2].item;
1430        while (*value && (*value == '"' || *value == '\'' ||
1431        *value == ' ' || *value == '\t'))
1432     value++;
1433        cfg->defaultImage = strtol(value, &end, 10);
1434        while (*end && (*end == '"' || *end == '\'' ||
1435        *end == ' ' || *end == '\t'))
1436     end++;
1437        if (*end) cfg->defaultImage = -1;
1438     } else if (cfi->defaultSupportSaved &&
1439   !strncmp(defaultLine->elements[1].item, "saved", 5)) {   !strncmp(defaultLine->elements[1].item, "saved", 5)) {
1440      cfg->defaultImage = DEFAULT_SAVED;      cfg->defaultImage = DEFAULT_SAVED;
1441   } else if (cfi->defaultIsIndex) {   } else if (cfi->defaultIsIndex) {
1442      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);
1443      if (*end) cfg->defaultImage = -1;      if (*end) cfg->defaultImage = -1;
1444   } else if (defaultLine->numElements >= 2) {   } else if (defaultLine->numElements >= 2) {
1445      i = 0;      int i = 0;
1446      while ((entry = findEntryByIndex(cfg, i))) {      while ((entry = findEntryByIndex(cfg, i))) {
1447   for (line = entry->lines; line; line = line->next)   for (line = entry->lines; line; line = line->next)
1448      if (line->type == LT_TITLE) break;      if (line->type == LT_TITLE) break;
# Line 964  static struct grubConfig * readConfig(co Line 1453  static struct grubConfig * readConfig(co
1453                                  line->elements[1].item)) break;                                  line->elements[1].item)) break;
1454                  } else if (line) {                  } else if (line) {
1455                      if (!strcmp(defaultLine->elements[1].item,                      if (!strcmp(defaultLine->elements[1].item,
1456                                  extractTitle(line))) break;                                  extractTitle(cfg, line))) break;
1457                  }                  }
1458   i++;   i++;
1459   entry = NULL;   entry = NULL;
# Line 976  static struct grubConfig * readConfig(co Line 1465  static struct grubConfig * readConfig(co
1465          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1466      }      }
1467   }   }
1468        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1469     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1470     if (defTitle) {
1471        int index = 0;
1472        if (isnumber(defTitle)) {
1473     index = atoi(defTitle);
1474     entry = findEntryByIndex(cfg, index);
1475        } else {
1476     entry = findEntryByTitle(cfg, defTitle, &index);
1477        }
1478        if (entry)
1479     cfg->defaultImage = index;
1480     }
1481      } else {      } else {
1482          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1483      }      }
# Line 993  static void writeDefault(FILE * out, cha Line 1495  static void writeDefault(FILE * out, cha
1495    
1496      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1497   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1498      else if (cfg->defaultImage > -1) {      else if (cfg->cfi->defaultIsSaved) {
1499     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1500     if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1501        char *title;
1502        entry = findEntryByIndex(cfg, cfg->defaultImage);
1503        line = getLineByType(LT_MENUENTRY, entry->lines);
1504        if (!line)
1505     line = getLineByType(LT_TITLE, entry->lines);
1506        if (line) {
1507     title = extractTitle(cfg, line);
1508     if (title)
1509        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1510        }
1511     }
1512        } else if (cfg->defaultImage > -1) {
1513   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1514      fprintf(out, "%sdefault%s%d\n", indent, separator,      if (cfg->cfi->defaultIsVariable) {
1515      cfg->defaultImage);          fprintf(out, "%sset default=\"%d\"\n", indent,
1516     cfg->defaultImage);
1517        } else {
1518     fprintf(out, "%sdefault%s%d\n", indent, separator,
1519     cfg->defaultImage);
1520        }
1521   } else {   } else {
1522      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1523    
# Line 1021  static void writeDefault(FILE * out, cha Line 1542  static void writeDefault(FILE * out, cha
1542              else if (line && (line->numElements == 1) &&              else if (line && (line->numElements == 1) &&
1543                       cfg->cfi->titleBracketed) {                       cfg->cfi->titleBracketed) {
1544   fprintf(out, "%sdefault%s%s\n", indent, separator,   fprintf(out, "%sdefault%s%s\n", indent, separator,
1545                          extractTitle(line));                          extractTitle(cfg, line));
1546              }              }
1547   }   }
1548      }      }
# Line 1048  static int writeConfig(struct grubConfig Line 1569  static int writeConfig(struct grubConfig
1569    
1570      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1571         directory to the dir of the symlink */         directory to the dir of the symlink */
1572              rc = chdir(dirname(strdupa(outName)));      char *dir = strdupa(outName);
1573        rc = chdir(dirname(dir));
1574      do {      do {
1575   buf = alloca(len + 1);   buf = alloca(len + 1);
1576   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1086  static int writeConfig(struct grubConfig Line 1608  static int writeConfig(struct grubConfig
1608      }      }
1609    
1610      line = cfg->theLines;      line = cfg->theLines;
1611        struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);
1612      while (line) {      while (line) {
1613   if (line->type == LT_DEFAULT) {          if (line->type == LT_SET_VARIABLE && defaultKw &&
1614     line->numElements == 3 &&
1615     !strcmp(line->elements[1].item, defaultKw->key) &&
1616     !is_special_grub2_variable(line->elements[2].item)) {
1617        writeDefault(out, line->indent, line->elements[0].indent, cfg);
1618        needs &= ~MAIN_DEFAULT;
1619     } else if (line->type == LT_DEFAULT) {
1620      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1621      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1622   } else if (line->type == LT_FALLBACK) {   } else if (line->type == LT_FALLBACK) {
# Line 1155  static int numEntries(struct grubConfig Line 1684  static int numEntries(struct grubConfig
1684      return i;      return i;
1685  }  }
1686    
1687    static char *findDiskForRoot()
1688    {
1689        int fd;
1690        char buf[65536];
1691        char *devname;
1692        char *chptr;
1693        int rc;
1694    
1695        if ((fd = open(_PATH_MOUNTED, O_RDONLY)) < 0) {
1696            fprintf(stderr, "grubby: failed to open %s: %s\n",
1697                    _PATH_MOUNTED, strerror(errno));
1698            return NULL;
1699        }
1700    
1701        rc = read(fd, buf, sizeof(buf) - 1);
1702        if (rc <= 0) {
1703            fprintf(stderr, "grubby: failed to read %s: %s\n",
1704                    _PATH_MOUNTED, strerror(errno));
1705            close(fd);
1706            return NULL;
1707        }
1708        close(fd);
1709        buf[rc] = '\0';
1710        chptr = buf;
1711    
1712        char *foundanswer = NULL;
1713    
1714        while (chptr && chptr != buf+rc) {
1715            devname = chptr;
1716    
1717            /*
1718             * The first column of a mtab entry is the device, but if the entry is a
1719             * special device it won't start with /, so move on to the next line.
1720             */
1721            if (*devname != '/') {
1722                chptr = strchr(chptr, '\n');
1723                if (chptr)
1724                    chptr++;
1725                continue;
1726            }
1727    
1728            /* Seek to the next space */
1729            chptr = strchr(chptr, ' ');
1730            if (!chptr) {
1731                fprintf(stderr, "grubby: error parsing %s: %s\n",
1732                        _PATH_MOUNTED, strerror(errno));
1733                return NULL;
1734            }
1735    
1736            /*
1737             * The second column of a mtab entry is the mount point, we are looking
1738             * for '/' obviously.
1739             */
1740            if (*(++chptr) == '/' && *(++chptr) == ' ') {
1741                /* remember the last / entry in mtab */
1742               foundanswer = devname;
1743            }
1744    
1745            /* Next line */
1746            chptr = strchr(chptr, '\n');
1747            if (chptr)
1748                chptr++;
1749        }
1750    
1751        /* Return the last / entry found */
1752        if (foundanswer) {
1753            chptr = strchr(foundanswer, ' ');
1754            *chptr = '\0';
1755            return strdup(foundanswer);
1756        }
1757    
1758        return NULL;
1759    }
1760    
1761    void printEntry(struct singleEntry * entry, FILE *f) {
1762        int i;
1763        struct singleLine * line;
1764    
1765        for (line = entry->lines; line; line = line->next) {
1766     log_message(f, "DBG: %s", line->indent);
1767     for (i = 0; i < line->numElements; i++) {
1768        /* Need to handle this, because we strip the quotes from
1769         * menuentry when read it. */
1770        if (line->type == LT_MENUENTRY && i == 1) {
1771     if(!isquote(*line->elements[i].item))
1772        log_message(f, "\'%s\'", line->elements[i].item);
1773     else
1774        log_message(f, "%s", line->elements[i].item);
1775     log_message(f, "%s", line->elements[i].indent);
1776    
1777     continue;
1778        }
1779        
1780        log_message(f, "%s%s",
1781        line->elements[i].item, line->elements[i].indent);
1782     }
1783     log_message(f, "\n");
1784        }
1785    }
1786    
1787    void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1788    {
1789        static int once;
1790        va_list argp, argq;
1791    
1792        va_start(argp, fmt);
1793    
1794        va_copy(argq, argp);
1795        if (!once) {
1796     log_time(NULL);
1797     log_message(NULL, "command line: %s\n", saved_command_line);
1798        }
1799        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1800        log_vmessage(NULL, fmt, argq);
1801    
1802        printEntry(entry, NULL);
1803        va_end(argq);
1804    
1805        if (!debug) {
1806     once = 1;
1807         va_end(argp);
1808     return;
1809        }
1810    
1811        if (okay) {
1812     va_end(argp);
1813     return;
1814        }
1815    
1816        if (!once)
1817     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1818        once = 1;
1819        fprintf(stderr, "DBG: Image entry failed: ");
1820        vfprintf(stderr, fmt, argp);
1821        printEntry(entry, stderr);
1822        va_end(argp);
1823    }
1824    
1825    #define beginswith(s, c) ((s) && (s)[0] == (c))
1826    
1827    static int endswith(const char *s, char c)
1828    {
1829     int slen;
1830    
1831     if (!s || !s[0])
1832     return 0;
1833     slen = strlen(s) - 1;
1834    
1835     return s[slen] == c;
1836    }
1837    
1838  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1839    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1840      struct singleLine * line;      struct singleLine * line;
1841      char * fullName;      char * fullName;
1842      int i;      int i;
     struct stat sb, sb2;  
1843      char * dev;      char * dev;
1844      char * rootspec;      char * rootspec;
1845        char * rootdev;
1846    
1847      if (skipRemoved && entry->skip) return 0;      if (skipRemoved && entry->skip) {
1848     notSuitablePrintf(entry, 0, "marked to skip\n");
1849     return 0;
1850        }
1851    
1852      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
1853      if (!line || line->numElements < 2) return 0;      if (!line) {
1854     notSuitablePrintf(entry, 0, "no line found\n");
1855     return 0;
1856        }
1857        if (line->numElements < 2) {
1858     notSuitablePrintf(entry, 0, "line has only %d elements\n",
1859        line->numElements);
1860     return 0;
1861        }
1862    
1863      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) {
1864        notSuitablePrintf(entry, 1, "\n");
1865        return 1;
1866        }
1867    
1868      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1869        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
1870      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1871      sprintf(fullName, "%s%s", bootPrefix,      int rootspec_offset = rootspec ? strlen(rootspec) : 0;
1872              line->elements[1].item + (rootspec ? strlen(rootspec) : 0));      int hasslash = endswith(bootPrefix, '/') ||
1873      if (access(fullName, R_OK)) return 0;       beginswith(line->elements[1].item + rootspec_offset, '/');
1874        sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1875                line->elements[1].item + rootspec_offset);
1876        if (access(fullName, R_OK)) {
1877     notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1878     return 0;
1879        }
1880      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
1881   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1882      if (i < line->numElements) {      if (i < line->numElements) {
# Line 1195  int suitableImage(struct singleEntry * e Line 1894  int suitableImage(struct singleEntry * e
1894      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);
1895    
1896              /* failed to find one */              /* failed to find one */
1897              if (!line) return 0;              if (!line) {
1898     notSuitablePrintf(entry, 0, "no line found\n");
1899     return 0;
1900                }
1901    
1902      for (i = 1; i < line->numElements; i++)      for (i = 1; i < line->numElements; i++)
1903          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1904      if (i < line->numElements)      if (i < line->numElements)
1905          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1906      else {      else {
1907     notSuitablePrintf(entry, 0, "no root= entry found\n");
1908   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1909          return 0;          return 0;
1910              }              }
# Line 1209  int suitableImage(struct singleEntry * e Line 1912  int suitableImage(struct singleEntry * e
1912      }      }
1913    
1914      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1915      if (!dev)      if (!getpathbyspec(dev)) {
1916            notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1917          return 0;          return 0;
1918        } else
1919     dev = getpathbyspec(dev);
1920    
1921      i = stat(dev, &sb);      rootdev = findDiskForRoot();
1922      if (i)      if (!rootdev) {
1923            notSuitablePrintf(entry, 0, "can't find root device\n");
1924   return 0;   return 0;
1925        }
1926    
1927      stat("/", &sb2);      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1928            notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1929     getuuidbydev(rootdev), getuuidbydev(dev));
1930            free(rootdev);
1931            return 0;
1932        }
1933    
1934      if (sb.st_rdev != sb2.st_dev)      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1935            notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n",
1936     getuuidbydev(rootdev), getuuidbydev(dev));
1937     free(rootdev);
1938          return 0;          return 0;
1939        }
1940    
1941        free(rootdev);
1942        notSuitablePrintf(entry, 1, "\n");
1943    
1944      return 1;      return 1;
1945  }  }
# Line 1253  struct singleEntry * findEntryByPath(str Line 1973  struct singleEntry * findEntryByPath(str
1973   }   }
1974    
1975   indexVars[i + 1] = -1;   indexVars[i + 1] = -1;
1976    
1977   i = 0;   i = 0;
1978   if (index) {   if (index) {
1979      while (i < *index) i++;      while (i < *index) {
1980      if (indexVars[i] == -1) return NULL;   i++;
1981     if (indexVars[i] == -1) return NULL;
1982        }
1983   }   }
1984    
1985   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
1986   if (!entry) return NULL;   if (!entry) return NULL;
1987    
1988   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
1989   if (!line) return NULL;   if (!line) return NULL;
1990    
1991   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1301  struct singleEntry * findEntryByPath(str Line 2023  struct singleEntry * findEntryByPath(str
2023    
2024   if (!strncmp(kernel, "TITLE=", 6)) {   if (!strncmp(kernel, "TITLE=", 6)) {
2025      prefix = "";      prefix = "";
2026      checkType = LT_TITLE;      checkType = LT_TITLE|LT_MENUENTRY;
2027      kernel += 6;      kernel += 6;
2028   }   }
2029    
# Line 1312  struct singleEntry * findEntryByPath(str Line 2034  struct singleEntry * findEntryByPath(str
2034    
2035      /* check all the lines matching checkType */      /* check all the lines matching checkType */
2036      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
2037   line = getLineByType(entry->multiboot && checkType == LT_KERNEL ?   enum lineType_e ct = checkType;
2038       LT_KERNEL|LT_MBMODULE|LT_HYPER :   if (entry->multiboot && checkType == LT_KERNEL)
2039       checkType, line);      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER|LT_KERNEL_16;
2040   if (!line) break;  /* not found in this entry */   else if (checkType & LT_KERNEL)
2041        ct = checkType | LT_KERNEL_EFI | LT_KERNEL_16;
2042     line = getLineByType(ct, line);
2043     if (!line)
2044        break;  /* not found in this entry */
2045    
2046   if (line && line->numElements >= 2) {   if (line && line->type != LT_MENUENTRY &&
2047     line->numElements >= 2) {
2048      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
2049      if (!strcmp(line->elements[1].item +      if (!strcmp(line->elements[1].item +
2050   ((rootspec != NULL) ? strlen(rootspec) : 0),   ((rootspec != NULL) ? strlen(rootspec) : 0),
2051   kernel + strlen(prefix)))   kernel + strlen(prefix)))
2052   break;   break;
2053   }   }
2054     if(line->type == LT_MENUENTRY &&
2055     !strcmp(line->elements[1].item, kernel))
2056        break;
2057      }      }
2058    
2059      /* make sure this entry has a kernel identifier; this skips      /* make sure this entry has a kernel identifier; this skips
2060       * non-Linux boot entries (could find netbsd etc, though, which is       * non-Linux boot entries (could find netbsd etc, though, which is
2061       * unfortunate)       * unfortunate)
2062       */       */
2063      if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines))      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines))
2064   break; /* found 'im! */   break; /* found 'im! */
2065   }   }
2066    
# Line 1340  struct singleEntry * findEntryByPath(str Line 2070  struct singleEntry * findEntryByPath(str
2070      return entry;      return entry;
2071  }  }
2072    
2073    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2074          int * index) {
2075        struct singleEntry * entry;
2076        struct singleLine * line;
2077        int i;
2078        char * newtitle;
2079    
2080        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2081     if (index && i < *index)
2082        continue;
2083     line = getLineByType(LT_TITLE, entry->lines);
2084     if (!line)
2085        line = getLineByType(LT_MENUENTRY, entry->lines);
2086     if (!line)
2087        continue;
2088     newtitle = grub2ExtractTitle(line);
2089     if (!newtitle)
2090        continue;
2091     if (!strcmp(title, newtitle))
2092        break;
2093        }
2094    
2095        if (!entry)
2096     return NULL;
2097    
2098        if (index)
2099     *index = i;
2100        return entry;
2101    }
2102    
2103  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
2104      struct singleEntry * entry;      struct singleEntry * entry;
2105    
# Line 1362  struct singleEntry * findTemplate(struct Line 2122  struct singleEntry * findTemplate(struct
2122      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2123      int index;      int index;
2124    
2125      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2126     if (cfg->cfi->getEnv) {
2127        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2128        if (defTitle) {
2129     int index = 0;
2130     if (isnumber(defTitle)) {
2131        index = atoi(defTitle);
2132        entry = findEntryByIndex(cfg, index);
2133     } else {
2134        entry = findEntryByTitle(cfg, defTitle, &index);
2135     }
2136     if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2137        cfg->defaultImage = index;
2138        if (indexPtr)
2139     *indexPtr = index;
2140        return entry;
2141     }
2142        }
2143     }
2144        } else if (cfg->defaultImage > -1) {
2145   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2146   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2147      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 1415  void markRemovedImage(struct grubConfig Line 2194  void markRemovedImage(struct grubConfig
2194        const char * prefix) {        const char * prefix) {
2195      struct singleEntry * entry;      struct singleEntry * entry;
2196    
2197      if (!image) return;      if (!image)
2198     return;
2199    
2200        /* check and see if we're removing the default image */
2201        if (isdigit(*image)) {
2202     entry = findEntryByPath(cfg, image, prefix, NULL);
2203     if(entry)
2204        entry->skip = 1;
2205     return;
2206        }
2207    
2208      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
2209   entry->skip = 1;   entry->skip = 1;
# Line 1423  void markRemovedImage(struct grubConfig Line 2211  void markRemovedImage(struct grubConfig
2211    
2212  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
2213       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
2214       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
2215      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
2216      int i, j;      int i, j;
2217    
2218      if (newIsDefault) {      if (newIsDefault) {
2219   config->defaultImage = 0;   config->defaultImage = 0;
2220   return;   return;
2221        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
2222     if (findEntryByIndex(config, index))
2223        config->defaultImage = index;
2224     else
2225        config->defaultImage = -1;
2226     return;
2227      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
2228   i = 0;   i = 0;
2229   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1442  void setDefaultImage(struct grubConfig * Line 2236  void setDefaultImage(struct grubConfig *
2236    
2237      /* defaultImage now points to what we'd like to use, but before any order      /* defaultImage now points to what we'd like to use, but before any order
2238         changes */         changes */
2239      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
2240     (config->defaultImage == DEFAULT_SAVED_GRUB2))
2241        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
2242        return;        return;
2243    
# Line 1500  void displayEntry(struct singleEntry * e Line 2295  void displayEntry(struct singleEntry * e
2295      struct singleLine * line;      struct singleLine * line;
2296      char * root = NULL;      char * root = NULL;
2297      int i;      int i;
2298        int j;
2299    
2300        printf("index=%d\n", index);
2301    
2302        line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
2303        if (!line) {
2304            printf("non linux entry\n");
2305            return;
2306        }
2307    
2308        if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2309     printf("kernel=%s\n", line->elements[1].item);
2310        else
2311     printf("kernel=%s%s\n", prefix, line->elements[1].item);
2312    
2313        if (line->numElements >= 3) {
2314     printf("args=\"");
2315     i = 2;
2316     while (i < line->numElements) {
2317        if (!strncmp(line->elements[i].item, "root=", 5)) {
2318     root = line->elements[i].item + 5;
2319        } else {
2320     printf("%s%s", line->elements[i].item,
2321           line->elements[i].indent);
2322        }
2323    
2324        i++;
2325     }
2326     printf("\"\n");
2327        } else {
2328     line = getLineByType(LT_KERNELARGS, entry->lines);
2329     if (line) {
2330        char * s;
2331    
2332        printf("args=\"");
2333        i = 1;
2334        while (i < line->numElements) {
2335     if (!strncmp(line->elements[i].item, "root=", 5)) {
2336        root = line->elements[i].item + 5;
2337     } else {
2338        s = line->elements[i].item;
2339    
2340        printf("%s%s", s, line->elements[i].indent);
2341     }
2342    
2343     i++;
2344        }
2345    
2346        s = line->elements[i - 1].indent;
2347        printf("\"\n");
2348     }
2349        }
2350    
2351        if (!root) {
2352     line = getLineByType(LT_ROOT, entry->lines);
2353     if (line && line->numElements >= 2)
2354        root=line->elements[1].item;
2355        }
2356    
2357        if (root) {
2358     char * s = alloca(strlen(root) + 1);
2359    
2360     strcpy(s, root);
2361     if (s[strlen(s) - 1] == '"')
2362        s[strlen(s) - 1] = '\0';
2363     /* make sure the root doesn't have a trailing " */
2364     printf("root=%s\n", s);
2365        }
2366    
2367        line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
2368    
2369        if (line && line->numElements >= 2) {
2370     if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2371        printf("initrd=");
2372     else
2373        printf("initrd=%s", prefix);
2374    
2375     for (i = 1; i < line->numElements; i++)
2376        printf("%s%s", line->elements[i].item, line->elements[i].indent);
2377     printf("\n");
2378        }
2379    
2380        line = getLineByType(LT_TITLE, entry->lines);
2381        if (line) {
2382     printf("title=%s\n", line->elements[1].item);
2383        } else {
2384     char * title;
2385     line = getLineByType(LT_MENUENTRY, entry->lines);
2386     if (line) {
2387        title = grub2ExtractTitle(line);
2388        if (title)
2389     printf("title=%s\n", title);
2390     }
2391        }
2392    
2393        for (j = 0, line = entry->lines; line; line = line->next) {
2394     if ((line->type & LT_MBMODULE) && line->numElements >= 2) {
2395        if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2396     printf("mbmodule%d=", j);
2397        else
2398     printf("mbmodule%d=%s", j, prefix);
2399    
2400        for (i = 1; i < line->numElements; i++)
2401     printf("%s%s", line->elements[i].item, line->elements[i].indent);
2402        printf("\n");
2403        j++;
2404     }
2405        }
2406    }
2407    
2408    int isSuseSystem(void) {
2409        const char * path;
2410        const static char default_path[] = "/etc/SuSE-release";
2411    
2412        if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL)
2413     path = default_path;
2414    
2415        if (!access(path, R_OK))
2416     return 1;
2417        return 0;
2418    }
2419    
2420    int isSuseGrubConf(const char * path) {
2421        FILE * grubConf;
2422        char * line = NULL;
2423        size_t len = 0, res = 0;
2424    
2425        grubConf = fopen(path, "r");
2426        if (!grubConf) {
2427            dbgPrintf("Could not open SuSE configuration file '%s'\n", path);
2428     return 0;
2429        }
2430    
2431        while ((res = getline(&line, &len, grubConf)) != -1) {
2432     if (!strncmp(line, "setup", 5)) {
2433        fclose(grubConf);
2434        free(line);
2435        return 1;
2436     }
2437        }
2438    
2439        dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n",
2440          path);
2441    
2442        fclose(grubConf);
2443        free(line);
2444        return 0;
2445    }
2446    
2447    int suseGrubConfGetLba(const char * path, int * lbaPtr) {
2448        FILE * grubConf;
2449        char * line = NULL;
2450        size_t res = 0, len = 0;
2451    
2452        if (!path) return 1;
2453        if (!lbaPtr) return 1;
2454    
2455        grubConf = fopen(path, "r");
2456        if (!grubConf) return 1;
2457    
2458        while ((res = getline(&line, &len, grubConf)) != -1) {
2459     if (line[res - 1] == '\n')
2460        line[res - 1] = '\0';
2461     else if (len > res)
2462        line[res] = '\0';
2463     else {
2464        line = realloc(line, res + 1);
2465        line[res] = '\0';
2466     }
2467    
2468     if (!strncmp(line, "setup", 5)) {
2469        if (strstr(line, "--force-lba")) {
2470            *lbaPtr = 1;
2471        } else {
2472            *lbaPtr = 0;
2473        }
2474        dbgPrintf("lba: %i\n", *lbaPtr);
2475        break;
2476     }
2477        }
2478    
2479        free(line);
2480        fclose(grubConf);
2481        return 0;
2482    }
2483    
2484    int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) {
2485        FILE * grubConf;
2486        char * line = NULL;
2487        size_t res = 0, len = 0;
2488        char * lastParamPtr = NULL;
2489        char * secLastParamPtr = NULL;
2490        char installDeviceNumber = '\0';
2491        char * bounds = NULL;
2492    
2493        if (!path) return 1;
2494        if (!devicePtr) return 1;
2495    
2496        grubConf = fopen(path, "r");
2497        if (!grubConf) return 1;
2498    
2499        while ((res = getline(&line, &len, grubConf)) != -1) {
2500     if (strncmp(line, "setup", 5))
2501        continue;
2502    
2503     if (line[res - 1] == '\n')
2504        line[res - 1] = '\0';
2505     else if (len > res)
2506        line[res] = '\0';
2507     else {
2508        line = realloc(line, res + 1);
2509        line[res] = '\0';
2510     }
2511    
2512     lastParamPtr = bounds = line + res;
2513    
2514     /* Last parameter in grub may be an optional IMAGE_DEVICE */
2515     while (!isspace(*lastParamPtr))
2516        lastParamPtr--;
2517     lastParamPtr++;
2518    
2519     secLastParamPtr = lastParamPtr - 2;
2520     dbgPrintf("lastParamPtr: %s\n", lastParamPtr);
2521    
2522     if (lastParamPtr + 3 > bounds) {
2523        dbgPrintf("lastParamPtr going over boundary");
2524        fclose(grubConf);
2525        free(line);
2526        return 1;
2527     }
2528     if (!strncmp(lastParamPtr, "(hd", 3))
2529        lastParamPtr += 3;
2530     dbgPrintf("lastParamPtr: %c\n", *lastParamPtr);
2531    
2532     /*
2533     * Second last parameter will decide wether last parameter is
2534     * an IMAGE_DEVICE or INSTALL_DEVICE
2535     */
2536     while (!isspace(*secLastParamPtr))
2537        secLastParamPtr--;
2538     secLastParamPtr++;
2539    
2540     if (secLastParamPtr + 3 > bounds) {
2541        dbgPrintf("secLastParamPtr going over boundary");
2542        fclose(grubConf);
2543        free(line);
2544        return 1;
2545     }
2546     dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr);
2547     if (!strncmp(secLastParamPtr, "(hd", 3)) {
2548        secLastParamPtr += 3;
2549        dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr);
2550        installDeviceNumber = *secLastParamPtr;
2551     } else {
2552        installDeviceNumber = *lastParamPtr;
2553     }
2554    
2555     *devicePtr = malloc(6);
2556     snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber);
2557     dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber);
2558     fclose(grubConf);
2559     free(line);
2560     return 0;
2561        }
2562    
2563        free(line);
2564        fclose(grubConf);
2565        return 1;
2566    }
2567    
2568    int grubGetBootFromDeviceMap(const char * device,
2569         char ** bootPtr) {
2570        FILE * deviceMap;
2571        char * line = NULL;
2572        size_t res = 0, len = 0;
2573        char * devicePtr;
2574        char * bounds = NULL;
2575        const char * path;
2576        const static char default_path[] = "/boot/grub/device.map";
2577    
2578        if (!device) return 1;
2579        if (!bootPtr) return 1;
2580    
2581        if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL)
2582     path = default_path;
2583    
2584        dbgPrintf("opening grub device.map file from: %s\n", path);
2585        deviceMap = fopen(path, "r");
2586        if (!deviceMap)
2587     return 1;
2588    
2589        while ((res = getline(&line, &len, deviceMap)) != -1) {
2590            if (!strncmp(line, "#", 1))
2591        continue;
2592    
2593      printf("index=%d\n", index);   if (line[res - 1] == '\n')
2594        line[res - 1] = '\0';
2595     else if (len > res)
2596        line[res] = '\0';
2597     else {
2598        line = realloc(line, res + 1);
2599        line[res] = '\0';
2600     }
2601    
2602     devicePtr = line;
2603     bounds = line + res;
2604    
2605     while ((isspace(*line) && ((devicePtr + 1) <= bounds)))
2606        devicePtr++;
2607     dbgPrintf("device: %s\n", devicePtr);
2608    
2609     if (!strncmp(devicePtr, device, strlen(device))) {
2610        devicePtr += strlen(device);
2611        while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds))
2612            devicePtr++;
2613    
2614      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      *bootPtr = strdup(devicePtr);
2615      if (!line) {      break;
2616          printf("non linux entry\n");   }
         return;  
2617      }      }
2618    
2619      printf("kernel=%s\n", line->elements[1].item);      free(line);
2620        fclose(deviceMap);
2621        return 0;
2622    }
2623    
2624      if (line->numElements >= 3) {  int suseGrubConfGetBoot(const char * path, char ** bootPtr) {
2625   printf("args=\"");      char * grubDevice;
  i = 2;  
  while (i < line->numElements) {  
     if (!strncmp(line->elements[i].item, "root=", 5)) {  
  root = line->elements[i].item + 5;  
     } else {  
  printf("%s%s", line->elements[i].item,  
        line->elements[i].indent);  
     }  
2626    
2627      i++;      if (suseGrubConfGetInstallDevice(path, &grubDevice))
2628   }   dbgPrintf("error looking for grub installation device\n");
2629   printf("\"\n");      else
2630      } else {   dbgPrintf("grubby installation device: %s\n", grubDevice);
  line = getLineByType(LT_KERNELARGS, entry->lines);  
  if (line) {  
     char * s;  
2631    
2632      printf("args=\"");      if (grubGetBootFromDeviceMap(grubDevice, bootPtr))
2633      i = 1;   dbgPrintf("error looking for grub boot device\n");
2634      while (i < line->numElements) {      else
2635   if (!strncmp(line->elements[i].item, "root=", 5)) {   dbgPrintf("grubby boot device: %s\n", *bootPtr);
     root = line->elements[i].item + 5;  
  } else {  
     s = line->elements[i].item;  
2636    
2637      printf("%s%s", s, line->elements[i].indent);      free(grubDevice);
2638   }      return 0;
2639    }
2640    
2641   i++;  int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) {
2642      }      /*
2643         * This SuSE grub configuration file at this location is not your average
2644         * grub configuration file, but instead the grub commands used to setup
2645         * grub on that system.
2646         */
2647        const char * path;
2648        const static char default_path[] = "/etc/grub.conf";
2649    
2650      s = line->elements[i - 1].indent;      if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL)
2651      printf("\"\n");   path = default_path;
  }  
     }  
2652    
2653      if (!root) {      if (!isSuseGrubConf(path)) return 1;
  line = getLineByType(LT_ROOT, entry->lines);  
  if (line && line->numElements >= 2)  
     root=line->elements[1].item;  
     }  
2654    
2655      if (root) {      if (lbaPtr) {
2656   char * s = alloca(strlen(root) + 1);          *lbaPtr = 0;
2657            if (suseGrubConfGetLba(path, lbaPtr))
2658   strcpy(s, root);              return 1;
  if (s[strlen(s) - 1] == '"')  
     s[strlen(s) - 1] = '\0';  
  /* make sure the root doesn't have a trailing " */  
  printf("root=%s\n", s);  
2659      }      }
2660    
2661      line = getLineByType(LT_INITRD, entry->lines);      if (bootPtr) {
2662            *bootPtr = NULL;
2663      if (line && line->numElements >= 2) {          suseGrubConfGetBoot(path, bootPtr);
  printf("initrd=%s", prefix);  
  for (i = 1; i < line->numElements; i++)  
     printf("%s%s", line->elements[i].item, line->elements[i].indent);  
  printf("\n");  
2664      }      }
2665    
2666        return 0;
2667  }  }
2668    
2669  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 1582  int parseSysconfigGrub(int * lbaPtr, cha Line 2673  int parseSysconfigGrub(int * lbaPtr, cha
2673      char * start;      char * start;
2674      char * param;      char * param;
2675    
2676      in = fopen("/etc/sysconfig/grub", "r");      in = fopen("/etc/conf.d/grub", "r");
2677      if (!in) return 1;      if (!in) return 1;
2678    
2679      if (lbaPtr) *lbaPtr = 0;      if (lbaPtr) *lbaPtr = 0;
# Line 1623  int parseSysconfigGrub(int * lbaPtr, cha Line 2714  int parseSysconfigGrub(int * lbaPtr, cha
2714  }  }
2715    
2716  void dumpSysconfigGrub(void) {  void dumpSysconfigGrub(void) {
2717      char * boot;      char * boot = NULL;
2718      int lba;      int lba;
2719    
2720      if (!parseSysconfigGrub(&lba, &boot)) {      if (isSuseSystem()) {
2721   if (lba) printf("lba\n");          if (parseSuseGrubConf(&lba, &boot)) {
2722   if (boot) printf("boot=%s\n", boot);      free(boot);
2723        return;
2724     }
2725        } else {
2726            if (parseSysconfigGrub(&lba, &boot)) {
2727        free(boot);
2728        return;
2729     }
2730        }
2731    
2732        if (lba) printf("lba\n");
2733        if (boot) {
2734     printf("boot=%s\n", boot);
2735     free(boot);
2736      }      }
2737  }  }
2738    
# Line 1644  int displayInfo(struct grubConfig * conf Line 2748  int displayInfo(struct grubConfig * conf
2748   return 1;   return 1;
2749      }      }
2750    
2751      /* this is a horrible hack to support /etc/sysconfig/grub; there must      /* this is a horrible hack to support /etc/conf.d/grub; there must
2752         be a better way */         be a better way */
2753      if (config->cfi == &grubConfigType) {      if (config->cfi == &grubConfigType) {
2754   dumpSysconfigGrub();   dumpSysconfigGrub();
# Line 1677  struct singleLine * addLineTmpl(struct s Line 2781  struct singleLine * addLineTmpl(struct s
2781  {  {
2782      struct singleLine * newLine = lineDup(tmplLine);      struct singleLine * newLine = lineDup(tmplLine);
2783    
2784        if (isEfi && cfi == &grub2ConfigType) {
2785     enum lineType_e old = newLine->type;
2786     newLine->type = preferredLineType(newLine->type, cfi);
2787     if (old != newLine->type)
2788        newLine->elements[0].item = getKeyByType(newLine->type, cfi);
2789        }
2790    
2791      if (val) {      if (val) {
2792   /* override the inherited value with our own.   /* override the inherited value with our own.
2793   * 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 1686  struct singleLine * addLineTmpl(struct s Line 2797  struct singleLine * addLineTmpl(struct s
2797   insertElement(newLine, val, 1, cfi);   insertElement(newLine, val, 1, cfi);
2798    
2799   /* but try to keep the rootspec from the template... sigh */   /* but try to keep the rootspec from the template... sigh */
2800   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)) {
2801      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2802      if (rootspec != NULL) {      if (rootspec != NULL) {
2803   free(newLine->elements[1].item);   free(newLine->elements[1].item);
# Line 1723  struct singleLine *  addLine(struct sing Line 2834  struct singleLine *  addLine(struct sing
2834      /* 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
2835       * stack since it calls addLineTmpl which will make copies.       * stack since it calls addLineTmpl which will make copies.
2836       */       */
   
2837      if (type == LT_TITLE && cfi->titleBracketed) {      if (type == LT_TITLE && cfi->titleBracketed) {
2838   /* we're doing a bracketed title (zipl) */   /* we're doing a bracketed title (zipl) */
2839   tmpl.type = type;   tmpl.type = type;
# Line 1733  struct singleLine *  addLine(struct sing Line 2843  struct singleLine *  addLine(struct sing
2843   sprintf(tmpl.elements[0].item, "[%s]", val);   sprintf(tmpl.elements[0].item, "[%s]", val);
2844   tmpl.elements[0].indent = "";   tmpl.elements[0].indent = "";
2845   val = NULL;   val = NULL;
2846        } else if (type == LT_MENUENTRY) {
2847     char *lineend = "--class gnu-linux --class gnu --class os {";
2848     if (!val) {
2849        fprintf(stderr, "Line type LT_MENUENTRY requires a value\n");
2850        abort();
2851     }
2852     kw = getKeywordByType(type, cfi);
2853     if (!kw) {
2854        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
2855        abort();
2856     }
2857     tmpl.indent = "";
2858     tmpl.type = type;
2859     tmpl.numElements = 3;
2860     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
2861     tmpl.elements[0].item = kw->key;
2862     tmpl.elements[0].indent = alloca(2);
2863     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
2864     tmpl.elements[1].item = (char *)val;
2865     tmpl.elements[1].indent = alloca(2);
2866     sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);
2867     tmpl.elements[2].item = alloca(strlen(lineend)+1);
2868     strcpy(tmpl.elements[2].item, lineend);
2869     tmpl.elements[2].indent = "";
2870      } else {      } else {
2871   kw = getKeywordByType(type, cfi);   kw = getKeywordByType(type, cfi);
2872   if (!kw) abort();   if (!kw) {
2873        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
2874        abort();
2875     }
2876   tmpl.type = type;   tmpl.type = type;
2877   tmpl.numElements = val ? 2 : 1;   tmpl.numElements = val ? 2 : 1;
2878   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
# Line 1759  struct singleLine *  addLine(struct sing Line 2896  struct singleLine *  addLine(struct sing
2896   if (!line->next && !prev) prev = line;   if (!line->next && !prev) prev = line;
2897      }      }
2898    
2899      if (prev == entry->lines)      struct singleLine *menuEntry;
2900   tmpl.indent = defaultIndent ?: "";      menuEntry = getLineByType(LT_MENUENTRY, entry->lines);
2901      else      if (tmpl.type == LT_ENTRY_END) {
2902   tmpl.indent = prev->indent;   if (menuEntry)
2903        tmpl.indent = menuEntry->indent;
2904     else
2905        tmpl.indent = defaultIndent ?: "";
2906        } else if (tmpl.type != LT_MENUENTRY) {
2907     if (menuEntry)
2908        tmpl.indent = "\t";
2909     else if (prev == entry->lines)
2910        tmpl.indent = defaultIndent ?: "";
2911     else
2912        tmpl.indent = prev->indent;
2913        }
2914    
2915      return addLineTmpl(entry, &tmpl, prev, val, cfi);      return addLineTmpl(entry, &tmpl, prev, val, cfi);
2916  }  }
# Line 1789  void removeLine(struct singleEntry * ent Line 2937  void removeLine(struct singleEntry * ent
2937      free(line);      free(line);
2938  }  }
2939    
2940    static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2941    {
2942        struct singleLine newLine = {
2943     .indent = tmplLine->indent,
2944     .type = tmplLine->type,
2945     .next = tmplLine->next,
2946        };
2947        int firstQuotedItem = -1;
2948        int quoteLen = 0;
2949        int j;
2950        int element = 0;
2951        char *c;
2952    
2953        c = malloc(strlen(tmplLine->elements[0].item) + 1);
2954        strcpy(c, tmplLine->elements[0].item);
2955        insertElement(&newLine, c, element++, cfi);
2956        free(c);
2957        c = NULL;
2958    
2959        for (j = 1; j < tmplLine->numElements; j++) {
2960     if (firstQuotedItem == -1) {
2961        quoteLen += strlen(tmplLine->elements[j].item);
2962        
2963        if (isquote(tmplLine->elements[j].item[0])) {
2964     firstQuotedItem = j;
2965            quoteLen += strlen(tmplLine->elements[j].indent);
2966        } else {
2967     c = malloc(quoteLen + 1);
2968     strcpy(c, tmplLine->elements[j].item);
2969     insertElement(&newLine, c, element++, cfi);
2970     free(c);
2971     quoteLen = 0;
2972        }
2973     } else {
2974        int itemlen = strlen(tmplLine->elements[j].item);
2975        quoteLen += itemlen;
2976        quoteLen += strlen(tmplLine->elements[j].indent);
2977        
2978        if (isquote(tmplLine->elements[j].item[itemlen - 1])) {
2979     c = malloc(quoteLen + 1);
2980     c[0] = '\0';
2981     for (int i = firstQuotedItem; i < j+1; i++) {
2982        strcat(c, tmplLine->elements[i].item);
2983        strcat(c, tmplLine->elements[i].indent);
2984     }
2985     insertElement(&newLine, c, element++, cfi);
2986     free(c);
2987    
2988     firstQuotedItem = -1;
2989     quoteLen = 0;
2990        }
2991     }
2992        }
2993        while (tmplLine->numElements)
2994     removeElement(tmplLine, 0);
2995        if (tmplLine->elements)
2996     free(tmplLine->elements);
2997    
2998        tmplLine->numElements = newLine.numElements;
2999        tmplLine->elements = newLine.elements;
3000    }
3001    
3002  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
3003    const char * item, int insertHere,    const char * item, int insertHere,
3004    struct configFileInfo * cfi)    struct configFileInfo * cfi)
# Line 1895  int updateActualImage(struct grubConfig Line 3105  int updateActualImage(struct grubConfig
3105      const char ** arg;      const char ** arg;
3106      int useKernelArgs, useRoot;      int useKernelArgs, useRoot;
3107      int firstElement;      int firstElement;
3108      int *usedElements, *usedArgs;      int *usedElements;
3109      int doreplace;      int doreplace;
3110    
3111      if (!image) return 0;      if (!image) return 0;
# Line 1930  int updateActualImage(struct grubConfig Line 3140  int updateActualImage(struct grubConfig
3140      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)
3141         && !multibootArgs);         && !multibootArgs);
3142    
     for (k = 0, arg = newArgs; *arg; arg++, k++) ;  
     usedArgs = calloc(k, sizeof(*usedArgs));  
   
3143      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3144    
3145   if (multibootArgs && !entry->multiboot)   if (multibootArgs && !entry->multiboot)
# Line 1960  int updateActualImage(struct grubConfig Line 3167  int updateActualImage(struct grubConfig
3167      firstElement = 2;      firstElement = 2;
3168    
3169   } else {   } else {
3170      line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3171      if (!line) {      if (!line) {
3172   /* no LT_KERNEL or LT_MBMODULE in this entry? */   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3173   continue;   continue;
# Line 2008  int updateActualImage(struct grubConfig Line 3215  int updateActualImage(struct grubConfig
3215          usedElements = calloc(line->numElements, sizeof(*usedElements));          usedElements = calloc(line->numElements, sizeof(*usedElements));
3216    
3217   for (k = 0, arg = newArgs; *arg; arg++, k++) {   for (k = 0, arg = newArgs; *arg; arg++, k++) {
             if (usedArgs[k]) continue;  
3218    
3219      doreplace = 1;      doreplace = 1;
3220      for (i = firstElement; i < line->numElements; i++) {      for (i = firstElement; i < line->numElements; i++) {
# Line 2023  int updateActualImage(struct grubConfig Line 3229  int updateActualImage(struct grubConfig
3229                      continue;                      continue;
3230   if (!argMatch(line->elements[i].item, *arg)) {   if (!argMatch(line->elements[i].item, *arg)) {
3231                      usedElements[i]=1;                      usedElements[i]=1;
                     usedArgs[k]=1;  
3232      break;      break;
3233                  }                  }
3234              }              }
# Line 2093  int updateActualImage(struct grubConfig Line 3298  int updateActualImage(struct grubConfig
3298   }   }
3299      }      }
3300    
     free(usedArgs);  
3301      free(newArgs);      free(newArgs);
3302      free(oldArgs);      free(oldArgs);
3303    
# Line 2119  int updateImage(struct grubConfig * cfg, Line 3323  int updateImage(struct grubConfig * cfg,
3323      return rc;      return rc;
3324  }  }
3325    
3326    int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
3327     const char * image, const char * prefix, const char * initrd,
3328     const char * title) {
3329        struct singleEntry * entry;
3330        struct singleLine * line, * kernelLine, *endLine = NULL;
3331        int index = 0;
3332    
3333        if (!image) return 0;
3334    
3335        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3336            kernelLine = getLineByType(LT_MBMODULE, entry->lines);
3337            if (!kernelLine) continue;
3338    
3339     /* if title is supplied, the entry's title must match it. */
3340     if (title) {
3341        char *linetitle;
3342    
3343        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3344        if (!line)
3345     continue;
3346    
3347        linetitle = extractTitle(cfg, line);
3348        if (!linetitle)
3349     continue;
3350        if (strcmp(title, linetitle)) {
3351     free(linetitle);
3352     continue;
3353        }
3354        free(linetitle);
3355     }
3356    
3357            if (prefix) {
3358                int prefixLen = strlen(prefix);
3359                if (!strncmp(initrd, prefix, prefixLen))
3360                    initrd += prefixLen;
3361            }
3362     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3363     if (endLine)
3364        removeLine(entry, endLine);
3365            line = addLine(entry, cfg->cfi, preferredLineType(LT_MBMODULE,cfg->cfi),
3366     kernelLine->indent, initrd);
3367            if (!line)
3368        return 1;
3369     if (endLine) {
3370        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3371                if (!line)
3372     return 1;
3373     }
3374    
3375            break;
3376        }
3377    
3378        return 0;
3379    }
3380    
3381    int updateInitrd(struct grubConfig * cfg, const char * image,
3382                     const char * prefix, const char * initrd, const char * title) {
3383        struct singleEntry * entry;
3384        struct singleLine * line, * kernelLine, *endLine = NULL;
3385        int index = 0;
3386    
3387        if (!image) return 0;
3388    
3389        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3390            kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3391            if (!kernelLine) continue;
3392    
3393     /* if title is supplied, the entry's title must match it. */
3394     if (title) {
3395        char *linetitle;
3396    
3397        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3398        if (!line)
3399     continue;
3400    
3401        linetitle = extractTitle(cfg, line);
3402        if (!linetitle)
3403     continue;
3404        if (strcmp(title, linetitle)) {
3405     free(linetitle);
3406     continue;
3407        }
3408        free(linetitle);
3409     }
3410    
3411            line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
3412            if (line)
3413                removeLine(entry, line);
3414            if (prefix) {
3415                int prefixLen = strlen(prefix);
3416                if (!strncmp(initrd, prefix, prefixLen))
3417                    initrd += prefixLen;
3418            }
3419     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3420     if (endLine)
3421        removeLine(entry, endLine);
3422     enum lineType_e lt;
3423     switch(kernelLine->type) {
3424        case LT_KERNEL:
3425            lt = LT_INITRD;
3426     break;
3427        case LT_KERNEL_EFI:
3428            lt = LT_INITRD_EFI;
3429     break;
3430        case LT_KERNEL_16:
3431            lt = LT_INITRD_16;
3432     break;
3433        default:
3434            lt = preferredLineType(LT_INITRD, cfg->cfi);
3435     }
3436            line = addLine(entry, cfg->cfi, lt, kernelLine->indent, initrd);
3437            if (!line)
3438        return 1;
3439     if (endLine) {
3440        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3441                if (!line)
3442     return 1;
3443     }
3444    
3445            break;
3446        }
3447    
3448        return 0;
3449    }
3450    
3451  int checkDeviceBootloader(const char * device, const unsigned char * boot) {  int checkDeviceBootloader(const char * device, const unsigned char * boot) {
3452      int fd;      int fd;
3453      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 2142  int checkDeviceBootloader(const char * d Line 3471  int checkDeviceBootloader(const char * d
3471      if (memcmp(boot, bootSect, 3))      if (memcmp(boot, bootSect, 3))
3472   return 0;   return 0;
3473    
3474      if (boot[1] == 0xeb) {      if (boot[1] == JMP_SHORT_OPCODE) {
3475   offset = boot[2] + 2;   offset = boot[2] + 2;
3476      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {
3477   offset = (boot[3] << 8) + boot[2] + 2;   offset = (boot[3] << 8) + boot[2] + 2;
3478      } else if (boot[0] == 0xeb) {      } else if (boot[0] == JMP_SHORT_OPCODE) {
3479   offset = boot[1] + 2;        offset = boot[1] + 2;
3480            /*
3481     * it looks like grub, when copying stage1 into the mbr, patches stage1
3482     * right after the JMP location, replacing other instructions such as
3483     * JMPs for NOOPs. So, relax the check a little bit by skipping those
3484     * different bytes.
3485     */
3486          if ((bootSect[offset + 1] == NOOP_OPCODE)
3487      && (bootSect[offset + 2] == NOOP_OPCODE)) {
3488     offset = offset + 3;
3489          }
3490      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {
3491   offset = (boot[2] << 8) + boot[1] + 2;   offset = (boot[2] << 8) + boot[1] + 2;
3492      } else {      } else {
# Line 2289  int checkForLilo(struct grubConfig * con Line 3628  int checkForLilo(struct grubConfig * con
3628      return checkDeviceBootloader(line->elements[1].item, boot);      return checkDeviceBootloader(line->elements[1].item, boot);
3629  }  }
3630    
3631    int checkForGrub2(struct grubConfig * config) {
3632        if (!access("/etc/grub.d/", R_OK))
3633     return 2;
3634    
3635        return 1;
3636    }
3637    
3638  int checkForGrub(struct grubConfig * config) {  int checkForGrub(struct grubConfig * config) {
3639      int fd;      int fd;
3640      unsigned char bootSect[512];      unsigned char bootSect[512];
3641      char * boot;      char * boot;
3642        int onSuse = isSuseSystem();
3643    
3644      if (parseSysconfigGrub(NULL, &boot))  
3645   return 0;      if (onSuse) {
3646     if (parseSuseGrubConf(NULL, &boot))
3647        return 0;
3648        } else {
3649     if (parseSysconfigGrub(NULL, &boot))
3650        return 0;
3651        }
3652    
3653      /* assume grub is not installed -- not an error condition */      /* assume grub is not installed -- not an error condition */
3654      if (!boot)      if (!boot)
# Line 2314  int checkForGrub(struct grubConfig * con Line 3667  int checkForGrub(struct grubConfig * con
3667      }      }
3668      close(fd);      close(fd);
3669    
3670        /* The more elaborate checks do not work on SuSE. The checks done
3671         * seem to be reasonble (at least for now), so just return success
3672         */
3673        if (onSuse)
3674     return 2;
3675    
3676      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3677  }  }
3678    
# Line 2347  int checkForExtLinux(struct grubConfig * Line 3706  int checkForExtLinux(struct grubConfig *
3706      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3707  }  }
3708    
3709    int checkForYaboot(struct grubConfig * config) {
3710        /*
3711         * This is a simplistic check that we consider good enough for own puporses
3712         *
3713         * If we were to properly check if yaboot is *installed* we'd need to:
3714         * 1) get the system boot device (LT_BOOT)
3715         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3716         *    the content on the boot device
3717         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3718         * 4) check again if binary and boot device contents match
3719         */
3720        if (!access("/etc/yaboot.conf", R_OK))
3721     return 2;
3722    
3723        return 1;
3724    }
3725    
3726    int checkForElilo(struct grubConfig * config) {
3727        if (!access("/etc/elilo.conf", R_OK))
3728     return 2;
3729    
3730        return 1;
3731    }
3732    
3733  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3734      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3735    
# Line 2361  static char * getRootSpecifier(char * st Line 3744  static char * getRootSpecifier(char * st
3744  static char * getInitrdVal(struct grubConfig * config,  static char * getInitrdVal(struct grubConfig * config,
3745     const char * prefix, struct singleLine *tmplLine,     const char * prefix, struct singleLine *tmplLine,
3746     const char * newKernelInitrd,     const char * newKernelInitrd,
3747     char ** extraInitrds, int extraInitrdCount)     const char ** extraInitrds, int extraInitrdCount)
3748  {  {
3749      char *initrdVal, *end;      char *initrdVal, *end;
3750      int i;      int i;
# Line 2406  static char * getInitrdVal(struct grubCo Line 3789  static char * getInitrdVal(struct grubCo
3789    
3790  int addNewKernel(struct grubConfig * config, struct singleEntry * template,  int addNewKernel(struct grubConfig * config, struct singleEntry * template,
3791           const char * prefix,           const char * prefix,
3792   char * newKernelPath, char * newKernelTitle,   const char * newKernelPath, const char * newKernelTitle,
3793   char * newKernelArgs, char * newKernelInitrd,   const char * newKernelArgs, const char * newKernelInitrd,
3794   char ** extraInitrds, int extraInitrdCount,   const char ** extraInitrds, int extraInitrdCount,
3795                   char * newMBKernel, char * newMBKernelArgs) {                   const char * newMBKernel, const char * newMBKernelArgs,
3796     const char * newDevTreePath) {
3797      struct singleEntry * new;      struct singleEntry * new;
3798      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;
3799      int needs;      int needs;
# Line 2450  int addNewKernel(struct grubConfig * con Line 3834  int addNewKernel(struct grubConfig * con
3834          needs |= NEED_MB;          needs |= NEED_MB;
3835          new->multiboot = 1;          new->multiboot = 1;
3836      }      }
3837        if (newDevTreePath && getKeywordByType(LT_DEVTREE, config->cfi))
3838     needs |= NEED_DEVTREE;
3839    
3840      if (template) {      if (template) {
3841   for (masterLine = template->lines;   for (masterLine = template->lines;
# Line 2463  int addNewKernel(struct grubConfig * con Line 3849  int addNewKernel(struct grubConfig * con
3849      while (*chptr && isspace(*chptr)) chptr++;      while (*chptr && isspace(*chptr)) chptr++;
3850      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3851    
3852      if (tmplLine->type == LT_KERNEL &&      if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) {
  tmplLine->numElements >= 2) {  
3853   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
3854      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
3855       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 2541  int addNewKernel(struct grubConfig * con Line 3926  int addNewKernel(struct grubConfig * con
3926      /* template is multi but new is not,      /* template is multi but new is not,
3927       * insert the kernel in the first module slot       * insert the kernel in the first module slot
3928       */       */
3929      tmplLine->type = LT_KERNEL;      tmplLine->type = preferredLineType(LT_KERNEL, config->cfi);
3930      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3931      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3932   strdup(getKeywordByType(LT_KERNEL, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3933     config->cfi)->key);
3934      newLine = addLineTmpl(new, tmplLine, newLine,      newLine = addLineTmpl(new, tmplLine, newLine,
3935    newKernelPath + strlen(prefix), config->cfi);    newKernelPath + strlen(prefix),
3936      config->cfi);
3937      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
3938   } else if (needs & NEED_INITRD) {   } else if (needs & NEED_INITRD) {
3939      char *initrdVal;      char *initrdVal;
3940      /* template is multi but new is not,      /* template is multi but new is not,
3941       * insert the initrd in the second module slot       * insert the initrd in the second module slot
3942       */       */
3943      tmplLine->type = LT_INITRD;      tmplLine->type = preferredLineType(LT_INITRD, config->cfi);
3944      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3945      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3946   strdup(getKeywordByType(LT_INITRD, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3947     config->cfi)->key);
3948      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3949      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3950      free(initrdVal);      free(initrdVal);
3951      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3952   }   }
3953    
3954      } else if (tmplLine->type == LT_INITRD &&      } else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) {
        tmplLine->numElements >= 2) {  
3955   if (needs & NEED_INITRD &&   if (needs & NEED_INITRD &&
3956      new->multiboot && !template->multiboot &&      new->multiboot && !template->multiboot &&
3957      config->cfi->mbInitRdIsModule) {      config->cfi->mbInitRdIsModule) {
# Line 2590  int addNewKernel(struct grubConfig * con Line 3977  int addNewKernel(struct grubConfig * con
3977      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3978   }   }
3979    
3980        } else if (tmplLine->type == LT_MENUENTRY &&
3981           (needs & NEED_TITLE)) {
3982     requote(tmplLine, config->cfi);
3983     char *nkt = malloc(strlen(newKernelTitle)+3);
3984     strcpy(nkt, "'");
3985     strcat(nkt, newKernelTitle);
3986     strcat(nkt, "'");
3987     newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi);
3988     free(nkt);
3989     needs &= ~NEED_TITLE;
3990      } else if (tmplLine->type == LT_TITLE &&      } else if (tmplLine->type == LT_TITLE &&
3991         (needs & NEED_TITLE)) {         (needs & NEED_TITLE)) {
3992   if (tmplLine->numElements >= 2) {   if (tmplLine->numElements >= 2) {
# Line 2603  int addNewKernel(struct grubConfig * con Line 4000  int addNewKernel(struct grubConfig * con
4000        tmplLine->indent, newKernelTitle);        tmplLine->indent, newKernelTitle);
4001      needs &= ~NEED_TITLE;      needs &= ~NEED_TITLE;
4002   }   }
4003        } else if (tmplLine->type == LT_ECHO) {
4004        requote(tmplLine, config->cfi);
4005        static const char *prefix = "'Loading ";
4006        if (tmplLine->numElements > 1 &&
4007        strstr(tmplLine->elements[1].item, prefix) &&
4008        masterLine->next &&
4009        iskernel(masterLine->next->type)) {
4010     char *newTitle = malloc(strlen(prefix) +
4011     strlen(newKernelTitle) + 2);
4012    
4013     strcpy(newTitle, prefix);
4014     strcat(newTitle, newKernelTitle);
4015     strcat(newTitle, "'");
4016     newLine = addLine(new, config->cfi, LT_ECHO,
4017     tmplLine->indent, newTitle);
4018     free(newTitle);
4019        } else {
4020     /* pass through other lines from the template */
4021     newLine = addLineTmpl(new, tmplLine, newLine, NULL,
4022     config->cfi);
4023        }
4024        } else if (tmplLine->type == LT_DEVTREE &&
4025           tmplLine->numElements == 2 && newDevTreePath) {
4026            newLine = addLineTmpl(new, tmplLine, newLine,
4027          newDevTreePath + strlen(prefix),
4028          config->cfi);
4029     needs &= ~NEED_DEVTREE;
4030        } else if (tmplLine->type == LT_ENTRY_END && needs & NEED_DEVTREE) {
4031     const char *ndtp = newDevTreePath;
4032     if (!strncmp(newDevTreePath, prefix, strlen(prefix)))
4033        ndtp += strlen(prefix);
4034     newLine = addLine(new, config->cfi, LT_DEVTREE,
4035      config->secondaryIndent,
4036      ndtp);
4037     needs &= ~NEED_DEVTREE;
4038     newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
4039      } else {      } else {
4040   /* pass through other lines from the template */   /* pass through other lines from the template */
4041   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
# Line 2614  int addNewKernel(struct grubConfig * con Line 4046  int addNewKernel(struct grubConfig * con
4046   /* don't have a template, so start the entry with the   /* don't have a template, so start the entry with the
4047   * appropriate starting line   * appropriate starting line
4048   */   */
4049   switch (config->cfi->entrySeparator) {   switch (config->cfi->entryStart) {
4050      case LT_KERNEL:      case LT_KERNEL:
4051        case LT_KERNEL_EFI:
4052        case LT_KERNEL_16:
4053   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
4054      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
4055   } else {   } else {
4056      newLine = addLine(new, config->cfi, LT_KERNEL,      newLine = addLine(new, config->cfi,
4057              preferredLineType(LT_KERNEL, config->cfi),
4058        config->primaryIndent,        config->primaryIndent,
4059        newKernelPath + strlen(prefix));        newKernelPath + strlen(prefix));
4060      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
# Line 2633  int addNewKernel(struct grubConfig * con Line 4068  int addNewKernel(struct grubConfig * con
4068   needs &= ~NEED_MB;   needs &= ~NEED_MB;
4069   break;   break;
4070    
4071        case LT_MENUENTRY: {
4072     char *nkt = malloc(strlen(newKernelTitle)+3);
4073     strcpy(nkt, "'");
4074     strcat(nkt, newKernelTitle);
4075     strcat(nkt, "'");
4076            newLine = addLine(new, config->cfi, LT_MENUENTRY,
4077      config->primaryIndent, nkt);
4078     free(nkt);
4079     needs &= ~NEED_TITLE;
4080     needs |= NEED_END;
4081     break;
4082        }
4083      case LT_TITLE:      case LT_TITLE:
4084   if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above)   if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above)
4085   char * templabel;   char * templabel;
# Line 2664  int addNewKernel(struct grubConfig * con Line 4111  int addNewKernel(struct grubConfig * con
4111   }   }
4112      }      }
4113    
4114        struct singleLine *endLine = NULL;
4115        endLine = getLineByType(LT_ENTRY_END, new->lines);
4116        if (endLine) {
4117        removeLine(new, endLine);
4118        needs |= NEED_END;
4119        }
4120    
4121      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
4122       * 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,
4123       * all the lines following the entrySeparator.       * all the lines following the entryStart.
4124       */       */
4125      if (needs & NEED_TITLE) {      if (needs & NEED_TITLE) {
4126   newLine = addLine(new, config->cfi, LT_TITLE,   newLine = addLine(new, config->cfi, LT_TITLE,
# Line 2683  int addNewKernel(struct grubConfig * con Line 4137  int addNewKernel(struct grubConfig * con
4137      if (needs & NEED_KERNEL) {      if (needs & NEED_KERNEL) {
4138   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
4139    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
4140        config->cfi)) ?        config->cfi))
4141    LT_MBMODULE : LT_KERNEL,     ? LT_MBMODULE
4142     : preferredLineType(LT_KERNEL, config->cfi),
4143    config->secondaryIndent,    config->secondaryIndent,
4144    newKernelPath + strlen(prefix));    newKernelPath + strlen(prefix));
4145   needs &= ~NEED_KERNEL;   needs &= ~NEED_KERNEL;
# Line 2700  int addNewKernel(struct grubConfig * con Line 4155  int addNewKernel(struct grubConfig * con
4155   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);
4156   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
4157    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
4158        config->cfi)) ?        config->cfi))
4159    LT_MBMODULE : LT_INITRD,     ? LT_MBMODULE
4160       : preferredLineType(LT_INITRD, config->cfi),
4161    config->secondaryIndent,    config->secondaryIndent,
4162    initrdVal);    initrdVal);
4163   free(initrdVal);   free(initrdVal);
4164   needs &= ~NEED_INITRD;   needs &= ~NEED_INITRD;
4165      }      }
4166        if (needs & NEED_DEVTREE) {
4167     newLine = addLine(new, config->cfi, LT_DEVTREE,
4168      config->secondaryIndent,
4169      newDevTreePath);
4170     needs &= ~NEED_DEVTREE;
4171        }
4172    
4173        /* NEEDS_END must be last on bootloaders that need it... */
4174        if (needs & NEED_END) {
4175     newLine = addLine(new, config->cfi, LT_ENTRY_END,
4176     config->secondaryIndent, NULL);
4177     needs &= ~NEED_END;
4178        }
4179    
4180      if (needs) {      if (needs) {
4181   printf(_("grubby: needs=%d, aborting\n"), needs);   printf(_("grubby: needs=%d, aborting\n"), needs);
# Line 2728  static void traceback(int signum) Line 4197  static void traceback(int signum)
4197      memset(array, '\0', sizeof (array));      memset(array, '\0', sizeof (array));
4198      size = backtrace(array, 40);      size = backtrace(array, 40);
4199    
4200      fprintf(stderr, "grubby recieved SIGSEGV!  Backtrace (%ld):\n",      fprintf(stderr, "grubby received SIGSEGV!  Backtrace (%ld):\n",
4201              (unsigned long)size);              (unsigned long)size);
4202      backtrace_symbols_fd(array, size, STDERR_FILENO);      backtrace_symbols_fd(array, size, STDERR_FILENO);
4203      exit(1);      exit(1);
# Line 2736  static void traceback(int signum) Line 4205  static void traceback(int signum)
4205    
4206  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
4207      poptContext optCon;      poptContext optCon;
4208      char * grubConfig = NULL;      const char * grubConfig = NULL;
4209      char * outputFile = NULL;      char * outputFile = NULL;
4210      int arg = 0;      int arg = 0;
4211      int flags = 0;      int flags = 0;
4212      int badImageOkay = 0;      int badImageOkay = 0;
4213        int configureGrub2 = 0;
4214      int configureLilo = 0, configureELilo = 0, configureGrub = 0;      int configureLilo = 0, configureELilo = 0, configureGrub = 0;
4215      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;
4216      int configureExtLinux = 0;      int configureExtLinux = 0;
# Line 2752  int main(int argc, const char ** argv) { Line 4222  int main(int argc, const char ** argv) {
4222      char * newKernelArgs = NULL;      char * newKernelArgs = NULL;
4223      char * newKernelInitrd = NULL;      char * newKernelInitrd = NULL;
4224      char * newKernelTitle = NULL;      char * newKernelTitle = NULL;
4225      char * newKernelVersion = NULL;      char * newDevTreePath = NULL;
4226      char * newMBKernel = NULL;      char * newMBKernel = NULL;
4227      char * newMBKernelArgs = NULL;      char * newMBKernelArgs = NULL;
4228      char * removeMBKernelArgs = NULL;      char * removeMBKernelArgs = NULL;
# Line 2762  int main(int argc, const char ** argv) { Line 4232  int main(int argc, const char ** argv) {
4232      char * removeArgs = NULL;      char * removeArgs = NULL;
4233      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4234      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4235        char * envPath = NULL;
4236      const char * chptr = NULL;      const char * chptr = NULL;
4237      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4238      struct grubConfig * config;      struct grubConfig * config;
4239      struct singleEntry * template = NULL;      struct singleEntry * template = NULL;
4240      int copyDefault = 0, makeDefault = 0;      int copyDefault = 0, makeDefault = 0;
4241      int displayDefault = 0;      int displayDefault = 0;
4242        int displayDefaultIndex = 0;
4243        int displayDefaultTitle = 0;
4244        int defaultIndex = -1;
4245      struct poptOption options[] = {      struct poptOption options[] = {
4246   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
4247      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 2785  int main(int argc, const char ** argv) { Line 4259  int main(int argc, const char ** argv) {
4259   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4260      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
4261      _("bootfs") },      _("bootfs") },
4262  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4263   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
4264      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
4265  #endif  #endif
4266   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
4267      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 2798  int main(int argc, const char ** argv) { Line 4272  int main(int argc, const char ** argv) {
4272        "the kernel referenced by the default image does not exist, "        "the kernel referenced by the default image does not exist, "
4273        "the first linux entry whose kernel does exist is used as the "        "the first linux entry whose kernel does exist is used as the "
4274        "template"), NULL },        "template"), NULL },
4275     { "debug", 0, 0, &debug, 0,
4276        _("print debugging information for failures") },
4277   { "default-kernel", 0, 0, &displayDefault, 0,   { "default-kernel", 0, 0, &displayDefault, 0,
4278      _("display the path of the default kernel") },      _("display the path of the default kernel") },
4279     { "default-index", 0, 0, &displayDefaultIndex, 0,
4280        _("display the index of the default kernel") },
4281     { "default-title", 0, 0, &displayDefaultTitle, 0,
4282        _("display the title of the default kernel") },
4283     { "devtree", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4284        _("device tree file for new stanza"), _("dtb-path") },
4285     { "devtreedir", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4286        _("device tree directory for new stanza"), _("dtb-path") },
4287   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4288      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4289     { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4290        _("force grub2 stanzas to use efi") },
4291     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4292        _("path for environment data"),
4293        _("path") },
4294   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4295      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4296   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
4297      _("configure grub bootloader") },      _("configure grub bootloader") },
4298     { "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,
4299        _("configure grub2 bootloader") },
4300   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,
4301      _("display boot information for specified kernel"),      _("display boot information for specified kernel"),
4302      _("kernel-path") },      _("kernel-path") },
4303   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4304      _("initrd image for the new kernel"), _("initrd-path") },      _("initrd image for the new kernel"), _("initrd-path") },
4305   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4306      _("auxilliary initrd image for things other than the new kernel"), _("initrd-path") },      _("auxiliary initrd image for things other than the new kernel"), _("initrd-path") },
4307   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4308      _("configure lilo bootloader") },      _("configure lilo bootloader") },
4309   { "make-default", 0, 0, &makeDefault, 0,   { "make-default", 0, 0, &makeDefault, 0,
# Line 2832  int main(int argc, const char ** argv) { Line 4323  int main(int argc, const char ** argv) {
4323   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
4324      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
4325        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
4326     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4327        _("make the given entry index the default entry"),
4328        _("entry-index") },
4329   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4330      _("configure silo bootloader") },      _("configure silo bootloader") },
4331   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 2853  int main(int argc, const char ** argv) { Line 4347  int main(int argc, const char ** argv) {
4347    
4348      signal(SIGSEGV, traceback);      signal(SIGSEGV, traceback);
4349    
4350        int i = 0;
4351        for (int j = 1; j < argc; j++)
4352     i += strlen(argv[j]) + 1;
4353        saved_command_line = malloc(i);
4354        if (!saved_command_line) {
4355     fprintf(stderr, "grubby: %m\n");
4356     exit(1);
4357        }
4358        saved_command_line[0] = '\0';
4359        for (int j = 1; j < argc; j++) {
4360     strcat(saved_command_line, argv[j]);
4361     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4362        }
4363    
4364      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4365      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
4366    
# Line 2885  int main(int argc, const char ** argv) { Line 4393  int main(int argc, const char ** argv) {
4393   return 1;   return 1;
4394      }      }
4395    
4396      if ((configureLilo + configureGrub + configureELilo +      if ((configureLilo + configureGrub2 + configureGrub + configureELilo +
4397   configureYaboot + configureSilo + configureZipl +   configureYaboot + configureSilo + configureZipl +
4398   configureExtLinux ) > 1) {   configureExtLinux ) > 1) {
4399   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));
# Line 2894  int main(int argc, const char ** argv) { Line 4402  int main(int argc, const char ** argv) {
4402   fprintf(stderr,   fprintf(stderr,
4403      _("grubby: cannot specify config file with --bootloader-probe\n"));      _("grubby: cannot specify config file with --bootloader-probe\n"));
4404   return 1;   return 1;
4405        } else if (configureGrub2) {
4406     cfi = &grub2ConfigType;
4407     if (envPath)
4408        cfi->envFile = envPath;
4409      } else if (configureLilo) {      } else if (configureLilo) {
4410   cfi = &liloConfigType;   cfi = &liloConfigType;
4411      } else if (configureGrub) {      } else if (configureGrub) {
# Line 2912  int main(int argc, const char ** argv) { Line 4424  int main(int argc, const char ** argv) {
4424      }      }
4425    
4426      if (!cfi) {      if (!cfi) {
4427            if (grub2FindConfig(&grub2ConfigType))
4428        cfi = &grub2ConfigType;
4429     else
4430        #ifdef __ia64__        #ifdef __ia64__
4431   cfi = &eliloConfigType;      cfi = &eliloConfigType;
4432        #elif __powerpc__        #elif __powerpc__
4433   cfi = &yabootConfigType;      cfi = &yabootConfigType;
4434        #elif __sparc__        #elif __sparc__
4435          cfi = &siloConfigType;              cfi = &siloConfigType;
4436        #elif __s390__        #elif __s390__
4437          cfi = &ziplConfigType;              cfi = &ziplConfigType;
4438        #elif __s390x__        #elif __s390x__
4439          cfi = &ziplConfigtype;              cfi = &ziplConfigtype;
4440        #else        #else
4441   cfi = &grubConfigType;      cfi = &grubConfigType;
4442        #endif        #endif
4443      }      }
4444    
4445      if (!grubConfig)      if (!grubConfig) {
4446   grubConfig = cfi->defaultConfig;   if (cfi->findConfig)
4447        grubConfig = cfi->findConfig(cfi);
4448     if (!grubConfig)
4449        grubConfig = cfi->defaultConfig;
4450        }
4451    
4452      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo ||
4453    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
4454    defaultKernel)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
4455        (defaultIndex >= 0))) {
4456   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
4457    "specified option"));    "specified option"));
4458   return 1;   return 1;
4459      }      }
4460    
4461      if ((displayDefault || kernelInfo) && (newKernelVersion || newKernelPath ||      if ((displayDefault || kernelInfo) && (newKernelPath ||
4462     removeKernelPath)) {     removeKernelPath)) {
4463   fprintf(stderr, _("grubby: --default-kernel and --info may not "   fprintf(stderr, _("grubby: --default-kernel and --info may not "
4464    "be used when adding or removing kernels\n"));    "be used when adding or removing kernels\n"));
# Line 2948  int main(int argc, const char ** argv) { Line 4468  int main(int argc, const char ** argv) {
4468      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
4469   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
4470   return 1;   return 1;
4471      } else if (!newKernelPath && (newKernelTitle  || newKernelInitrd ||      } else if (!newKernelPath && (copyDefault ||
4472    newKernelInitrd || copyDefault     ||    (newKernelInitrd && !updateKernelPath)||
4473    makeDefault || extraInitrdCount > 0)) {    makeDefault || extraInitrdCount > 0)) {
4474   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
4475   return 1;   return 1;
# Line 2974  int main(int argc, const char ** argv) { Line 4494  int main(int argc, const char ** argv) {
4494   makeDefault = 1;   makeDefault = 1;
4495   defaultKernel = NULL;   defaultKernel = NULL;
4496      }      }
4497        else if (defaultKernel && (defaultIndex >= 0)) {
4498     fprintf(stderr, _("grubby: --set-default and --set-default-index "
4499      "may not be used together\n"));
4500     return 1;
4501        }
4502    
4503      if (!strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
4504   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
4505   "is used\n"));   "is used\n"));
4506   return 1;   return 1;
4507      }      }
4508    
4509      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
4510   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
4511          && !removeMBKernel) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
4512     && (defaultIndex == -1)) {
4513   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
4514   return 1;   return 1;
4515      }      }
# Line 3010  int main(int argc, const char ** argv) { Line 4536  int main(int argc, const char ** argv) {
4536      }      }
4537    
4538      if (bootloaderProbe) {      if (bootloaderProbe) {
4539   int lrc = 0, grc = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4540   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4541    
4542     const char *grub2config = grub2FindConfig(&grub2ConfigType);
4543     if (grub2config) {
4544        gconfig = readConfig(grub2config, &grub2ConfigType);
4545        if (!gconfig)
4546     gr2c = 1;
4547        else
4548     gr2c = checkForGrub2(gconfig);
4549     }
4550    
4551   if (!access(grubConfigType.defaultConfig, F_OK)) {   const char *grubconfig = grubFindConfig(&grubConfigType);
4552      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);   if (!access(grubconfig, F_OK)) {
4553        gconfig = readConfig(grubconfig, &grubConfigType);
4554      if (!gconfig)      if (!gconfig)
4555   grc = 1;   grc = 1;
4556      else      else
# Line 3029  int main(int argc, const char ** argv) { Line 4565  int main(int argc, const char ** argv) {
4565   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4566   }   }
4567    
4568     if (!access(eliloConfigType.defaultConfig, F_OK)) {
4569        econfig = readConfig(eliloConfigType.defaultConfig,
4570     &eliloConfigType);
4571        if (!econfig)
4572     erc = 1;
4573        else
4574     erc = checkForElilo(econfig);
4575     }
4576    
4577   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4578      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4579      if (!lconfig)      if (!lconfig)
4580   erc = 1;   extrc = 1;
4581      else      else
4582   erc = checkForExtLinux(lconfig);   extrc = checkForExtLinux(lconfig);
4583   }   }
4584    
4585   if (lrc == 1 || grc == 1) return 1;  
4586     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4587        yconfig = readConfig(yabootConfigType.defaultConfig,
4588     &yabootConfigType);
4589        if (!yconfig)
4590     yrc = 1;
4591        else
4592     yrc = checkForYaboot(yconfig);
4593     }
4594    
4595     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4596     erc == 1)
4597        return 1;
4598    
4599   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4600     if (gr2c == 2) printf("grub2\n");
4601   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4602   if (erc == 2) printf("extlinux\n");   if (extrc == 2) printf("extlinux\n");
4603     if (yrc == 2) printf("yaboot\n");
4604     if (erc == 2) printf("elilo\n");
4605    
4606   return 0;   return 0;
4607      }      }
4608    
4609        if (grubConfig == NULL) {
4610     printf("Could not find bootloader configuration file.\n");
4611     exit(1);
4612        }
4613    
4614      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4615      if (!config) return 1;      if (!config) return 1;
4616    
# Line 3055  int main(int argc, const char ** argv) { Line 4620  int main(int argc, const char ** argv) {
4620          char * rootspec;          char * rootspec;
4621    
4622   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4623     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4624     cfi->defaultIsSaved)
4625        config->defaultImage = 0;
4626   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4627   if (!entry) return 0;   if (!entry) return 0;
4628   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4629    
4630   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
4631   if (!line) return 0;   if (!line) return 0;
4632    
4633          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 3067  int main(int argc, const char ** argv) { Line 4635  int main(int argc, const char ** argv) {
4635                 ((rootspec != NULL) ? strlen(rootspec) : 0));                 ((rootspec != NULL) ? strlen(rootspec) : 0));
4636    
4637   return 0;   return 0;
4638    
4639        } else if (displayDefaultTitle) {
4640     struct singleLine * line;
4641     struct singleEntry * entry;
4642    
4643     if (config->defaultImage == -1) return 0;
4644     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4645     cfi->defaultIsSaved)
4646        config->defaultImage = 0;
4647     entry = findEntryByIndex(config, config->defaultImage);
4648     if (!entry) return 0;
4649    
4650     if (!configureGrub2) {
4651      line = getLineByType(LT_TITLE, entry->lines);
4652      if (!line) return 0;
4653      printf("%s\n", line->elements[1].item);
4654    
4655     } else {
4656      char * title;
4657    
4658      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
4659      line = getLineByType(LT_MENUENTRY, entry->lines);
4660      if (!line) return 0;
4661      title = grub2ExtractTitle(line);
4662      if (title)
4663        printf("%s\n", title);
4664     }
4665     return 0;
4666    
4667        } else if (displayDefaultIndex) {
4668            if (config->defaultImage == -1) return 0;
4669     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4670     cfi->defaultIsSaved)
4671        config->defaultImage = 0;
4672            printf("%i\n", config->defaultImage);
4673            return 0;
4674    
4675      } else if (kernelInfo)      } else if (kernelInfo)
4676   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
4677    
# Line 3078  int main(int argc, const char ** argv) { Line 4683  int main(int argc, const char ** argv) {
4683      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4684      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4685      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4686      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4687      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4688      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4689                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
4690        if (updateKernelPath && newKernelInitrd) {
4691        if (newMBKernel) {
4692        if (addMBInitrd(config, newMBKernel, updateKernelPath,
4693     bootPrefix, newKernelInitrd,
4694     newKernelTitle))
4695        return 1;
4696        } else {
4697        if (updateInitrd(config, updateKernelPath, bootPrefix,
4698     newKernelInitrd, newKernelTitle))
4699     return 1;
4700        }
4701        }
4702      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
4703                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
4704                       extraInitrds, extraInitrdCount,                       (const char **)extraInitrds, extraInitrdCount,
4705                       newMBKernel, newMBKernelArgs)) return 1;                       newMBKernel, newMBKernelArgs, newDevTreePath)) return 1;
4706            
4707    
4708      if (numEntries(config) == 0) {      if (numEntries(config) == 0) {
# Line 3095  int main(int argc, const char ** argv) { Line 4712  int main(int argc, const char ** argv) {
4712      }      }
4713    
4714      if (!outputFile)      if (!outputFile)
4715   outputFile = grubConfig;   outputFile = (char *)grubConfig;
4716    
4717      return writeConfig(config, outputFile, bootPrefix);      return writeConfig(config, outputFile, bootPrefix);
4718  }  }

Legend:
Removed from v.914  
changed lines
  Added in v.2983