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 532 by niro, Sat Sep 1 22:45:15 2007 UTC trunk/grubby/grubby.c revision 2991 by niro, Thu Jun 30 10:34:31 2016 UTC
# Line 1  Line 1 
1  /* Copyright (C) 2001-2005 Red Hat, Inc.  /*
2     * grubby.c
3     This program is free software; you can redistribute it and/or   *
4     modify it under the terms of the General Public License as published   * Copyright (C) 2001-2008 Red Hat, Inc.
5     by the Free Software Foundation; either version 2 of the License, or   * All rights reserved.
6     (at your option) any later version.   *
7     * This program is free software; you can redistribute it and/or modify
8     This program is distributed in the hope that it will be useful,   * it under the terms of the GNU General Public License as published by
9     but WITHOUT ANY WARRANTY; without even the implied warranty of   * the Free Software Foundation; either version 2 of the License, or
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   * (at your option) any later version.
11     General Public License for more details.   *
12     * This program is distributed in the hope that it will be useful,
13     You should have received a copy of the GNU General Public   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     License along with this program; if not, write to the Free   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA   * GNU General Public License for more details.
16     02111-1307 USA.  */   *
17     * You should have received a copy of the GNU General Public License
18     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19     */
20    
21    #ifndef _GNU_SOURCE
22    #define _GNU_SOURCE
23    #endif
24  #include <ctype.h>  #include <ctype.h>
25  #include <errno.h>  #include <errno.h>
26  #include <fcntl.h>  #include <fcntl.h>
# Line 25  Line 31 
31  #include <string.h>  #include <string.h>
32  #include <sys/stat.h>  #include <sys/stat.h>
33  #include <unistd.h>  #include <unistd.h>
34    #include <libgen.h>
35    #include <execinfo.h>
36    #include <signal.h>
37    #include <blkid/blkid.h>
38    
39    #include "log.h"
40    
41    #ifndef DEBUG
42    #define DEBUG 0
43    #endif
44    
45    #if DEBUG
46    #define dbgPrintf(format, args...) fprintf(stderr, format , ## args)
47    #else
48    #define dbgPrintf(format, args...)
49    #endif
50    
51  #include "mount_by_label.h"  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 */
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;
74      char * indent;      char * indent;
75  };  };
76    
77  enum lineType_e { LT_WHITESPACE, LT_TITLE, LT_KERNEL, LT_INITRD, LT_DEFAULT,  enum lineType_e {
78         LT_UNKNOWN, LT_ROOT, LT_FALLBACK, LT_KERNELARGS, LT_BOOT,      LT_WHITESPACE   = 1 << 0,
79         LT_BOOTROOT, LT_LBA, LT_MBMODULE, LT_OTHER, LT_GENERIC };      LT_TITLE        = 1 << 1,
80        LT_KERNEL       = 1 << 2,
81        LT_INITRD       = 1 << 3,
82        LT_HYPER        = 1 << 4,
83        LT_DEFAULT      = 1 << 5,
84        LT_MBMODULE     = 1 << 6,
85        LT_ROOT         = 1 << 7,
86        LT_FALLBACK     = 1 << 8,
87        LT_KERNELARGS   = 1 << 9,
88        LT_BOOT         = 1 << 10,
89        LT_BOOTROOT     = 1 << 11,
90        LT_LBA          = 1 << 12,
91        LT_OTHER        = 1 << 13,
92        LT_GENERIC      = 1 << 14,
93        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 {
106      char * indent;      char * indent;
# Line 61  struct singleEntry { Line 121  struct singleEntry {
121    
122  #define GRUB_CONFIG_NO_DEFAULT    (1 << 0) /* don't write out default=0 */  #define GRUB_CONFIG_NO_DEFAULT    (1 << 0) /* don't write out default=0 */
123    
124  #define KERNEL_KERNEL    (1 << 0)  /* These defines are (only) used in addNewKernel() */
125  #define KERNEL_INITRD    (1 << 2)  #define NEED_KERNEL  (1 << 0)
126  #define KERNEL_TITLE    (1 << 3)  #define NEED_INITRD  (1 << 1)
127  #define KERNEL_ARGS    (1 << 4)  #define NEED_TITLE   (1 << 2)
128  #define KERNEL_MB           (1 << 5)  #define NEED_ARGS    (1 << 3)
129    #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;
139      enum lineType_e type;      enum lineType_e type;
140      char nextChar;      char nextChar;
141  } ;      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;
173        int mbInitRdIsModule;
174        int mbConcatArgs;
175        int mbAllowExtraInitRds;
176        char *envFile;
177  };  };
178    
179  struct keywordTypes grubKeywords[] = {  struct keywordTypes grubKeywords[] = {
# Line 94  struct keywordTypes grubKeywords[] = { Line 182  struct keywordTypes grubKeywords[] = {
182      { "default",    LT_DEFAULT,    ' ' },      { "default",    LT_DEFAULT,    ' ' },
183      { "fallback",   LT_FALLBACK,    ' ' },      { "fallback",   LT_FALLBACK,    ' ' },
184      { "kernel",    LT_KERNEL,    ' ' },      { "kernel",    LT_KERNEL,    ' ' },
185      { "initrd",    LT_INITRD,    ' ' },      { "initrd",    LT_INITRD,    ' ', ' ' },
186      { "module",     LT_MBMODULE,    ' ' },      { "module",     LT_MBMODULE,    ' ' },
187        { "kernel",     LT_HYPER,       ' ' },
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      "/boot/grub/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        .titlePosition = 1,
226    };
227    
228    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        /* need to ensure that ' does not match " as we search */
456        char quote_char = *current;
457        
458        resultMaxSize = sizeOfSingleLine(line);
459        result = malloc(resultMaxSize);
460        snprintf(result, resultMaxSize, "%s", ++current);
461        
462        i++;
463        for (; i < line->numElements; ++i) {
464     current = line->elements[i].item;
465     current_len = strlen(current);
466     current_indent = line->elements[i].indent;
467     current_indent_len = strlen(current_indent);
468    
469     strncat(result, current_indent, current_indent_len);
470     if (current[current_len-1] != quote_char) {
471        strncat(result, current, current_len);
472     } else {
473        strncat(result, current, current_len - 1);
474        break;
475     }
476        }
477        return result;
478    }
479    
480    struct configFileInfo grub2ConfigType = {
481        .findConfig = grub2FindConfig,
482        .getEnv = grub2GetEnv,
483        .setEnv = grub2SetEnv,
484        .keywords = grub2Keywords,
485        .defaultIsIndex = 1,
486        .defaultSupportSaved = 1,
487        .defaultIsVariable = 1,
488        .entryStart = LT_MENUENTRY,
489        .entryEnd = LT_ENTRY_END,
490        .titlePosition = 1,
491        .needsBootPrefix = 1,
492        .mbHyperFirst = 1,
493        .mbInitRdIsModule = 1,
494        .mbAllowExtraInitRds = 1,
495  };  };
496    
497  struct keywordTypes yabootKeywords[] = {  struct keywordTypes yabootKeywords[] = {
# Line 142  struct keywordTypes yabootKeywords[] = { Line 525  struct keywordTypes yabootKeywords[] = {
525      { "partition",  LT_GENERIC,    '=' },      { "partition",  LT_GENERIC,    '=' },
526      { "device",    LT_GENERIC,    '=' },      { "device",    LT_GENERIC,    '=' },
527      { "fstype",    LT_GENERIC,    '=' },      { "fstype",    LT_GENERIC,    '=' },
528      { "initrd",    LT_INITRD,    '=' },      { "initrd",    LT_INITRD,    '=', ';' },
529      { "append",    LT_KERNELARGS,  '=' },      { "append",    LT_KERNELARGS,  '=' },
530      { "boot",    LT_BOOT,    '=' },      { "boot",    LT_BOOT,    '=' },
531      { "lba",    LT_LBA,    ' ' },      { "lba",    LT_LBA,    ' ' },
# Line 162  struct keywordTypes liloKeywords[] = { Line 545  struct keywordTypes liloKeywords[] = {
545      { NULL,    0, 0 },      { NULL,    0, 0 },
546  };  };
547    
548    struct keywordTypes eliloKeywords[] = {
549        { "label",    LT_TITLE,    '=' },
550        { "root",    LT_ROOT,    '=' },
551        { "default",    LT_DEFAULT,    '=' },
552        { "image",    LT_KERNEL,    '=' },
553        { "initrd",    LT_INITRD,    '=' },
554        { "append",    LT_KERNELARGS,  '=' },
555        { "vmm",    LT_HYPER,       '=' },
556        { NULL,    0, 0 },
557    };
558    
559  struct keywordTypes siloKeywords[] = {  struct keywordTypes siloKeywords[] = {
560      { "label",    LT_TITLE,    '=' },      { "label",    LT_TITLE,    '=' },
561      { "root",    LT_ROOT,    '=' },      { "root",    LT_ROOT,    '=' },
# Line 183  struct keywordTypes ziplKeywords[] = { Line 577  struct keywordTypes ziplKeywords[] = {
577      { NULL,         0, 0 },      { NULL,         0, 0 },
578  };  };
579    
580    struct keywordTypes extlinuxKeywords[] = {
581        { "label",    LT_TITLE,    ' ' },
582        { "root",    LT_ROOT,    ' ' },
583        { "default",    LT_DEFAULT,    ' ' },
584        { "kernel",    LT_KERNEL,    ' ' },
585        { "initrd",    LT_INITRD,      ' ', ',' },
586        { "append",    LT_KERNELARGS,  ' ' },
587        { "prompt",     LT_UNKNOWN,     ' ' },
588        { "fdt",        LT_DEVTREE,     ' ' },
589        { "fdtdir",     LT_DEVTREE,     ' ' },
590        { NULL,    0, 0 },
591    };
592    int useextlinuxmenu;
593  struct configFileInfo eliloConfigType = {  struct configFileInfo eliloConfigType = {
594      "/boot/efi/EFI/redhat/elilo.conf",    /* defaultConfig */      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",
595      liloKeywords,    /* keywords */      .keywords = eliloKeywords,
596      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
597      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
598      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
599      1,                    /* needsBootPrefix */      .mbConcatArgs = 1,
600      1,    /* argsInQuotes */      .titlePosition = 1,
     0,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
601  };  };
602    
603  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
604      "/etc/lilo.conf",    /* defaultConfig */      .defaultConfig = "/etc/lilo.conf",
605      liloKeywords,    /* keywords */      .keywords = liloKeywords,
606      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
607      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
608      LT_KERNEL,    /* entrySeparator */      .maxTitleLength = 15,
609      0,    /* needsBootPrefix */      .titlePosition = 1,
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
610  };  };
611    
612  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
613      "/etc/yaboot.conf",    /* defaultConfig */      .defaultConfig = "/etc/yaboot.conf",
614      yabootKeywords,    /* keywords */      .keywords = yabootKeywords,
615      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
616      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
617      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
618      1,    /* needsBootPrefix */      .maxTitleLength = 15,
619      1,    /* argsInQuotes */      .mbAllowExtraInitRds = 1,
620      15,    /* maxTitleLength */      .titlePosition = 1,
     0,                                      /* titleBracketed */  
621  };  };
622    
623  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
624      "/etc/silo.conf",    /* defaultConfig */      .defaultConfig = "/etc/silo.conf",
625      siloKeywords,    /* keywords */      .keywords = siloKeywords,
626      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
627      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
628      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
629      1,    /* needsBootPrefix */      .maxTitleLength = 15,
630      1,    /* argsInQuotes */      .titlePosition = 1,
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
631  };  };
632    
633  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
634      "/etc/zipl.conf",    /* defaultConfig */      .defaultConfig = "/etc/zipl.conf",
635      ziplKeywords,    /* keywords */      .keywords = ziplKeywords,
636      0,    /* defaultIsIndex */      .entryStart = LT_TITLE,
637      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
638      LT_TITLE,    /* entrySeparator */      .titleBracketed = 1,
639      0,    /* needsBootPrefix */  };
640      1,    /* argsInQuotes */  
641      15,    /* maxTitleLength */  struct configFileInfo extlinuxConfigType = {
642      1,                                      /* titleBracketed */      .defaultConfig = "/boot/extlinux/extlinux.conf",
643        .keywords = extlinuxKeywords,
644        .caseInsensitive = 1,
645        .entryStart = LT_TITLE,
646        .needsBootPrefix = 1,
647        .maxTitleLength = 255,
648        .mbAllowExtraInitRds = 1,
649        .defaultIsUnquoted = 1,
650        .titlePosition = 1,
651  };  };
652    
653  struct grubConfig {  struct grubConfig {
# Line 255  struct grubConfig { Line 662  struct grubConfig {
662      struct configFileInfo * cfi;      struct configFileInfo * cfi;
663  };  };
664    
665    blkid_cache blkid;
666    
667  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);
668  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
669       const char * path, const char * prefix,       const char * path, const char * prefix,
670       int * index);       int * index);
671  static char * strndup(char * from, int len);  struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
672          int * index);
673  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
674  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
675    struct singleLine * lineDup(struct singleLine * line);
676  static void lineFree(struct singleLine * line);  static void lineFree(struct singleLine * line);
677  static int lineWrite(FILE * out, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine * line,
678       struct configFileInfo * cfi);       struct configFileInfo * cfi);
679  static int getNextLine(char ** bufPtr, struct singleLine * line,  static int getNextLine(char ** bufPtr, struct singleLine * line,
680         struct configFileInfo * cfi);         struct configFileInfo * cfi);
681  static char * getRootSpecifier(char * str);  static char * getRootSpecifier(char * str);
682    static void requote(struct singleLine *line, struct configFileInfo * cfi);
683  static char * strndup(char * from, int len) {  static void insertElement(struct singleLine * line,
684      char * to;    const char * item, int insertHere,
685      struct configFileInfo * cfi);
686      to = malloc(len + 1);  static void removeElement(struct singleLine * line, int removeHere);
687      strncpy(to, from, len);  static struct keywordTypes * getKeywordByType(enum lineType_e type,
688      to[len] = '\0';        struct configFileInfo * cfi);
689    static enum lineType_e getTypeByKeyword(char * keyword,
690      return to;   struct configFileInfo * cfi);
691  }  static struct singleLine * getLineByType(enum lineType_e type,
692     struct singleLine * line);
693    static int checkForExtLinux(struct grubConfig * config);
694    struct singleLine * addLineTmpl(struct singleEntry * entry,
695                                    struct singleLine * tmplLine,
696                                    struct singleLine * prevLine,
697                                    const char * val,
698     struct configFileInfo * cfi);
699    struct singleLine *  addLine(struct singleEntry * entry,
700                                 struct configFileInfo * cfi,
701                                 enum lineType_e type, char * defaultIndent,
702                                 const char * val);
703    
704  static char * sdupprintf(const char *format, ...)  static char * sdupprintf(const char *format, ...)
705  #ifdef __GNUC__  #ifdef __GNUC__
# Line 313  static char * sdupprintf(const char *for Line 734  static char * sdupprintf(const char *for
734      return buf;      return buf;
735  }  }
736    
737    static enum lineType_e preferredLineType(enum lineType_e type,
738     struct configFileInfo *cfi) {
739        if (isEfi && cfi == &grub2ConfigType) {
740     switch (type) {
741     case LT_KERNEL:
742        return isEfiOnly ? LT_KERNEL : LT_KERNEL_EFI;
743     case LT_INITRD:
744        return isEfiOnly ? LT_INITRD : LT_INITRD_EFI;
745     default:
746        return type;
747     }
748    #if defined(__i386__) || defined(__x86_64__)
749        } else if (cfi == &grub2ConfigType) {
750     switch (type) {
751     case LT_KERNEL:
752        return LT_KERNEL_16;
753     case LT_INITRD:
754        return LT_INITRD_16;
755     default:
756        return type;
757     }
758    #endif
759        }
760        return type;
761    }
762    
763    static struct keywordTypes * getKeywordByType(enum lineType_e type,
764          struct configFileInfo * cfi) {
765        for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
766     if (kw->type == type)
767        return kw;
768        }
769        return NULL;
770    }
771    
772    static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) {
773        struct keywordTypes *kt = getKeywordByType(type, cfi);
774        if (kt)
775     return kt->key;
776        return "unknown";
777    }
778    
779    static char * getpathbyspec(char *device) {
780        if (!blkid)
781            blkid_get_cache(&blkid, NULL);
782    
783        return blkid_get_devname(blkid, device, NULL);
784    }
785    
786    static char * getuuidbydev(char *device) {
787        if (!blkid)
788     blkid_get_cache(&blkid, NULL);
789    
790        return blkid_get_tag_value(blkid, "UUID", device);
791    }
792    
793    static enum lineType_e getTypeByKeyword(char * keyword,
794     struct configFileInfo * cfi) {
795        for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
796     if (cfi->caseInsensitive) {
797        if (!strcasecmp(keyword, kw->key))
798                    return kw->type;
799     } else {
800        if (!strcmp(keyword, kw->key))
801            return kw->type;
802     }
803        }
804        return LT_UNKNOWN;
805    }
806    
807    static struct singleLine * getLineByType(enum lineType_e type,
808     struct singleLine * line) {
809        dbgPrintf("getLineByType(%d): ", type);
810        for (; line; line = line->next) {
811     dbgPrintf("%d:%s ", line->type,
812      line->numElements ? line->elements[0].item : "(empty)");
813     if (line->type & type) break;
814        }
815        dbgPrintf(line ? "\n" : " (failed)\n");
816        return line;
817    }
818    
819  static int isBracketedTitle(struct singleLine * line) {  static int isBracketedTitle(struct singleLine * line) {
820      if ((*line->elements[0].item == '[') && (line->numElements == 1)) {      if (line->numElements == 1 && *line->elements[0].item == '[') {
821          int len = strlen(line->elements[0].item);          int len = strlen(line->elements[0].item);
822          if (*(line->elements[0].item + len - 1) == ']') {          if (*(line->elements[0].item + len - 1) == ']') {
823              /* FIXME: this is a hack... */              /* FIXME: this is a hack... */
# Line 326  static int isBracketedTitle(struct singl Line 829  static int isBracketedTitle(struct singl
829      return 0;      return 0;
830  }  }
831    
832  /* figure out if this is a entry separator */  static int isEntryStart(struct singleLine * line,
 static int isEntrySeparator(struct singleLine * line,  
833                              struct configFileInfo * cfi) {                              struct configFileInfo * cfi) {
834      if (line->type == LT_WHITESPACE)      return line->type == cfi->entryStart || line->type == LT_OTHER ||
835   return 0;   (cfi->titleBracketed && isBracketedTitle(line));
     if (line->type == cfi->entrySeparator)  
         return 1;  
     if (line->type == LT_OTHER)  
         return 1;  
     if (cfi->titleBracketed && isBracketedTitle(line)) {  
         return 1;  
     }  
     return 0;  
836  }  }
837    
838  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
839  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct grubConfig *cfg, struct singleLine * line) {
840      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it */
841      char * title;      char * title = NULL;
842      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
843      title++;   char *tmp = line->elements[cfg->cfi->titlePosition].item;
844      *(title + strlen(title) - 1) = '\0';   if (cfg->cfi->titleBracketed) {
845        tmp++;
846        title = strdup(tmp);
847        *(title + strlen(title) - 1) = '\0';
848     } else {
849        title = strdup(tmp);
850     }
851        } else if (line->type == LT_MENUENTRY)
852     title = strdup(line->elements[1].item);
853        else
854     return NULL;
855      return title;      return title;
856  }  }
857    
# Line 389  static void lineInit(struct singleLine * Line 893  static void lineInit(struct singleLine *
893      line->next = NULL;      line->next = NULL;
894  }  }
895    
896  static void lineFree(struct singleLine * line) {  struct singleLine * lineDup(struct singleLine * line) {
897      int i;      struct singleLine * newLine = malloc(sizeof(*newLine));
898    
899        newLine->indent = strdup(line->indent);
900        newLine->next = NULL;
901        newLine->type = line->type;
902        newLine->numElements = line->numElements;
903        newLine->elements = malloc(sizeof(*newLine->elements) *
904           newLine->numElements);
905    
906        for (int i = 0; i < newLine->numElements; i++) {
907     newLine->elements[i].indent = strdup(line->elements[i].indent);
908     newLine->elements[i].item = strdup(line->elements[i].item);
909        }
910    
911        return newLine;
912    }
913    
914    static void lineFree(struct singleLine * line) {
915      if (line->indent) free(line->indent);      if (line->indent) free(line->indent);
916    
917      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
918   free(line->elements[i].item);   free(line->elements[i].item);
919   free(line->elements[i].indent);   free(line->elements[i].indent);
920      }      }
# Line 405  static void lineFree(struct singleLine * Line 925  static void lineFree(struct singleLine *
925    
926  static int lineWrite(FILE * out, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine * line,
927       struct configFileInfo * cfi) {       struct configFileInfo * cfi) {
     int i;  
   
928      if (fprintf(out, "%s", line->indent) == -1) return -1;      if (fprintf(out, "%s", line->indent) == -1) return -1;
929    
930      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
931     /* Need to handle this, because we strip the quotes from
932     * menuentry when read it. */
933     if (line->type == LT_MENUENTRY && i == 1) {
934        if(!isquote(*line->elements[i].item)) {
935     int substring = 0;
936     /* If the line contains nested quotes, we did not strip
937     * the "interna" quotes and we must use the right quotes
938     * again when writing the updated file. */
939     for (int j = i; j < line->numElements; j++) {
940        if (strchr(line->elements[i].item, '\'') != NULL) {
941           substring = 1;
942           fprintf(out, "\"%s\"", line->elements[i].item);
943           break;
944        }
945     }
946     if (!substring)
947        fprintf(out, "\'%s\'", line->elements[i].item);
948        } else {
949     fprintf(out, "%s", line->elements[i].item);
950        }
951        fprintf(out, "%s", line->elements[i].indent);
952    
953        continue;
954     }
955    
956   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)
957      if (fputc('"', out) == EOF) return -1;      if (fputc('"', out) == EOF) return -1;
958    
959   if (fprintf(out, "%s", line->elements[i].item) == -1) return -1;   if (fprintf(out, "%s", line->elements[i].item) == -1) return -1;
960   if (fprintf(out, "%s", line->elements[i].indent) == -1) return -1;   if (i < line->numElements - 1)
961        if (fprintf(out, "%s", line->elements[i].indent) == -1) return -1;
962      }      }
963    
964      if (line->type == LT_KERNELARGS && cfi->argsInQuotes)      if (line->type == LT_KERNELARGS && cfi->argsInQuotes)
# Line 433  static int getNextLine(char ** bufPtr, s Line 977  static int getNextLine(char ** bufPtr, s
977      char * chptr;      char * chptr;
978      int elementsAlloced = 0;      int elementsAlloced = 0;
979      struct lineElement * element;      struct lineElement * element;
     struct keywordTypes * keywords = cfi->keywords;  
980      int first = 1;      int first = 1;
     int i;  
981    
982      lineFree(line);      lineFree(line);
983    
# Line 489  static int getNextLine(char ** bufPtr, s Line 1031  static int getNextLine(char ** bufPtr, s
1031      if (!line->numElements)      if (!line->numElements)
1032   line->type = LT_WHITESPACE;   line->type = LT_WHITESPACE;
1033      else {      else {
1034   for (i = 0; keywords[i].key; i++)   line->type = getTypeByKeyword(line->elements[0].item, cfi);
1035      if (!strcmp(line->elements[0].item, keywords[i].key)) break;   if (line->type == LT_UNKNOWN) {
   
  if (keywords[i].key) {  
     line->type = keywords[i].type;  
  } else {  
     line->type = LT_UNKNOWN;  
               
1036              /* zipl does [title] instead of something reasonable like all              /* zipl does [title] instead of something reasonable like all
1037               * the other boot loaders.  kind of ugly */               * the other boot loaders.  kind of ugly */
1038              if (cfi->titleBracketed && isBracketedTitle(line)) {              if (cfi->titleBracketed && isBracketedTitle(line)) {
# Line 510  static int getNextLine(char ** bufPtr, s Line 1046  static int getNextLine(char ** bufPtr, s
1046      if (*line->elements[0].item == '#') {      if (*line->elements[0].item == '#') {
1047   char * fullLine;   char * fullLine;
1048   int len;   int len;
  int i;  
1049    
1050   len = strlen(line->indent);   len = strlen(line->indent);
1051   for (i = 0; i < line->numElements; i++)   for (int i = 0; i < line->numElements; i++)
1052      len += strlen(line->elements[i].item) +      len += strlen(line->elements[i].item) +
1053     strlen(line->elements[i].indent);     strlen(line->elements[i].indent);
1054    
# Line 522  static int getNextLine(char ** bufPtr, s Line 1057  static int getNextLine(char ** bufPtr, s
1057   free(line->indent);   free(line->indent);
1058   line->indent = fullLine;   line->indent = fullLine;
1059    
1060   for (i = 0; i < line->numElements; i++) {   for (int i = 0; i < line->numElements; i++) {
1061      strcat(fullLine, line->elements[i].item);      strcat(fullLine, line->elements[i].item);
1062      strcat(fullLine, line->elements[i].indent);      strcat(fullLine, line->elements[i].indent);
1063      free(line->elements[i].item);      free(line->elements[i].item);
# Line 532  static int getNextLine(char ** bufPtr, s Line 1067  static int getNextLine(char ** bufPtr, s
1067   line->type = LT_WHITESPACE;   line->type = LT_WHITESPACE;
1068   line->numElements = 0;   line->numElements = 0;
1069      }      }
1070     } else {
1071     struct keywordTypes *kw;
1072    
1073     kw = getKeywordByType(line->type, cfi);
1074    
1075     /* space isn't the only separator, we need to split
1076     * elements up more
1077     */
1078     if (!isspace(kw->separatorChar)) {
1079        char indent[2] = "";
1080        indent[0] = kw->separatorChar;
1081        for (int i = 1; i < line->numElements; i++) {
1082     char *p;
1083     int numNewElements;
1084    
1085     numNewElements = 0;
1086     p = line->elements[i].item;
1087     while (*p != '\0') {
1088     if (*p == kw->separatorChar)
1089     numNewElements++;
1090     p++;
1091     }
1092     if (line->numElements + numNewElements >= elementsAlloced) {
1093     elementsAlloced += numNewElements + 5;
1094     line->elements = realloc(line->elements,
1095        sizeof(*line->elements) * elementsAlloced);
1096     }
1097    
1098     for (int j = line->numElements; j > i; j--) {
1099     line->elements[j + numNewElements] = line->elements[j];
1100     }
1101     line->numElements += numNewElements;
1102    
1103     p = line->elements[i].item;
1104     while (*p != '\0') {
1105    
1106     while (*p != kw->separatorChar && *p != '\0') p++;
1107     if (*p == '\0') {
1108     break;
1109     }
1110    
1111     line->elements[i + 1].indent = line->elements[i].indent;
1112     line->elements[i].indent = strdup(indent);
1113     *p++ = '\0';
1114     i++;
1115     line->elements[i].item = strdup(p);
1116     }
1117        }
1118     }
1119   }   }
1120      }      }
1121    
1122      return 0;      return 0;
1123  }  }
1124    
1125    static int isnumber(const char *s)
1126    {
1127        int i;
1128        for (i = 0; s[i] != '\0'; i++)
1129     if (s[i] < '0' || s[i] > '9')
1130        return 0;
1131        return i;
1132    }
1133    
1134  static struct grubConfig * readConfig(const char * inName,  static struct grubConfig * readConfig(const char * inName,
1135        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
1136      int in;      int in;
# Line 549  static struct grubConfig * readConfig(co Line 1142  static struct grubConfig * readConfig(co
1142      struct singleLine * last = NULL, * line, * defaultLine = NULL;      struct singleLine * last = NULL, * line, * defaultLine = NULL;
1143      char * end;      char * end;
1144      struct singleEntry * entry = NULL;      struct singleEntry * entry = NULL;
1145      int i, len;      int len;
1146      char * buf;      char * buf;
1147    
1148      if (!strcmp(inName, "-")) {      if (inName == NULL) {
1149            printf("Could not find bootloader configuration\n");
1150            exit(1);
1151        } else if (!strcmp(inName, "-")) {
1152   in = 0;   in = 0;
1153      } else {      } else {
1154   if ((in = open(inName, O_RDONLY)) < 0) {   if ((in = open(inName, O_RDONLY)) < 0) {
# Line 595  static struct grubConfig * readConfig(co Line 1191  static struct grubConfig * readConfig(co
1191      cfg->secondaryIndent = strdup(line->indent);      cfg->secondaryIndent = strdup(line->indent);
1192   }   }
1193    
1194   if (isEntrySeparator(line, cfi)) {   if (isEntryStart(line, cfi) || (cfg->entries && !sawEntry)) {
1195      sawEntry = 1;      sawEntry = 1;
1196      if (!entry) {      if (!entry) {
1197   cfg->entries = malloc(sizeof(*entry));   cfg->entries = malloc(sizeof(*entry));
# Line 611  static struct grubConfig * readConfig(co Line 1207  static struct grubConfig * readConfig(co
1207      entry->next = NULL;      entry->next = NULL;
1208   }   }
1209    
1210   if (line->type == LT_DEFAULT && line->numElements == 2) {   if (line->type == LT_SET_VARIABLE) {
1211      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      dbgPrintf("found 'set' command (%d elements): ", line->numElements);
1212      defaultLine = line;      dbgPrintf("%s", line->indent);
1213        for (int i = 0; i < line->numElements; i++)
1214     dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);
1215        dbgPrintf("\n");
1216        struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1217        if (kwType && line->numElements == 3 &&
1218        !strcmp(line->elements[1].item, kwType->key) &&
1219        !is_special_grub2_variable(line->elements[2].item)) {
1220     dbgPrintf("Line sets default config\n");
1221     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1222     defaultLine = line;
1223        }
1224    
1225            } else if (iskernel(line->type)) {
1226        /* if by some freak chance this is multiboot and the "module"
1227         * lines came earlier in the template, make sure to use LT_HYPER
1228         * instead of LT_KERNEL now
1229         */
1230        if (entry && entry->multiboot)
1231     line->type = LT_HYPER;
1232    
1233          } else if (line->type == LT_MBMODULE) {          } else if (line->type == LT_MBMODULE) {
1234        /* go back and fix the LT_KERNEL line to indicate LT_HYPER
1235         * instead, now that we know this is a multiboot entry.
1236         * This only applies to grub, but that's the only place we
1237         * should find LT_MBMODULE lines anyway.
1238         */
1239        for (struct singleLine *l = entry->lines; l; l = l->next) {
1240     if (l->type == LT_HYPER)
1241        break;
1242     else if (iskernel(l->type)) {
1243        l->type = LT_HYPER;
1244        break;
1245     }
1246        }
1247              entry->multiboot = 1;              entry->multiboot = 1;
1248    
1249     } else if (line->type == LT_HYPER) {
1250        entry->multiboot = 1;
1251    
1252   } else if (line->type == LT_FALLBACK && line->numElements == 2) {   } else if (line->type == LT_FALLBACK && line->numElements == 2) {
1253      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);
1254      if (*end) cfg->fallbackImage = -1;      if (*end) cfg->fallbackImage = -1;
1255   } else if (line->type == LT_TITLE && line->numElements > 1) {  
1256      /* make the title a single argument (undoing our parsing) */   } else if ((line->type == LT_DEFAULT && cfi->defaultIsUnquoted) ||
1257                    (line->type == LT_TITLE && line->numElements > 1)) {
1258        /* make the title/default a single argument (undoing our parsing) */
1259      len = 0;      len = 0;
1260      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1261   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
1262   len += strlen(line->elements[i].indent);   len += strlen(line->elements[i].indent);
1263      }      }
1264      buf = malloc(len + 1);      buf = malloc(len + 1);
1265      *buf = '\0';      *buf = '\0';
1266    
1267      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1268   strcat(buf, line->elements[i].item);   strcat(buf, line->elements[i].item);
1269   free(line->elements[i].item);   free(line->elements[i].item);
1270    
# Line 643  static struct grubConfig * readConfig(co Line 1278  static struct grubConfig * readConfig(co
1278      line->elements[line->numElements - 1].indent;      line->elements[line->numElements - 1].indent;
1279      line->elements[1].item = buf;      line->elements[1].item = buf;
1280      line->numElements = 2;      line->numElements = 2;
1281     } else if (line->type == LT_MENUENTRY && line->numElements > 3) {
1282        /* let --remove-kernel="TITLE=what" work */
1283        len = 0;
1284        char *extras;
1285        char *title;
1286        /* initially unseen value */
1287        char quote_char = '\0';
1288    
1289        for (int i = 1; i < line->numElements; i++) {
1290     len += strlen(line->elements[i].item);
1291     len += strlen(line->elements[i].indent);
1292        }
1293        buf = malloc(len + 1);
1294        *buf = '\0';
1295    
1296        /* allocate mem for extra flags. */
1297        extras = malloc(len + 1);
1298        *extras = '\0';
1299    
1300        /* get title. */
1301        for (int i = 0; i < line->numElements; i++) {
1302     if (!strcmp(line->elements[i].item, "menuentry"))
1303        continue;
1304     if (isquote(*line->elements[i].item) && quote_char == '\0') {
1305        /* ensure we properly pair off quotes */
1306        quote_char = *line->elements[i].item;
1307        title = line->elements[i].item + 1;
1308     } else {
1309        title = line->elements[i].item;
1310     }
1311    
1312     len = strlen(title);
1313            if (title[len-1] == quote_char) {
1314        strncat(buf, title,len-1);
1315        break;
1316     } else {
1317        strcat(buf, title);
1318        strcat(buf, line->elements[i].indent);
1319     }
1320        }
1321    
1322        /* get extras */
1323        int count = 0;
1324        quote_char = '\0';
1325        for (int i = 0; i < line->numElements; i++) {
1326     if (count >= 2) {
1327        strcat(extras, line->elements[i].item);
1328        strcat(extras, line->elements[i].indent);
1329     }
1330    
1331     if (!strcmp(line->elements[i].item, "menuentry"))
1332        continue;
1333    
1334     /* count ' or ", there should be two in menuentry line. */
1335     if (isquote(*line->elements[i].item) && quote_char == '\0') {
1336        /* ensure we properly pair off quotes */
1337                quote_char = *line->elements[i].item;
1338        count++;
1339     }
1340    
1341     len = strlen(line->elements[i].item);
1342    
1343     if (line->elements[i].item[len -1] == quote_char)
1344        count++;
1345    
1346     /* ok, we get the final ' or ", others are extras. */
1347                }
1348        line->elements[1].indent =
1349     line->elements[line->numElements - 2].indent;
1350        line->elements[1].item = buf;
1351        line->elements[2].indent =
1352     line->elements[line->numElements - 2].indent;
1353        line->elements[2].item = extras;
1354        line->numElements = 3;
1355   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {
1356      /* Strip off any " which may be present; they'll be put back      /* Strip off any " which may be present; they'll be put back
1357         on write. This is one of the few (the only?) places that grubby         on write. This is one of the few (the only?) places that grubby
# Line 651  static struct grubConfig * readConfig(co Line 1360  static struct grubConfig * readConfig(co
1360      if (line->numElements >= 2) {      if (line->numElements >= 2) {
1361   int last, len;   int last, len;
1362    
1363   if (*line->elements[1].item == '"')   if (isquote(*line->elements[1].item))
1364      memcpy(line->elements[1].item, line->elements[1].item + 1,      memmove(line->elements[1].item, line->elements[1].item + 1,
1365     strlen(line->elements[1].item + 1) + 1);      strlen(line->elements[1].item + 1) + 1);
1366    
1367   last = line->numElements - 1;   last = line->numElements - 1;
1368   len = strlen(line->elements[last].item) - 1;   len = strlen(line->elements[last].item) - 1;
1369   if (line->elements[last].item[len] == '"')   if (isquote(line->elements[last].item[len]))
1370      line->elements[last].item[len] = '\0';      line->elements[last].item[len] = '\0';
1371      }      }
1372     }
1373    
1374     if (line->type == LT_DEFAULT && line->numElements == 2) {
1375        cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1376        defaultLine = line;
1377   }   }
1378    
1379   /* If we find a generic config option which should live at the   /* If we find a generic config option which should live at the
# Line 680  static struct grubConfig * readConfig(co Line 1393  static struct grubConfig * readConfig(co
1393   movedLine = 1;   movedLine = 1;
1394   continue; /* without setting 'last' */   continue; /* without setting 'last' */
1395   }   }
1396    
1397   /* If a second line of whitespace happens after a generic option   /* If a second line of whitespace happens after a generic option
1398     which was moved, drop it. */     which was moved, drop it. */
1399   if (movedLine && line->type == LT_WHITESPACE && last->type == LT_WHITESPACE) {   if (movedLine && line->type == LT_WHITESPACE && last->type == LT_WHITESPACE) {
# Line 695  static struct grubConfig * readConfig(co Line 1409  static struct grubConfig * readConfig(co
1409   entry->lines = line;   entry->lines = line;
1410      else      else
1411   last->next = line;   last->next = line;
1412        dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry);
1413    
1414        /* we could have seen this outside of an entry... if so, we
1415         * ignore it like any other line we don't grok */
1416        if (line->type == LT_ENTRY_END && sawEntry)
1417     sawEntry = 0;
1418   } else {   } else {
1419      if (!cfg->theLines)      if (!cfg->theLines)
1420   cfg->theLines = line;   cfg->theLines = line;
1421      else {      else
1422   last->next = line;   last->next = line;
1423      }      dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi));
1424   }   }
1425    
1426   last = line;   last = line;
# Line 708  static struct grubConfig * readConfig(co Line 1428  static struct grubConfig * readConfig(co
1428    
1429      free(incoming);      free(incoming);
1430    
1431        dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1432      if (defaultLine) {      if (defaultLine) {
1433   if (cfi->defaultSupportSaved &&          if (defaultLine->numElements > 2 &&
1434        cfi->defaultSupportSaved &&
1435        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1436     cfg->cfi->defaultIsSaved = 1;
1437     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1438     if (cfg->cfi->getEnv) {
1439        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1440        if (defTitle) {
1441     int index = 0;
1442     if (isnumber(defTitle)) {
1443        index = atoi(defTitle);
1444        entry = findEntryByIndex(cfg, index);
1445     } else {
1446        entry = findEntryByTitle(cfg, defTitle, &index);
1447     }
1448     if (entry)
1449        cfg->defaultImage = index;
1450        }
1451     }
1452     } else if (cfi->defaultIsVariable) {
1453        char *value = defaultLine->elements[2].item;
1454        while (*value && (*value == '"' || *value == '\'' ||
1455        *value == ' ' || *value == '\t'))
1456     value++;
1457        cfg->defaultImage = strtol(value, &end, 10);
1458        while (*end && (*end == '"' || *end == '\'' ||
1459        *end == ' ' || *end == '\t'))
1460     end++;
1461        if (*end) cfg->defaultImage = -1;
1462     } else if (cfi->defaultSupportSaved &&
1463   !strncmp(defaultLine->elements[1].item, "saved", 5)) {   !strncmp(defaultLine->elements[1].item, "saved", 5)) {
1464      cfg->defaultImage = DEFAULT_SAVED;      cfg->defaultImage = DEFAULT_SAVED;
1465   } else if (cfi->defaultIsIndex) {   } else if (cfi->defaultIsIndex) {
1466      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);
1467      if (*end) cfg->defaultImage = -1;      if (*end) cfg->defaultImage = -1;
1468   } else if (defaultLine->numElements >= 2) {   } else if (defaultLine->numElements >= 2) {
1469      i = 0;      int i = 0;
1470      while ((entry = findEntryByIndex(cfg, i))) {      while ((entry = findEntryByIndex(cfg, i))) {
1471   for (line = entry->lines; line; line = line->next)   for (line = entry->lines; line; line = line->next)
1472      if (line->type == LT_TITLE) break;      if (line->type == LT_TITLE) break;
# Line 727  static struct grubConfig * readConfig(co Line 1477  static struct grubConfig * readConfig(co
1477                                  line->elements[1].item)) break;                                  line->elements[1].item)) break;
1478                  } else if (line) {                  } else if (line) {
1479                      if (!strcmp(defaultLine->elements[1].item,                      if (!strcmp(defaultLine->elements[1].item,
1480                                  extractTitle(line))) break;                                  extractTitle(cfg, line))) break;
1481                  }                  }
1482   i++;   i++;
1483     entry = NULL;
1484      }      }
1485    
1486      if (entry) cfg->defaultImage = i;      if (entry){
1487            cfg->defaultImage = i;
1488        }else{
1489            cfg->defaultImage = -1;
1490        }
1491     }
1492        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1493     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1494     if (defTitle) {
1495        int index = 0;
1496        if (isnumber(defTitle)) {
1497     index = atoi(defTitle);
1498     entry = findEntryByIndex(cfg, index);
1499        } else {
1500     entry = findEntryByTitle(cfg, defTitle, &index);
1501        }
1502        if (entry)
1503     cfg->defaultImage = index;
1504   }   }
1505        } else {
1506            cfg->defaultImage = 0;
1507      }      }
1508    
1509      return cfg;      return cfg;
# Line 749  static void writeDefault(FILE * out, cha Line 1519  static void writeDefault(FILE * out, cha
1519    
1520      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1521   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1522      else if (cfg->defaultImage > -1) {      else if (cfg->cfi->defaultIsSaved) {
1523     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1524     if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1525        char *title;
1526        entry = findEntryByIndex(cfg, cfg->defaultImage);
1527        line = getLineByType(LT_MENUENTRY, entry->lines);
1528        if (!line)
1529     line = getLineByType(LT_TITLE, entry->lines);
1530        if (line) {
1531     title = extractTitle(cfg, line);
1532     if (title)
1533        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1534        }
1535     }
1536        } else if (cfg->defaultImage > -1) {
1537   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1538      fprintf(out, "%sdefault%s%d\n", indent, separator,      if (cfg->cfi->defaultIsVariable) {
1539      cfg->defaultImage);          fprintf(out, "%sset default=\"%d\"\n", indent,
1540     cfg->defaultImage);
1541        } else {
1542     fprintf(out, "%sdefault%s%d\n", indent, separator,
1543     cfg->defaultImage);
1544        }
1545   } else {   } else {
1546      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1547    
# Line 769  static void writeDefault(FILE * out, cha Line 1558  static void writeDefault(FILE * out, cha
1558    
1559      if (!entry) return;      if (!entry) return;
1560    
1561      line = entry->lines;      line = getLineByType(LT_TITLE, entry->lines);
     while (line && line->type != LT_TITLE) line = line->next;  
1562    
1563      if (line && line->numElements >= 2)      if (line && line->numElements >= 2)
1564   fprintf(out, "%sdefault%s%s\n", indent, separator,   fprintf(out, "%sdefault%s%s\n", indent, separator,
# Line 778  static void writeDefault(FILE * out, cha Line 1566  static void writeDefault(FILE * out, cha
1566              else if (line && (line->numElements == 1) &&              else if (line && (line->numElements == 1) &&
1567                       cfg->cfi->titleBracketed) {                       cfg->cfi->titleBracketed) {
1568   fprintf(out, "%sdefault%s%s\n", indent, separator,   fprintf(out, "%sdefault%s%s\n", indent, separator,
1569                          extractTitle(line));                          extractTitle(cfg, line));
1570              }              }
1571   }   }
1572      }      }
# Line 804  static int writeConfig(struct grubConfig Line 1592  static int writeConfig(struct grubConfig
1592      int rc;      int rc;
1593    
1594      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1595         directory to / */         directory to the dir of the symlink */
1596      rc = chdir("/");      char *dir = strdupa(outName);
1597        rc = chdir(dirname(dir));
1598      do {      do {
1599   buf = alloca(len + 1);   buf = alloca(len + 1);
1600   rc = readlink(outName, buf, len);   rc = readlink(basename(outName), buf, len);
1601   if (rc == len) len += 256;   if (rc == len) len += 256;
1602      } while (rc == len);      } while (rc == len);
1603            
# Line 843  static int writeConfig(struct grubConfig Line 1632  static int writeConfig(struct grubConfig
1632      }      }
1633    
1634      line = cfg->theLines;      line = cfg->theLines;
1635        struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);
1636      while (line) {      while (line) {
1637   if (line->type == LT_DEFAULT) {          if (line->type == LT_SET_VARIABLE && defaultKw &&
1638     line->numElements == 3 &&
1639     !strcmp(line->elements[1].item, defaultKw->key) &&
1640     !is_special_grub2_variable(line->elements[2].item)) {
1641        writeDefault(out, line->indent, line->elements[0].indent, cfg);
1642        needs &= ~MAIN_DEFAULT;
1643     } else if (line->type == LT_DEFAULT) {
1644      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1645      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1646   } else if (line->type == LT_FALLBACK) {   } else if (line->type == LT_FALLBACK) {
# Line 912  static int numEntries(struct grubConfig Line 1708  static int numEntries(struct grubConfig
1708      return i;      return i;
1709  }  }
1710    
1711  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  static char *findDiskForRoot()
1712    int skipRemoved, int flags) {  {
1713      struct singleLine * line;      int fd;
1714      char * fullName;      char buf[65536];
1715      int i;      char *devname;
1716      struct stat sb, sb2;      char *chptr;
1717        int rc;
1718    
1719        if ((fd = open(_PATH_MOUNTED, O_RDONLY)) < 0) {
1720            fprintf(stderr, "grubby: failed to open %s: %s\n",
1721                    _PATH_MOUNTED, strerror(errno));
1722            return NULL;
1723        }
1724    
1725        rc = read(fd, buf, sizeof(buf) - 1);
1726        if (rc <= 0) {
1727            fprintf(stderr, "grubby: failed to read %s: %s\n",
1728                    _PATH_MOUNTED, strerror(errno));
1729            close(fd);
1730            return NULL;
1731        }
1732        close(fd);
1733        buf[rc] = '\0';
1734        chptr = buf;
1735    
1736        char *foundanswer = NULL;
1737    
1738        while (chptr && chptr != buf+rc) {
1739            devname = chptr;
1740    
1741            /*
1742             * The first column of a mtab entry is the device, but if the entry is a
1743             * special device it won't start with /, so move on to the next line.
1744             */
1745            if (*devname != '/') {
1746                chptr = strchr(chptr, '\n');
1747                if (chptr)
1748                    chptr++;
1749                continue;
1750            }
1751    
1752            /* Seek to the next space */
1753            chptr = strchr(chptr, ' ');
1754            if (!chptr) {
1755                fprintf(stderr, "grubby: error parsing %s: %s\n",
1756                        _PATH_MOUNTED, strerror(errno));
1757                return NULL;
1758            }
1759    
1760            /*
1761             * The second column of a mtab entry is the mount point, we are looking
1762             * for '/' obviously.
1763             */
1764            if (*(++chptr) == '/' && *(++chptr) == ' ') {
1765                /* remember the last / entry in mtab */
1766               foundanswer = devname;
1767            }
1768    
1769            /* Next line */
1770            chptr = strchr(chptr, '\n');
1771            if (chptr)
1772                chptr++;
1773        }
1774    
1775        /* Return the last / entry found */
1776        if (foundanswer) {
1777            chptr = strchr(foundanswer, ' ');
1778            *chptr = '\0';
1779            return strdup(foundanswer);
1780        }
1781    
1782        return NULL;
1783    }
1784    
1785    void printEntry(struct singleEntry * entry, FILE *f) {
1786        int i;
1787        struct singleLine * line;
1788    
1789        for (line = entry->lines; line; line = line->next) {
1790     log_message(f, "DBG: %s", line->indent);
1791     for (i = 0; i < line->numElements; i++) {
1792        /* Need to handle this, because we strip the quotes from
1793         * menuentry when read it. */
1794        if (line->type == LT_MENUENTRY && i == 1) {
1795     if(!isquote(*line->elements[i].item))
1796        log_message(f, "\'%s\'", line->elements[i].item);
1797     else
1798        log_message(f, "%s", line->elements[i].item);
1799     log_message(f, "%s", line->elements[i].indent);
1800    
1801     continue;
1802        }
1803        
1804        log_message(f, "%s%s",
1805        line->elements[i].item, line->elements[i].indent);
1806     }
1807     log_message(f, "\n");
1808        }
1809    }
1810    
1811    void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1812    {
1813        static int once;
1814        va_list argp, argq;
1815    
1816        va_start(argp, fmt);
1817    
1818        va_copy(argq, argp);
1819        if (!once) {
1820     log_time(NULL);
1821     log_message(NULL, "command line: %s\n", saved_command_line);
1822        }
1823        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1824        log_vmessage(NULL, fmt, argq);
1825    
1826        printEntry(entry, NULL);
1827        va_end(argq);
1828    
1829        if (!debug) {
1830     once = 1;
1831         va_end(argp);
1832     return;
1833        }
1834    
1835        if (okay) {
1836     va_end(argp);
1837     return;
1838        }
1839    
1840        if (!once)
1841     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1842        once = 1;
1843        fprintf(stderr, "DBG: Image entry failed: ");
1844        vfprintf(stderr, fmt, argp);
1845        printEntry(entry, stderr);
1846        va_end(argp);
1847    }
1848    
1849    #define beginswith(s, c) ((s) && (s)[0] == (c))
1850    
1851    static int endswith(const char *s, char c)
1852    {
1853     int slen;
1854    
1855     if (!s || !s[0])
1856     return 0;
1857     slen = strlen(s) - 1;
1858    
1859     return s[slen] == c;
1860    }
1861    
1862    int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1863      int skipRemoved, int flags) {
1864        struct singleLine * line;
1865        char * fullName;
1866        int i;
1867      char * dev;      char * dev;
     char * end;  
1868      char * rootspec;      char * rootspec;
1869        char * rootdev;
1870    
1871      line = entry->lines;      if (skipRemoved && entry->skip) {
1872      while (line && line->type != LT_KERNEL) line = line->next;   notSuitablePrintf(entry, 0, "marked to skip\n");
1873         return 0;
1874      if (!line) return 0;      }
     if (skipRemoved && entry->skip) return 0;  
     if (line->numElements < 2) return 0;  
1875    
1876      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
1877        if (!line) {
1878     notSuitablePrintf(entry, 0, "no line found\n");
1879     return 0;
1880        }
1881        if (line->numElements < 2) {
1882     notSuitablePrintf(entry, 0, "line has only %d elements\n",
1883        line->numElements);
1884     return 0;
1885        }
1886    
1887        if (flags & GRUBBY_BADIMAGE_OKAY) {
1888        notSuitablePrintf(entry, 1, "\n");
1889        return 1;
1890        }
1891    
1892      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1893        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
1894      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1895      sprintf(fullName, "%s%s", bootPrefix,      int rootspec_offset = rootspec ? strlen(rootspec) : 0;
1896              line->elements[1].item + ((rootspec != NULL) ?      int hasslash = endswith(bootPrefix, '/') ||
1897                                        strlen(rootspec) : 0));       beginswith(line->elements[1].item + rootspec_offset, '/');
1898      if (access(fullName, R_OK)) return 0;      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1899                line->elements[1].item + rootspec_offset);
1900        if (access(fullName, R_OK)) {
1901     notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1902     return 0;
1903        }
1904      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
1905   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1906      if (i < line->numElements) {      if (i < line->numElements) {
1907   dev = line->elements[i].item + 5;   dev = line->elements[i].item + 5;
1908      } else {      } else {
1909   /* look for a lilo style LT_ROOT line */   /* look for a lilo style LT_ROOT line */
1910   line = entry->lines;   line = getLineByType(LT_ROOT, entry->lines);
  while (line && line->type != LT_ROOT) line = line->next;  
1911    
1912   if (line && line->numElements >= 2) {   if (line && line->numElements >= 2) {
1913      dev = line->elements[1].item;      dev = line->elements[1].item;
1914   } else {   } else {
1915              int type;      /* didn't succeed in finding a LT_ROOT, let's try LT_KERNELARGS.
1916      /* didn't succeed in finding a LT_ROOT, let's try LT_KERNELARGS */       * grub+multiboot uses LT_MBMODULE for the args, so check that too.
1917      line = entry->lines;       */
1918        line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);
             type = ((entry->multiboot) ? LT_MBMODULE : LT_KERNELARGS);  
   
     while (line && line->type != type) line = line->next;  
1919    
1920              /* failed to find one */              /* failed to find one */
1921              if (!line) return 0;              if (!line) {
1922     notSuitablePrintf(entry, 0, "no line found\n");
1923     return 0;
1924                }
1925    
1926      for (i = 1; i < line->numElements; i++)      for (i = 1; i < line->numElements; i++)
1927          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1928      if (i < line->numElements)      if (i < line->numElements)
1929          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1930      else {      else {
1931     notSuitablePrintf(entry, 0, "no root= entry found\n");
1932   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1933          return 0;          return 0;
1934              }              }
1935   }   }
1936      }      }
1937    
1938      if (!strncmp(dev, "LABEL=", 6)) {      dev = getpathbyspec(dev);
1939   dev += 6;      if (!getpathbyspec(dev)) {
1940            notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1941   /* check which device has this label */          return 0;
1942   dev = get_spec_by_volume_label(dev, &i, &i);      } else
1943   if (!dev) return 0;   dev = getpathbyspec(dev);
1944    
1945        rootdev = findDiskForRoot();
1946        if (!rootdev) {
1947            notSuitablePrintf(entry, 0, "can't find root device\n");
1948     return 0;
1949      }      }
1950    
1951      if (*dev == '/') {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1952   if (stat(dev, &sb))          notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1953      return 0;   getuuidbydev(rootdev), getuuidbydev(dev));
1954      } else {          free(rootdev);
1955   sb.st_rdev = strtol(dev, &end, 16);          return 0;
1956   if (*end) return 0;      }
1957    
1958        if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1959            notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n",
1960     getuuidbydev(rootdev), getuuidbydev(dev));
1961     free(rootdev);
1962            return 0;
1963      }      }
     stat("/", &sb2);  
1964    
1965      if (sb.st_rdev != sb2.st_dev) return 0;      free(rootdev);
1966        notSuitablePrintf(entry, 1, "\n");
1967    
1968      return 1;      return 1;
1969  }  }
# Line 1024  struct singleEntry * findEntryByPath(str Line 1997  struct singleEntry * findEntryByPath(str
1997   }   }
1998    
1999   indexVars[i + 1] = -1;   indexVars[i + 1] = -1;
2000    
2001   i = 0;   i = 0;
2002   if (index) {   if (index) {
2003      while (i < *index) i++;      while (i < *index) {
2004      if (indexVars[i] == -1) return NULL;   i++;
2005     if (indexVars[i] == -1) return NULL;
2006        }
2007   }   }
2008    
2009   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
2010   if (!entry) return NULL;   if (!entry) return NULL;
2011    
2012   line = entry->lines;   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
  while (line && line->type != LT_KERNEL)  
     line = line->next;  
   
2013   if (!line) return NULL;   if (!line) return NULL;
2014    
2015   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1075  struct singleEntry * findEntryByPath(str Line 2047  struct singleEntry * findEntryByPath(str
2047    
2048   if (!strncmp(kernel, "TITLE=", 6)) {   if (!strncmp(kernel, "TITLE=", 6)) {
2049      prefix = "";      prefix = "";
2050      checkType = LT_TITLE;      checkType = LT_TITLE|LT_MENUENTRY;
2051      kernel += 6;      kernel += 6;
2052   }   }
2053    
2054   while ((entry = findEntryByIndex(config, i))) {   for (entry = findEntryByIndex(config, i); entry; entry = entry->next, i++) {
2055      line = entry->lines;      if (entry->skip) continue;
     while (line && line->type != checkType) line=line->next;  
2056    
2057        dbgPrintf("findEntryByPath looking for %d %s in %p\n", checkType, kernel, entry);
2058    
2059      if (line && line->numElements >= 2 && !entry->skip) {      /* check all the lines matching checkType */
2060                  rootspec = getRootSpecifier(line->elements[1].item);      for (line = entry->lines; line; line = line->next) {
2061          if (!strcmp(line->elements[1].item  +   enum lineType_e ct = checkType;
2062                              ((rootspec != NULL) ? strlen(rootspec) : 0),   if (entry->multiboot && checkType == LT_KERNEL)
2063                              kernel + strlen(prefix)))      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER|LT_KERNEL_16;
2064                      break;   else if (checkType & LT_KERNEL)
2065              }      ct = checkType | LT_KERNEL_EFI | LT_KERNEL_16;
2066                 line = getLineByType(ct, line);
2067              /* have to check multiboot lines too */   if (!line)
2068              if (entry->multiboot) {      break;  /* not found in this entry */
2069                  while (line && line->type != LT_MBMODULE) line = line->next;  
2070                  if (line && line->numElements >= 2 && !entry->skip) {   if (line && line->type != LT_MENUENTRY &&
2071                      rootspec = getRootSpecifier(line->elements[1].item);   line->numElements >= 2) {
2072                      if (!strcmp(line->elements[1].item  +      rootspec = getRootSpecifier(line->elements[1].item);
2073                                  ((rootspec != NULL) ? strlen(rootspec) : 0),      if (!strcmp(line->elements[1].item +
2074                                  kernel + strlen(prefix)))   ((rootspec != NULL) ? strlen(rootspec) : 0),
2075                          break;   kernel + strlen(prefix)))
2076                  }   break;
2077              }   }
2078     if(line->type == LT_MENUENTRY &&
2079     !strcmp(line->elements[1].item, kernel))
2080        break;
2081        }
2082    
2083      i++;      /* make sure this entry has a kernel identifier; this skips
2084         * non-Linux boot entries (could find netbsd etc, though, which is
2085         * unfortunate)
2086         */
2087        if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines))
2088     break; /* found 'im! */
2089   }   }
2090    
2091   if (index) *index = i;   if (index) *index = i;
2092      }      }
2093    
2094      if (!entry) return NULL;      return entry;
2095    }
2096    
2097    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2098          int * index) {
2099        struct singleEntry * entry;
2100        struct singleLine * line;
2101        int i;
2102        char * newtitle;
2103    
2104      /* make sure this entry has a kernel identifier; this skips non-Linux      for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2105         boot entries (could find netbsd etc, though, which is unfortunate) */   if (index && i < *index)
2106      line = entry->lines;      continue;
2107      while (line && line->type != LT_KERNEL) line = line->next;   line = getLineByType(LT_TITLE, entry->lines);
2108      if (!line) {   if (!line)
2109   if (!index) index = &i;      line = getLineByType(LT_MENUENTRY, entry->lines);
2110   (*index)++;   if (!line)
2111   return findEntryByPath(config, kernel, prefix, index);      continue;
2112     newtitle = grub2ExtractTitle(line);
2113     if (!newtitle)
2114        continue;
2115     if (!strcmp(title, newtitle))
2116        break;
2117      }      }
2118    
2119        if (!entry)
2120     return NULL;
2121    
2122        if (index)
2123     *index = i;
2124      return entry;      return entry;
2125  }  }
2126    
# Line 1147  struct singleEntry * findTemplate(struct Line 2146  struct singleEntry * findTemplate(struct
2146      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2147      int index;      int index;
2148    
2149      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2150     if (cfg->cfi->getEnv) {
2151        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2152        if (defTitle) {
2153     int index = 0;
2154     if (isnumber(defTitle)) {
2155        index = atoi(defTitle);
2156        entry = findEntryByIndex(cfg, index);
2157     } else {
2158        entry = findEntryByTitle(cfg, defTitle, &index);
2159     }
2160     if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2161        cfg->defaultImage = index;
2162        if (indexPtr)
2163     *indexPtr = index;
2164        return entry;
2165     }
2166        }
2167     }
2168        } else if (cfg->defaultImage > -1) {
2169   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2170   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2171      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 1200  void markRemovedImage(struct grubConfig Line 2218  void markRemovedImage(struct grubConfig
2218        const char * prefix) {        const char * prefix) {
2219      struct singleEntry * entry;      struct singleEntry * entry;
2220    
2221      if (!image) return;      if (!image)
2222     return;
2223    
2224        /* check and see if we're removing the default image */
2225        if (isdigit(*image)) {
2226     entry = findEntryByPath(cfg, image, prefix, NULL);
2227     if(entry)
2228        entry->skip = 1;
2229     return;
2230        }
2231    
2232      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
2233   entry->skip = 1;   entry->skip = 1;
# Line 1208  void markRemovedImage(struct grubConfig Line 2235  void markRemovedImage(struct grubConfig
2235    
2236  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
2237       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
2238       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
2239      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
2240      int i, j;      int i, j;
2241    
2242      if (newIsDefault) {      if (newIsDefault) {
2243   config->defaultImage = 0;   config->defaultImage = 0;
2244   return;   return;
2245        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
2246     if (findEntryByIndex(config, index))
2247        config->defaultImage = index;
2248     else
2249        config->defaultImage = -1;
2250     return;
2251      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
2252   i = 0;   i = 0;
2253   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1227  void setDefaultImage(struct grubConfig * Line 2260  void setDefaultImage(struct grubConfig *
2260    
2261      /* 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
2262         changes */         changes */
2263      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
2264     (config->defaultImage == DEFAULT_SAVED_GRUB2))
2265        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
2266        return;        return;
2267    
# Line 1285  void displayEntry(struct singleEntry * e Line 2319  void displayEntry(struct singleEntry * e
2319      struct singleLine * line;      struct singleLine * line;
2320      char * root = NULL;      char * root = NULL;
2321      int i;      int i;
2322        int j;
     line = entry->lines;  
     while (line && line->type != LT_KERNEL) line = line->next;  
2323    
2324      printf("index=%d\n", index);      printf("index=%d\n", index);
2325    
2326      printf("kernel=%s\n", line->elements[1].item);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
2327        if (!line) {
2328            printf("non linux entry\n");
2329            return;
2330        }
2331    
2332        if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2333     printf("kernel=%s\n", line->elements[1].item);
2334        else
2335     printf("kernel=%s%s\n", prefix, line->elements[1].item);
2336    
2337      if (line->numElements >= 3) {      if (line->numElements >= 3) {
2338   printf("args=\"");   printf("args=\"");
# Line 1308  void displayEntry(struct singleEntry * e Line 2349  void displayEntry(struct singleEntry * e
2349   }   }
2350   printf("\"\n");   printf("\"\n");
2351      } else {      } else {
2352   line = entry->lines;   line = getLineByType(LT_KERNELARGS, entry->lines);
  while (line && line->type != LT_KERNELARGS) line=line->next;  
   
2353   if (line) {   if (line) {
2354      char * s;      char * s;
2355    
# Line 1334  void displayEntry(struct singleEntry * e Line 2373  void displayEntry(struct singleEntry * e
2373      }      }
2374    
2375      if (!root) {      if (!root) {
2376   line = entry->lines;   line = getLineByType(LT_ROOT, entry->lines);
  while (line && line->type != LT_ROOT) line = line->next;  
   
2377   if (line && line->numElements >= 2)   if (line && line->numElements >= 2)
2378      root=line->elements[1].item;      root=line->elements[1].item;
2379      }      }
# Line 1351  void displayEntry(struct singleEntry * e Line 2388  void displayEntry(struct singleEntry * e
2388   printf("root=%s\n", s);   printf("root=%s\n", s);
2389      }      }
2390    
2391      line = entry->lines;      line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
     while (line && line->type != LT_INITRD) line = line->next;  
2392    
2393      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2394   printf("initrd=%s", prefix);   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2395        printf("initrd=");
2396     else
2397        printf("initrd=%s", prefix);
2398    
2399   for (i = 1; i < line->numElements; i++)   for (i = 1; i < line->numElements; i++)
2400      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2401   printf("\n");   printf("\n");
2402      }      }
2403    
2404        line = getLineByType(LT_TITLE, entry->lines);
2405        if (line) {
2406     printf("title=%s\n", line->elements[1].item);
2407        } else {
2408     char * title;
2409     line = getLineByType(LT_MENUENTRY, entry->lines);
2410     if (line) {
2411        title = grub2ExtractTitle(line);
2412        if (title)
2413     printf("title=%s\n", title);
2414     }
2415        }
2416    
2417        for (j = 0, line = entry->lines; line; line = line->next) {
2418     if ((line->type & LT_MBMODULE) && line->numElements >= 2) {
2419        if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2420     printf("mbmodule%d=", j);
2421        else
2422     printf("mbmodule%d=%s", j, prefix);
2423    
2424        for (i = 1; i < line->numElements; i++)
2425     printf("%s%s", line->elements[i].item, line->elements[i].indent);
2426        printf("\n");
2427        j++;
2428     }
2429        }
2430    }
2431    
2432    int isSuseSystem(void) {
2433        const char * path;
2434        const static char default_path[] = "/etc/SuSE-release";
2435    
2436        if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL)
2437     path = default_path;
2438    
2439        if (!access(path, R_OK))
2440     return 1;
2441        return 0;
2442    }
2443    
2444    int isSuseGrubConf(const char * path) {
2445        FILE * grubConf;
2446        char * line = NULL;
2447        size_t len = 0, res = 0;
2448    
2449        grubConf = fopen(path, "r");
2450        if (!grubConf) {
2451            dbgPrintf("Could not open SuSE configuration file '%s'\n", path);
2452     return 0;
2453        }
2454    
2455        while ((res = getline(&line, &len, grubConf)) != -1) {
2456     if (!strncmp(line, "setup", 5)) {
2457        fclose(grubConf);
2458        free(line);
2459        return 1;
2460     }
2461        }
2462    
2463        dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n",
2464          path);
2465    
2466        fclose(grubConf);
2467        free(line);
2468        return 0;
2469    }
2470    
2471    int suseGrubConfGetLba(const char * path, int * lbaPtr) {
2472        FILE * grubConf;
2473        char * line = NULL;
2474        size_t res = 0, len = 0;
2475    
2476        if (!path) return 1;
2477        if (!lbaPtr) return 1;
2478    
2479        grubConf = fopen(path, "r");
2480        if (!grubConf) return 1;
2481    
2482        while ((res = getline(&line, &len, grubConf)) != -1) {
2483     if (line[res - 1] == '\n')
2484        line[res - 1] = '\0';
2485     else if (len > res)
2486        line[res] = '\0';
2487     else {
2488        line = realloc(line, res + 1);
2489        line[res] = '\0';
2490     }
2491    
2492     if (!strncmp(line, "setup", 5)) {
2493        if (strstr(line, "--force-lba")) {
2494            *lbaPtr = 1;
2495        } else {
2496            *lbaPtr = 0;
2497        }
2498        dbgPrintf("lba: %i\n", *lbaPtr);
2499        break;
2500     }
2501        }
2502    
2503        free(line);
2504        fclose(grubConf);
2505        return 0;
2506    }
2507    
2508    int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) {
2509        FILE * grubConf;
2510        char * line = NULL;
2511        size_t res = 0, len = 0;
2512        char * lastParamPtr = NULL;
2513        char * secLastParamPtr = NULL;
2514        char installDeviceNumber = '\0';
2515        char * bounds = NULL;
2516    
2517        if (!path) return 1;
2518        if (!devicePtr) return 1;
2519    
2520        grubConf = fopen(path, "r");
2521        if (!grubConf) return 1;
2522    
2523        while ((res = getline(&line, &len, grubConf)) != -1) {
2524     if (strncmp(line, "setup", 5))
2525        continue;
2526    
2527     if (line[res - 1] == '\n')
2528        line[res - 1] = '\0';
2529     else if (len > res)
2530        line[res] = '\0';
2531     else {
2532        line = realloc(line, res + 1);
2533        line[res] = '\0';
2534     }
2535    
2536     lastParamPtr = bounds = line + res;
2537    
2538     /* Last parameter in grub may be an optional IMAGE_DEVICE */
2539     while (!isspace(*lastParamPtr))
2540        lastParamPtr--;
2541     lastParamPtr++;
2542    
2543     secLastParamPtr = lastParamPtr - 2;
2544     dbgPrintf("lastParamPtr: %s\n", lastParamPtr);
2545    
2546     if (lastParamPtr + 3 > bounds) {
2547        dbgPrintf("lastParamPtr going over boundary");
2548        fclose(grubConf);
2549        free(line);
2550        return 1;
2551     }
2552     if (!strncmp(lastParamPtr, "(hd", 3))
2553        lastParamPtr += 3;
2554     dbgPrintf("lastParamPtr: %c\n", *lastParamPtr);
2555    
2556     /*
2557     * Second last parameter will decide wether last parameter is
2558     * an IMAGE_DEVICE or INSTALL_DEVICE
2559     */
2560     while (!isspace(*secLastParamPtr))
2561        secLastParamPtr--;
2562     secLastParamPtr++;
2563    
2564     if (secLastParamPtr + 3 > bounds) {
2565        dbgPrintf("secLastParamPtr going over boundary");
2566        fclose(grubConf);
2567        free(line);
2568        return 1;
2569     }
2570     dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr);
2571     if (!strncmp(secLastParamPtr, "(hd", 3)) {
2572        secLastParamPtr += 3;
2573        dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr);
2574        installDeviceNumber = *secLastParamPtr;
2575     } else {
2576        installDeviceNumber = *lastParamPtr;
2577     }
2578    
2579     *devicePtr = malloc(6);
2580     snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber);
2581     dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber);
2582     fclose(grubConf);
2583     free(line);
2584     return 0;
2585        }
2586    
2587        free(line);
2588        fclose(grubConf);
2589        return 1;
2590    }
2591    
2592    int grubGetBootFromDeviceMap(const char * device,
2593         char ** bootPtr) {
2594        FILE * deviceMap;
2595        char * line = NULL;
2596        size_t res = 0, len = 0;
2597        char * devicePtr;
2598        char * bounds = NULL;
2599        const char * path;
2600        const static char default_path[] = "/boot/grub/device.map";
2601    
2602        if (!device) return 1;
2603        if (!bootPtr) return 1;
2604    
2605        if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL)
2606     path = default_path;
2607    
2608        dbgPrintf("opening grub device.map file from: %s\n", path);
2609        deviceMap = fopen(path, "r");
2610        if (!deviceMap)
2611     return 1;
2612    
2613        while ((res = getline(&line, &len, deviceMap)) != -1) {
2614            if (!strncmp(line, "#", 1))
2615        continue;
2616    
2617     if (line[res - 1] == '\n')
2618        line[res - 1] = '\0';
2619     else if (len > res)
2620        line[res] = '\0';
2621     else {
2622        line = realloc(line, res + 1);
2623        line[res] = '\0';
2624     }
2625    
2626     devicePtr = line;
2627     bounds = line + res;
2628    
2629     while ((isspace(*line) && ((devicePtr + 1) <= bounds)))
2630        devicePtr++;
2631     dbgPrintf("device: %s\n", devicePtr);
2632    
2633     if (!strncmp(devicePtr, device, strlen(device))) {
2634        devicePtr += strlen(device);
2635        while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds))
2636            devicePtr++;
2637    
2638        *bootPtr = strdup(devicePtr);
2639        break;
2640     }
2641        }
2642    
2643        free(line);
2644        fclose(deviceMap);
2645        return 0;
2646    }
2647    
2648    int suseGrubConfGetBoot(const char * path, char ** bootPtr) {
2649        char * grubDevice;
2650    
2651        if (suseGrubConfGetInstallDevice(path, &grubDevice))
2652     dbgPrintf("error looking for grub installation device\n");
2653        else
2654     dbgPrintf("grubby installation device: %s\n", grubDevice);
2655    
2656        if (grubGetBootFromDeviceMap(grubDevice, bootPtr))
2657     dbgPrintf("error looking for grub boot device\n");
2658        else
2659     dbgPrintf("grubby boot device: %s\n", *bootPtr);
2660    
2661        free(grubDevice);
2662        return 0;
2663    }
2664    
2665    int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) {
2666        /*
2667         * This SuSE grub configuration file at this location is not your average
2668         * grub configuration file, but instead the grub commands used to setup
2669         * grub on that system.
2670         */
2671        const char * path;
2672        const static char default_path[] = "/etc/grub.conf";
2673    
2674        if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL)
2675     path = default_path;
2676    
2677        if (!isSuseGrubConf(path)) return 1;
2678    
2679        if (lbaPtr) {
2680            *lbaPtr = 0;
2681            if (suseGrubConfGetLba(path, lbaPtr))
2682                return 1;
2683        }
2684    
2685        if (bootPtr) {
2686            *bootPtr = NULL;
2687            suseGrubConfGetBoot(path, bootPtr);
2688        }
2689    
2690        return 0;
2691  }  }
2692    
2693  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 1369  int parseSysconfigGrub(int * lbaPtr, cha Line 2697  int parseSysconfigGrub(int * lbaPtr, cha
2697      char * start;      char * start;
2698      char * param;      char * param;
2699    
2700      in = fopen("/etc/sysconfig/grub", "r");      in = fopen("/etc/conf.d/grub", "r");
2701      if (!in) return 1;      if (!in) return 1;
2702    
2703      if (lbaPtr) *lbaPtr = 0;      if (lbaPtr) *lbaPtr = 0;
# Line 1410  int parseSysconfigGrub(int * lbaPtr, cha Line 2738  int parseSysconfigGrub(int * lbaPtr, cha
2738  }  }
2739    
2740  void dumpSysconfigGrub(void) {  void dumpSysconfigGrub(void) {
2741      char * boot;      char * boot = NULL;
2742      int lba;      int lba;
2743    
2744      if (!parseSysconfigGrub(&lba, &boot)) {      if (isSuseSystem()) {
2745   if (lba) printf("lba\n");          if (parseSuseGrubConf(&lba, &boot)) {
2746   if (boot) printf("boot=%s\n", boot);      free(boot);
2747        return;
2748     }
2749        } else {
2750            if (parseSysconfigGrub(&lba, &boot)) {
2751        free(boot);
2752        return;
2753     }
2754        }
2755    
2756        if (lba) printf("lba\n");
2757        if (boot) {
2758     printf("boot=%s\n", boot);
2759     free(boot);
2760      }      }
2761  }  }
2762    
# Line 1431  int displayInfo(struct grubConfig * conf Line 2772  int displayInfo(struct grubConfig * conf
2772   return 1;   return 1;
2773      }      }
2774    
2775      /* 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
2776         be a better way */         be a better way */
2777      if (config->cfi == &grubConfigType) {      if (config->cfi == &grubConfigType) {
2778   dumpSysconfigGrub();   dumpSysconfigGrub();
2779      } else {      } else {
2780   line = config->theLines;   line = getLineByType(LT_BOOT, config->theLines);
  while (line && line->type != LT_BOOT) line = line->next;  
2781   if (line && line->numElements >= 1) {   if (line && line->numElements >= 1) {
2782      printf("boot=%s\n", line->elements[1].item);      printf("boot=%s\n", line->elements[1].item);
2783   }   }
2784    
2785   line = config->theLines;   line = getLineByType(LT_LBA, config->theLines);
  while (line && line->type != LT_LBA) line = line->next;  
2786   if (line) printf("lba\n");   if (line) printf("lba\n");
2787      }      }
2788    
# Line 1458  int displayInfo(struct grubConfig * conf Line 2797  int displayInfo(struct grubConfig * conf
2797      return 0;      return 0;
2798  }  }
2799    
2800    struct singleLine * addLineTmpl(struct singleEntry * entry,
2801     struct singleLine * tmplLine,
2802     struct singleLine * prevLine,
2803     const char * val,
2804     struct configFileInfo * cfi)
2805    {
2806        struct singleLine * newLine = lineDup(tmplLine);
2807    
2808        if (isEfi && cfi == &grub2ConfigType) {
2809     enum lineType_e old = newLine->type;
2810     newLine->type = preferredLineType(newLine->type, cfi);
2811     if (old != newLine->type)
2812        newLine->elements[0].item = getKeyByType(newLine->type, cfi);
2813        }
2814    
2815        if (val) {
2816     /* override the inherited value with our own.
2817     * This is a little weak because it only applies to elements[1]
2818     */
2819     if (newLine->numElements > 1)
2820        removeElement(newLine, 1);
2821     insertElement(newLine, val, 1, cfi);
2822    
2823     /* but try to keep the rootspec from the template... sigh */
2824     if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI|LT_KERNEL_16|LT_INITRD_16)) {
2825        char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2826        if (rootspec != NULL) {
2827     free(newLine->elements[1].item);
2828     newLine->elements[1].item =
2829        sdupprintf("%s%s", rootspec, val);
2830        }
2831     }
2832        }
2833    
2834        dbgPrintf("addLineTmpl(%s)\n", newLine->numElements ?
2835          newLine->elements[0].item : "");
2836    
2837        if (!entry->lines) {
2838     /* first one on the list */
2839     entry->lines = newLine;
2840        } else if (prevLine) {
2841     /* add after prevLine */
2842     newLine->next = prevLine->next;
2843     prevLine->next = newLine;
2844        }
2845    
2846        return newLine;
2847    }
2848    
2849  /* val may be NULL */  /* val may be NULL */
2850  struct singleLine *  addLine(struct singleEntry * entry,  struct singleLine *  addLine(struct singleEntry * entry,
2851       struct configFileInfo * cfi,       struct configFileInfo * cfi,
2852       enum lineType_e type, const char * defaultIndent,       enum lineType_e type, char * defaultIndent,
2853       char * val) {       const char * val) {
2854      struct singleLine * line, * prev;      struct singleLine * line, * prev;
2855      int i;      struct keywordTypes * kw;
2856        struct singleLine tmpl;
2857    
2858      for (i = 0; cfi->keywords[i].key; i++)      /* NB: This function shouldn't allocate items on the heap, rather on the
2859   if (cfi->keywords[i].type == type) break;       * stack since it calls addLineTmpl which will make copies.
2860      if (type != LT_TITLE || !cfi->titleBracketed)       */
2861          if (!cfi->keywords[i].key) abort();      if (type == LT_TITLE && cfi->titleBracketed) {
2862     /* we're doing a bracketed title (zipl) */
2863     tmpl.type = type;
2864     tmpl.numElements = 1;
2865     tmpl.elements = alloca(sizeof(*tmpl.elements));
2866     tmpl.elements[0].item = alloca(strlen(val)+3);
2867     sprintf(tmpl.elements[0].item, "[%s]", val);
2868     tmpl.elements[0].indent = "";
2869     val = NULL;
2870        } else if (type == LT_MENUENTRY) {
2871     char *lineend = "--class gnu-linux --class gnu --class os {";
2872     if (!val) {
2873        fprintf(stderr, "Line type LT_MENUENTRY requires a value\n");
2874        abort();
2875     }
2876     kw = getKeywordByType(type, cfi);
2877     if (!kw) {
2878        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
2879        abort();
2880     }
2881     tmpl.indent = "";
2882     tmpl.type = type;
2883     tmpl.numElements = 3;
2884     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
2885     tmpl.elements[0].item = kw->key;
2886     tmpl.elements[0].indent = alloca(2);
2887     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
2888     tmpl.elements[1].item = (char *)val;
2889     tmpl.elements[1].indent = alloca(2);
2890     sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);
2891     tmpl.elements[2].item = alloca(strlen(lineend)+1);
2892     strcpy(tmpl.elements[2].item, lineend);
2893     tmpl.elements[2].indent = "";
2894        } else {
2895     kw = getKeywordByType(type, cfi);
2896     if (!kw) {
2897        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
2898        abort();
2899     }
2900     tmpl.type = type;
2901     tmpl.numElements = val ? 2 : 1;
2902     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
2903     tmpl.elements[0].item = kw->key;
2904     tmpl.elements[0].indent = alloca(2);
2905     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
2906     if (val) {
2907        tmpl.elements[1].item = (char *)val;
2908        tmpl.elements[1].indent = "";
2909     }
2910        }
2911    
2912      /* The last non-empty line gives us the indention to us and the line      /* The last non-empty line gives us the indention to us and the line
2913         to insert after. Note that comments are considered empty lines, which         to insert after. Note that comments are considered empty lines, which
2914         may not be ideal? If there are no lines or we are looking at the         may not be ideal? If there are no lines or we are looking at the
2915         first line, we use defaultIndent (the first line is normally indented         first line, we use defaultIndent (the first line is normally indented
2916         differently from the rest) */         differently from the rest) */
2917      if (entry->lines) {      for (line = entry->lines, prev = NULL; line; line = line->next) {
2918   line = entry->lines;   if (line->numElements) prev = line;
2919   prev = NULL;   /* fall back on the last line if prev isn't otherwise set */
2920   while (line) {   if (!line->next && !prev) prev = line;
     if (line->numElements) prev = line;  
     line = line->next;  
  }  
  if (!prev) {  
     /* just use the last line */  
     prev = entry->lines;  
     while (prev->next) prev = prev->next;  
  }  
   
  line = prev->next;  
  prev->next = malloc(sizeof(*line));  
  prev->next->next = line;  
  line = prev->next;  
   
  if (prev == entry->lines)  
     line->indent = strdup(defaultIndent);  
  else  
     line->indent = strdup(prev->indent);  
     } else {  
  line = malloc(sizeof(*line));  
  line->indent = strdup(defaultIndent);  
  line->next = NULL;  
2921      }      }
2922    
2923      if (type != LT_TITLE || !cfi->titleBracketed) {      struct singleLine *menuEntry;
2924          line->type = type;      menuEntry = getLineByType(LT_MENUENTRY, entry->lines);
2925          line->numElements = val ? 2 : 1;      if (tmpl.type == LT_ENTRY_END) {
2926          line->elements = malloc(sizeof(*line->elements) * line->numElements);   if (menuEntry)
2927          line->elements[0].item = strdup(cfi->keywords[i].key);      tmpl.indent = menuEntry->indent;
2928          line->elements[0].indent = malloc(2);   else
2929          line->elements[0].indent[0] = cfi->keywords[i].nextChar;      tmpl.indent = defaultIndent ?: "";
2930          line->elements[0].indent[1] = '\0';      } else if (tmpl.type != LT_MENUENTRY) {
2931             if (menuEntry)
2932          if (val) {      tmpl.indent = "\t";
2933              line->elements[1].item = val;   else if (prev == entry->lines)
2934              line->elements[1].indent = strdup("");      tmpl.indent = defaultIndent ?: "";
2935          }   else
2936      } else {      tmpl.indent = prev->indent;
         /* we're doing the title of a bracketed title (zipl) */  
         line->type = type;  
         line->numElements = 1;  
         line->elements = malloc(sizeof(*line->elements) * line->numElements);  
   
         line->elements[0].item = malloc(strlen(val) + 3);  
         sprintf(line->elements[0].item, "[%s]", val);  
         line->elements[0].indent = strdup("");  
2937      }      }
2938    
2939      return line;      return addLineTmpl(entry, &tmpl, prev, val, cfi);
2940  }  }
2941    
2942  void removeLine(struct singleEntry * entry, struct singleLine * line) {  void removeLine(struct singleEntry * entry, struct singleLine * line) {
# Line 1553  void removeLine(struct singleEntry * ent Line 2961  void removeLine(struct singleEntry * ent
2961      free(line);      free(line);
2962  }  }
2963    
2964    static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2965    {
2966        struct singleLine newLine = {
2967     .indent = tmplLine->indent,
2968     .type = tmplLine->type,
2969     .next = tmplLine->next,
2970        };
2971        int firstQuotedItem = -1;
2972        int quoteLen = 0;
2973        int j;
2974        int element = 0;
2975        char *c;
2976    
2977        c = malloc(strlen(tmplLine->elements[0].item) + 1);
2978        strcpy(c, tmplLine->elements[0].item);
2979        insertElement(&newLine, c, element++, cfi);
2980        free(c);
2981        c = NULL;
2982    
2983        for (j = 1; j < tmplLine->numElements; j++) {
2984     if (firstQuotedItem == -1) {
2985        quoteLen += strlen(tmplLine->elements[j].item);
2986        
2987        if (isquote(tmplLine->elements[j].item[0])) {
2988     firstQuotedItem = j;
2989            quoteLen += strlen(tmplLine->elements[j].indent);
2990        } else {
2991     c = malloc(quoteLen + 1);
2992     strcpy(c, tmplLine->elements[j].item);
2993     insertElement(&newLine, c, element++, cfi);
2994     free(c);
2995     quoteLen = 0;
2996        }
2997     } else {
2998        int itemlen = strlen(tmplLine->elements[j].item);
2999        quoteLen += itemlen;
3000        quoteLen += strlen(tmplLine->elements[j].indent);
3001        
3002        if (isquote(tmplLine->elements[j].item[itemlen - 1])) {
3003     c = malloc(quoteLen + 1);
3004     c[0] = '\0';
3005     for (int i = firstQuotedItem; i < j+1; i++) {
3006        strcat(c, tmplLine->elements[i].item);
3007        strcat(c, tmplLine->elements[i].indent);
3008     }
3009     insertElement(&newLine, c, element++, cfi);
3010     free(c);
3011    
3012     firstQuotedItem = -1;
3013     quoteLen = 0;
3014        }
3015     }
3016        }
3017        while (tmplLine->numElements)
3018     removeElement(tmplLine, 0);
3019        if (tmplLine->elements)
3020     free(tmplLine->elements);
3021    
3022        tmplLine->numElements = newLine.numElements;
3023        tmplLine->elements = newLine.elements;
3024    }
3025    
3026    static void insertElement(struct singleLine * line,
3027      const char * item, int insertHere,
3028      struct configFileInfo * cfi)
3029    {
3030        struct keywordTypes * kw;
3031        char indent[2] = "";
3032    
3033        /* sanity check */
3034        if (insertHere > line->numElements) {
3035     dbgPrintf("insertElement() adjusting insertHere from %d to %d\n",
3036      insertHere, line->numElements);
3037     insertHere = line->numElements;
3038        }
3039    
3040        line->elements = realloc(line->elements, (line->numElements + 1) *
3041         sizeof(*line->elements));
3042        memmove(&line->elements[insertHere+1],
3043        &line->elements[insertHere],
3044        (line->numElements - insertHere) *
3045        sizeof(*line->elements));
3046        line->elements[insertHere].item = strdup(item);
3047    
3048        kw = getKeywordByType(line->type, cfi);
3049    
3050        if (line->numElements == 0) {
3051     indent[0] = '\0';
3052        } else if (insertHere == 0) {
3053     indent[0] = kw->nextChar;
3054        } else if (kw->separatorChar != '\0') {
3055     indent[0] = kw->separatorChar;
3056        } else {
3057     indent[0] = ' ';
3058        }
3059    
3060        if (insertHere > 0 && line->elements[insertHere-1].indent[0] == '\0') {
3061     /* move the end-of-line forward */
3062     line->elements[insertHere].indent =
3063        line->elements[insertHere-1].indent;
3064     line->elements[insertHere-1].indent = strdup(indent);
3065        } else {
3066     line->elements[insertHere].indent = strdup(indent);
3067        }
3068    
3069        line->numElements++;
3070    
3071        dbgPrintf("insertElement(%s, '%s%s', %d)\n",
3072          line->elements[0].item,
3073          line->elements[insertHere].item,
3074          line->elements[insertHere].indent,
3075          insertHere);
3076    }
3077    
3078    static void removeElement(struct singleLine * line, int removeHere) {
3079        int i;
3080    
3081        /* sanity check */
3082        if (removeHere >= line->numElements) return;
3083    
3084        dbgPrintf("removeElement(%s, %d:%s)\n", line->elements[0].item,
3085          removeHere, line->elements[removeHere].item);
3086    
3087        free(line->elements[removeHere].item);
3088    
3089        if (removeHere > 1) {
3090     /* previous argument gets this argument's post-indentation */
3091     free(line->elements[removeHere-1].indent);
3092     line->elements[removeHere-1].indent =
3093        line->elements[removeHere].indent;
3094        } else {
3095     free(line->elements[removeHere].indent);
3096        }
3097    
3098        /* now collapse the array, but don't bother to realloc smaller */
3099        for (i = removeHere; i < line->numElements - 1; i++)
3100     line->elements[i] = line->elements[i + 1];
3101    
3102        line->numElements--;
3103    }
3104    
3105  int argMatch(const char * one, const char * two) {  int argMatch(const char * one, const char * two) {
3106      char * first, * second;      char * first, * second;
3107      char * chptr;      char * chptr;
# Line 1575  int updateActualImage(struct grubConfig Line 3124  int updateActualImage(struct grubConfig
3124      struct singleEntry * entry;      struct singleEntry * entry;
3125      struct singleLine * line, * rootLine;      struct singleLine * line, * rootLine;
3126      int index = 0;      int index = 0;
3127      int i, j, k;      int i, k;
3128      const char ** newArgs, ** oldArgs;      const char ** newArgs, ** oldArgs;
3129      const char ** arg;      const char ** arg;
3130      const char * chptr;      int useKernelArgs, useRoot;
     int useKernelArgs = 0;  
     int useRoot = 0;  
3131      int firstElement;      int firstElement;
3132      int *usedElements, *usedArgs;      int *usedElements;
3133        int doreplace;
3134    
3135      if (!image) return 0;      if (!image) return 0;
3136    
# Line 1609  int updateActualImage(struct grubConfig Line 3157  int updateActualImage(struct grubConfig
3157   }   }
3158      }      }
3159    
     for (i = 0; cfg->cfi->keywords[i].key; i++)  
  if (cfg->cfi->keywords[i].type == LT_KERNELARGS) break;  
3160    
3161      if (cfg->cfi->keywords[i].key)      useKernelArgs = (getKeywordByType(LT_KERNELARGS, cfg->cfi)
3162   useKernelArgs = 1;       && (!multibootArgs || cfg->cfi->mbConcatArgs));
3163    
3164      for (i = 0; cfg->cfi->keywords[i].key; i++)      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)
3165   if (cfg->cfi->keywords[i].type == LT_ROOT) break;         && !multibootArgs);
3166    
3167      if (cfg->cfi->keywords[i].key)      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
  useRoot = 1;  
3168    
3169      k = 0;   if (multibootArgs && !entry->multiboot)
3170      for (arg = newArgs; *arg; arg++)      continue;
3171          k++;  
3172      usedArgs = calloc(k, sizeof(int));   /* Determine where to put the args.  If this config supports
3173     * LT_KERNELARGS, use that.  Otherwise use
3174     * LT_HYPER/LT_KERNEL/LT_MBMODULE lines.
3175     */
3176     if (useKernelArgs) {
3177        line = getLineByType(LT_KERNELARGS, entry->lines);
3178        if (!line) {
3179     /* no LT_KERNELARGS, need to add it */
3180     line = addLine(entry, cfg->cfi, LT_KERNELARGS,
3181           cfg->secondaryIndent, NULL);
3182        }
3183        firstElement = 1;
3184    
3185      while ((entry = findEntryByPath(cfg, image, prefix, &index))) {   } else if (multibootArgs) {
3186   index++;      line = getLineByType(LT_HYPER, entry->lines);
3187        if (!line) {
3188     /* a multiboot entry without LT_HYPER? */
3189     continue;
3190        }
3191        firstElement = 2;
3192    
3193   line = entry->lines;   } else {
3194   while (line && line->type != LT_KERNEL) line = line->next;      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3195   if (!line) continue;      if (!line) {
3196   firstElement = 2;   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3197     continue;
3198          if (entry->multiboot && !multibootArgs) {      }
3199              /* first mb module line is the real kernel */      firstElement = 2;
3200              while (line && line->type != LT_MBMODULE) line = line->next;   }
3201              firstElement = 2;  
3202          } else if (useKernelArgs) {   /* handle the elilo case which does:
3203      while (line && line->type != LT_KERNELARGS) line = line->next;   *   append="hypervisor args -- kernel args"
3204      firstElement = 1;   */
3205     if (entry->multiboot && cfg->cfi->mbConcatArgs) {
3206        /* this is a multiboot entry, make sure there's
3207         * -- on the args line
3208         */
3209        for (i = firstElement; i < line->numElements; i++) {
3210     if (!strcmp(line->elements[i].item, "--"))
3211        break;
3212        }
3213        if (i == line->numElements) {
3214     /* assume all existing args are kernel args,
3215     * prepend -- to make it official
3216     */
3217     insertElement(line, "--", firstElement, cfg->cfi);
3218     i = firstElement;
3219        }
3220        if (!multibootArgs) {
3221     /* kernel args start after the -- */
3222     firstElement = i + 1;
3223        }
3224     } else if (cfg->cfi->mbConcatArgs) {
3225        /* this is a non-multiboot entry, remove hyper args */
3226        for (i = firstElement; i < line->numElements; i++) {
3227     if (!strcmp(line->elements[i].item, "--"))
3228        break;
3229        }
3230        if (i < line->numElements) {
3231     /* remove args up to -- */
3232     while (strcmp(line->elements[firstElement].item, "--"))
3233        removeElement(line, firstElement);
3234     /* remove -- */
3235     removeElement(line, firstElement);
3236        }
3237   }   }
3238    
3239   if (!line && useKernelArgs) {          usedElements = calloc(line->numElements, sizeof(*usedElements));
     /* no append in there, need to add it */  
     line = addLine(entry, cfg->cfi, LT_KERNELARGS, NULL, NULL);  
  }  
3240    
3241          usedElements = calloc(line->numElements, sizeof(int));   for (k = 0, arg = newArgs; *arg; arg++, k++) {
3242    
3243          k = 0;      doreplace = 1;
  for (arg = newArgs; *arg; arg++) {  
             if (usedArgs[k]) {  
                 k++;  
                 continue;  
             }  
3244      for (i = firstElement; i < line->numElements; i++) {      for (i = firstElement; i < line->numElements; i++) {
3245     if (multibootArgs && cfg->cfi->mbConcatArgs &&
3246        !strcmp(line->elements[i].item, "--"))
3247     {
3248        /* reached the end of hyper args, insert here */
3249        doreplace = 0;
3250        break;  
3251     }
3252                  if (usedElements[i])                  if (usedElements[i])
3253                      continue;                      continue;
3254   if (!argMatch(line->elements[i].item, *arg)) {   if (!argMatch(line->elements[i].item, *arg)) {
3255                      usedElements[i]=1;                      usedElements[i]=1;
                     usedArgs[k]=1;  
3256      break;      break;
3257                  }                  }
3258              }              }
     chptr = strchr(*arg, '=');  
3259    
3260      if (i < line->numElements) {      if (i < line->numElements && doreplace) {
3261   /* replace */   /* direct replacement */
3262   free(line->elements[i].item);   free(line->elements[i].item);
3263   line->elements[i].item = strdup(*arg);   line->elements[i].item = strdup(*arg);
     } else if (useRoot && !strncmp(*arg, "root=/dev/", 10) && *chptr) {  
  rootLine = entry->lines;  
  while (rootLine && rootLine->type != LT_ROOT)  
     rootLine = rootLine->next;  
  if (!rootLine) {  
     rootLine = addLine(entry, cfg->cfi, LT_ROOT, NULL, NULL);  
     rootLine->elements = realloc(rootLine->elements,  
     2 * sizeof(*rootLine->elements));  
     rootLine->numElements++;  
     rootLine->elements[1].indent = strdup("");  
     rootLine->elements[1].item = strdup("");  
  }  
3264    
3265   free(rootLine->elements[1].item);      } else if (useRoot && !strncmp(*arg, "root=/dev/", 10)) {
3266   rootLine->elements[1].item = strdup(chptr + 1);   /* root= replacement */
3267      } else {   rootLine = getLineByType(LT_ROOT, entry->lines);
3268   /* append */   if (rootLine) {
3269   line->elements = realloc(line->elements,      free(rootLine->elements[1].item);
3270   (line->numElements + 1) * sizeof(*line->elements));      rootLine->elements[1].item = strdup(*arg + 5);
  line->elements[line->numElements].item = strdup(*arg);  
  usedElements = realloc(usedElements,  
  (line->numElements + 1) * sizeof(int));  
  usedElements[line->numElements] = 1;  
   
  if (line->numElements > 1) {  
     /* add to existing list of arguments */  
     line->elements[line->numElements].indent =  
  line->elements[line->numElements - 1].indent;  
     line->elements[line->numElements - 1].indent = strdup(" ");  
3271   } else {   } else {
3272      /* First thing on this line; treat a bit differently. Note      rootLine = addLine(entry, cfg->cfi, LT_ROOT,
3273         this is only possible if we've added a LT_KERNELARGS         cfg->secondaryIndent, *arg + 5);
        entry */  
     line->elements[line->numElements].indent = strdup("");  
3274   }   }
3275        }
3276    
3277   line->numElements++;      else {
3278     /* insert/append */
3279     insertElement(line, *arg, i, cfg->cfi);
3280     usedElements = realloc(usedElements, line->numElements *
3281           sizeof(*usedElements));
3282     memmove(&usedElements[i + 1], &usedElements[i],
3283     line->numElements - i - 1);
3284     usedElements[i] = 1;
3285    
3286   /* if we updated a root= here even though there is a   /* if we updated a root= here even though there is a
3287     LT_ROOT available we need to remove the LT_ROOT entry     LT_ROOT available we need to remove the LT_ROOT entry
3288     (this will happen if we switch from a device to a label) */     (this will happen if we switch from a device to a label) */
3289   if (useRoot && !strncmp(*arg, "root=", 5)) {   if (useRoot && !strncmp(*arg, "root=", 5)) {
3290      rootLine = entry->lines;      rootLine = getLineByType(LT_ROOT, entry->lines);
3291      while (rootLine && rootLine->type != LT_ROOT)      if (rootLine)
  rootLine = rootLine->next;  
     if (rootLine) {  
3292   removeLine(entry, rootLine);   removeLine(entry, rootLine);
     }  
3293   }   }
3294      }      }
             k++;  
3295   }   }
3296    
3297          free(usedElements);          free(usedElements);
3298    
  /* no arguments to remove (i.e. no append line) */  
  if (!line) continue;  
   
  /* this won't remove an LT_ROOT item properly (but then again,  
    who cares? */  
3299   for (arg = oldArgs; *arg; arg++) {   for (arg = oldArgs; *arg; arg++) {
3300      for (i = firstElement; i < line->numElements; i++)      for (i = firstElement; i < line->numElements; i++) {
3301   if (!argMatch(line->elements[i].item, *arg))   if (multibootArgs && cfg->cfi->mbConcatArgs &&
3302        !strcmp(line->elements[i].item, "--"))
3303        /* reached the end of hyper args, stop here */
3304        break;
3305     if (!argMatch(line->elements[i].item, *arg)) {
3306        removeElement(line, i);
3307      break;      break;
   
     if (i < line->numElements) {  
  /* if this isn't the first argument the previous argument  
    gets this arguments post-indention */  
  if (i > firstElement) {  
     free(line->elements[i - 1].indent);  
     line->elements[i - 1].indent = line->elements[i].indent;  
3308   }   }
3309        }
3310   free(line->elements[i].item);      /* handle removing LT_ROOT line too */
3311        if (useRoot && !strncmp(*arg, "root=", 5)) {
3312   for (j = i + 1; j < line->numElements; j++)   rootLine = getLineByType(LT_ROOT, entry->lines);
3313      line->elements[j - 1] = line->elements[j];   if (rootLine)
3314        removeLine(entry, rootLine);
  line->numElements--;  
3315      }      }
3316   }   }
3317    
# Line 1760  int updateActualImage(struct grubConfig Line 3322  int updateActualImage(struct grubConfig
3322   }   }
3323      }      }
3324    
     free(usedArgs);  
3325      free(newArgs);      free(newArgs);
3326      free(oldArgs);      free(oldArgs);
3327    
# Line 1786  int updateImage(struct grubConfig * cfg, Line 3347  int updateImage(struct grubConfig * cfg,
3347      return rc;      return rc;
3348  }  }
3349    
3350    int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
3351     const char * image, const char * prefix, const char * initrd,
3352     const char * title) {
3353        struct singleEntry * entry;
3354        struct singleLine * line, * kernelLine, *endLine = NULL;
3355        int index = 0;
3356    
3357        if (!image) return 0;
3358    
3359        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3360            kernelLine = getLineByType(LT_MBMODULE, entry->lines);
3361            if (!kernelLine) continue;
3362    
3363     /* if title is supplied, the entry's title must match it. */
3364     if (title) {
3365        char *linetitle;
3366    
3367        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3368        if (!line)
3369     continue;
3370    
3371        linetitle = extractTitle(cfg, line);
3372        if (!linetitle)
3373     continue;
3374        if (strcmp(title, linetitle)) {
3375     free(linetitle);
3376     continue;
3377        }
3378        free(linetitle);
3379     }
3380    
3381            if (prefix) {
3382                int prefixLen = strlen(prefix);
3383                if (!strncmp(initrd, prefix, prefixLen))
3384                    initrd += prefixLen;
3385            }
3386     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3387     if (endLine)
3388        removeLine(entry, endLine);
3389            line = addLine(entry, cfg->cfi, preferredLineType(LT_MBMODULE,cfg->cfi),
3390     kernelLine->indent, initrd);
3391            if (!line)
3392        return 1;
3393     if (endLine) {
3394        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3395                if (!line)
3396     return 1;
3397     }
3398    
3399            break;
3400        }
3401    
3402        return 0;
3403    }
3404    
3405    int updateInitrd(struct grubConfig * cfg, const char * image,
3406                     const char * prefix, const char * initrd, const char * title) {
3407        struct singleEntry * entry;
3408        struct singleLine * line, * kernelLine, *endLine = NULL;
3409        int index = 0;
3410    
3411        if (!image) return 0;
3412    
3413        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3414            kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3415            if (!kernelLine) continue;
3416    
3417     /* if title is supplied, the entry's title must match it. */
3418     if (title) {
3419        char *linetitle;
3420    
3421        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3422        if (!line)
3423     continue;
3424    
3425        linetitle = extractTitle(cfg, line);
3426        if (!linetitle)
3427     continue;
3428        if (strcmp(title, linetitle)) {
3429     free(linetitle);
3430     continue;
3431        }
3432        free(linetitle);
3433     }
3434    
3435            line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
3436            if (line)
3437                removeLine(entry, line);
3438            if (prefix) {
3439                int prefixLen = strlen(prefix);
3440                if (!strncmp(initrd, prefix, prefixLen))
3441                    initrd += prefixLen;
3442            }
3443     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3444     if (endLine)
3445        removeLine(entry, endLine);
3446     enum lineType_e lt;
3447     switch(kernelLine->type) {
3448        case LT_KERNEL:
3449            lt = LT_INITRD;
3450     break;
3451        case LT_KERNEL_EFI:
3452            lt = LT_INITRD_EFI;
3453     break;
3454        case LT_KERNEL_16:
3455            lt = LT_INITRD_16;
3456     break;
3457        default:
3458            lt = preferredLineType(LT_INITRD, cfg->cfi);
3459     }
3460            line = addLine(entry, cfg->cfi, lt, kernelLine->indent, initrd);
3461            if (!line)
3462        return 1;
3463     if (endLine) {
3464        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3465                if (!line)
3466     return 1;
3467     }
3468    
3469            break;
3470        }
3471    
3472        return 0;
3473    }
3474    
3475  int checkDeviceBootloader(const char * device, const unsigned char * boot) {  int checkDeviceBootloader(const char * device, const unsigned char * boot) {
3476      int fd;      int fd;
3477      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 1809  int checkDeviceBootloader(const char * d Line 3495  int checkDeviceBootloader(const char * d
3495      if (memcmp(boot, bootSect, 3))      if (memcmp(boot, bootSect, 3))
3496   return 0;   return 0;
3497    
3498      if (boot[1] == 0xeb) {      if (boot[1] == JMP_SHORT_OPCODE) {
3499   offset = boot[2] + 2;   offset = boot[2] + 2;
3500      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {
3501   offset = (boot[3] << 8) + boot[2] + 2;   offset = (boot[3] << 8) + boot[2] + 2;
3502      } else if (boot[0] == 0xeb) {      } else if (boot[0] == JMP_SHORT_OPCODE) {
3503   offset = boot[1] + 2;        offset = boot[1] + 2;
3504            /*
3505     * it looks like grub, when copying stage1 into the mbr, patches stage1
3506     * right after the JMP location, replacing other instructions such as
3507     * JMPs for NOOPs. So, relax the check a little bit by skipping those
3508     * different bytes.
3509     */
3510          if ((bootSect[offset + 1] == NOOP_OPCODE)
3511      && (bootSect[offset + 2] == NOOP_OPCODE)) {
3512     offset = offset + 3;
3513          }
3514      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {
3515   offset = (boot[2] << 8) + boot[1] + 2;   offset = (boot[2] << 8) + boot[1] + 2;
3516      } else {      } else {
# Line 1956  int checkForLilo(struct grubConfig * con Line 3652  int checkForLilo(struct grubConfig * con
3652      return checkDeviceBootloader(line->elements[1].item, boot);      return checkDeviceBootloader(line->elements[1].item, boot);
3653  }  }
3654    
3655    int checkForGrub2(struct grubConfig * config) {
3656        if (!access("/etc/grub.d/", R_OK))
3657     return 2;
3658    
3659        return 1;
3660    }
3661    
3662  int checkForGrub(struct grubConfig * config) {  int checkForGrub(struct grubConfig * config) {
3663      int fd;      int fd;
3664      unsigned char bootSect[512];      unsigned char bootSect[512];
3665      char * boot;      char * boot;
3666        int onSuse = isSuseSystem();
3667    
3668      if (parseSysconfigGrub(NULL, &boot))  
3669   return 0;      if (onSuse) {
3670     if (parseSuseGrubConf(NULL, &boot))
3671        return 0;
3672        } else {
3673     if (parseSysconfigGrub(NULL, &boot))
3674        return 0;
3675        }
3676    
3677      /* assume grub is not installed -- not an error condition */      /* assume grub is not installed -- not an error condition */
3678      if (!boot)      if (!boot)
# Line 1976  int checkForGrub(struct grubConfig * con Line 3686  int checkForGrub(struct grubConfig * con
3686      if (read(fd, bootSect, 512) != 512) {      if (read(fd, bootSect, 512) != 512) {
3687   fprintf(stderr, _("grubby: unable to read %s: %s\n"),   fprintf(stderr, _("grubby: unable to read %s: %s\n"),
3688   "/boot/grub/stage1", strerror(errno));   "/boot/grub/stage1", strerror(errno));
3689     close(fd);
3690     return 1;
3691        }
3692        close(fd);
3693    
3694        /* The more elaborate checks do not work on SuSE. The checks done
3695         * seem to be reasonble (at least for now), so just return success
3696         */
3697        if (onSuse)
3698     return 2;
3699    
3700        return checkDeviceBootloader(boot, bootSect);
3701    }
3702    
3703    int checkForExtLinux(struct grubConfig * config) {
3704        int fd;
3705        unsigned char bootSect[512];
3706        char * boot;
3707        char executable[] = "/boot/extlinux/extlinux";
3708    
3709        printf("entered: checkForExtLinux()\n");
3710    
3711        if (parseSysconfigGrub(NULL, &boot))
3712     return 0;
3713    
3714        /* assume grub is not installed -- not an error condition */
3715        if (!boot)
3716     return 0;
3717    
3718        fd = open(executable, O_RDONLY);
3719        if (fd < 0)
3720     /* this doesn't exist if grub hasn't been installed */
3721     return 0;
3722    
3723        if (read(fd, bootSect, 512) != 512) {
3724     fprintf(stderr, _("grubby: unable to read %s: %s\n"),
3725     executable, strerror(errno));
3726   return 1;   return 1;
3727      }      }
3728      close(fd);      close(fd);
# Line 1983  int checkForGrub(struct grubConfig * con Line 3730  int checkForGrub(struct grubConfig * con
3730      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3731  }  }
3732    
3733    int checkForYaboot(struct grubConfig * config) {
3734        /*
3735         * This is a simplistic check that we consider good enough for own puporses
3736         *
3737         * If we were to properly check if yaboot is *installed* we'd need to:
3738         * 1) get the system boot device (LT_BOOT)
3739         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3740         *    the content on the boot device
3741         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3742         * 4) check again if binary and boot device contents match
3743         */
3744        if (!access("/etc/yaboot.conf", R_OK))
3745     return 2;
3746    
3747        return 1;
3748    }
3749    
3750    int checkForElilo(struct grubConfig * config) {
3751        if (!access("/etc/elilo.conf", R_OK))
3752     return 2;
3753    
3754        return 1;
3755    }
3756    
3757  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3758      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3759    
# Line 1994  static char * getRootSpecifier(char * st Line 3765  static char * getRootSpecifier(char * st
3765      return rootspec;      return rootspec;
3766  }  }
3767    
3768    static char * getInitrdVal(struct grubConfig * config,
3769       const char * prefix, struct singleLine *tmplLine,
3770       const char * newKernelInitrd,
3771       const char ** extraInitrds, int extraInitrdCount)
3772    {
3773        char *initrdVal, *end;
3774        int i;
3775        size_t totalSize;
3776        size_t prefixLen;
3777        char separatorChar;
3778    
3779        prefixLen = strlen(prefix);
3780        totalSize = strlen(newKernelInitrd) - prefixLen + 1 /* \0 */;
3781    
3782        for (i = 0; i < extraInitrdCount; i++) {
3783     totalSize += sizeof(separatorChar);
3784     totalSize += strlen(extraInitrds[i]) - prefixLen;
3785        }
3786    
3787        initrdVal = end = malloc(totalSize);
3788    
3789        end = stpcpy (end, newKernelInitrd + prefixLen);
3790    
3791        separatorChar = getKeywordByType(LT_INITRD, config->cfi)->separatorChar;
3792        for (i = 0; i < extraInitrdCount; i++) {
3793     const char *extraInitrd;
3794     int j;
3795    
3796     extraInitrd = extraInitrds[i] + prefixLen;
3797     /* Don't add entries that are already there */
3798     if (tmplLine != NULL) {
3799        for (j = 2; j < tmplLine->numElements; j++)
3800     if (strcmp(extraInitrd, tmplLine->elements[j].item) == 0)
3801        break;
3802    
3803        if (j != tmplLine->numElements)
3804     continue;
3805     }
3806    
3807     *end++ = separatorChar;
3808     end = stpcpy(end, extraInitrd);
3809        }
3810    
3811        return initrdVal;
3812    }
3813    
3814  int addNewKernel(struct grubConfig * config, struct singleEntry * template,  int addNewKernel(struct grubConfig * config, struct singleEntry * template,
3815           const char * prefix,           const char * prefix,
3816   char * newKernelPath, char * newKernelTitle,   const char * newKernelPath, const char * newKernelTitle,
3817   char * newKernelArgs, char * newKernelInitrd,   const char * newKernelArgs, const char * newKernelInitrd,
3818                   char * newMBKernel, char * newMBKernelArgs) {   const char ** extraInitrds, int extraInitrdCount,
3819                     const char * newMBKernel, const char * newMBKernelArgs,
3820     const char * newDevTreePath) {
3821      struct singleEntry * new;      struct singleEntry * new;
3822      struct singleLine * newLine = NULL, * tmplLine = NULL, * lastLine = NULL;      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;
3823      int needs;      int needs;
     char * indent = NULL;  
     char * rootspec = NULL;  
3824      char * chptr;      char * chptr;
     int i;  
     enum lineType_e type;  
3825    
3826      if (!newKernelPath) return 0;      if (!newKernelPath) return 0;
3827    
# Line 2036  int addNewKernel(struct grubConfig * con Line 3851  int addNewKernel(struct grubConfig * con
3851      config->entries = new;      config->entries = new;
3852    
3853      /* copy/update from the template */      /* copy/update from the template */
3854      needs = KERNEL_KERNEL | KERNEL_INITRD | KERNEL_TITLE;      needs = NEED_KERNEL | NEED_TITLE;
3855        if (newKernelInitrd)
3856     needs |= NEED_INITRD;
3857      if (newMBKernel) {      if (newMBKernel) {
3858          needs |= KERNEL_MB;          needs |= NEED_MB;
3859          new->multiboot = 1;          new->multiboot = 1;
3860      }      }
3861        if (newDevTreePath && getKeywordByType(LT_DEVTREE, config->cfi))
3862     needs |= NEED_DEVTREE;
3863    
3864      if (template) {      if (template) {
3865   for (tmplLine = template->lines; tmplLine; tmplLine = tmplLine->next) {   for (masterLine = template->lines;
3866      /* remember the indention level; we may need it for new lines */       masterLine && (tmplLine = lineDup(masterLine));
3867      if (tmplLine->numElements)       lineFree(tmplLine), masterLine = masterLine->next)
3868   indent = tmplLine->indent;   {
3869        dbgPrintf("addNewKernel processing %d\n", tmplLine->type);
3870    
3871      /* skip comments */      /* skip comments */
3872      chptr = tmplLine->indent;      chptr = tmplLine->indent;
3873      while (*chptr && isspace(*chptr)) chptr++;      while (*chptr && isspace(*chptr)) chptr++;
3874      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3875    
3876      /* we don't need an initrd here */      if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) {
3877      if (tmplLine->type == LT_INITRD && !newKernelInitrd) continue;   if (!template->multiboot && (needs & NEED_MB)) {
3878        /* it's not a multiboot template and this is the kernel
3879              if (tmplLine->type == LT_KERNEL &&       * line.  Try to be intelligent about inserting the
3880                  !template->multiboot && (needs & KERNEL_MB)) {       * hypervisor at the same time.
3881                  struct singleLine *l;       */
3882                  needs &= ~ KERNEL_MB;      if (config->cfi->mbHyperFirst) {
3883     /* insert the hypervisor first */
3884                  l = addLine(new, config->cfi, LT_KERNEL,   newLine = addLine(new, config->cfi, LT_HYPER,
3885                                    config->secondaryIndent,    tmplLine->indent,
3886                                    newMBKernel + strlen(prefix));    newMBKernel + strlen(prefix));
3887                     /* set up for adding the kernel line */
3888                  tmplLine = lastLine;   free(tmplLine->indent);
3889                  if (!new->lines) {   tmplLine->indent = strdup(config->secondaryIndent);
3890                      new->lines = l;   needs &= ~NEED_MB;
3891                  } else {      }
3892                      newLine->next = l;      if (needs & NEED_KERNEL) {
3893                      newLine = l;   /* use addLineTmpl to preserve line elements,
3894                  }   * otherwise we could just call addLine.  Unfortunately
3895                  continue;   * this means making some changes to the template
3896              } else if (tmplLine->type == LT_KERNEL &&   * such as the indent change above and the type
3897                         template->multiboot && !new->multiboot) {   * change below.
3898                  continue; /* don't need multiboot kernel here */   */
3899              }   struct keywordTypes * mbm_kw =
3900        getKeywordByType(LT_MBMODULE, config->cfi);
3901      if (!new->lines) {   if (mbm_kw) {
3902   newLine = malloc(sizeof(*newLine));      tmplLine->type = LT_MBMODULE;
3903   new->lines = newLine;      free(tmplLine->elements[0].item);
3904      } else {      tmplLine->elements[0].item = strdup(mbm_kw->key);
3905   newLine->next = malloc(sizeof(*newLine));   }
3906   newLine = newLine->next;   newLine = addLineTmpl(new, tmplLine, newLine,
3907      }        newKernelPath + strlen(prefix), config->cfi);
3908     needs &= ~NEED_KERNEL;
3909        }
3910        if (needs & NEED_MB) { /* !mbHyperFirst */
3911     newLine = addLine(new, config->cfi, LT_HYPER,
3912      config->secondaryIndent,
3913      newMBKernel + strlen(prefix));
3914     needs &= ~NEED_MB;
3915        }
3916     } else if (needs & NEED_KERNEL) {
3917        newLine = addLineTmpl(new, tmplLine, newLine,
3918      newKernelPath + strlen(prefix), config->cfi);
3919        needs &= ~NEED_KERNEL;
3920     }
3921    
3922        } else if (tmplLine->type == LT_HYPER &&
3923           tmplLine->numElements >= 2) {
3924     if (needs & NEED_MB) {
3925        newLine = addLineTmpl(new, tmplLine, newLine,
3926      newMBKernel + strlen(prefix), config->cfi);
3927        needs &= ~NEED_MB;
3928     }
3929    
3930      newLine->indent = strdup(tmplLine->indent);      } else if (tmplLine->type == LT_MBMODULE &&
3931      newLine->next = NULL;         tmplLine->numElements >= 2) {
3932      newLine->type = tmplLine->type;   if (new->multiboot) {
3933      newLine->numElements = tmplLine->numElements;      if (needs & NEED_KERNEL) {
3934      newLine->elements = malloc(sizeof(*newLine->elements) *   newLine = addLineTmpl(new, tmplLine, newLine,
3935      newLine->numElements);        newKernelPath +
3936      for (i = 0; i < newLine->numElements; i++) {        strlen(prefix), config->cfi);
3937   newLine->elements[i].item = strdup(tmplLine->elements[i].item);   needs &= ~NEED_KERNEL;
3938   newLine->elements[i].indent =      } else if (config->cfi->mbInitRdIsModule &&
3939   strdup(tmplLine->elements[i].indent);         (needs & NEED_INITRD)) {
3940      }   char *initrdVal;
3941     initrdVal = getInitrdVal(config, prefix, tmplLine,
3942              lastLine = tmplLine;   newKernelInitrd, extraInitrds,
3943      if (tmplLine->type == LT_KERNEL && tmplLine->numElements >= 2) {   extraInitrdCount);
3944                  char * repl;   newLine = addLineTmpl(new, tmplLine, newLine,
3945                  if (!template->multiboot) {        initrdVal, config->cfi);
3946                      needs &= ~KERNEL_KERNEL;   free(initrdVal);
3947                      repl = newKernelPath;   needs &= ~NEED_INITRD;
3948                  } else {      }
3949                      needs &= ~KERNEL_MB;   } else if (needs & NEED_KERNEL) {
3950                      repl = newMBKernel;      /* template is multi but new is not,
3951                  }       * insert the kernel in the first module slot
3952                  if (new->multiboot && !template->multiboot) {       */
3953                      free(newLine->elements[0].item);      tmplLine->type = preferredLineType(LT_KERNEL, config->cfi);
3954                      newLine->elements[0].item = strdup("module");      free(tmplLine->elements[0].item);
3955                      newLine->type = LT_MBMODULE;      tmplLine->elements[0].item =
3956                  }   strdup(getKeywordByType(tmplLine->type,
3957   free(newLine->elements[1].item);   config->cfi)->key);
3958                  rootspec = getRootSpecifier(tmplLine->elements[1].item);      newLine = addLineTmpl(new, tmplLine, newLine,
3959                  if (rootspec != NULL) {    newKernelPath + strlen(prefix),
3960                      newLine->elements[1].item = sdupprintf("%s%s",    config->cfi);
3961                                                             rootspec,      needs &= ~NEED_KERNEL;
3962                                                             repl +   } else if (needs & NEED_INITRD) {
3963                                                             strlen(prefix));      char *initrdVal;
3964                  } else {      /* template is multi but new is not,
3965                      newLine->elements[1].item = strdup(repl +       * insert the initrd in the second module slot
3966                                                         strlen(prefix));       */
3967                  }      tmplLine->type = preferredLineType(LT_INITRD, config->cfi);
3968              } else if (tmplLine->type == LT_MBMODULE &&      free(tmplLine->elements[0].item);
3969                         tmplLine->numElements >= 2 && (needs & KERNEL_KERNEL)) {      tmplLine->elements[0].item =
3970                  needs &= ~KERNEL_KERNEL;   strdup(getKeywordByType(tmplLine->type,
3971                  if (!new->multiboot && template->multiboot) {   config->cfi)->key);
3972                      free(newLine->elements[0].item);      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3973                      newLine->elements[0].item = strdup("kernel");      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3974                      newLine->type = LT_KERNEL;      free(initrdVal);
3975                  }      needs &= ~NEED_INITRD;
3976   free(newLine->elements[1].item);   }
                 rootspec = getRootSpecifier(tmplLine->elements[1].item);  
                 if (rootspec != NULL) {  
                     newLine->elements[1].item = sdupprintf("%s%s",  
                                                            rootspec,  
                                                            newKernelPath +  
                                                            strlen(prefix));  
                 } else {  
                     newLine->elements[1].item = strdup(newKernelPath +  
                                                        strlen(prefix));  
                 }  
     } else if (tmplLine->type == LT_INITRD &&  
     tmplLine->numElements >= 2) {  
  needs &= ~KERNEL_INITRD;  
  free(newLine->elements[1].item);  
                 if (new->multiboot && !template->multiboot) {  
                     free(newLine->elements[0].item);  
                     newLine->elements[0].item = strdup("module");  
                     newLine->type = LT_MBMODULE;  
                 }  
                 rootspec = getRootSpecifier(tmplLine->elements[1].item);  
                 if (rootspec != NULL) {  
                     newLine->elements[1].item = sdupprintf("%s%s",  
                                                            rootspec,  
                                                            newKernelInitrd +  
                                                            strlen(prefix));  
                 } else {  
                     newLine->elements[1].item = strdup(newKernelInitrd +  
                                                        strlen(prefix));  
                 }  
             } else if (tmplLine->type == LT_MBMODULE &&  
                        tmplLine->numElements >= 2 && (needs & KERNEL_INITRD)) {  
  needs &= ~KERNEL_INITRD;  
                 if (!new->multiboot && template->multiboot) {  
                     free(newLine->elements[0].item);  
                     newLine->elements[0].item = strdup("initrd");  
                     newLine->type = LT_INITRD;  
                 }  
  free(newLine->elements[1].item);  
                 rootspec = getRootSpecifier(tmplLine->elements[1].item);  
                 if (rootspec != NULL) {  
                     newLine->elements[1].item = sdupprintf("%s%s",  
                                                            rootspec,  
                                                            newKernelInitrd +  
                                                            strlen(prefix));  
                 } else {  
                     newLine->elements[1].item = strdup(newKernelInitrd +  
                                                        strlen(prefix));  
                 }  
     } else if (tmplLine->type == LT_TITLE &&  
     tmplLine->numElements >= 2) {  
  needs &= ~KERNEL_TITLE;  
3977    
3978   for (i = 1; i < newLine->numElements; i++) {      } else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) {
3979      free(newLine->elements[i].item);   if (needs & NEED_INITRD &&
3980      free(newLine->elements[i].indent);      new->multiboot && !template->multiboot &&
3981        config->cfi->mbInitRdIsModule) {
3982        /* make sure we don't insert the module initrd
3983         * before the module kernel... if we don't do it here,
3984         * it will be inserted following the template.
3985         */
3986        if (!needs & NEED_KERNEL) {
3987     char *initrdVal;
3988    
3989     initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3990     newLine = addLine(new, config->cfi, LT_MBMODULE,
3991      config->secondaryIndent,
3992      initrdVal);
3993     free(initrdVal);
3994     needs &= ~NEED_INITRD;
3995        }
3996     } else if (needs & NEED_INITRD) {
3997        char *initrdVal;
3998        initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3999        newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
4000        free(initrdVal);
4001        needs &= ~NEED_INITRD;
4002   }   }
4003    
4004   newLine->elements[1].item = strdup(newKernelTitle);      } else if (tmplLine->type == LT_MENUENTRY &&
4005   newLine->elements[1].indent = strdup("");         (needs & NEED_TITLE)) {
4006   newLine->numElements = 2;   requote(tmplLine, config->cfi);
4007     char *nkt = malloc(strlen(newKernelTitle)+3);
4008     strcpy(nkt, "'");
4009     strcat(nkt, newKernelTitle);
4010     strcat(nkt, "'");
4011     newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi);
4012     free(nkt);
4013     needs &= ~NEED_TITLE;
4014      } else if (tmplLine->type == LT_TITLE &&      } else if (tmplLine->type == LT_TITLE &&
4015                         config->cfi->titleBracketed &&         (needs & NEED_TITLE)) {
4016                         tmplLine->numElements == 1) {   if (tmplLine->numElements >= 2) {
4017                  needs &= ~KERNEL_TITLE;      newLine = addLineTmpl(new, tmplLine, newLine,
4018                  free(newLine->elements[0].item);    newKernelTitle, config->cfi);
4019                  free(newLine->elements[0].indent);      needs &= ~NEED_TITLE;
4020                  newLine->elements = malloc(sizeof(*newLine->elements) *   } else if (tmplLine->numElements == 1 &&
4021                                             newLine->numElements);     config->cfi->titleBracketed) {
4022        /* addLineTmpl doesn't handle titleBracketed */
4023                  newLine->elements[0].item = malloc(strlen(newKernelTitle) + 3);      newLine = addLine(new, config->cfi, LT_TITLE,
4024                  sprintf(newLine->elements[0].item, "[%s]", newKernelTitle);        tmplLine->indent, newKernelTitle);
4025                  newLine->elements[0].indent = strdup("");      needs &= ~NEED_TITLE;
4026                  newLine->numElements = 1;   }
4027              }      } else if (tmplLine->type == LT_ECHO) {
4028        requote(tmplLine, config->cfi);
4029        static const char *prefix = "'Loading ";
4030        if (tmplLine->numElements > 1 &&
4031        strstr(tmplLine->elements[1].item, prefix) &&
4032        masterLine->next &&
4033        iskernel(masterLine->next->type)) {
4034     char *newTitle = malloc(strlen(prefix) +
4035     strlen(newKernelTitle) + 2);
4036    
4037     strcpy(newTitle, prefix);
4038     strcat(newTitle, newKernelTitle);
4039     strcat(newTitle, "'");
4040     newLine = addLine(new, config->cfi, LT_ECHO,
4041     tmplLine->indent, newTitle);
4042     free(newTitle);
4043        } else {
4044     /* pass through other lines from the template */
4045     newLine = addLineTmpl(new, tmplLine, newLine, NULL,
4046     config->cfi);
4047        }
4048        } else if (tmplLine->type == LT_DEVTREE &&
4049           tmplLine->numElements == 2 && newDevTreePath) {
4050            newLine = addLineTmpl(new, tmplLine, newLine,
4051          newDevTreePath + strlen(prefix),
4052          config->cfi);
4053     needs &= ~NEED_DEVTREE;
4054        } else if (tmplLine->type == LT_ENTRY_END && needs & NEED_DEVTREE) {
4055     const char *ndtp = newDevTreePath;
4056     if (!strncmp(newDevTreePath, prefix, strlen(prefix)))
4057        ndtp += strlen(prefix);
4058     newLine = addLine(new, config->cfi, LT_DEVTREE,
4059      config->secondaryIndent,
4060      ndtp);
4061     needs &= ~NEED_DEVTREE;
4062     newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
4063        } else {
4064     /* pass through other lines from the template */
4065     newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
4066        }
4067   }   }
4068    
4069      } else {      } else {
4070   for (i = 0; config->cfi->keywords[i].key; i++) {   /* don't have a template, so start the entry with the
4071      if ((config->cfi->keywords[i].type == config->cfi->entrySeparator) || (config->cfi->keywords[i].type == LT_OTHER))   * appropriate starting line
4072     */
4073     switch (config->cfi->entryStart) {
4074        case LT_KERNEL:
4075        case LT_KERNEL_EFI:
4076        case LT_KERNEL_16:
4077     if (new->multiboot && config->cfi->mbHyperFirst) {
4078        /* fall through to LT_HYPER */
4079     } else {
4080        newLine = addLine(new, config->cfi,
4081              preferredLineType(LT_KERNEL, config->cfi),
4082          config->primaryIndent,
4083          newKernelPath + strlen(prefix));
4084        needs &= ~NEED_KERNEL;
4085        break;
4086     }
4087    
4088        case LT_HYPER:
4089     newLine = addLine(new, config->cfi, LT_HYPER,
4090      config->primaryIndent,
4091      newMBKernel + strlen(prefix));
4092     needs &= ~NEED_MB;
4093   break;   break;
         }  
4094    
4095   switch (config->cfi->keywords[i].type) {      case LT_MENUENTRY: {
4096      case LT_KERNEL:  needs &= ~KERNEL_KERNEL,   char *nkt = malloc(strlen(newKernelTitle)+3);
4097       chptr = newKernelPath + strlen(prefix);   strcpy(nkt, "'");
4098       type = LT_KERNEL; break;   strcat(nkt, newKernelTitle);
4099      case LT_TITLE:   needs &= ~KERNEL_TITLE, chptr = newKernelTitle;   strcat(nkt, "'");
4100       type = LT_TITLE; break;          newLine = addLine(new, config->cfi, LT_MENUENTRY,
4101      default:        config->primaryIndent, nkt);
4102                  /* zipl strikes again */   free(nkt);
4103                  if (config->cfi->titleBracketed) {   needs &= ~NEED_TITLE;
4104                      needs &= ~KERNEL_TITLE;   needs |= NEED_END;
4105                      chptr = newKernelTitle;   break;
4106                      type = LT_TITLE;      }
4107                      break;      case LT_TITLE:
4108                  } else {   if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above)
4109                      abort();   char * templabel;
4110                  }   int x = 0, y = 0;
4111   }  
4112     templabel = strdup(newKernelTitle);
4113     while( templabel[x]){
4114     if( templabel[x] == ' ' ){
4115     y = x;
4116     while( templabel[y] ){
4117     templabel[y] = templabel[y+1];
4118     y++;
4119     }
4120     }
4121     x++;
4122     }
4123     newLine = addLine(new, config->cfi, LT_TITLE,
4124      config->primaryIndent, templabel);
4125     free(templabel);
4126     }else{
4127     newLine = addLine(new, config->cfi, LT_TITLE,
4128      config->primaryIndent, newKernelTitle);
4129     }
4130     needs &= ~NEED_TITLE;
4131     break;
4132    
4133   newLine = addLine(new, config->cfi, type, config->primaryIndent, chptr);      default:
4134   new->lines = newLine;   abort();
4135     }
4136      }      }
4137    
4138      if (new->multiboot) {      struct singleLine *endLine = NULL;
4139          if (needs & KERNEL_MB)      endLine = getLineByType(LT_ENTRY_END, new->lines);
4140              newLine = addLine(new, config->cfi, LT_KERNEL,      if (endLine) {
4141                                config->secondaryIndent,      removeLine(new, endLine);
4142                                newMBKernel + strlen(prefix));      needs |= NEED_END;
4143          if (needs & KERNEL_KERNEL)      }
4144              newLine = addLine(new, config->cfi, LT_MBMODULE,  
4145                                config->secondaryIndent,      /* add the remainder of the lines, i.e. those that either
4146                                newKernelPath + strlen(prefix));       * weren't present in the template, or in the case of no template,
4147          /* don't need to check for title as it's guaranteed to have been       * all the lines following the entryStart.
4148           * done as we only do multiboot with grub which uses title as       */
4149           * a separator */      if (needs & NEED_TITLE) {
4150          if (needs & KERNEL_INITRD && newKernelInitrd)   newLine = addLine(new, config->cfi, LT_TITLE,
4151              newLine = addLine(new, config->cfi, LT_MBMODULE,    config->secondaryIndent,
4152                                config->secondaryIndent,    newKernelTitle);
4153                                newKernelInitrd + strlen(prefix));   needs &= ~NEED_TITLE;
4154      } else {      }
4155          if (needs & KERNEL_KERNEL)      if ((needs & NEED_MB) && config->cfi->mbHyperFirst) {
4156              newLine = addLine(new, config->cfi, LT_KERNEL,   newLine = addLine(new, config->cfi, LT_HYPER,
4157                                config->secondaryIndent,    config->secondaryIndent,
4158                                newKernelPath + strlen(prefix));    newMBKernel + strlen(prefix));
4159          if (needs & KERNEL_TITLE)   needs &= ~NEED_MB;
4160              newLine = addLine(new, config->cfi, LT_TITLE,      }
4161                                config->secondaryIndent,      if (needs & NEED_KERNEL) {
4162                                newKernelTitle);   newLine = addLine(new, config->cfi,
4163          if (needs & KERNEL_INITRD && newKernelInitrd)    (new->multiboot && getKeywordByType(LT_MBMODULE,
4164              newLine = addLine(new, config->cfi, LT_INITRD,        config->cfi))
4165                                config->secondaryIndent,     ? LT_MBMODULE
4166                                newKernelInitrd + strlen(prefix));   : preferredLineType(LT_KERNEL, config->cfi),
4167      config->secondaryIndent,
4168      newKernelPath + strlen(prefix));
4169     needs &= ~NEED_KERNEL;
4170        }
4171        if (needs & NEED_MB) {
4172     newLine = addLine(new, config->cfi, LT_HYPER,
4173      config->secondaryIndent,
4174      newMBKernel + strlen(prefix));
4175     needs &= ~NEED_MB;
4176        }
4177        if (needs & NEED_INITRD) {
4178     char *initrdVal;
4179     initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);
4180     newLine = addLine(new, config->cfi,
4181      (new->multiboot && getKeywordByType(LT_MBMODULE,
4182          config->cfi))
4183       ? LT_MBMODULE
4184       : preferredLineType(LT_INITRD, config->cfi),
4185      config->secondaryIndent,
4186      initrdVal);
4187     free(initrdVal);
4188     needs &= ~NEED_INITRD;
4189        }
4190        if (needs & NEED_DEVTREE) {
4191     newLine = addLine(new, config->cfi, LT_DEVTREE,
4192      config->secondaryIndent,
4193      newDevTreePath);
4194     needs &= ~NEED_DEVTREE;
4195        }
4196    
4197        /* NEEDS_END must be last on bootloaders that need it... */
4198        if (needs & NEED_END) {
4199     newLine = addLine(new, config->cfi, LT_ENTRY_END,
4200     config->secondaryIndent, NULL);
4201     needs &= ~NEED_END;
4202        }
4203    
4204        if (needs) {
4205     printf(_("grubby: needs=%d, aborting\n"), needs);
4206     abort();
4207      }      }
4208    
4209      if (updateImage(config, "0", prefix, newKernelArgs, NULL,      if (updateImage(config, "0", prefix, newKernelArgs, NULL,
# Line 2276  int addNewKernel(struct grubConfig * con Line 4214  int addNewKernel(struct grubConfig * con
4214    
4215  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
4216      poptContext optCon;      poptContext optCon;
4217      char * grubConfig = NULL;      const char * grubConfig = NULL;
4218      char * outputFile = NULL;      char * outputFile = NULL;
4219      int arg = 0;      int arg = 0;
4220      int flags = 0;      int flags = 0;
4221      int badImageOkay = 0;      int badImageOkay = 0;
4222        int configureGrub2 = 0;
4223      int configureLilo = 0, configureELilo = 0, configureGrub = 0;      int configureLilo = 0, configureELilo = 0, configureGrub = 0;
4224      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;
4225        int configureExtLinux = 0;
4226      int bootloaderProbe = 0;      int bootloaderProbe = 0;
4227        int extraInitrdCount = 0;
4228      char * updateKernelPath = NULL;      char * updateKernelPath = NULL;
4229      char * newKernelPath = NULL;      char * newKernelPath = NULL;
4230      char * removeKernelPath = NULL;      char * removeKernelPath = NULL;
4231      char * newKernelArgs = NULL;      char * newKernelArgs = NULL;
4232      char * newKernelInitrd = NULL;      char * newKernelInitrd = NULL;
4233      char * newKernelTitle = NULL;      char * newKernelTitle = NULL;
4234      char * newKernelVersion = NULL;      char * newDevTreePath = NULL;
4235      char * newMBKernel = NULL;      char * newMBKernel = NULL;
4236      char * newMBKernelArgs = NULL;      char * newMBKernelArgs = NULL;
4237      char * removeMBKernelArgs = NULL;      char * removeMBKernelArgs = NULL;
# Line 2299  int main(int argc, const char ** argv) { Line 4240  int main(int argc, const char ** argv) {
4240      char * defaultKernel = NULL;      char * defaultKernel = NULL;
4241      char * removeArgs = NULL;      char * removeArgs = NULL;
4242      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4243        char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4244        char * envPath = NULL;
4245      const char * chptr = NULL;      const char * chptr = NULL;
4246      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4247      struct grubConfig * config;      struct grubConfig * config;
4248      struct singleEntry * template = NULL;      struct singleEntry * template = NULL;
4249      int copyDefault = 0, makeDefault = 0;      int copyDefault = 0, makeDefault = 0;
4250      int displayDefault = 0;      int displayDefault = 0;
4251        int displayDefaultIndex = 0;
4252        int displayDefaultTitle = 0;
4253        int defaultIndex = -1;
4254      struct poptOption options[] = {      struct poptOption options[] = {
4255   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
4256      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 2322  int main(int argc, const char ** argv) { Line 4268  int main(int argc, const char ** argv) {
4268   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4269      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
4270      _("bootfs") },      _("bootfs") },
4271  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4272   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
4273      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
4274  #endif  #endif
4275   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
4276      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 2335  int main(int argc, const char ** argv) { Line 4281  int main(int argc, const char ** argv) {
4281        "the kernel referenced by the default image does not exist, "        "the kernel referenced by the default image does not exist, "
4282        "the first linux entry whose kernel does exist is used as the "        "the first linux entry whose kernel does exist is used as the "
4283        "template"), NULL },        "template"), NULL },
4284     { "debug", 0, 0, &debug, 0,
4285        _("print debugging information for failures") },
4286   { "default-kernel", 0, 0, &displayDefault, 0,   { "default-kernel", 0, 0, &displayDefault, 0,
4287      _("display the path of the default kernel") },      _("display the path of the default kernel") },
4288     { "default-index", 0, 0, &displayDefaultIndex, 0,
4289        _("display the index of the default kernel") },
4290     { "default-title", 0, 0, &displayDefaultTitle, 0,
4291        _("display the title of the default kernel") },
4292     { "devtree", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4293        _("device tree file for new stanza"), _("dtb-path") },
4294     { "devtreedir", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4295        _("device tree directory for new stanza"), _("dtb-path") },
4296   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4297      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4298     { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4299        _("force grub2 stanzas to use efi") },
4300     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4301        _("path for environment data"),
4302        _("path") },
4303     { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4304        _("configure extlinux bootloader (from syslinux)") },
4305   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
4306      _("configure grub bootloader") },      _("configure grub bootloader") },
4307     { "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,
4308        _("configure grub2 bootloader") },
4309   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,
4310      _("display boot information for specified kernel"),      _("display boot information for specified kernel"),
4311      _("kernel-path") },      _("kernel-path") },
4312   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4313      _("initrd image for the new kernel"), _("initrd-path") },      _("initrd image for the new kernel"), _("initrd-path") },
4314     { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4315        _("auxiliary initrd image for things other than the new kernel"), _("initrd-path") },
4316   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4317      _("configure lilo bootloader") },      _("configure lilo bootloader") },
4318   { "make-default", 0, 0, &makeDefault, 0,   { "make-default", 0, 0, &makeDefault, 0,
# Line 2365  int main(int argc, const char ** argv) { Line 4332  int main(int argc, const char ** argv) {
4332   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
4333      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
4334        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
4335     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4336        _("make the given entry index the default entry"),
4337        _("entry-index") },
4338   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4339      _("configure silo bootloader") },      _("configure silo bootloader") },
4340   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 2382  int main(int argc, const char ** argv) { Line 4352  int main(int argc, const char ** argv) {
4352   { 0, 0, 0, 0, 0 }   { 0, 0, 0, 0, 0 }
4353      };      };
4354    
4355        useextlinuxmenu=0;
4356    
4357        int i = 0;
4358        for (int j = 1; j < argc; j++)
4359     i += strlen(argv[j]) + 1;
4360        saved_command_line = malloc(i);
4361        if (!saved_command_line) {
4362     fprintf(stderr, "grubby: %m\n");
4363     exit(1);
4364        }
4365        saved_command_line[0] = '\0';
4366        for (int j = 1; j < argc; j++) {
4367     strcat(saved_command_line, argv[j]);
4368     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4369        }
4370    
4371      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4372      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
4373    
# Line 2391  int main(int argc, const char ** argv) { Line 4377  int main(int argc, const char ** argv) {
4377      printf("grubby version %s\n", VERSION);      printf("grubby version %s\n", VERSION);
4378      exit(0);      exit(0);
4379      break;      break;
4380      case 'i':
4381        if (extraInitrdCount < MAX_EXTRA_INITRDS) {
4382         extraInitrds[extraInitrdCount++] = strdup(poptGetOptArg(optCon));
4383        } else {
4384     fprintf(stderr, _("grubby: extra initrd maximum is %d\n"), extraInitrdCount);
4385     return 1;
4386        }
4387        break;
4388   }   }
4389      }      }
4390    
# Line 2406  int main(int argc, const char ** argv) { Line 4400  int main(int argc, const char ** argv) {
4400   return 1;   return 1;
4401      }      }
4402    
4403      if ((configureLilo + configureGrub + configureELilo +      if ((configureLilo + configureGrub2 + configureGrub + configureELilo +
4404   configureYaboot + configureSilo + configureZipl) > 1) {   configureYaboot + configureSilo + configureZipl +
4405     configureExtLinux ) > 1) {
4406   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));
4407   return 1;   return 1;
4408      } else if (bootloaderProbe && grubConfig) {      } else if (bootloaderProbe && grubConfig) {
4409   fprintf(stderr,   fprintf(stderr,
4410      _("grubby: cannot specify config file with --bootloader-probe\n"));      _("grubby: cannot specify config file with --bootloader-probe\n"));
4411   return 1;   return 1;
4412        } else if (configureGrub2) {
4413     cfi = &grub2ConfigType;
4414     if (envPath)
4415        cfi->envFile = envPath;
4416      } else if (configureLilo) {      } else if (configureLilo) {
4417   cfi = &liloConfigType;   cfi = &liloConfigType;
4418      } else if (configureGrub) {      } else if (configureGrub) {
# Line 2426  int main(int argc, const char ** argv) { Line 4425  int main(int argc, const char ** argv) {
4425          cfi = &siloConfigType;          cfi = &siloConfigType;
4426      } else if (configureZipl) {      } else if (configureZipl) {
4427          cfi = &ziplConfigType;          cfi = &ziplConfigType;
4428        } else if (configureExtLinux) {
4429     cfi = &extlinuxConfigType;
4430     useextlinuxmenu=1;
4431      }      }
4432    
4433      if (!cfi) {      if (!cfi) {
4434            if (grub2FindConfig(&grub2ConfigType)) {
4435        cfi = &grub2ConfigType;
4436        if (envPath)
4437     cfi->envFile = envPath;
4438            } else
4439        #ifdef __ia64__        #ifdef __ia64__
4440   cfi = &eliloConfigType;      cfi = &eliloConfigType;
4441        #elif __powerpc__        #elif __powerpc__
4442   cfi = &yabootConfigType;      cfi = &yabootConfigType;
4443        #elif __sparc__        #elif __sparc__
4444          cfi = &siloConfigType;              cfi = &siloConfigType;
4445        #elif __s390__        #elif __s390__
4446          cfi = &ziplConfigType;              cfi = &ziplConfigType;
4447        #elif __s390x__        #elif __s390x__
4448          cfi = &ziplConfigtype;              cfi = &ziplConfigtype;
4449        #else        #else
4450   cfi = &grubConfigType;      cfi = &grubConfigType;
4451        #endif        #endif
4452      }      }
4453    
4454      if (!grubConfig)      if (!grubConfig) {
4455   grubConfig = cfi->defaultConfig;   if (cfi->findConfig)
4456        grubConfig = cfi->findConfig(cfi);
4457     if (!grubConfig)
4458        grubConfig = cfi->defaultConfig;
4459        }
4460    
4461      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo ||
4462    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
4463    defaultKernel)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
4464        (defaultIndex >= 0))) {
4465   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
4466    "specified option"));    "specified option"));
4467   return 1;   return 1;
4468      }      }
4469    
4470      if ((displayDefault || kernelInfo) && (newKernelVersion || newKernelPath ||      if ((displayDefault || kernelInfo) && (newKernelPath ||
4471     removeKernelPath)) {     removeKernelPath)) {
4472   fprintf(stderr, _("grubby: --default-kernel and --info may not "   fprintf(stderr, _("grubby: --default-kernel and --info may not "
4473    "be used when adding or removing kernels\n"));    "be used when adding or removing kernels\n"));
# Line 2465  int main(int argc, const char ** argv) { Line 4477  int main(int argc, const char ** argv) {
4477      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
4478   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
4479   return 1;   return 1;
4480      } else if (!newKernelPath && (newKernelTitle  || newKernelInitrd ||      } else if (!newKernelPath && (copyDefault ||
4481    newKernelInitrd || copyDefault     ||    (newKernelInitrd && !updateKernelPath)||
4482    makeDefault)) {    makeDefault || extraInitrdCount > 0)) {
4483   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
4484   return 1;   return 1;
4485      }      }
# Line 2491  int main(int argc, const char ** argv) { Line 4503  int main(int argc, const char ** argv) {
4503   makeDefault = 1;   makeDefault = 1;
4504   defaultKernel = NULL;   defaultKernel = NULL;
4505      }      }
4506        else if (defaultKernel && (defaultIndex >= 0)) {
4507     fprintf(stderr, _("grubby: --set-default and --set-default-index "
4508      "may not be used together\n"));
4509     return 1;
4510        }
4511    
4512      if (!strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
4513   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
4514   "is used\n"));   "is used\n"));
4515   return 1;   return 1;
4516      }      }
4517    
4518      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
4519   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
4520          && !removeMBKernel) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
4521     && (defaultIndex == -1)) {
4522   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
4523   return 1;   return 1;
4524      }      }
# Line 2520  int main(int argc, const char ** argv) { Line 4538  int main(int argc, const char ** argv) {
4538   bootPrefix = "";   bootPrefix = "";
4539      }      }
4540    
4541        if (!cfi->mbAllowExtraInitRds &&
4542     extraInitrdCount > 0) {
4543     fprintf(stderr, _("grubby: %s doesn't allow multiple initrds\n"), cfi->defaultConfig);
4544     return 1;
4545        }
4546    
4547      if (bootloaderProbe) {      if (bootloaderProbe) {
4548   int lrc = 0, grc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4549   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4550    
4551     const char *grub2config = grub2FindConfig(&grub2ConfigType);
4552     if (grub2config) {
4553        gconfig = readConfig(grub2config, &grub2ConfigType);
4554        if (!gconfig)
4555     gr2c = 1;
4556        else
4557     gr2c = checkForGrub2(gconfig);
4558     }
4559    
4560   if (!access(grubConfigType.defaultConfig, F_OK)) {   const char *grubconfig = grubFindConfig(&grubConfigType);
4561      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);   if (!access(grubconfig, F_OK)) {
4562        gconfig = readConfig(grubconfig, &grubConfigType);
4563      if (!gconfig)      if (!gconfig)
4564   grc = 1;   grc = 1;
4565      else      else
# Line 2540  int main(int argc, const char ** argv) { Line 4574  int main(int argc, const char ** argv) {
4574   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4575   }   }
4576    
4577   if (lrc == 1 || grc == 1) return 1;   if (!access(eliloConfigType.defaultConfig, F_OK)) {
4578        econfig = readConfig(eliloConfigType.defaultConfig,
4579     &eliloConfigType);
4580        if (!econfig)
4581     erc = 1;
4582        else
4583     erc = checkForElilo(econfig);
4584     }
4585    
4586     if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4587        lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4588        if (!lconfig)
4589     extrc = 1;
4590        else
4591     extrc = checkForExtLinux(lconfig);
4592     }
4593    
4594    
4595     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4596        yconfig = readConfig(yabootConfigType.defaultConfig,
4597     &yabootConfigType);
4598        if (!yconfig)
4599     yrc = 1;
4600        else
4601     yrc = checkForYaboot(yconfig);
4602     }
4603    
4604     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4605     erc == 1)
4606        return 1;
4607    
4608   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4609     if (gr2c == 2) printf("grub2\n");
4610   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4611     if (extrc == 2) printf("extlinux\n");
4612     if (yrc == 2) printf("yaboot\n");
4613     if (erc == 2) printf("elilo\n");
4614    
4615   return 0;   return 0;
4616      }      }
4617    
4618        if (grubConfig == NULL) {
4619     printf("Could not find bootloader configuration file.\n");
4620     exit(1);
4621        }
4622    
4623      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4624      if (!config) return 1;      if (!config) return 1;
4625    
# Line 2557  int main(int argc, const char ** argv) { Line 4629  int main(int argc, const char ** argv) {
4629          char * rootspec;          char * rootspec;
4630    
4631   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4632     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4633     cfi->defaultIsSaved)
4634        config->defaultImage = 0;
4635   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4636   if (!entry) return 0;   if (!entry) return 0;
4637   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4638    
4639   line = entry->lines;   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
  while (line && line->type != LT_KERNEL) line = line->next;  
4640   if (!line) return 0;   if (!line) return 0;
4641    
4642          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 2570  int main(int argc, const char ** argv) { Line 4644  int main(int argc, const char ** argv) {
4644                 ((rootspec != NULL) ? strlen(rootspec) : 0));                 ((rootspec != NULL) ? strlen(rootspec) : 0));
4645    
4646   return 0;   return 0;
4647    
4648        } else if (displayDefaultTitle) {
4649     struct singleLine * line;
4650     struct singleEntry * entry;
4651    
4652     if (config->defaultImage == -1) return 0;
4653     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4654     cfi->defaultIsSaved)
4655        config->defaultImage = 0;
4656     entry = findEntryByIndex(config, config->defaultImage);
4657     if (!entry) return 0;
4658    
4659     if (!configureGrub2) {
4660      line = getLineByType(LT_TITLE, entry->lines);
4661      if (!line) return 0;
4662      printf("%s\n", line->elements[1].item);
4663    
4664     } else {
4665      char * title;
4666    
4667      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
4668      line = getLineByType(LT_MENUENTRY, entry->lines);
4669      if (!line) return 0;
4670      title = grub2ExtractTitle(line);
4671      if (title)
4672        printf("%s\n", title);
4673     }
4674     return 0;
4675    
4676        } else if (displayDefaultIndex) {
4677            if (config->defaultImage == -1) return 0;
4678     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4679     cfi->defaultIsSaved)
4680        config->defaultImage = 0;
4681            printf("%i\n", config->defaultImage);
4682            return 0;
4683    
4684      } else if (kernelInfo)      } else if (kernelInfo)
4685   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
4686    
# Line 2581  int main(int argc, const char ** argv) { Line 4692  int main(int argc, const char ** argv) {
4692      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4693      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4694      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4695      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4696      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4697      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4698                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
4699        if (updateKernelPath && newKernelInitrd) {
4700        if (newMBKernel) {
4701        if (addMBInitrd(config, newMBKernel, updateKernelPath,
4702     bootPrefix, newKernelInitrd,
4703     newKernelTitle))
4704        return 1;
4705        } else {
4706        if (updateInitrd(config, updateKernelPath, bootPrefix,
4707     newKernelInitrd, newKernelTitle))
4708     return 1;
4709        }
4710        }
4711      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
4712                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
4713                       newMBKernel, newMBKernelArgs)) return 1;                       (const char **)extraInitrds, extraInitrdCount,
4714                         newMBKernel, newMBKernelArgs, newDevTreePath)) return 1;
4715            
4716    
4717      if (numEntries(config) == 0) {      if (numEntries(config) == 0) {
# Line 2597  int main(int argc, const char ** argv) { Line 4721  int main(int argc, const char ** argv) {
4721      }      }
4722    
4723      if (!outputFile)      if (!outputFile)
4724   outputFile = grubConfig;   outputFile = (char *)grubConfig;
4725    
4726      return writeConfig(config, outputFile, bootPrefix);      return writeConfig(config, outputFile, bootPrefix);
4727  }  }

Legend:
Removed from v.532  
changed lines
  Added in v.2991