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 2977 by niro, Thu Jun 30 10:24:27 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    };
226    
227    struct keywordTypes grub2Keywords[] = {
228        { "menuentry",  LT_MENUENTRY,   ' ' },
229        { "}",          LT_ENTRY_END,   ' ' },
230        { "echo",       LT_ECHO,        ' ' },
231        { "set",        LT_SET_VARIABLE,' ', '=' },
232        { "root",       LT_BOOTROOT,    ' ' },
233        { "default",    LT_DEFAULT,     ' ' },
234        { "fallback",   LT_FALLBACK,    ' ' },
235        { "linux",      LT_KERNEL,      ' ' },
236        { "linuxefi",   LT_KERNEL_EFI,  ' ' },
237        { "linux16",    LT_KERNEL_16,   ' ' },
238        { "initrd",     LT_INITRD,      ' ', ' ' },
239        { "initrdefi",  LT_INITRD_EFI,  ' ', ' ' },
240        { "initrd16",   LT_INITRD_16,   ' ', ' ' },
241        { "module",     LT_MBMODULE,    ' ' },
242        { "kernel",     LT_HYPER,       ' ' },
243        { "devicetree", LT_DEVTREE,  ' ' },
244        { NULL, 0, 0 },
245    };
246    
247    const char *grub2FindConfig(struct configFileInfo *cfi) {
248        static const char *configFiles[] = {
249     "/boot/grub/grub-efi.cfg",
250     "/boot/grub/grub.cfg",
251     NULL
252        };
253        static int i = -1;
254        static const char *grub_cfg = "/boot/grub/grub.cfg";
255        int rc = -1;
256    
257        if (i == -1) {
258     for (i = 0; configFiles[i] != NULL; i++) {
259        dbgPrintf("Checking \"%s\": ", configFiles[i]);
260        if ((rc = access(configFiles[i], R_OK))) {
261     if (errno == EACCES) {
262        printf("Unable to access bootloader configuration file "
263           "\"%s\": %m\n", configFiles[i]);
264        exit(1);
265     }
266     continue;
267        } else {
268     dbgPrintf("found\n");
269     return configFiles[i];
270        }
271     }
272        }
273    
274        /* Ubuntu renames grub2 to grub, so check for the grub.d directory
275         * that isn't in grub1, and if it exists, return the config file path
276         * that they use. */
277        if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) {
278     dbgPrintf("found\n");
279     return grub_cfg;
280        }
281    
282        dbgPrintf("not found\n");
283        return configFiles[i];
284    }
285    
286    /* kind of hacky.  It'll give the first 1024 bytes, ish. */
287    static char *grub2GetEnv(struct configFileInfo *info, char *name)
288    {
289        static char buf[1025];
290        char *s = NULL;
291        char *ret = NULL;
292        char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv";
293        int rc = asprintf(&s, "grub-editenv %s list | grep '^%s='", envFile, name);
294    
295        if (rc < 0)
296     return NULL;
297    
298        FILE *f = popen(s, "r");
299        if (!f)
300     goto out;
301    
302        memset(buf, '\0', sizeof (buf));
303        ret = fgets(buf, 1024, f);
304        pclose(f);
305    
306        if (ret) {
307     ret += strlen(name) + 1;
308     ret[strlen(ret) - 1] = '\0';
309        }
310        dbgPrintf("grub2GetEnv(%s): %s\n", name, ret);
311    out:
312        free(s);
313        return ret;
314    }
315    
316    static int sPopCount(const char *s, const char *c)
317    {
318        int ret = 0;
319        if (!s)
320     return -1;
321        for (int i = 0; s[i] != '\0'; i++)
322     for (int j = 0; c[j] != '\0'; j++)
323        if (s[i] == c[j])
324     ret++;
325        return ret;
326    }
327    
328    static char *shellEscape(const char *s)
329    {
330        int l = strlen(s) + sPopCount(s, "'") * 2;
331    
332        char *ret = calloc(l+1, sizeof (*ret));
333        if (!ret)
334     return NULL;
335        for (int i = 0, j = 0; s[i] != '\0'; i++, j++) {
336     if (s[i] == '\'')
337        ret[j++] = '\\';
338     ret[j] = s[i];
339        }
340        return ret;
341    }
342    
343    static void unquote(char *s)
344    {
345        int l = strlen(s);
346    
347        if ((s[l-1] == '\'' && s[0] == '\'') || (s[l-1] == '"' && s[0] == '"')) {
348     memmove(s, s+1, l-2);
349     s[l-2] = '\0';
350        }
351    }
352    
353    static int grub2SetEnv(struct configFileInfo *info, char *name, char *value)
354    {
355        char *s = NULL;
356        int rc = 0;
357        char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv";
358    
359        unquote(value);
360        value = shellEscape(value);
361        if (!value)
362        return -1;
363    
364        rc = asprintf(&s, "grub-editenv %s set '%s=%s'", envFile, name, value);
365        free(value);
366        if (rc <0)
367     return -1;
368    
369        dbgPrintf("grub2SetEnv(%s): %s\n", name, s);
370        rc = system(s);
371        free(s);
372        return rc;
373    }
374    
375    /* this is a gigantic hack to avoid clobbering grub2 variables... */
376    static int is_special_grub2_variable(const char *name)
377    {
378        if (!strcmp(name,"\"${next_entry}\""))
379     return 1;
380        if (!strcmp(name,"\"${prev_saved_entry}\""))
381     return 1;
382        return 0;
383    }
384    
385    int sizeOfSingleLine(struct singleLine * line) {
386      int count = 0;
387    
388      for (int i = 0; i < line->numElements; i++) {
389        int indentSize = 0;
390    
391        count = count + strlen(line->elements[i].item);
392    
393        indentSize = strlen(line->elements[i].indent);
394        if (indentSize > 0)
395          count = count + indentSize;
396        else
397          /* be extra safe and add room for whitespaces */
398          count = count + 1;
399      }
400    
401      /* room for trailing terminator */
402      count = count + 1;
403    
404      return count;
405    }
406    
407    static int isquote(char q)
408    {
409        if (q == '\'' || q == '\"')
410     return 1;
411        return 0;
412    }
413    
414    static int iskernel(enum lineType_e type) {
415        return (type == LT_KERNEL || type == LT_KERNEL_EFI || type == LT_KERNEL_16);
416    }
417    
418    static int isinitrd(enum lineType_e type) {
419        return (type == LT_INITRD || type == LT_INITRD_EFI || type == LT_INITRD_16);
420    }
421    
422    char *grub2ExtractTitle(struct singleLine * line) {
423        char * current;
424        char * current_indent;
425        int current_len;
426        int current_indent_len;
427        int i;
428    
429        /* bail out if line does not start with menuentry */
430        if (strcmp(line->elements[0].item, "menuentry"))
431          return NULL;
432    
433        i = 1;
434        current = line->elements[i].item;
435        current_len = strlen(current);
436    
437        /* if second word is quoted, strip the quotes and return single word */
438        if (isquote(*current) && isquote(current[current_len - 1])) {
439     char *tmp;
440    
441     tmp = strdup(current);
442     *(tmp + current_len - 1) = '\0';
443     return ++tmp;
444        }
445    
446        /* if no quotes, return second word verbatim */
447        if (!isquote(*current))
448     return current;
449    
450        /* second element start with a quote, so we have to find the element
451         * whose last character is also quote (assuming it's the closing one) */
452        int resultMaxSize;
453        char * result;
454        
455        resultMaxSize = sizeOfSingleLine(line);
456        result = malloc(resultMaxSize);
457        snprintf(result, resultMaxSize, "%s", ++current);
458        
459        i++;
460        for (; i < line->numElements; ++i) {
461     current = line->elements[i].item;
462     current_len = strlen(current);
463     current_indent = line->elements[i].indent;
464     current_indent_len = strlen(current_indent);
465    
466     strncat(result, current_indent, current_indent_len);
467     if (!isquote(current[current_len-1])) {
468        strncat(result, current, current_len);
469     } else {
470        strncat(result, current, current_len - 1);
471        break;
472     }
473        }
474        return result;
475    }
476    
477    struct configFileInfo grub2ConfigType = {
478        .findConfig = grub2FindConfig,
479        .getEnv = grub2GetEnv,
480        .setEnv = grub2SetEnv,
481        .keywords = grub2Keywords,
482        .defaultIsIndex = 1,
483        .defaultSupportSaved = 1,
484        .defaultIsVariable = 1,
485        .entryStart = LT_MENUENTRY,
486        .entryEnd = LT_ENTRY_END,
487        .titlePosition = 1,
488        .needsBootPrefix = 1,
489        .mbHyperFirst = 1,
490        .mbInitRdIsModule = 1,
491        .mbAllowExtraInitRds = 1,
492  };  };
493    
494  struct keywordTypes yabootKeywords[] = {  struct keywordTypes yabootKeywords[] = {
# Line 142  struct keywordTypes yabootKeywords[] = { Line 522  struct keywordTypes yabootKeywords[] = {
522      { "partition",  LT_GENERIC,    '=' },      { "partition",  LT_GENERIC,    '=' },
523      { "device",    LT_GENERIC,    '=' },      { "device",    LT_GENERIC,    '=' },
524      { "fstype",    LT_GENERIC,    '=' },      { "fstype",    LT_GENERIC,    '=' },
525      { "initrd",    LT_INITRD,    '=' },      { "initrd",    LT_INITRD,    '=', ';' },
526      { "append",    LT_KERNELARGS,  '=' },      { "append",    LT_KERNELARGS,  '=' },
527      { "boot",    LT_BOOT,    '=' },      { "boot",    LT_BOOT,    '=' },
528      { "lba",    LT_LBA,    ' ' },      { "lba",    LT_LBA,    ' ' },
# Line 162  struct keywordTypes liloKeywords[] = { Line 542  struct keywordTypes liloKeywords[] = {
542      { NULL,    0, 0 },      { NULL,    0, 0 },
543  };  };
544    
545    struct keywordTypes eliloKeywords[] = {
546        { "label",    LT_TITLE,    '=' },
547        { "root",    LT_ROOT,    '=' },
548        { "default",    LT_DEFAULT,    '=' },
549        { "image",    LT_KERNEL,    '=' },
550        { "initrd",    LT_INITRD,    '=' },
551        { "append",    LT_KERNELARGS,  '=' },
552        { "vmm",    LT_HYPER,       '=' },
553        { NULL,    0, 0 },
554    };
555    
556  struct keywordTypes siloKeywords[] = {  struct keywordTypes siloKeywords[] = {
557      { "label",    LT_TITLE,    '=' },      { "label",    LT_TITLE,    '=' },
558      { "root",    LT_ROOT,    '=' },      { "root",    LT_ROOT,    '=' },
# Line 183  struct keywordTypes ziplKeywords[] = { Line 574  struct keywordTypes ziplKeywords[] = {
574      { NULL,         0, 0 },      { NULL,         0, 0 },
575  };  };
576    
577    struct keywordTypes extlinuxKeywords[] = {
578        { "label",    LT_TITLE,    ' ' },
579        { "root",    LT_ROOT,    ' ' },
580        { "default",    LT_DEFAULT,    ' ' },
581        { "kernel",    LT_KERNEL,    ' ' },
582        { "initrd",    LT_INITRD,      ' ', ',' },
583        { "append",    LT_KERNELARGS,  ' ' },
584        { "prompt",     LT_UNKNOWN,     ' ' },
585        { "fdt",        LT_DEVTREE,     ' ' },
586        { "fdtdir",     LT_DEVTREE,     ' ' },
587        { NULL,    0, 0 },
588    };
589    int useextlinuxmenu;
590  struct configFileInfo eliloConfigType = {  struct configFileInfo eliloConfigType = {
591      "/boot/efi/EFI/redhat/elilo.conf",    /* defaultConfig */      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",
592      liloKeywords,    /* keywords */      .keywords = eliloKeywords,
593      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
594      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
595      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
596      1,                    /* needsBootPrefix */      .mbConcatArgs = 1,
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
597  };  };
598    
599  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
600      "/etc/lilo.conf",    /* defaultConfig */      .defaultConfig = "/etc/lilo.conf",
601      liloKeywords,    /* keywords */      .keywords = liloKeywords,
602      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
603      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
604      LT_KERNEL,    /* entrySeparator */      .maxTitleLength = 15,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
605  };  };
606    
607  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
608      "/etc/yaboot.conf",    /* defaultConfig */      .defaultConfig = "/etc/yaboot.conf",
609      yabootKeywords,    /* keywords */      .keywords = yabootKeywords,
610      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
611      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
612      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
613      1,    /* needsBootPrefix */      .maxTitleLength = 15,
614      1,    /* argsInQuotes */      .mbAllowExtraInitRds = 1,
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
615  };  };
616    
617  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
618      "/etc/silo.conf",    /* defaultConfig */      .defaultConfig = "/etc/silo.conf",
619      siloKeywords,    /* keywords */      .keywords = siloKeywords,
620      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
621      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
622      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
623      1,    /* needsBootPrefix */      .maxTitleLength = 15,
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
624  };  };
625    
626  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
627      "/etc/zipl.conf",    /* defaultConfig */      .defaultConfig = "/etc/zipl.conf",
628      ziplKeywords,    /* keywords */      .keywords = ziplKeywords,
629      0,    /* defaultIsIndex */      .entryStart = LT_TITLE,
630      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
631      LT_TITLE,    /* entrySeparator */      .titleBracketed = 1,
632      0,    /* needsBootPrefix */  };
633      1,    /* argsInQuotes */  
634      15,    /* maxTitleLength */  struct configFileInfo extlinuxConfigType = {
635      1,                                      /* titleBracketed */      .defaultConfig = "/boot/extlinux/extlinux.conf",
636        .keywords = extlinuxKeywords,
637        .caseInsensitive = 1,
638        .entryStart = LT_TITLE,
639        .needsBootPrefix = 1,
640        .maxTitleLength = 255,
641        .mbAllowExtraInitRds = 1,
642        .defaultIsUnquoted = 1,
643  };  };
644    
645  struct grubConfig {  struct grubConfig {
# Line 255  struct grubConfig { Line 654  struct grubConfig {
654      struct configFileInfo * cfi;      struct configFileInfo * cfi;
655  };  };
656    
657    blkid_cache blkid;
658    
659  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index);
660  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
661       const char * path, const char * prefix,       const char * path, const char * prefix,
662       int * index);       int * index);
663  static char * strndup(char * from, int len);  struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
664          int * index);
665  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
666  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
667    struct singleLine * lineDup(struct singleLine * line);
668  static void lineFree(struct singleLine * line);  static void lineFree(struct singleLine * line);
669  static int lineWrite(FILE * out, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine * line,
670       struct configFileInfo * cfi);       struct configFileInfo * cfi);
671  static int getNextLine(char ** bufPtr, struct singleLine * line,  static int getNextLine(char ** bufPtr, struct singleLine * line,
672         struct configFileInfo * cfi);         struct configFileInfo * cfi);
673  static char * getRootSpecifier(char * str);  static char * getRootSpecifier(char * str);
674    static void requote(struct singleLine *line, struct configFileInfo * cfi);
675  static char * strndup(char * from, int len) {  static void insertElement(struct singleLine * line,
676      char * to;    const char * item, int insertHere,
677      struct configFileInfo * cfi);
678      to = malloc(len + 1);  static void removeElement(struct singleLine * line, int removeHere);
679      strncpy(to, from, len);  static struct keywordTypes * getKeywordByType(enum lineType_e type,
680      to[len] = '\0';        struct configFileInfo * cfi);
681    static enum lineType_e getTypeByKeyword(char * keyword,
682      return to;   struct configFileInfo * cfi);
683  }  static struct singleLine * getLineByType(enum lineType_e type,
684     struct singleLine * line);
685    static int checkForExtLinux(struct grubConfig * config);
686    struct singleLine * addLineTmpl(struct singleEntry * entry,
687                                    struct singleLine * tmplLine,
688                                    struct singleLine * prevLine,
689                                    const char * val,
690     struct configFileInfo * cfi);
691    struct singleLine *  addLine(struct singleEntry * entry,
692                                 struct configFileInfo * cfi,
693                                 enum lineType_e type, char * defaultIndent,
694                                 const char * val);
695    
696  static char * sdupprintf(const char *format, ...)  static char * sdupprintf(const char *format, ...)
697  #ifdef __GNUC__  #ifdef __GNUC__
# Line 313  static char * sdupprintf(const char *for Line 726  static char * sdupprintf(const char *for
726      return buf;      return buf;
727  }  }
728    
729    static enum lineType_e preferredLineType(enum lineType_e type,
730     struct configFileInfo *cfi) {
731        if (isEfi && cfi == &grub2ConfigType) {
732     switch (type) {
733     case LT_KERNEL:
734        return isEfiOnly ? LT_KERNEL : LT_KERNEL_EFI;
735     case LT_INITRD:
736        return isEfiOnly ? LT_INITRD : LT_INITRD_EFI;
737     default:
738        return type;
739     }
740    #if defined(__i386__) || defined(__x86_64__)
741        } else if (cfi == &grub2ConfigType) {
742     switch (type) {
743     case LT_KERNEL:
744        return LT_KERNEL_16;
745     case LT_INITRD:
746        return LT_INITRD_16;
747     default:
748        return type;
749     }
750    #endif
751        }
752        return type;
753    }
754    
755    static struct keywordTypes * getKeywordByType(enum lineType_e type,
756          struct configFileInfo * cfi) {
757        for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
758     if (kw->type == type)
759        return kw;
760        }
761        return NULL;
762    }
763    
764    static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) {
765        struct keywordTypes *kt = getKeywordByType(type, cfi);
766        if (kt)
767     return kt->key;
768        return "unknown";
769    }
770    
771    static char * getpathbyspec(char *device) {
772        if (!blkid)
773            blkid_get_cache(&blkid, NULL);
774    
775        return blkid_get_devname(blkid, device, NULL);
776    }
777    
778    static char * getuuidbydev(char *device) {
779        if (!blkid)
780     blkid_get_cache(&blkid, NULL);
781    
782        return blkid_get_tag_value(blkid, "UUID", device);
783    }
784    
785    static enum lineType_e getTypeByKeyword(char * keyword,
786     struct configFileInfo * cfi) {
787        for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
788     if (cfi->caseInsensitive) {
789        if (!strcasecmp(keyword, kw->key))
790                    return kw->type;
791     } else {
792        if (!strcmp(keyword, kw->key))
793            return kw->type;
794     }
795        }
796        return LT_UNKNOWN;
797    }
798    
799    static struct singleLine * getLineByType(enum lineType_e type,
800     struct singleLine * line) {
801        dbgPrintf("getLineByType(%d): ", type);
802        for (; line; line = line->next) {
803     dbgPrintf("%d:%s ", line->type,
804      line->numElements ? line->elements[0].item : "(empty)");
805     if (line->type & type) break;
806        }
807        dbgPrintf(line ? "\n" : " (failed)\n");
808        return line;
809    }
810    
811  static int isBracketedTitle(struct singleLine * line) {  static int isBracketedTitle(struct singleLine * line) {
812      if ((*line->elements[0].item == '[') && (line->numElements == 1)) {      if (line->numElements == 1 && *line->elements[0].item == '[') {
813          int len = strlen(line->elements[0].item);          int len = strlen(line->elements[0].item);
814          if (*(line->elements[0].item + len - 1) == ']') {          if (*(line->elements[0].item + len - 1) == ']') {
815              /* FIXME: this is a hack... */              /* FIXME: this is a hack... */
# Line 326  static int isBracketedTitle(struct singl Line 821  static int isBracketedTitle(struct singl
821      return 0;      return 0;
822  }  }
823    
824  /* figure out if this is a entry separator */  static int isEntryStart(struct singleLine * line,
 static int isEntrySeparator(struct singleLine * line,  
825                              struct configFileInfo * cfi) {                              struct configFileInfo * cfi) {
826      if (line->type == LT_WHITESPACE)      return line->type == cfi->entryStart || line->type == LT_OTHER ||
827   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;  
828  }  }
829    
830  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
831  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct singleLine * line) {
832      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it */
833      char * title;      char * title = NULL;
834      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
835      title++;   title = strdup(line->elements[0].item + 1);
836      *(title + strlen(title) - 1) = '\0';   *(title + strlen(title) - 1) = '\0';
837        } else if (line->type == LT_MENUENTRY)
838     title = strdup(line->elements[1].item);
839        else
840     return NULL;
841      return title;      return title;
842  }  }
843    
# Line 389  static void lineInit(struct singleLine * Line 879  static void lineInit(struct singleLine *
879      line->next = NULL;      line->next = NULL;
880  }  }
881    
882  static void lineFree(struct singleLine * line) {  struct singleLine * lineDup(struct singleLine * line) {
883      int i;      struct singleLine * newLine = malloc(sizeof(*newLine));
884    
885        newLine->indent = strdup(line->indent);
886        newLine->next = NULL;
887        newLine->type = line->type;
888        newLine->numElements = line->numElements;
889        newLine->elements = malloc(sizeof(*newLine->elements) *
890           newLine->numElements);
891    
892        for (int i = 0; i < newLine->numElements; i++) {
893     newLine->elements[i].indent = strdup(line->elements[i].indent);
894     newLine->elements[i].item = strdup(line->elements[i].item);
895        }
896    
897        return newLine;
898    }
899    
900    static void lineFree(struct singleLine * line) {
901      if (line->indent) free(line->indent);      if (line->indent) free(line->indent);
902    
903      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
904   free(line->elements[i].item);   free(line->elements[i].item);
905   free(line->elements[i].indent);   free(line->elements[i].indent);
906      }      }
# Line 405  static void lineFree(struct singleLine * Line 911  static void lineFree(struct singleLine *
911    
912  static int lineWrite(FILE * out, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine * line,
913       struct configFileInfo * cfi) {       struct configFileInfo * cfi) {
     int i;  
   
914      if (fprintf(out, "%s", line->indent) == -1) return -1;      if (fprintf(out, "%s", line->indent) == -1) return -1;
915    
916      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
917     /* Need to handle this, because we strip the quotes from
918     * menuentry when read it. */
919     if (line->type == LT_MENUENTRY && i == 1) {
920        if(!isquote(*line->elements[i].item))
921     fprintf(out, "\'%s\'", line->elements[i].item);
922        else
923     fprintf(out, "%s", line->elements[i].item);
924        fprintf(out, "%s", line->elements[i].indent);
925    
926        continue;
927     }
928    
929   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)
930      if (fputc('"', out) == EOF) return -1;      if (fputc('"', out) == EOF) return -1;
931    
932   if (fprintf(out, "%s", line->elements[i].item) == -1) return -1;   if (fprintf(out, "%s", line->elements[i].item) == -1) return -1;
933   if (fprintf(out, "%s", line->elements[i].indent) == -1) return -1;   if (i < line->numElements - 1)
934        if (fprintf(out, "%s", line->elements[i].indent) == -1) return -1;
935      }      }
936    
937      if (line->type == LT_KERNELARGS && cfi->argsInQuotes)      if (line->type == LT_KERNELARGS && cfi->argsInQuotes)
# Line 433  static int getNextLine(char ** bufPtr, s Line 950  static int getNextLine(char ** bufPtr, s
950      char * chptr;      char * chptr;
951      int elementsAlloced = 0;      int elementsAlloced = 0;
952      struct lineElement * element;      struct lineElement * element;
     struct keywordTypes * keywords = cfi->keywords;  
953      int first = 1;      int first = 1;
     int i;  
954    
955      lineFree(line);      lineFree(line);
956    
# Line 489  static int getNextLine(char ** bufPtr, s Line 1004  static int getNextLine(char ** bufPtr, s
1004      if (!line->numElements)      if (!line->numElements)
1005   line->type = LT_WHITESPACE;   line->type = LT_WHITESPACE;
1006      else {      else {
1007   for (i = 0; keywords[i].key; i++)   line->type = getTypeByKeyword(line->elements[0].item, cfi);
1008      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;  
               
1009              /* zipl does [title] instead of something reasonable like all              /* zipl does [title] instead of something reasonable like all
1010               * the other boot loaders.  kind of ugly */               * the other boot loaders.  kind of ugly */
1011              if (cfi->titleBracketed && isBracketedTitle(line)) {              if (cfi->titleBracketed && isBracketedTitle(line)) {
# Line 510  static int getNextLine(char ** bufPtr, s Line 1019  static int getNextLine(char ** bufPtr, s
1019      if (*line->elements[0].item == '#') {      if (*line->elements[0].item == '#') {
1020   char * fullLine;   char * fullLine;
1021   int len;   int len;
  int i;  
1022    
1023   len = strlen(line->indent);   len = strlen(line->indent);
1024   for (i = 0; i < line->numElements; i++)   for (int i = 0; i < line->numElements; i++)
1025      len += strlen(line->elements[i].item) +      len += strlen(line->elements[i].item) +
1026     strlen(line->elements[i].indent);     strlen(line->elements[i].indent);
1027    
# Line 522  static int getNextLine(char ** bufPtr, s Line 1030  static int getNextLine(char ** bufPtr, s
1030   free(line->indent);   free(line->indent);
1031   line->indent = fullLine;   line->indent = fullLine;
1032    
1033   for (i = 0; i < line->numElements; i++) {   for (int i = 0; i < line->numElements; i++) {
1034      strcat(fullLine, line->elements[i].item);      strcat(fullLine, line->elements[i].item);
1035      strcat(fullLine, line->elements[i].indent);      strcat(fullLine, line->elements[i].indent);
1036      free(line->elements[i].item);      free(line->elements[i].item);
# Line 532  static int getNextLine(char ** bufPtr, s Line 1040  static int getNextLine(char ** bufPtr, s
1040   line->type = LT_WHITESPACE;   line->type = LT_WHITESPACE;
1041   line->numElements = 0;   line->numElements = 0;
1042      }      }
1043     } else {
1044     struct keywordTypes *kw;
1045    
1046     kw = getKeywordByType(line->type, cfi);
1047    
1048     /* space isn't the only separator, we need to split
1049     * elements up more
1050     */
1051     if (!isspace(kw->separatorChar)) {
1052        char indent[2] = "";
1053        indent[0] = kw->separatorChar;
1054        for (int i = 1; i < line->numElements; i++) {
1055     char *p;
1056     int numNewElements;
1057    
1058     numNewElements = 0;
1059     p = line->elements[i].item;
1060     while (*p != '\0') {
1061     if (*p == kw->separatorChar)
1062     numNewElements++;
1063     p++;
1064     }
1065     if (line->numElements + numNewElements >= elementsAlloced) {
1066     elementsAlloced += numNewElements + 5;
1067     line->elements = realloc(line->elements,
1068        sizeof(*line->elements) * elementsAlloced);
1069     }
1070    
1071     for (int j = line->numElements; j > i; j--) {
1072     line->elements[j + numNewElements] = line->elements[j];
1073     }
1074     line->numElements += numNewElements;
1075    
1076     p = line->elements[i].item;
1077     while (*p != '\0') {
1078    
1079     while (*p != kw->separatorChar && *p != '\0') p++;
1080     if (*p == '\0') {
1081     break;
1082     }
1083    
1084     line->elements[i + 1].indent = line->elements[i].indent;
1085     line->elements[i].indent = strdup(indent);
1086     *p++ = '\0';
1087     i++;
1088     line->elements[i].item = strdup(p);
1089     }
1090        }
1091     }
1092   }   }
1093      }      }
1094    
1095      return 0;      return 0;
1096  }  }
1097    
1098    static int isnumber(const char *s)
1099    {
1100        int i;
1101        for (i = 0; s[i] != '\0'; i++)
1102     if (s[i] < '0' || s[i] > '9')
1103        return 0;
1104        return i;
1105    }
1106    
1107  static struct grubConfig * readConfig(const char * inName,  static struct grubConfig * readConfig(const char * inName,
1108        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
1109      int in;      int in;
# Line 549  static struct grubConfig * readConfig(co Line 1115  static struct grubConfig * readConfig(co
1115      struct singleLine * last = NULL, * line, * defaultLine = NULL;      struct singleLine * last = NULL, * line, * defaultLine = NULL;
1116      char * end;      char * end;
1117      struct singleEntry * entry = NULL;      struct singleEntry * entry = NULL;
1118      int i, len;      int len;
1119      char * buf;      char * buf;
1120    
1121      if (!strcmp(inName, "-")) {      if (inName == NULL) {
1122            printf("Could not find bootloader configuration\n");
1123            exit(1);
1124        } else if (!strcmp(inName, "-")) {
1125   in = 0;   in = 0;
1126      } else {      } else {
1127   if ((in = open(inName, O_RDONLY)) < 0) {   if ((in = open(inName, O_RDONLY)) < 0) {
# Line 595  static struct grubConfig * readConfig(co Line 1164  static struct grubConfig * readConfig(co
1164      cfg->secondaryIndent = strdup(line->indent);      cfg->secondaryIndent = strdup(line->indent);
1165   }   }
1166    
1167   if (isEntrySeparator(line, cfi)) {   if (isEntryStart(line, cfi) || (cfg->entries && !sawEntry)) {
1168      sawEntry = 1;      sawEntry = 1;
1169      if (!entry) {      if (!entry) {
1170   cfg->entries = malloc(sizeof(*entry));   cfg->entries = malloc(sizeof(*entry));
# Line 611  static struct grubConfig * readConfig(co Line 1180  static struct grubConfig * readConfig(co
1180      entry->next = NULL;      entry->next = NULL;
1181   }   }
1182    
1183   if (line->type == LT_DEFAULT && line->numElements == 2) {   if (line->type == LT_SET_VARIABLE) {
1184      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      dbgPrintf("found 'set' command (%d elements): ", line->numElements);
1185      defaultLine = line;      dbgPrintf("%s", line->indent);
1186        for (int i = 0; i < line->numElements; i++)
1187     dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);
1188        dbgPrintf("\n");
1189        struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1190        if (kwType && line->numElements == 3 &&
1191        !strcmp(line->elements[1].item, kwType->key) &&
1192        !is_special_grub2_variable(line->elements[2].item)) {
1193     dbgPrintf("Line sets default config\n");
1194     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1195     defaultLine = line;
1196        }
1197    
1198            } else if (iskernel(line->type)) {
1199        /* if by some freak chance this is multiboot and the "module"
1200         * lines came earlier in the template, make sure to use LT_HYPER
1201         * instead of LT_KERNEL now
1202         */
1203        if (entry && entry->multiboot)
1204     line->type = LT_HYPER;
1205    
1206          } else if (line->type == LT_MBMODULE) {          } else if (line->type == LT_MBMODULE) {
1207        /* go back and fix the LT_KERNEL line to indicate LT_HYPER
1208         * instead, now that we know this is a multiboot entry.
1209         * This only applies to grub, but that's the only place we
1210         * should find LT_MBMODULE lines anyway.
1211         */
1212        for (struct singleLine *l = entry->lines; l; l = l->next) {
1213     if (l->type == LT_HYPER)
1214        break;
1215     else if (iskernel(l->type)) {
1216        l->type = LT_HYPER;
1217        break;
1218     }
1219        }
1220              entry->multiboot = 1;              entry->multiboot = 1;
1221    
1222     } else if (line->type == LT_HYPER) {
1223        entry->multiboot = 1;
1224    
1225   } else if (line->type == LT_FALLBACK && line->numElements == 2) {   } else if (line->type == LT_FALLBACK && line->numElements == 2) {
1226      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);
1227      if (*end) cfg->fallbackImage = -1;      if (*end) cfg->fallbackImage = -1;
1228   } else if (line->type == LT_TITLE && line->numElements > 1) {  
1229      /* make the title a single argument (undoing our parsing) */   } else if ((line->type == LT_DEFAULT && cfi->defaultIsUnquoted) ||
1230                    (line->type == LT_TITLE && line->numElements > 1)) {
1231        /* make the title/default a single argument (undoing our parsing) */
1232      len = 0;      len = 0;
1233      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1234   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
1235   len += strlen(line->elements[i].indent);   len += strlen(line->elements[i].indent);
1236      }      }
1237      buf = malloc(len + 1);      buf = malloc(len + 1);
1238      *buf = '\0';      *buf = '\0';
1239    
1240      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1241   strcat(buf, line->elements[i].item);   strcat(buf, line->elements[i].item);
1242   free(line->elements[i].item);   free(line->elements[i].item);
1243    
# Line 643  static struct grubConfig * readConfig(co Line 1251  static struct grubConfig * readConfig(co
1251      line->elements[line->numElements - 1].indent;      line->elements[line->numElements - 1].indent;
1252      line->elements[1].item = buf;      line->elements[1].item = buf;
1253      line->numElements = 2;      line->numElements = 2;
1254     } else if (line->type == LT_MENUENTRY && line->numElements > 3) {
1255        /* let --remove-kernel="TITLE=what" work */
1256        len = 0;
1257        char *extras;
1258        char *title;
1259    
1260        for (int i = 1; i < line->numElements; i++) {
1261     len += strlen(line->elements[i].item);
1262     len += strlen(line->elements[i].indent);
1263        }
1264        buf = malloc(len + 1);
1265        *buf = '\0';
1266    
1267        /* allocate mem for extra flags. */
1268        extras = malloc(len + 1);
1269        *extras = '\0';
1270    
1271        /* get title. */
1272        for (int i = 0; i < line->numElements; i++) {
1273     if (!strcmp(line->elements[i].item, "menuentry"))
1274        continue;
1275     if (isquote(*line->elements[i].item))
1276        title = line->elements[i].item + 1;
1277     else
1278        title = line->elements[i].item;
1279    
1280     len = strlen(title);
1281            if (isquote(title[len-1])) {
1282        strncat(buf, title,len-1);
1283        break;
1284     } else {
1285        strcat(buf, title);
1286        strcat(buf, line->elements[i].indent);
1287     }
1288        }
1289    
1290        /* get extras */
1291        int count = 0;
1292        for (int i = 0; i < line->numElements; i++) {
1293     if (count >= 2) {
1294        strcat(extras, line->elements[i].item);
1295        strcat(extras, line->elements[i].indent);
1296     }
1297    
1298     if (!strcmp(line->elements[i].item, "menuentry"))
1299        continue;
1300    
1301     /* count ' or ", there should be two in menuentry line. */
1302     if (isquote(*line->elements[i].item))
1303        count++;
1304    
1305     len = strlen(line->elements[i].item);
1306    
1307     if (isquote(line->elements[i].item[len -1]))
1308        count++;
1309    
1310     /* ok, we get the final ' or ", others are extras. */
1311                }
1312        line->elements[1].indent =
1313     line->elements[line->numElements - 2].indent;
1314        line->elements[1].item = buf;
1315        line->elements[2].indent =
1316     line->elements[line->numElements - 2].indent;
1317        line->elements[2].item = extras;
1318        line->numElements = 3;
1319   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {
1320      /* Strip off any " which may be present; they'll be put back      /* Strip off any " which may be present; they'll be put back
1321         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 1324  static struct grubConfig * readConfig(co
1324      if (line->numElements >= 2) {      if (line->numElements >= 2) {
1325   int last, len;   int last, len;
1326    
1327   if (*line->elements[1].item == '"')   if (isquote(*line->elements[1].item))
1328      memcpy(line->elements[1].item, line->elements[1].item + 1,      memmove(line->elements[1].item, line->elements[1].item + 1,
1329     strlen(line->elements[1].item + 1) + 1);      strlen(line->elements[1].item + 1) + 1);
1330    
1331   last = line->numElements - 1;   last = line->numElements - 1;
1332   len = strlen(line->elements[last].item) - 1;   len = strlen(line->elements[last].item) - 1;
1333   if (line->elements[last].item[len] == '"')   if (isquote(line->elements[last].item[len]))
1334      line->elements[last].item[len] = '\0';      line->elements[last].item[len] = '\0';
1335      }      }
1336     }
1337    
1338     if (line->type == LT_DEFAULT && line->numElements == 2) {
1339        cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1340        defaultLine = line;
1341   }   }
1342    
1343   /* 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 1357  static struct grubConfig * readConfig(co
1357   movedLine = 1;   movedLine = 1;
1358   continue; /* without setting 'last' */   continue; /* without setting 'last' */
1359   }   }
1360    
1361   /* If a second line of whitespace happens after a generic option   /* If a second line of whitespace happens after a generic option
1362     which was moved, drop it. */     which was moved, drop it. */
1363   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 1373  static struct grubConfig * readConfig(co
1373   entry->lines = line;   entry->lines = line;
1374      else      else
1375   last->next = line;   last->next = line;
1376        dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry);
1377    
1378        /* we could have seen this outside of an entry... if so, we
1379         * ignore it like any other line we don't grok */
1380        if (line->type == LT_ENTRY_END && sawEntry)
1381     sawEntry = 0;
1382   } else {   } else {
1383      if (!cfg->theLines)      if (!cfg->theLines)
1384   cfg->theLines = line;   cfg->theLines = line;
1385      else {      else
1386   last->next = line;   last->next = line;
1387      }      dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi));
1388   }   }
1389    
1390   last = line;   last = line;
# Line 708  static struct grubConfig * readConfig(co Line 1392  static struct grubConfig * readConfig(co
1392    
1393      free(incoming);      free(incoming);
1394    
1395        dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1396      if (defaultLine) {      if (defaultLine) {
1397   if (cfi->defaultSupportSaved &&          if (defaultLine->numElements > 2 &&
1398        cfi->defaultSupportSaved &&
1399        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1400     cfg->cfi->defaultIsSaved = 1;
1401     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1402     if (cfg->cfi->getEnv) {
1403        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1404        if (defTitle) {
1405     int index = 0;
1406     if (isnumber(defTitle)) {
1407        index = atoi(defTitle);
1408        entry = findEntryByIndex(cfg, index);
1409     } else {
1410        entry = findEntryByTitle(cfg, defTitle, &index);
1411     }
1412     if (entry)
1413        cfg->defaultImage = index;
1414        }
1415     }
1416     } else if (cfi->defaultIsVariable) {
1417        char *value = defaultLine->elements[2].item;
1418        while (*value && (*value == '"' || *value == '\'' ||
1419        *value == ' ' || *value == '\t'))
1420     value++;
1421        cfg->defaultImage = strtol(value, &end, 10);
1422        while (*end && (*end == '"' || *end == '\'' ||
1423        *end == ' ' || *end == '\t'))
1424     end++;
1425        if (*end) cfg->defaultImage = -1;
1426     } else if (cfi->defaultSupportSaved &&
1427   !strncmp(defaultLine->elements[1].item, "saved", 5)) {   !strncmp(defaultLine->elements[1].item, "saved", 5)) {
1428      cfg->defaultImage = DEFAULT_SAVED;      cfg->defaultImage = DEFAULT_SAVED;
1429   } else if (cfi->defaultIsIndex) {   } else if (cfi->defaultIsIndex) {
1430      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);
1431      if (*end) cfg->defaultImage = -1;      if (*end) cfg->defaultImage = -1;
1432   } else if (defaultLine->numElements >= 2) {   } else if (defaultLine->numElements >= 2) {
1433      i = 0;      int i = 0;
1434      while ((entry = findEntryByIndex(cfg, i))) {      while ((entry = findEntryByIndex(cfg, i))) {
1435   for (line = entry->lines; line; line = line->next)   for (line = entry->lines; line; line = line->next)
1436      if (line->type == LT_TITLE) break;      if (line->type == LT_TITLE) break;
# Line 730  static struct grubConfig * readConfig(co Line 1444  static struct grubConfig * readConfig(co
1444                                  extractTitle(line))) break;                                  extractTitle(line))) break;
1445                  }                  }
1446   i++;   i++;
1447     entry = NULL;
1448      }      }
1449    
1450      if (entry) cfg->defaultImage = i;      if (entry){
1451            cfg->defaultImage = i;
1452        }else{
1453            cfg->defaultImage = -1;
1454        }
1455   }   }
1456        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1457     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1458     if (defTitle) {
1459        int index = 0;
1460        if (isnumber(defTitle)) {
1461     index = atoi(defTitle);
1462     entry = findEntryByIndex(cfg, index);
1463        } else {
1464     entry = findEntryByTitle(cfg, defTitle, &index);
1465        }
1466        if (entry)
1467     cfg->defaultImage = index;
1468     }
1469        } else {
1470            cfg->defaultImage = 0;
1471      }      }
1472    
1473      return cfg;      return cfg;
# Line 749  static void writeDefault(FILE * out, cha Line 1483  static void writeDefault(FILE * out, cha
1483    
1484      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1485   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1486      else if (cfg->defaultImage > -1) {      else if (cfg->cfi->defaultIsSaved) {
1487     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1488     if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1489        char *title;
1490        entry = findEntryByIndex(cfg, cfg->defaultImage);
1491        line = getLineByType(LT_MENUENTRY, entry->lines);
1492        if (!line)
1493     line = getLineByType(LT_TITLE, entry->lines);
1494        if (line) {
1495     title = extractTitle(line);
1496     if (title)
1497        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1498        }
1499     }
1500        } else if (cfg->defaultImage > -1) {
1501   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1502      fprintf(out, "%sdefault%s%d\n", indent, separator,      if (cfg->cfi->defaultIsVariable) {
1503      cfg->defaultImage);          fprintf(out, "%sset default=\"%d\"\n", indent,
1504     cfg->defaultImage);
1505        } else {
1506     fprintf(out, "%sdefault%s%d\n", indent, separator,
1507     cfg->defaultImage);
1508        }
1509   } else {   } else {
1510      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1511    
# Line 769  static void writeDefault(FILE * out, cha Line 1522  static void writeDefault(FILE * out, cha
1522    
1523      if (!entry) return;      if (!entry) return;
1524    
1525      line = entry->lines;      line = getLineByType(LT_TITLE, entry->lines);
     while (line && line->type != LT_TITLE) line = line->next;  
1526    
1527      if (line && line->numElements >= 2)      if (line && line->numElements >= 2)
1528   fprintf(out, "%sdefault%s%s\n", indent, separator,   fprintf(out, "%sdefault%s%s\n", indent, separator,
# Line 804  static int writeConfig(struct grubConfig Line 1556  static int writeConfig(struct grubConfig
1556      int rc;      int rc;
1557    
1558      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1559         directory to / */         directory to the dir of the symlink */
1560      rc = chdir("/");      char *dir = strdupa(outName);
1561        rc = chdir(dirname(dir));
1562      do {      do {
1563   buf = alloca(len + 1);   buf = alloca(len + 1);
1564   rc = readlink(outName, buf, len);   rc = readlink(basename(outName), buf, len);
1565   if (rc == len) len += 256;   if (rc == len) len += 256;
1566      } while (rc == len);      } while (rc == len);
1567            
# Line 843  static int writeConfig(struct grubConfig Line 1596  static int writeConfig(struct grubConfig
1596      }      }
1597    
1598      line = cfg->theLines;      line = cfg->theLines;
1599        struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);
1600      while (line) {      while (line) {
1601   if (line->type == LT_DEFAULT) {          if (line->type == LT_SET_VARIABLE && defaultKw &&
1602     line->numElements == 3 &&
1603     !strcmp(line->elements[1].item, defaultKw->key) &&
1604     !is_special_grub2_variable(line->elements[2].item)) {
1605        writeDefault(out, line->indent, line->elements[0].indent, cfg);
1606        needs &= ~MAIN_DEFAULT;
1607     } else if (line->type == LT_DEFAULT) {
1608      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1609      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1610   } else if (line->type == LT_FALLBACK) {   } else if (line->type == LT_FALLBACK) {
# Line 912  static int numEntries(struct grubConfig Line 1672  static int numEntries(struct grubConfig
1672      return i;      return i;
1673  }  }
1674    
1675  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  static char *findDiskForRoot()
1676    int skipRemoved, int flags) {  {
1677      struct singleLine * line;      int fd;
1678      char * fullName;      char buf[65536];
1679      int i;      char *devname;
1680      struct stat sb, sb2;      char *chptr;
1681      char * dev;      int rc;
     char * end;  
     char * rootspec;  
1682    
1683      line = entry->lines;      if ((fd = open(_PATH_MOUNTED, O_RDONLY)) < 0) {
1684      while (line && line->type != LT_KERNEL) line = line->next;          fprintf(stderr, "grubby: failed to open %s: %s\n",
1685                        _PATH_MOUNTED, strerror(errno));
1686      if (!line) return 0;          return NULL;
1687      if (skipRemoved && entry->skip) return 0;      }
     if (line->numElements < 2) return 0;  
1688    
1689      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      rc = read(fd, buf, sizeof(buf) - 1);
1690        if (rc <= 0) {
1691            fprintf(stderr, "grubby: failed to read %s: %s\n",
1692                    _PATH_MOUNTED, strerror(errno));
1693            close(fd);
1694            return NULL;
1695        }
1696        close(fd);
1697        buf[rc] = '\0';
1698        chptr = buf;
1699    
1700        char *foundanswer = NULL;
1701    
1702        while (chptr && chptr != buf+rc) {
1703            devname = chptr;
1704    
1705            /*
1706             * The first column of a mtab entry is the device, but if the entry is a
1707             * special device it won't start with /, so move on to the next line.
1708             */
1709            if (*devname != '/') {
1710                chptr = strchr(chptr, '\n');
1711                if (chptr)
1712                    chptr++;
1713                continue;
1714            }
1715    
1716            /* Seek to the next space */
1717            chptr = strchr(chptr, ' ');
1718            if (!chptr) {
1719                fprintf(stderr, "grubby: error parsing %s: %s\n",
1720                        _PATH_MOUNTED, strerror(errno));
1721                return NULL;
1722            }
1723    
1724            /*
1725             * The second column of a mtab entry is the mount point, we are looking
1726             * for '/' obviously.
1727             */
1728            if (*(++chptr) == '/' && *(++chptr) == ' ') {
1729                /* remember the last / entry in mtab */
1730               foundanswer = devname;
1731            }
1732    
1733            /* Next line */
1734            chptr = strchr(chptr, '\n');
1735            if (chptr)
1736                chptr++;
1737        }
1738    
1739        /* Return the last / entry found */
1740        if (foundanswer) {
1741            chptr = strchr(foundanswer, ' ');
1742            *chptr = '\0';
1743            return strdup(foundanswer);
1744        }
1745    
1746        return NULL;
1747    }
1748    
1749    void printEntry(struct singleEntry * entry, FILE *f) {
1750        int i;
1751        struct singleLine * line;
1752    
1753        for (line = entry->lines; line; line = line->next) {
1754     log_message(f, "DBG: %s", line->indent);
1755     for (i = 0; i < line->numElements; i++) {
1756        /* Need to handle this, because we strip the quotes from
1757         * menuentry when read it. */
1758        if (line->type == LT_MENUENTRY && i == 1) {
1759     if(!isquote(*line->elements[i].item))
1760        log_message(f, "\'%s\'", line->elements[i].item);
1761     else
1762        log_message(f, "%s", line->elements[i].item);
1763     log_message(f, "%s", line->elements[i].indent);
1764    
1765     continue;
1766        }
1767        
1768        log_message(f, "%s%s",
1769        line->elements[i].item, line->elements[i].indent);
1770     }
1771     log_message(f, "\n");
1772        }
1773    }
1774    
1775    void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1776    {
1777        static int once;
1778        va_list argp, argq;
1779    
1780        va_start(argp, fmt);
1781    
1782        va_copy(argq, argp);
1783        if (!once) {
1784     log_time(NULL);
1785     log_message(NULL, "command line: %s\n", saved_command_line);
1786        }
1787        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1788        log_vmessage(NULL, fmt, argq);
1789    
1790        printEntry(entry, NULL);
1791        va_end(argq);
1792    
1793        if (!debug) {
1794     once = 1;
1795         va_end(argp);
1796     return;
1797        }
1798    
1799        if (okay) {
1800     va_end(argp);
1801     return;
1802        }
1803    
1804        if (!once)
1805     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1806        once = 1;
1807        fprintf(stderr, "DBG: Image entry failed: ");
1808        vfprintf(stderr, fmt, argp);
1809        printEntry(entry, stderr);
1810        va_end(argp);
1811    }
1812    
1813    #define beginswith(s, c) ((s) && (s)[0] == (c))
1814    
1815    static int endswith(const char *s, char c)
1816    {
1817     int slen;
1818    
1819     if (!s || !s[0])
1820     return 0;
1821     slen = strlen(s) - 1;
1822    
1823     return s[slen] == c;
1824    }
1825    
1826    int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1827      int skipRemoved, int flags) {
1828        struct singleLine * line;
1829        char * fullName;
1830        int i;
1831        char * dev;
1832        char * rootspec;
1833        char * rootdev;
1834    
1835        if (skipRemoved && entry->skip) {
1836     notSuitablePrintf(entry, 0, "marked to skip\n");
1837     return 0;
1838        }
1839    
1840        line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
1841        if (!line) {
1842     notSuitablePrintf(entry, 0, "no line found\n");
1843     return 0;
1844        }
1845        if (line->numElements < 2) {
1846     notSuitablePrintf(entry, 0, "line has only %d elements\n",
1847        line->numElements);
1848     return 0;
1849        }
1850    
1851        if (flags & GRUBBY_BADIMAGE_OKAY) {
1852        notSuitablePrintf(entry, 1, "\n");
1853        return 1;
1854        }
1855    
1856      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1857        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
1858      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1859      sprintf(fullName, "%s%s", bootPrefix,      int rootspec_offset = rootspec ? strlen(rootspec) : 0;
1860              line->elements[1].item + ((rootspec != NULL) ?      int hasslash = endswith(bootPrefix, '/') ||
1861                                        strlen(rootspec) : 0));       beginswith(line->elements[1].item + rootspec_offset, '/');
1862      if (access(fullName, R_OK)) return 0;      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1863                line->elements[1].item + rootspec_offset);
1864        if (access(fullName, R_OK)) {
1865     notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1866     return 0;
1867        }
1868      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
1869   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1870      if (i < line->numElements) {      if (i < line->numElements) {
1871   dev = line->elements[i].item + 5;   dev = line->elements[i].item + 5;
1872      } else {      } else {
1873   /* look for a lilo style LT_ROOT line */   /* look for a lilo style LT_ROOT line */
1874   line = entry->lines;   line = getLineByType(LT_ROOT, entry->lines);
  while (line && line->type != LT_ROOT) line = line->next;  
1875    
1876   if (line && line->numElements >= 2) {   if (line && line->numElements >= 2) {
1877      dev = line->elements[1].item;      dev = line->elements[1].item;
1878   } else {   } else {
1879              int type;      /* didn't succeed in finding a LT_ROOT, let's try LT_KERNELARGS.
1880      /* 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.
1881      line = entry->lines;       */
1882        line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);
             type = ((entry->multiboot) ? LT_MBMODULE : LT_KERNELARGS);  
   
     while (line && line->type != type) line = line->next;  
1883    
1884              /* failed to find one */              /* failed to find one */
1885              if (!line) return 0;              if (!line) {
1886     notSuitablePrintf(entry, 0, "no line found\n");
1887     return 0;
1888                }
1889    
1890      for (i = 1; i < line->numElements; i++)      for (i = 1; i < line->numElements; i++)
1891          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1892      if (i < line->numElements)      if (i < line->numElements)
1893          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1894      else {      else {
1895     notSuitablePrintf(entry, 0, "no root= entry found\n");
1896   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1897          return 0;          return 0;
1898              }              }
1899   }   }
1900      }      }
1901    
1902      if (!strncmp(dev, "LABEL=", 6)) {      dev = getpathbyspec(dev);
1903   dev += 6;      if (!getpathbyspec(dev)) {
1904            notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1905   /* check which device has this label */          return 0;
1906   dev = get_spec_by_volume_label(dev, &i, &i);      } else
1907   if (!dev) return 0;   dev = getpathbyspec(dev);
1908    
1909        rootdev = findDiskForRoot();
1910        if (!rootdev) {
1911            notSuitablePrintf(entry, 0, "can't find root device\n");
1912     return 0;
1913      }      }
1914    
1915      if (*dev == '/') {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1916   if (stat(dev, &sb))          notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1917      return 0;   getuuidbydev(rootdev), getuuidbydev(dev));
1918      } else {          free(rootdev);
1919   sb.st_rdev = strtol(dev, &end, 16);          return 0;
1920   if (*end) return 0;      }
1921    
1922        if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1923            notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n",
1924     getuuidbydev(rootdev), getuuidbydev(dev));
1925     free(rootdev);
1926            return 0;
1927      }      }
     stat("/", &sb2);  
1928    
1929      if (sb.st_rdev != sb2.st_dev) return 0;      free(rootdev);
1930        notSuitablePrintf(entry, 1, "\n");
1931    
1932      return 1;      return 1;
1933  }  }
# Line 1024  struct singleEntry * findEntryByPath(str Line 1961  struct singleEntry * findEntryByPath(str
1961   }   }
1962    
1963   indexVars[i + 1] = -1;   indexVars[i + 1] = -1;
1964    
1965   i = 0;   i = 0;
1966   if (index) {   if (index) {
1967      while (i < *index) i++;      while (i < *index) {
1968      if (indexVars[i] == -1) return NULL;   i++;
1969     if (indexVars[i] == -1) return NULL;
1970        }
1971   }   }
1972    
1973   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
1974   if (!entry) return NULL;   if (!entry) return NULL;
1975    
1976   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;  
   
1977   if (!line) return NULL;   if (!line) return NULL;
1978    
1979   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1075  struct singleEntry * findEntryByPath(str Line 2011  struct singleEntry * findEntryByPath(str
2011    
2012   if (!strncmp(kernel, "TITLE=", 6)) {   if (!strncmp(kernel, "TITLE=", 6)) {
2013      prefix = "";      prefix = "";
2014      checkType = LT_TITLE;      checkType = LT_TITLE|LT_MENUENTRY;
2015      kernel += 6;      kernel += 6;
2016   }   }
2017    
2018   while ((entry = findEntryByIndex(config, i))) {   for (entry = findEntryByIndex(config, i); entry; entry = entry->next, i++) {
2019      line = entry->lines;      if (entry->skip) continue;
     while (line && line->type != checkType) line=line->next;  
2020    
2021        dbgPrintf("findEntryByPath looking for %d %s in %p\n", checkType, kernel, entry);
2022    
2023      if (line && line->numElements >= 2 && !entry->skip) {      /* check all the lines matching checkType */
2024                  rootspec = getRootSpecifier(line->elements[1].item);      for (line = entry->lines; line; line = line->next) {
2025          if (!strcmp(line->elements[1].item  +   enum lineType_e ct = checkType;
2026                              ((rootspec != NULL) ? strlen(rootspec) : 0),   if (entry->multiboot && checkType == LT_KERNEL)
2027                              kernel + strlen(prefix)))      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER|LT_KERNEL_16;
2028                      break;   else if (checkType & LT_KERNEL)
2029              }      ct = checkType | LT_KERNEL_EFI | LT_KERNEL_16;
2030                 line = getLineByType(ct, line);
2031              /* have to check multiboot lines too */   if (!line)
2032              if (entry->multiboot) {      break;  /* not found in this entry */
2033                  while (line && line->type != LT_MBMODULE) line = line->next;  
2034                  if (line && line->numElements >= 2 && !entry->skip) {   if (line && line->type != LT_MENUENTRY &&
2035                      rootspec = getRootSpecifier(line->elements[1].item);   line->numElements >= 2) {
2036                      if (!strcmp(line->elements[1].item  +      rootspec = getRootSpecifier(line->elements[1].item);
2037                                  ((rootspec != NULL) ? strlen(rootspec) : 0),      if (!strcmp(line->elements[1].item +
2038                                  kernel + strlen(prefix)))   ((rootspec != NULL) ? strlen(rootspec) : 0),
2039                          break;   kernel + strlen(prefix)))
2040                  }   break;
2041              }   }
2042     if(line->type == LT_MENUENTRY &&
2043     !strcmp(line->elements[1].item, kernel))
2044        break;
2045        }
2046    
2047      i++;      /* make sure this entry has a kernel identifier; this skips
2048         * non-Linux boot entries (could find netbsd etc, though, which is
2049         * unfortunate)
2050         */
2051        if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines))
2052     break; /* found 'im! */
2053   }   }
2054    
2055   if (index) *index = i;   if (index) *index = i;
2056      }      }
2057    
2058      if (!entry) return NULL;      return entry;
2059    }
2060    
2061    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2062          int * index) {
2063        struct singleEntry * entry;
2064        struct singleLine * line;
2065        int i;
2066        char * newtitle;
2067    
2068      /* make sure this entry has a kernel identifier; this skips non-Linux      for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2069         boot entries (could find netbsd etc, though, which is unfortunate) */   if (index && i < *index)
2070      line = entry->lines;      continue;
2071      while (line && line->type != LT_KERNEL) line = line->next;   line = getLineByType(LT_TITLE, entry->lines);
2072      if (!line) {   if (!line)
2073   if (!index) index = &i;      line = getLineByType(LT_MENUENTRY, entry->lines);
2074   (*index)++;   if (!line)
2075   return findEntryByPath(config, kernel, prefix, index);      continue;
2076     newtitle = grub2ExtractTitle(line);
2077     if (!newtitle)
2078        continue;
2079     if (!strcmp(title, newtitle))
2080        break;
2081      }      }
2082    
2083        if (!entry)
2084     return NULL;
2085    
2086        if (index)
2087     *index = i;
2088      return entry;      return entry;
2089  }  }
2090    
# Line 1147  struct singleEntry * findTemplate(struct Line 2110  struct singleEntry * findTemplate(struct
2110      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2111      int index;      int index;
2112    
2113      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2114     if (cfg->cfi->getEnv) {
2115        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2116        if (defTitle) {
2117     int index = 0;
2118     if (isnumber(defTitle)) {
2119        index = atoi(defTitle);
2120        entry = findEntryByIndex(cfg, index);
2121     } else {
2122        entry = findEntryByTitle(cfg, defTitle, &index);
2123     }
2124     if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2125        cfg->defaultImage = index;
2126        if (indexPtr)
2127     *indexPtr = index;
2128        return entry;
2129     }
2130        }
2131     }
2132        } else if (cfg->defaultImage > -1) {
2133   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2134   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2135      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 1200  void markRemovedImage(struct grubConfig Line 2182  void markRemovedImage(struct grubConfig
2182        const char * prefix) {        const char * prefix) {
2183      struct singleEntry * entry;      struct singleEntry * entry;
2184    
2185      if (!image) return;      if (!image)
2186     return;
2187    
2188        /* check and see if we're removing the default image */
2189        if (isdigit(*image)) {
2190     entry = findEntryByPath(cfg, image, prefix, NULL);
2191     if(entry)
2192        entry->skip = 1;
2193     return;
2194        }
2195    
2196      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
2197   entry->skip = 1;   entry->skip = 1;
# Line 1208  void markRemovedImage(struct grubConfig Line 2199  void markRemovedImage(struct grubConfig
2199    
2200  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
2201       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
2202       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
2203      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
2204      int i, j;      int i, j;
2205    
2206      if (newIsDefault) {      if (newIsDefault) {
2207   config->defaultImage = 0;   config->defaultImage = 0;
2208   return;   return;
2209        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
2210     if (findEntryByIndex(config, index))
2211        config->defaultImage = index;
2212     else
2213        config->defaultImage = -1;
2214     return;
2215      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
2216   i = 0;   i = 0;
2217   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1227  void setDefaultImage(struct grubConfig * Line 2224  void setDefaultImage(struct grubConfig *
2224    
2225      /* 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
2226         changes */         changes */
2227      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
2228     (config->defaultImage == DEFAULT_SAVED_GRUB2))
2229        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
2230        return;        return;
2231    
# Line 1285  void displayEntry(struct singleEntry * e Line 2283  void displayEntry(struct singleEntry * e
2283      struct singleLine * line;      struct singleLine * line;
2284      char * root = NULL;      char * root = NULL;
2285      int i;      int i;
2286        int j;
     line = entry->lines;  
     while (line && line->type != LT_KERNEL) line = line->next;  
2287    
2288      printf("index=%d\n", index);      printf("index=%d\n", index);
2289    
2290      printf("kernel=%s\n", line->elements[1].item);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
2291        if (!line) {
2292            printf("non linux entry\n");
2293            return;
2294        }
2295    
2296        if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2297     printf("kernel=%s\n", line->elements[1].item);
2298        else
2299     printf("kernel=%s%s\n", prefix, line->elements[1].item);
2300    
2301      if (line->numElements >= 3) {      if (line->numElements >= 3) {
2302   printf("args=\"");   printf("args=\"");
# Line 1308  void displayEntry(struct singleEntry * e Line 2313  void displayEntry(struct singleEntry * e
2313   }   }
2314   printf("\"\n");   printf("\"\n");
2315      } else {      } else {
2316   line = entry->lines;   line = getLineByType(LT_KERNELARGS, entry->lines);
  while (line && line->type != LT_KERNELARGS) line=line->next;  
   
2317   if (line) {   if (line) {
2318      char * s;      char * s;
2319    
# Line 1334  void displayEntry(struct singleEntry * e Line 2337  void displayEntry(struct singleEntry * e
2337      }      }
2338    
2339      if (!root) {      if (!root) {
2340   line = entry->lines;   line = getLineByType(LT_ROOT, entry->lines);
  while (line && line->type != LT_ROOT) line = line->next;  
   
2341   if (line && line->numElements >= 2)   if (line && line->numElements >= 2)
2342      root=line->elements[1].item;      root=line->elements[1].item;
2343      }      }
# Line 1351  void displayEntry(struct singleEntry * e Line 2352  void displayEntry(struct singleEntry * e
2352   printf("root=%s\n", s);   printf("root=%s\n", s);
2353      }      }
2354    
2355      line = entry->lines;      line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
     while (line && line->type != LT_INITRD) line = line->next;  
2356    
2357      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2358   printf("initrd=%s", prefix);   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2359        printf("initrd=");
2360     else
2361        printf("initrd=%s", prefix);
2362    
2363   for (i = 1; i < line->numElements; i++)   for (i = 1; i < line->numElements; i++)
2364      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2365   printf("\n");   printf("\n");
2366      }      }
2367    
2368        line = getLineByType(LT_TITLE, entry->lines);
2369        if (line) {
2370     printf("title=%s\n", line->elements[1].item);
2371        } else {
2372     char * title;
2373     line = getLineByType(LT_MENUENTRY, entry->lines);
2374     if (line) {
2375        title = grub2ExtractTitle(line);
2376        if (title)
2377     printf("title=%s\n", title);
2378     }
2379        }
2380    
2381        for (j = 0, line = entry->lines; line; line = line->next) {
2382     if ((line->type & LT_MBMODULE) && line->numElements >= 2) {
2383        if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2384     printf("mbmodule%d=", j);
2385        else
2386     printf("mbmodule%d=%s", j, prefix);
2387    
2388        for (i = 1; i < line->numElements; i++)
2389     printf("%s%s", line->elements[i].item, line->elements[i].indent);
2390        printf("\n");
2391        j++;
2392     }
2393        }
2394    }
2395    
2396    int isSuseSystem(void) {
2397        const char * path;
2398        const static char default_path[] = "/etc/SuSE-release";
2399    
2400        if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL)
2401     path = default_path;
2402    
2403        if (!access(path, R_OK))
2404     return 1;
2405        return 0;
2406    }
2407    
2408    int isSuseGrubConf(const char * path) {
2409        FILE * grubConf;
2410        char * line = NULL;
2411        size_t len = 0, res = 0;
2412    
2413        grubConf = fopen(path, "r");
2414        if (!grubConf) {
2415            dbgPrintf("Could not open SuSE configuration file '%s'\n", path);
2416     return 0;
2417        }
2418    
2419        while ((res = getline(&line, &len, grubConf)) != -1) {
2420     if (!strncmp(line, "setup", 5)) {
2421        fclose(grubConf);
2422        free(line);
2423        return 1;
2424     }
2425        }
2426    
2427        dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n",
2428          path);
2429    
2430        fclose(grubConf);
2431        free(line);
2432        return 0;
2433    }
2434    
2435    int suseGrubConfGetLba(const char * path, int * lbaPtr) {
2436        FILE * grubConf;
2437        char * line = NULL;
2438        size_t res = 0, len = 0;
2439    
2440        if (!path) return 1;
2441        if (!lbaPtr) return 1;
2442    
2443        grubConf = fopen(path, "r");
2444        if (!grubConf) return 1;
2445    
2446        while ((res = getline(&line, &len, grubConf)) != -1) {
2447     if (line[res - 1] == '\n')
2448        line[res - 1] = '\0';
2449     else if (len > res)
2450        line[res] = '\0';
2451     else {
2452        line = realloc(line, res + 1);
2453        line[res] = '\0';
2454     }
2455    
2456     if (!strncmp(line, "setup", 5)) {
2457        if (strstr(line, "--force-lba")) {
2458            *lbaPtr = 1;
2459        } else {
2460            *lbaPtr = 0;
2461        }
2462        dbgPrintf("lba: %i\n", *lbaPtr);
2463        break;
2464     }
2465        }
2466    
2467        free(line);
2468        fclose(grubConf);
2469        return 0;
2470    }
2471    
2472    int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) {
2473        FILE * grubConf;
2474        char * line = NULL;
2475        size_t res = 0, len = 0;
2476        char * lastParamPtr = NULL;
2477        char * secLastParamPtr = NULL;
2478        char installDeviceNumber = '\0';
2479        char * bounds = NULL;
2480    
2481        if (!path) return 1;
2482        if (!devicePtr) return 1;
2483    
2484        grubConf = fopen(path, "r");
2485        if (!grubConf) return 1;
2486    
2487        while ((res = getline(&line, &len, grubConf)) != -1) {
2488     if (strncmp(line, "setup", 5))
2489        continue;
2490    
2491     if (line[res - 1] == '\n')
2492        line[res - 1] = '\0';
2493     else if (len > res)
2494        line[res] = '\0';
2495     else {
2496        line = realloc(line, res + 1);
2497        line[res] = '\0';
2498     }
2499    
2500     lastParamPtr = bounds = line + res;
2501    
2502     /* Last parameter in grub may be an optional IMAGE_DEVICE */
2503     while (!isspace(*lastParamPtr))
2504        lastParamPtr--;
2505     lastParamPtr++;
2506    
2507     secLastParamPtr = lastParamPtr - 2;
2508     dbgPrintf("lastParamPtr: %s\n", lastParamPtr);
2509    
2510     if (lastParamPtr + 3 > bounds) {
2511        dbgPrintf("lastParamPtr going over boundary");
2512        fclose(grubConf);
2513        free(line);
2514        return 1;
2515     }
2516     if (!strncmp(lastParamPtr, "(hd", 3))
2517        lastParamPtr += 3;
2518     dbgPrintf("lastParamPtr: %c\n", *lastParamPtr);
2519    
2520     /*
2521     * Second last parameter will decide wether last parameter is
2522     * an IMAGE_DEVICE or INSTALL_DEVICE
2523     */
2524     while (!isspace(*secLastParamPtr))
2525        secLastParamPtr--;
2526     secLastParamPtr++;
2527    
2528     if (secLastParamPtr + 3 > bounds) {
2529        dbgPrintf("secLastParamPtr going over boundary");
2530        fclose(grubConf);
2531        free(line);
2532        return 1;
2533     }
2534     dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr);
2535     if (!strncmp(secLastParamPtr, "(hd", 3)) {
2536        secLastParamPtr += 3;
2537        dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr);
2538        installDeviceNumber = *secLastParamPtr;
2539     } else {
2540        installDeviceNumber = *lastParamPtr;
2541     }
2542    
2543     *devicePtr = malloc(6);
2544     snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber);
2545     dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber);
2546     fclose(grubConf);
2547     free(line);
2548     return 0;
2549        }
2550    
2551        free(line);
2552        fclose(grubConf);
2553        return 1;
2554    }
2555    
2556    int grubGetBootFromDeviceMap(const char * device,
2557         char ** bootPtr) {
2558        FILE * deviceMap;
2559        char * line = NULL;
2560        size_t res = 0, len = 0;
2561        char * devicePtr;
2562        char * bounds = NULL;
2563        const char * path;
2564        const static char default_path[] = "/boot/grub/device.map";
2565    
2566        if (!device) return 1;
2567        if (!bootPtr) return 1;
2568    
2569        if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL)
2570     path = default_path;
2571    
2572        dbgPrintf("opening grub device.map file from: %s\n", path);
2573        deviceMap = fopen(path, "r");
2574        if (!deviceMap)
2575     return 1;
2576    
2577        while ((res = getline(&line, &len, deviceMap)) != -1) {
2578            if (!strncmp(line, "#", 1))
2579        continue;
2580    
2581     if (line[res - 1] == '\n')
2582        line[res - 1] = '\0';
2583     else if (len > res)
2584        line[res] = '\0';
2585     else {
2586        line = realloc(line, res + 1);
2587        line[res] = '\0';
2588     }
2589    
2590     devicePtr = line;
2591     bounds = line + res;
2592    
2593     while ((isspace(*line) && ((devicePtr + 1) <= bounds)))
2594        devicePtr++;
2595     dbgPrintf("device: %s\n", devicePtr);
2596    
2597     if (!strncmp(devicePtr, device, strlen(device))) {
2598        devicePtr += strlen(device);
2599        while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds))
2600            devicePtr++;
2601    
2602        *bootPtr = strdup(devicePtr);
2603        break;
2604     }
2605        }
2606    
2607        free(line);
2608        fclose(deviceMap);
2609        return 0;
2610    }
2611    
2612    int suseGrubConfGetBoot(const char * path, char ** bootPtr) {
2613        char * grubDevice;
2614    
2615        if (suseGrubConfGetInstallDevice(path, &grubDevice))
2616     dbgPrintf("error looking for grub installation device\n");
2617        else
2618     dbgPrintf("grubby installation device: %s\n", grubDevice);
2619    
2620        if (grubGetBootFromDeviceMap(grubDevice, bootPtr))
2621     dbgPrintf("error looking for grub boot device\n");
2622        else
2623     dbgPrintf("grubby boot device: %s\n", *bootPtr);
2624    
2625        free(grubDevice);
2626        return 0;
2627    }
2628    
2629    int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) {
2630        /*
2631         * This SuSE grub configuration file at this location is not your average
2632         * grub configuration file, but instead the grub commands used to setup
2633         * grub on that system.
2634         */
2635        const char * path;
2636        const static char default_path[] = "/etc/grub.conf";
2637    
2638        if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL)
2639     path = default_path;
2640    
2641        if (!isSuseGrubConf(path)) return 1;
2642    
2643        if (lbaPtr) {
2644            *lbaPtr = 0;
2645            if (suseGrubConfGetLba(path, lbaPtr))
2646                return 1;
2647        }
2648    
2649        if (bootPtr) {
2650            *bootPtr = NULL;
2651            suseGrubConfGetBoot(path, bootPtr);
2652        }
2653    
2654        return 0;
2655  }  }
2656    
2657  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 1369  int parseSysconfigGrub(int * lbaPtr, cha Line 2661  int parseSysconfigGrub(int * lbaPtr, cha
2661      char * start;      char * start;
2662      char * param;      char * param;
2663    
2664      in = fopen("/etc/sysconfig/grub", "r");      in = fopen("/etc/conf.d/grub", "r");
2665      if (!in) return 1;      if (!in) return 1;
2666    
2667      if (lbaPtr) *lbaPtr = 0;      if (lbaPtr) *lbaPtr = 0;
# Line 1410  int parseSysconfigGrub(int * lbaPtr, cha Line 2702  int parseSysconfigGrub(int * lbaPtr, cha
2702  }  }
2703    
2704  void dumpSysconfigGrub(void) {  void dumpSysconfigGrub(void) {
2705      char * boot;      char * boot = NULL;
2706      int lba;      int lba;
2707    
2708      if (!parseSysconfigGrub(&lba, &boot)) {      if (isSuseSystem()) {
2709   if (lba) printf("lba\n");          if (parseSuseGrubConf(&lba, &boot)) {
2710   if (boot) printf("boot=%s\n", boot);      free(boot);
2711        return;
2712     }
2713        } else {
2714            if (parseSysconfigGrub(&lba, &boot)) {
2715        free(boot);
2716        return;
2717     }
2718        }
2719    
2720        if (lba) printf("lba\n");
2721        if (boot) {
2722     printf("boot=%s\n", boot);
2723     free(boot);
2724      }      }
2725  }  }
2726    
# Line 1431  int displayInfo(struct grubConfig * conf Line 2736  int displayInfo(struct grubConfig * conf
2736   return 1;   return 1;
2737      }      }
2738    
2739      /* 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
2740         be a better way */         be a better way */
2741      if (config->cfi == &grubConfigType) {      if (config->cfi == &grubConfigType) {
2742   dumpSysconfigGrub();   dumpSysconfigGrub();
2743      } else {      } else {
2744   line = config->theLines;   line = getLineByType(LT_BOOT, config->theLines);
  while (line && line->type != LT_BOOT) line = line->next;  
2745   if (line && line->numElements >= 1) {   if (line && line->numElements >= 1) {
2746      printf("boot=%s\n", line->elements[1].item);      printf("boot=%s\n", line->elements[1].item);
2747   }   }
2748    
2749   line = config->theLines;   line = getLineByType(LT_LBA, config->theLines);
  while (line && line->type != LT_LBA) line = line->next;  
2750   if (line) printf("lba\n");   if (line) printf("lba\n");
2751      }      }
2752    
# Line 1458  int displayInfo(struct grubConfig * conf Line 2761  int displayInfo(struct grubConfig * conf
2761      return 0;      return 0;
2762  }  }
2763    
2764    struct singleLine * addLineTmpl(struct singleEntry * entry,
2765     struct singleLine * tmplLine,
2766     struct singleLine * prevLine,
2767     const char * val,
2768     struct configFileInfo * cfi)
2769    {
2770        struct singleLine * newLine = lineDup(tmplLine);
2771    
2772        if (isEfi && cfi == &grub2ConfigType) {
2773     enum lineType_e old = newLine->type;
2774     newLine->type = preferredLineType(newLine->type, cfi);
2775     if (old != newLine->type)
2776        newLine->elements[0].item = getKeyByType(newLine->type, cfi);
2777        }
2778    
2779        if (val) {
2780     /* override the inherited value with our own.
2781     * This is a little weak because it only applies to elements[1]
2782     */
2783     if (newLine->numElements > 1)
2784        removeElement(newLine, 1);
2785     insertElement(newLine, val, 1, cfi);
2786    
2787     /* but try to keep the rootspec from the template... sigh */
2788     if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI|LT_KERNEL_16|LT_INITRD_16)) {
2789        char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2790        if (rootspec != NULL) {
2791     free(newLine->elements[1].item);
2792     newLine->elements[1].item =
2793        sdupprintf("%s%s", rootspec, val);
2794        }
2795     }
2796        }
2797    
2798        dbgPrintf("addLineTmpl(%s)\n", newLine->numElements ?
2799          newLine->elements[0].item : "");
2800    
2801        if (!entry->lines) {
2802     /* first one on the list */
2803     entry->lines = newLine;
2804        } else if (prevLine) {
2805     /* add after prevLine */
2806     newLine->next = prevLine->next;
2807     prevLine->next = newLine;
2808        }
2809    
2810        return newLine;
2811    }
2812    
2813  /* val may be NULL */  /* val may be NULL */
2814  struct singleLine *  addLine(struct singleEntry * entry,  struct singleLine *  addLine(struct singleEntry * entry,
2815       struct configFileInfo * cfi,       struct configFileInfo * cfi,
2816       enum lineType_e type, const char * defaultIndent,       enum lineType_e type, char * defaultIndent,
2817       char * val) {       const char * val) {
2818      struct singleLine * line, * prev;      struct singleLine * line, * prev;
2819      int i;      struct keywordTypes * kw;
2820        struct singleLine tmpl;
2821    
2822      for (i = 0; cfi->keywords[i].key; i++)      /* NB: This function shouldn't allocate items on the heap, rather on the
2823   if (cfi->keywords[i].type == type) break;       * stack since it calls addLineTmpl which will make copies.
2824      if (type != LT_TITLE || !cfi->titleBracketed)       */
2825          if (!cfi->keywords[i].key) abort();      if (type == LT_TITLE && cfi->titleBracketed) {
2826     /* we're doing a bracketed title (zipl) */
2827     tmpl.type = type;
2828     tmpl.numElements = 1;
2829     tmpl.elements = alloca(sizeof(*tmpl.elements));
2830     tmpl.elements[0].item = alloca(strlen(val)+3);
2831     sprintf(tmpl.elements[0].item, "[%s]", val);
2832     tmpl.elements[0].indent = "";
2833     val = NULL;
2834        } else if (type == LT_MENUENTRY) {
2835     char *lineend = "--class gnu-linux --class gnu --class os {";
2836     if (!val) {
2837        fprintf(stderr, "Line type LT_MENUENTRY requires a value\n");
2838        abort();
2839     }
2840     kw = getKeywordByType(type, cfi);
2841     if (!kw) {
2842        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
2843        abort();
2844     }
2845     tmpl.indent = "";
2846     tmpl.type = type;
2847     tmpl.numElements = 3;
2848     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
2849     tmpl.elements[0].item = kw->key;
2850     tmpl.elements[0].indent = alloca(2);
2851     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
2852     tmpl.elements[1].item = (char *)val;
2853     tmpl.elements[1].indent = alloca(2);
2854     sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);
2855     tmpl.elements[2].item = alloca(strlen(lineend)+1);
2856     strcpy(tmpl.elements[2].item, lineend);
2857     tmpl.elements[2].indent = "";
2858        } else {
2859     kw = getKeywordByType(type, cfi);
2860     if (!kw) {
2861        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
2862        abort();
2863     }
2864     tmpl.type = type;
2865     tmpl.numElements = val ? 2 : 1;
2866     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
2867     tmpl.elements[0].item = kw->key;
2868     tmpl.elements[0].indent = alloca(2);
2869     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
2870     if (val) {
2871        tmpl.elements[1].item = (char *)val;
2872        tmpl.elements[1].indent = "";
2873     }
2874        }
2875    
2876      /* 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
2877         to insert after. Note that comments are considered empty lines, which         to insert after. Note that comments are considered empty lines, which
2878         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
2879         first line, we use defaultIndent (the first line is normally indented         first line, we use defaultIndent (the first line is normally indented
2880         differently from the rest) */         differently from the rest) */
2881      if (entry->lines) {      for (line = entry->lines, prev = NULL; line; line = line->next) {
2882   line = entry->lines;   if (line->numElements) prev = line;
2883   prev = NULL;   /* fall back on the last line if prev isn't otherwise set */
2884   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;  
2885      }      }
2886    
2887      if (type != LT_TITLE || !cfi->titleBracketed) {      struct singleLine *menuEntry;
2888          line->type = type;      menuEntry = getLineByType(LT_MENUENTRY, entry->lines);
2889          line->numElements = val ? 2 : 1;      if (tmpl.type == LT_ENTRY_END) {
2890          line->elements = malloc(sizeof(*line->elements) * line->numElements);   if (menuEntry)
2891          line->elements[0].item = strdup(cfi->keywords[i].key);      tmpl.indent = menuEntry->indent;
2892          line->elements[0].indent = malloc(2);   else
2893          line->elements[0].indent[0] = cfi->keywords[i].nextChar;      tmpl.indent = defaultIndent ?: "";
2894          line->elements[0].indent[1] = '\0';      } else if (tmpl.type != LT_MENUENTRY) {
2895             if (menuEntry)
2896          if (val) {      tmpl.indent = "\t";
2897              line->elements[1].item = val;   else if (prev == entry->lines)
2898              line->elements[1].indent = strdup("");      tmpl.indent = defaultIndent ?: "";
2899          }   else
2900      } 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("");  
2901      }      }
2902    
2903      return line;      return addLineTmpl(entry, &tmpl, prev, val, cfi);
2904  }  }
2905    
2906  void removeLine(struct singleEntry * entry, struct singleLine * line) {  void removeLine(struct singleEntry * entry, struct singleLine * line) {
# Line 1553  void removeLine(struct singleEntry * ent Line 2925  void removeLine(struct singleEntry * ent
2925      free(line);      free(line);
2926  }  }
2927    
2928    static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2929    {
2930        struct singleLine newLine = {
2931     .indent = tmplLine->indent,
2932     .type = tmplLine->type,
2933     .next = tmplLine->next,
2934        };
2935        int firstQuotedItem = -1;
2936        int quoteLen = 0;
2937        int j;
2938        int element = 0;
2939        char *c;
2940    
2941        c = malloc(strlen(tmplLine->elements[0].item) + 1);
2942        strcpy(c, tmplLine->elements[0].item);
2943        insertElement(&newLine, c, element++, cfi);
2944        free(c);
2945        c = NULL;
2946    
2947        for (j = 1; j < tmplLine->numElements; j++) {
2948     if (firstQuotedItem == -1) {
2949        quoteLen += strlen(tmplLine->elements[j].item);
2950        
2951        if (isquote(tmplLine->elements[j].item[0])) {
2952     firstQuotedItem = j;
2953            quoteLen += strlen(tmplLine->elements[j].indent);
2954        } else {
2955     c = malloc(quoteLen + 1);
2956     strcpy(c, tmplLine->elements[j].item);
2957     insertElement(&newLine, c, element++, cfi);
2958     free(c);
2959     quoteLen = 0;
2960        }
2961     } else {
2962        int itemlen = strlen(tmplLine->elements[j].item);
2963        quoteLen += itemlen;
2964        quoteLen += strlen(tmplLine->elements[j].indent);
2965        
2966        if (isquote(tmplLine->elements[j].item[itemlen - 1])) {
2967     c = malloc(quoteLen + 1);
2968     c[0] = '\0';
2969     for (int i = firstQuotedItem; i < j+1; i++) {
2970        strcat(c, tmplLine->elements[i].item);
2971        strcat(c, tmplLine->elements[i].indent);
2972     }
2973     insertElement(&newLine, c, element++, cfi);
2974     free(c);
2975    
2976     firstQuotedItem = -1;
2977     quoteLen = 0;
2978        }
2979     }
2980        }
2981        while (tmplLine->numElements)
2982     removeElement(tmplLine, 0);
2983        if (tmplLine->elements)
2984     free(tmplLine->elements);
2985    
2986        tmplLine->numElements = newLine.numElements;
2987        tmplLine->elements = newLine.elements;
2988    }
2989    
2990    static void insertElement(struct singleLine * line,
2991      const char * item, int insertHere,
2992      struct configFileInfo * cfi)
2993    {
2994        struct keywordTypes * kw;
2995        char indent[2] = "";
2996    
2997        /* sanity check */
2998        if (insertHere > line->numElements) {
2999     dbgPrintf("insertElement() adjusting insertHere from %d to %d\n",
3000      insertHere, line->numElements);
3001     insertHere = line->numElements;
3002        }
3003    
3004        line->elements = realloc(line->elements, (line->numElements + 1) *
3005         sizeof(*line->elements));
3006        memmove(&line->elements[insertHere+1],
3007        &line->elements[insertHere],
3008        (line->numElements - insertHere) *
3009        sizeof(*line->elements));
3010        line->elements[insertHere].item = strdup(item);
3011    
3012        kw = getKeywordByType(line->type, cfi);
3013    
3014        if (line->numElements == 0) {
3015     indent[0] = '\0';
3016        } else if (insertHere == 0) {
3017     indent[0] = kw->nextChar;
3018        } else if (kw->separatorChar != '\0') {
3019     indent[0] = kw->separatorChar;
3020        } else {
3021     indent[0] = ' ';
3022        }
3023    
3024        if (insertHere > 0 && line->elements[insertHere-1].indent[0] == '\0') {
3025     /* move the end-of-line forward */
3026     line->elements[insertHere].indent =
3027        line->elements[insertHere-1].indent;
3028     line->elements[insertHere-1].indent = strdup(indent);
3029        } else {
3030     line->elements[insertHere].indent = strdup(indent);
3031        }
3032    
3033        line->numElements++;
3034    
3035        dbgPrintf("insertElement(%s, '%s%s', %d)\n",
3036          line->elements[0].item,
3037          line->elements[insertHere].item,
3038          line->elements[insertHere].indent,
3039          insertHere);
3040    }
3041    
3042    static void removeElement(struct singleLine * line, int removeHere) {
3043        int i;
3044    
3045        /* sanity check */
3046        if (removeHere >= line->numElements) return;
3047    
3048        dbgPrintf("removeElement(%s, %d:%s)\n", line->elements[0].item,
3049          removeHere, line->elements[removeHere].item);
3050    
3051        free(line->elements[removeHere].item);
3052    
3053        if (removeHere > 1) {
3054     /* previous argument gets this argument's post-indentation */
3055     free(line->elements[removeHere-1].indent);
3056     line->elements[removeHere-1].indent =
3057        line->elements[removeHere].indent;
3058        } else {
3059     free(line->elements[removeHere].indent);
3060        }
3061    
3062        /* now collapse the array, but don't bother to realloc smaller */
3063        for (i = removeHere; i < line->numElements - 1; i++)
3064     line->elements[i] = line->elements[i + 1];
3065    
3066        line->numElements--;
3067    }
3068    
3069  int argMatch(const char * one, const char * two) {  int argMatch(const char * one, const char * two) {
3070      char * first, * second;      char * first, * second;
3071      char * chptr;      char * chptr;
# Line 1575  int updateActualImage(struct grubConfig Line 3088  int updateActualImage(struct grubConfig
3088      struct singleEntry * entry;      struct singleEntry * entry;
3089      struct singleLine * line, * rootLine;      struct singleLine * line, * rootLine;
3090      int index = 0;      int index = 0;
3091      int i, j, k;      int i, k;
3092      const char ** newArgs, ** oldArgs;      const char ** newArgs, ** oldArgs;
3093      const char ** arg;      const char ** arg;
3094      const char * chptr;      int useKernelArgs, useRoot;
     int useKernelArgs = 0;  
     int useRoot = 0;  
3095      int firstElement;      int firstElement;
3096      int *usedElements, *usedArgs;      int *usedElements;
3097        int doreplace;
3098    
3099      if (!image) return 0;      if (!image) return 0;
3100    
# Line 1609  int updateActualImage(struct grubConfig Line 3121  int updateActualImage(struct grubConfig
3121   }   }
3122      }      }
3123    
     for (i = 0; cfg->cfi->keywords[i].key; i++)  
  if (cfg->cfi->keywords[i].type == LT_KERNELARGS) break;  
3124    
3125      if (cfg->cfi->keywords[i].key)      useKernelArgs = (getKeywordByType(LT_KERNELARGS, cfg->cfi)
3126   useKernelArgs = 1;       && (!multibootArgs || cfg->cfi->mbConcatArgs));
3127    
3128      for (i = 0; cfg->cfi->keywords[i].key; i++)      useRoot = (getKeywordByType(LT_ROOT, cfg->cfi)
3129   if (cfg->cfi->keywords[i].type == LT_ROOT) break;         && !multibootArgs);
3130    
3131      if (cfg->cfi->keywords[i].key)      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
  useRoot = 1;  
3132    
3133      k = 0;   if (multibootArgs && !entry->multiboot)
3134      for (arg = newArgs; *arg; arg++)      continue;
3135          k++;  
3136      usedArgs = calloc(k, sizeof(int));   /* Determine where to put the args.  If this config supports
3137     * LT_KERNELARGS, use that.  Otherwise use
3138     * LT_HYPER/LT_KERNEL/LT_MBMODULE lines.
3139     */
3140     if (useKernelArgs) {
3141        line = getLineByType(LT_KERNELARGS, entry->lines);
3142        if (!line) {
3143     /* no LT_KERNELARGS, need to add it */
3144     line = addLine(entry, cfg->cfi, LT_KERNELARGS,
3145           cfg->secondaryIndent, NULL);
3146        }
3147        firstElement = 1;
3148    
3149      while ((entry = findEntryByPath(cfg, image, prefix, &index))) {   } else if (multibootArgs) {
3150   index++;      line = getLineByType(LT_HYPER, entry->lines);
3151        if (!line) {
3152     /* a multiboot entry without LT_HYPER? */
3153     continue;
3154        }
3155        firstElement = 2;
3156    
3157   line = entry->lines;   } else {
3158   while (line && line->type != LT_KERNEL) line = line->next;      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3159   if (!line) continue;      if (!line) {
3160   firstElement = 2;   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3161     continue;
3162          if (entry->multiboot && !multibootArgs) {      }
3163              /* first mb module line is the real kernel */      firstElement = 2;
3164              while (line && line->type != LT_MBMODULE) line = line->next;   }
3165              firstElement = 2;  
3166          } else if (useKernelArgs) {   /* handle the elilo case which does:
3167      while (line && line->type != LT_KERNELARGS) line = line->next;   *   append="hypervisor args -- kernel args"
3168      firstElement = 1;   */
3169     if (entry->multiboot && cfg->cfi->mbConcatArgs) {
3170        /* this is a multiboot entry, make sure there's
3171         * -- on the args line
3172         */
3173        for (i = firstElement; i < line->numElements; i++) {
3174     if (!strcmp(line->elements[i].item, "--"))
3175        break;
3176        }
3177        if (i == line->numElements) {
3178     /* assume all existing args are kernel args,
3179     * prepend -- to make it official
3180     */
3181     insertElement(line, "--", firstElement, cfg->cfi);
3182     i = firstElement;
3183        }
3184        if (!multibootArgs) {
3185     /* kernel args start after the -- */
3186     firstElement = i + 1;
3187        }
3188     } else if (cfg->cfi->mbConcatArgs) {
3189        /* this is a non-multiboot entry, remove hyper args */
3190        for (i = firstElement; i < line->numElements; i++) {
3191     if (!strcmp(line->elements[i].item, "--"))
3192        break;
3193        }
3194        if (i < line->numElements) {
3195     /* remove args up to -- */
3196     while (strcmp(line->elements[firstElement].item, "--"))
3197        removeElement(line, firstElement);
3198     /* remove -- */
3199     removeElement(line, firstElement);
3200        }
3201   }   }
3202    
3203   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);  
  }  
3204    
3205          usedElements = calloc(line->numElements, sizeof(int));   for (k = 0, arg = newArgs; *arg; arg++, k++) {
3206    
3207          k = 0;      doreplace = 1;
  for (arg = newArgs; *arg; arg++) {  
             if (usedArgs[k]) {  
                 k++;  
                 continue;  
             }  
3208      for (i = firstElement; i < line->numElements; i++) {      for (i = firstElement; i < line->numElements; i++) {
3209     if (multibootArgs && cfg->cfi->mbConcatArgs &&
3210        !strcmp(line->elements[i].item, "--"))
3211     {
3212        /* reached the end of hyper args, insert here */
3213        doreplace = 0;
3214        break;  
3215     }
3216                  if (usedElements[i])                  if (usedElements[i])
3217                      continue;                      continue;
3218   if (!argMatch(line->elements[i].item, *arg)) {   if (!argMatch(line->elements[i].item, *arg)) {
3219                      usedElements[i]=1;                      usedElements[i]=1;
                     usedArgs[k]=1;  
3220      break;      break;
3221                  }                  }
3222              }              }
     chptr = strchr(*arg, '=');  
3223    
3224      if (i < line->numElements) {      if (i < line->numElements && doreplace) {
3225   /* replace */   /* direct replacement */
3226   free(line->elements[i].item);   free(line->elements[i].item);
3227   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("");  
  }  
3228    
3229   free(rootLine->elements[1].item);      } else if (useRoot && !strncmp(*arg, "root=/dev/", 10)) {
3230   rootLine->elements[1].item = strdup(chptr + 1);   /* root= replacement */
3231      } else {   rootLine = getLineByType(LT_ROOT, entry->lines);
3232   /* append */   if (rootLine) {
3233   line->elements = realloc(line->elements,      free(rootLine->elements[1].item);
3234   (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(" ");  
3235   } else {   } else {
3236      /* First thing on this line; treat a bit differently. Note      rootLine = addLine(entry, cfg->cfi, LT_ROOT,
3237         this is only possible if we've added a LT_KERNELARGS         cfg->secondaryIndent, *arg + 5);
        entry */  
     line->elements[line->numElements].indent = strdup("");  
3238   }   }
3239        }
3240    
3241   line->numElements++;      else {
3242     /* insert/append */
3243     insertElement(line, *arg, i, cfg->cfi);
3244     usedElements = realloc(usedElements, line->numElements *
3245           sizeof(*usedElements));
3246     memmove(&usedElements[i + 1], &usedElements[i],
3247     line->numElements - i - 1);
3248     usedElements[i] = 1;
3249    
3250   /* if we updated a root= here even though there is a   /* if we updated a root= here even though there is a
3251     LT_ROOT available we need to remove the LT_ROOT entry     LT_ROOT available we need to remove the LT_ROOT entry
3252     (this will happen if we switch from a device to a label) */     (this will happen if we switch from a device to a label) */
3253   if (useRoot && !strncmp(*arg, "root=", 5)) {   if (useRoot && !strncmp(*arg, "root=", 5)) {
3254      rootLine = entry->lines;      rootLine = getLineByType(LT_ROOT, entry->lines);
3255      while (rootLine && rootLine->type != LT_ROOT)      if (rootLine)
  rootLine = rootLine->next;  
     if (rootLine) {  
3256   removeLine(entry, rootLine);   removeLine(entry, rootLine);
     }  
3257   }   }
3258      }      }
             k++;  
3259   }   }
3260    
3261          free(usedElements);          free(usedElements);
3262    
  /* 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? */  
3263   for (arg = oldArgs; *arg; arg++) {   for (arg = oldArgs; *arg; arg++) {
3264      for (i = firstElement; i < line->numElements; i++)      for (i = firstElement; i < line->numElements; i++) {
3265   if (!argMatch(line->elements[i].item, *arg))   if (multibootArgs && cfg->cfi->mbConcatArgs &&
3266        !strcmp(line->elements[i].item, "--"))
3267        /* reached the end of hyper args, stop here */
3268        break;
3269     if (!argMatch(line->elements[i].item, *arg)) {
3270        removeElement(line, i);
3271      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;  
3272   }   }
3273        }
3274   free(line->elements[i].item);      /* handle removing LT_ROOT line too */
3275        if (useRoot && !strncmp(*arg, "root=", 5)) {
3276   for (j = i + 1; j < line->numElements; j++)   rootLine = getLineByType(LT_ROOT, entry->lines);
3277      line->elements[j - 1] = line->elements[j];   if (rootLine)
3278        removeLine(entry, rootLine);
  line->numElements--;  
3279      }      }
3280   }   }
3281    
# Line 1760  int updateActualImage(struct grubConfig Line 3286  int updateActualImage(struct grubConfig
3286   }   }
3287      }      }
3288    
     free(usedArgs);  
3289      free(newArgs);      free(newArgs);
3290      free(oldArgs);      free(oldArgs);
3291    
# Line 1786  int updateImage(struct grubConfig * cfg, Line 3311  int updateImage(struct grubConfig * cfg,
3311      return rc;      return rc;
3312  }  }
3313    
3314    int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
3315     const char * image, const char * prefix, const char * initrd,
3316     const char * title) {
3317        struct singleEntry * entry;
3318        struct singleLine * line, * kernelLine, *endLine = NULL;
3319        int index = 0;
3320    
3321        if (!image) return 0;
3322    
3323        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3324            kernelLine = getLineByType(LT_MBMODULE, entry->lines);
3325            if (!kernelLine) continue;
3326    
3327     /* if title is supplied, the entry's title must match it. */
3328     if (title) {
3329        char *linetitle;
3330    
3331        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3332        if (!line)
3333     continue;
3334    
3335        linetitle = extractTitle(line);
3336        if (!linetitle)
3337     continue;
3338        if (strcmp(title, linetitle)) {
3339     free(linetitle);
3340     continue;
3341        }
3342        free(linetitle);
3343     }
3344    
3345            if (prefix) {
3346                int prefixLen = strlen(prefix);
3347                if (!strncmp(initrd, prefix, prefixLen))
3348                    initrd += prefixLen;
3349            }
3350     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3351     if (endLine)
3352        removeLine(entry, endLine);
3353            line = addLine(entry, cfg->cfi, preferredLineType(LT_MBMODULE,cfg->cfi),
3354     kernelLine->indent, initrd);
3355            if (!line)
3356        return 1;
3357     if (endLine) {
3358        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3359                if (!line)
3360     return 1;
3361     }
3362    
3363            break;
3364        }
3365    
3366        return 0;
3367    }
3368    
3369    int updateInitrd(struct grubConfig * cfg, const char * image,
3370                     const char * prefix, const char * initrd, const char * title) {
3371        struct singleEntry * entry;
3372        struct singleLine * line, * kernelLine, *endLine = NULL;
3373        int index = 0;
3374    
3375        if (!image) return 0;
3376    
3377        for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3378            kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3379            if (!kernelLine) continue;
3380    
3381     /* if title is supplied, the entry's title must match it. */
3382     if (title) {
3383        char *linetitle;
3384    
3385        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3386        if (!line)
3387     continue;
3388    
3389        linetitle = extractTitle(line);
3390        if (!linetitle)
3391     continue;
3392        if (strcmp(title, linetitle)) {
3393     free(linetitle);
3394     continue;
3395        }
3396        free(linetitle);
3397     }
3398    
3399            line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
3400            if (line)
3401                removeLine(entry, line);
3402            if (prefix) {
3403                int prefixLen = strlen(prefix);
3404                if (!strncmp(initrd, prefix, prefixLen))
3405                    initrd += prefixLen;
3406            }
3407     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3408     if (endLine)
3409        removeLine(entry, endLine);
3410     enum lineType_e lt;
3411     switch(kernelLine->type) {
3412        case LT_KERNEL:
3413            lt = LT_INITRD;
3414     break;
3415        case LT_KERNEL_EFI:
3416            lt = LT_INITRD_EFI;
3417     break;
3418        case LT_KERNEL_16:
3419            lt = LT_INITRD_16;
3420     break;
3421        default:
3422            lt = preferredLineType(LT_INITRD, cfg->cfi);
3423     }
3424            line = addLine(entry, cfg->cfi, lt, kernelLine->indent, initrd);
3425            if (!line)
3426        return 1;
3427     if (endLine) {
3428        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3429                if (!line)
3430     return 1;
3431     }
3432    
3433            break;
3434        }
3435    
3436        return 0;
3437    }
3438    
3439  int checkDeviceBootloader(const char * device, const unsigned char * boot) {  int checkDeviceBootloader(const char * device, const unsigned char * boot) {
3440      int fd;      int fd;
3441      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 1809  int checkDeviceBootloader(const char * d Line 3459  int checkDeviceBootloader(const char * d
3459      if (memcmp(boot, bootSect, 3))      if (memcmp(boot, bootSect, 3))
3460   return 0;   return 0;
3461    
3462      if (boot[1] == 0xeb) {      if (boot[1] == JMP_SHORT_OPCODE) {
3463   offset = boot[2] + 2;   offset = boot[2] + 2;
3464      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {
3465   offset = (boot[3] << 8) + boot[2] + 2;   offset = (boot[3] << 8) + boot[2] + 2;
3466      } else if (boot[0] == 0xeb) {      } else if (boot[0] == JMP_SHORT_OPCODE) {
3467   offset = boot[1] + 2;        offset = boot[1] + 2;
3468            /*
3469     * it looks like grub, when copying stage1 into the mbr, patches stage1
3470     * right after the JMP location, replacing other instructions such as
3471     * JMPs for NOOPs. So, relax the check a little bit by skipping those
3472     * different bytes.
3473     */
3474          if ((bootSect[offset + 1] == NOOP_OPCODE)
3475      && (bootSect[offset + 2] == NOOP_OPCODE)) {
3476     offset = offset + 3;
3477          }
3478      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {
3479   offset = (boot[2] << 8) + boot[1] + 2;   offset = (boot[2] << 8) + boot[1] + 2;
3480      } else {      } else {
# Line 1956  int checkForLilo(struct grubConfig * con Line 3616  int checkForLilo(struct grubConfig * con
3616      return checkDeviceBootloader(line->elements[1].item, boot);      return checkDeviceBootloader(line->elements[1].item, boot);
3617  }  }
3618    
3619    int checkForGrub2(struct grubConfig * config) {
3620        if (!access("/etc/grub.d/", R_OK))
3621     return 2;
3622    
3623        return 1;
3624    }
3625    
3626  int checkForGrub(struct grubConfig * config) {  int checkForGrub(struct grubConfig * config) {
3627      int fd;      int fd;
3628      unsigned char bootSect[512];      unsigned char bootSect[512];
3629      char * boot;      char * boot;
3630        int onSuse = isSuseSystem();
3631    
3632      if (parseSysconfigGrub(NULL, &boot))  
3633   return 0;      if (onSuse) {
3634     if (parseSuseGrubConf(NULL, &boot))
3635        return 0;
3636        } else {
3637     if (parseSysconfigGrub(NULL, &boot))
3638        return 0;
3639        }
3640    
3641      /* assume grub is not installed -- not an error condition */      /* assume grub is not installed -- not an error condition */
3642      if (!boot)      if (!boot)
# Line 1976  int checkForGrub(struct grubConfig * con Line 3650  int checkForGrub(struct grubConfig * con
3650      if (read(fd, bootSect, 512) != 512) {      if (read(fd, bootSect, 512) != 512) {
3651   fprintf(stderr, _("grubby: unable to read %s: %s\n"),   fprintf(stderr, _("grubby: unable to read %s: %s\n"),
3652   "/boot/grub/stage1", strerror(errno));   "/boot/grub/stage1", strerror(errno));
3653     close(fd);
3654     return 1;
3655        }
3656        close(fd);
3657    
3658        /* The more elaborate checks do not work on SuSE. The checks done
3659         * seem to be reasonble (at least for now), so just return success
3660         */
3661        if (onSuse)
3662     return 2;
3663    
3664        return checkDeviceBootloader(boot, bootSect);
3665    }
3666    
3667    int checkForExtLinux(struct grubConfig * config) {
3668        int fd;
3669        unsigned char bootSect[512];
3670        char * boot;
3671        char executable[] = "/boot/extlinux/extlinux";
3672    
3673        printf("entered: checkForExtLinux()\n");
3674    
3675        if (parseSysconfigGrub(NULL, &boot))
3676     return 0;
3677    
3678        /* assume grub is not installed -- not an error condition */
3679        if (!boot)
3680     return 0;
3681    
3682        fd = open(executable, O_RDONLY);
3683        if (fd < 0)
3684     /* this doesn't exist if grub hasn't been installed */
3685     return 0;
3686    
3687        if (read(fd, bootSect, 512) != 512) {
3688     fprintf(stderr, _("grubby: unable to read %s: %s\n"),
3689     executable, strerror(errno));
3690   return 1;   return 1;
3691      }      }
3692      close(fd);      close(fd);
# Line 1983  int checkForGrub(struct grubConfig * con Line 3694  int checkForGrub(struct grubConfig * con
3694      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3695  }  }
3696    
3697    int checkForYaboot(struct grubConfig * config) {
3698        /*
3699         * This is a simplistic check that we consider good enough for own puporses
3700         *
3701         * If we were to properly check if yaboot is *installed* we'd need to:
3702         * 1) get the system boot device (LT_BOOT)
3703         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3704         *    the content on the boot device
3705         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3706         * 4) check again if binary and boot device contents match
3707         */
3708        if (!access("/etc/yaboot.conf", R_OK))
3709     return 2;
3710    
3711        return 1;
3712    }
3713    
3714    int checkForElilo(struct grubConfig * config) {
3715        if (!access("/etc/elilo.conf", R_OK))
3716     return 2;
3717    
3718        return 1;
3719    }
3720    
3721  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3722      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3723    
# Line 1994  static char * getRootSpecifier(char * st Line 3729  static char * getRootSpecifier(char * st
3729      return rootspec;      return rootspec;
3730  }  }
3731    
3732    static char * getInitrdVal(struct grubConfig * config,
3733       const char * prefix, struct singleLine *tmplLine,
3734       const char * newKernelInitrd,
3735       const char ** extraInitrds, int extraInitrdCount)
3736    {
3737        char *initrdVal, *end;
3738        int i;
3739        size_t totalSize;
3740        size_t prefixLen;
3741        char separatorChar;
3742    
3743        prefixLen = strlen(prefix);
3744        totalSize = strlen(newKernelInitrd) - prefixLen + 1 /* \0 */;
3745    
3746        for (i = 0; i < extraInitrdCount; i++) {
3747     totalSize += sizeof(separatorChar);
3748     totalSize += strlen(extraInitrds[i]) - prefixLen;
3749        }
3750    
3751        initrdVal = end = malloc(totalSize);
3752    
3753        end = stpcpy (end, newKernelInitrd + prefixLen);
3754    
3755        separatorChar = getKeywordByType(LT_INITRD, config->cfi)->separatorChar;
3756        for (i = 0; i < extraInitrdCount; i++) {
3757     const char *extraInitrd;
3758     int j;
3759    
3760     extraInitrd = extraInitrds[i] + prefixLen;
3761     /* Don't add entries that are already there */
3762     if (tmplLine != NULL) {
3763        for (j = 2; j < tmplLine->numElements; j++)
3764     if (strcmp(extraInitrd, tmplLine->elements[j].item) == 0)
3765        break;
3766    
3767        if (j != tmplLine->numElements)
3768     continue;
3769     }
3770    
3771     *end++ = separatorChar;
3772     end = stpcpy(end, extraInitrd);
3773        }
3774    
3775        return initrdVal;
3776    }
3777    
3778  int addNewKernel(struct grubConfig * config, struct singleEntry * template,  int addNewKernel(struct grubConfig * config, struct singleEntry * template,
3779           const char * prefix,           const char * prefix,
3780   char * newKernelPath, char * newKernelTitle,   const char * newKernelPath, const char * newKernelTitle,
3781   char * newKernelArgs, char * newKernelInitrd,   const char * newKernelArgs, const char * newKernelInitrd,
3782                   char * newMBKernel, char * newMBKernelArgs) {   const char ** extraInitrds, int extraInitrdCount,
3783                     const char * newMBKernel, const char * newMBKernelArgs,
3784     const char * newDevTreePath) {
3785      struct singleEntry * new;      struct singleEntry * new;
3786      struct singleLine * newLine = NULL, * tmplLine = NULL, * lastLine = NULL;      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;
3787      int needs;      int needs;
     char * indent = NULL;  
     char * rootspec = NULL;  
3788      char * chptr;      char * chptr;
     int i;  
     enum lineType_e type;  
3789    
3790      if (!newKernelPath) return 0;      if (!newKernelPath) return 0;
3791    
# Line 2036  int addNewKernel(struct grubConfig * con Line 3815  int addNewKernel(struct grubConfig * con
3815      config->entries = new;      config->entries = new;
3816    
3817      /* copy/update from the template */      /* copy/update from the template */
3818      needs = KERNEL_KERNEL | KERNEL_INITRD | KERNEL_TITLE;      needs = NEED_KERNEL | NEED_TITLE;
3819        if (newKernelInitrd)
3820     needs |= NEED_INITRD;
3821      if (newMBKernel) {      if (newMBKernel) {
3822          needs |= KERNEL_MB;          needs |= NEED_MB;
3823          new->multiboot = 1;          new->multiboot = 1;
3824      }      }
3825        if (newDevTreePath && getKeywordByType(LT_DEVTREE, config->cfi))
3826     needs |= NEED_DEVTREE;
3827    
3828      if (template) {      if (template) {
3829   for (tmplLine = template->lines; tmplLine; tmplLine = tmplLine->next) {   for (masterLine = template->lines;
3830      /* remember the indention level; we may need it for new lines */       masterLine && (tmplLine = lineDup(masterLine));
3831      if (tmplLine->numElements)       lineFree(tmplLine), masterLine = masterLine->next)
3832   indent = tmplLine->indent;   {
3833        dbgPrintf("addNewKernel processing %d\n", tmplLine->type);
3834    
3835      /* skip comments */      /* skip comments */
3836      chptr = tmplLine->indent;      chptr = tmplLine->indent;
3837      while (*chptr && isspace(*chptr)) chptr++;      while (*chptr && isspace(*chptr)) chptr++;
3838      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3839    
3840      /* we don't need an initrd here */      if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) {
3841      if (tmplLine->type == LT_INITRD && !newKernelInitrd) continue;   if (!template->multiboot && (needs & NEED_MB)) {
3842        /* it's not a multiboot template and this is the kernel
3843              if (tmplLine->type == LT_KERNEL &&       * line.  Try to be intelligent about inserting the
3844                  !template->multiboot && (needs & KERNEL_MB)) {       * hypervisor at the same time.
3845                  struct singleLine *l;       */
3846                  needs &= ~ KERNEL_MB;      if (config->cfi->mbHyperFirst) {
3847     /* insert the hypervisor first */
3848                  l = addLine(new, config->cfi, LT_KERNEL,   newLine = addLine(new, config->cfi, LT_HYPER,
3849                                    config->secondaryIndent,    tmplLine->indent,
3850                                    newMBKernel + strlen(prefix));    newMBKernel + strlen(prefix));
3851                     /* set up for adding the kernel line */
3852                  tmplLine = lastLine;   free(tmplLine->indent);
3853                  if (!new->lines) {   tmplLine->indent = strdup(config->secondaryIndent);
3854                      new->lines = l;   needs &= ~NEED_MB;
3855                  } else {      }
3856                      newLine->next = l;      if (needs & NEED_KERNEL) {
3857                      newLine = l;   /* use addLineTmpl to preserve line elements,
3858                  }   * otherwise we could just call addLine.  Unfortunately
3859                  continue;   * this means making some changes to the template
3860              } else if (tmplLine->type == LT_KERNEL &&   * such as the indent change above and the type
3861                         template->multiboot && !new->multiboot) {   * change below.
3862                  continue; /* don't need multiboot kernel here */   */
3863              }   struct keywordTypes * mbm_kw =
3864        getKeywordByType(LT_MBMODULE, config->cfi);
3865      if (!new->lines) {   if (mbm_kw) {
3866   newLine = malloc(sizeof(*newLine));      tmplLine->type = LT_MBMODULE;
3867   new->lines = newLine;      free(tmplLine->elements[0].item);
3868      } else {      tmplLine->elements[0].item = strdup(mbm_kw->key);
3869   newLine->next = malloc(sizeof(*newLine));   }
3870   newLine = newLine->next;   newLine = addLineTmpl(new, tmplLine, newLine,
3871      }        newKernelPath + strlen(prefix), config->cfi);
3872     needs &= ~NEED_KERNEL;
3873        }
3874        if (needs & NEED_MB) { /* !mbHyperFirst */
3875     newLine = addLine(new, config->cfi, LT_HYPER,
3876      config->secondaryIndent,
3877      newMBKernel + strlen(prefix));
3878     needs &= ~NEED_MB;
3879        }
3880     } else if (needs & NEED_KERNEL) {
3881        newLine = addLineTmpl(new, tmplLine, newLine,
3882      newKernelPath + strlen(prefix), config->cfi);
3883        needs &= ~NEED_KERNEL;
3884     }
3885    
3886        } else if (tmplLine->type == LT_HYPER &&
3887           tmplLine->numElements >= 2) {
3888     if (needs & NEED_MB) {
3889        newLine = addLineTmpl(new, tmplLine, newLine,
3890      newMBKernel + strlen(prefix), config->cfi);
3891        needs &= ~NEED_MB;
3892     }
3893    
3894      newLine->indent = strdup(tmplLine->indent);      } else if (tmplLine->type == LT_MBMODULE &&
3895      newLine->next = NULL;         tmplLine->numElements >= 2) {
3896      newLine->type = tmplLine->type;   if (new->multiboot) {
3897      newLine->numElements = tmplLine->numElements;      if (needs & NEED_KERNEL) {
3898      newLine->elements = malloc(sizeof(*newLine->elements) *   newLine = addLineTmpl(new, tmplLine, newLine,
3899      newLine->numElements);        newKernelPath +
3900      for (i = 0; i < newLine->numElements; i++) {        strlen(prefix), config->cfi);
3901   newLine->elements[i].item = strdup(tmplLine->elements[i].item);   needs &= ~NEED_KERNEL;
3902   newLine->elements[i].indent =      } else if (config->cfi->mbInitRdIsModule &&
3903   strdup(tmplLine->elements[i].indent);         (needs & NEED_INITRD)) {
3904      }   char *initrdVal;
3905     initrdVal = getInitrdVal(config, prefix, tmplLine,
3906              lastLine = tmplLine;   newKernelInitrd, extraInitrds,
3907      if (tmplLine->type == LT_KERNEL && tmplLine->numElements >= 2) {   extraInitrdCount);
3908                  char * repl;   newLine = addLineTmpl(new, tmplLine, newLine,
3909                  if (!template->multiboot) {        initrdVal, config->cfi);
3910                      needs &= ~KERNEL_KERNEL;   free(initrdVal);
3911                      repl = newKernelPath;   needs &= ~NEED_INITRD;
3912                  } else {      }
3913                      needs &= ~KERNEL_MB;   } else if (needs & NEED_KERNEL) {
3914                      repl = newMBKernel;      /* template is multi but new is not,
3915                  }       * insert the kernel in the first module slot
3916                  if (new->multiboot && !template->multiboot) {       */
3917                      free(newLine->elements[0].item);      tmplLine->type = preferredLineType(LT_KERNEL, config->cfi);
3918                      newLine->elements[0].item = strdup("module");      free(tmplLine->elements[0].item);
3919                      newLine->type = LT_MBMODULE;      tmplLine->elements[0].item =
3920                  }   strdup(getKeywordByType(tmplLine->type,
3921   free(newLine->elements[1].item);   config->cfi)->key);
3922                  rootspec = getRootSpecifier(tmplLine->elements[1].item);      newLine = addLineTmpl(new, tmplLine, newLine,
3923                  if (rootspec != NULL) {    newKernelPath + strlen(prefix),
3924                      newLine->elements[1].item = sdupprintf("%s%s",    config->cfi);
3925                                                             rootspec,      needs &= ~NEED_KERNEL;
3926                                                             repl +   } else if (needs & NEED_INITRD) {
3927                                                             strlen(prefix));      char *initrdVal;
3928                  } else {      /* template is multi but new is not,
3929                      newLine->elements[1].item = strdup(repl +       * insert the initrd in the second module slot
3930                                                         strlen(prefix));       */
3931                  }      tmplLine->type = preferredLineType(LT_INITRD, config->cfi);
3932              } else if (tmplLine->type == LT_MBMODULE &&      free(tmplLine->elements[0].item);
3933                         tmplLine->numElements >= 2 && (needs & KERNEL_KERNEL)) {      tmplLine->elements[0].item =
3934                  needs &= ~KERNEL_KERNEL;   strdup(getKeywordByType(tmplLine->type,
3935                  if (!new->multiboot && template->multiboot) {   config->cfi)->key);
3936                      free(newLine->elements[0].item);      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3937                      newLine->elements[0].item = strdup("kernel");      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3938                      newLine->type = LT_KERNEL;      free(initrdVal);
3939                  }      needs &= ~NEED_INITRD;
3940   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;  
3941    
3942   for (i = 1; i < newLine->numElements; i++) {      } else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) {
3943      free(newLine->elements[i].item);   if (needs & NEED_INITRD &&
3944      free(newLine->elements[i].indent);      new->multiboot && !template->multiboot &&
3945        config->cfi->mbInitRdIsModule) {
3946        /* make sure we don't insert the module initrd
3947         * before the module kernel... if we don't do it here,
3948         * it will be inserted following the template.
3949         */
3950        if (!needs & NEED_KERNEL) {
3951     char *initrdVal;
3952    
3953     initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3954     newLine = addLine(new, config->cfi, LT_MBMODULE,
3955      config->secondaryIndent,
3956      initrdVal);
3957     free(initrdVal);
3958     needs &= ~NEED_INITRD;
3959        }
3960     } else if (needs & NEED_INITRD) {
3961        char *initrdVal;
3962        initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3963        newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3964        free(initrdVal);
3965        needs &= ~NEED_INITRD;
3966   }   }
3967    
3968   newLine->elements[1].item = strdup(newKernelTitle);      } else if (tmplLine->type == LT_MENUENTRY &&
3969   newLine->elements[1].indent = strdup("");         (needs & NEED_TITLE)) {
3970   newLine->numElements = 2;   requote(tmplLine, config->cfi);
3971     char *nkt = malloc(strlen(newKernelTitle)+3);
3972     strcpy(nkt, "'");
3973     strcat(nkt, newKernelTitle);
3974     strcat(nkt, "'");
3975     newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi);
3976     free(nkt);
3977     needs &= ~NEED_TITLE;
3978      } else if (tmplLine->type == LT_TITLE &&      } else if (tmplLine->type == LT_TITLE &&
3979                         config->cfi->titleBracketed &&         (needs & NEED_TITLE)) {
3980                         tmplLine->numElements == 1) {   if (tmplLine->numElements >= 2) {
3981                  needs &= ~KERNEL_TITLE;      newLine = addLineTmpl(new, tmplLine, newLine,
3982                  free(newLine->elements[0].item);    newKernelTitle, config->cfi);
3983                  free(newLine->elements[0].indent);      needs &= ~NEED_TITLE;
3984                  newLine->elements = malloc(sizeof(*newLine->elements) *   } else if (tmplLine->numElements == 1 &&
3985                                             newLine->numElements);     config->cfi->titleBracketed) {
3986        /* addLineTmpl doesn't handle titleBracketed */
3987                  newLine->elements[0].item = malloc(strlen(newKernelTitle) + 3);      newLine = addLine(new, config->cfi, LT_TITLE,
3988                  sprintf(newLine->elements[0].item, "[%s]", newKernelTitle);        tmplLine->indent, newKernelTitle);
3989                  newLine->elements[0].indent = strdup("");      needs &= ~NEED_TITLE;
3990                  newLine->numElements = 1;   }
3991              }      } else if (tmplLine->type == LT_ECHO) {
3992        requote(tmplLine, config->cfi);
3993        static const char *prefix = "'Loading ";
3994        if (tmplLine->numElements > 1 &&
3995        strstr(tmplLine->elements[1].item, prefix) &&
3996        masterLine->next &&
3997        iskernel(masterLine->next->type)) {
3998     char *newTitle = malloc(strlen(prefix) +
3999     strlen(newKernelTitle) + 2);
4000    
4001     strcpy(newTitle, prefix);
4002     strcat(newTitle, newKernelTitle);
4003     strcat(newTitle, "'");
4004     newLine = addLine(new, config->cfi, LT_ECHO,
4005     tmplLine->indent, newTitle);
4006     free(newTitle);
4007        } else {
4008     /* pass through other lines from the template */
4009     newLine = addLineTmpl(new, tmplLine, newLine, NULL,
4010     config->cfi);
4011        }
4012        } else if (tmplLine->type == LT_DEVTREE &&
4013           tmplLine->numElements == 2 && newDevTreePath) {
4014            newLine = addLineTmpl(new, tmplLine, newLine,
4015          newDevTreePath + strlen(prefix),
4016          config->cfi);
4017     needs &= ~NEED_DEVTREE;
4018        } else if (tmplLine->type == LT_ENTRY_END && needs & NEED_DEVTREE) {
4019     const char *ndtp = newDevTreePath;
4020     if (!strncmp(newDevTreePath, prefix, strlen(prefix)))
4021        ndtp += strlen(prefix);
4022     newLine = addLine(new, config->cfi, LT_DEVTREE,
4023      config->secondaryIndent,
4024      ndtp);
4025     needs &= ~NEED_DEVTREE;
4026     newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
4027        } else {
4028     /* pass through other lines from the template */
4029     newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
4030        }
4031   }   }
4032    
4033      } else {      } else {
4034   for (i = 0; config->cfi->keywords[i].key; i++) {   /* don't have a template, so start the entry with the
4035      if ((config->cfi->keywords[i].type == config->cfi->entrySeparator) || (config->cfi->keywords[i].type == LT_OTHER))   * appropriate starting line
4036     */
4037     switch (config->cfi->entryStart) {
4038        case LT_KERNEL:
4039        case LT_KERNEL_EFI:
4040        case LT_KERNEL_16:
4041     if (new->multiboot && config->cfi->mbHyperFirst) {
4042        /* fall through to LT_HYPER */
4043     } else {
4044        newLine = addLine(new, config->cfi,
4045              preferredLineType(LT_KERNEL, config->cfi),
4046          config->primaryIndent,
4047          newKernelPath + strlen(prefix));
4048        needs &= ~NEED_KERNEL;
4049        break;
4050     }
4051    
4052        case LT_HYPER:
4053     newLine = addLine(new, config->cfi, LT_HYPER,
4054      config->primaryIndent,
4055      newMBKernel + strlen(prefix));
4056     needs &= ~NEED_MB;
4057   break;   break;
         }  
4058    
4059   switch (config->cfi->keywords[i].type) {      case LT_MENUENTRY: {
4060      case LT_KERNEL:  needs &= ~KERNEL_KERNEL,   char *nkt = malloc(strlen(newKernelTitle)+3);
4061       chptr = newKernelPath + strlen(prefix);   strcpy(nkt, "'");
4062       type = LT_KERNEL; break;   strcat(nkt, newKernelTitle);
4063      case LT_TITLE:   needs &= ~KERNEL_TITLE, chptr = newKernelTitle;   strcat(nkt, "'");
4064       type = LT_TITLE; break;          newLine = addLine(new, config->cfi, LT_MENUENTRY,
4065      default:        config->primaryIndent, nkt);
4066                  /* zipl strikes again */   free(nkt);
4067                  if (config->cfi->titleBracketed) {   needs &= ~NEED_TITLE;
4068                      needs &= ~KERNEL_TITLE;   needs |= NEED_END;
4069                      chptr = newKernelTitle;   break;
4070                      type = LT_TITLE;      }
4071                      break;      case LT_TITLE:
4072                  } else {   if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above)
4073                      abort();   char * templabel;
4074                  }   int x = 0, y = 0;
4075   }  
4076     templabel = strdup(newKernelTitle);
4077     while( templabel[x]){
4078     if( templabel[x] == ' ' ){
4079     y = x;
4080     while( templabel[y] ){
4081     templabel[y] = templabel[y+1];
4082     y++;
4083     }
4084     }
4085     x++;
4086     }
4087     newLine = addLine(new, config->cfi, LT_TITLE,
4088      config->primaryIndent, templabel);
4089     free(templabel);
4090     }else{
4091     newLine = addLine(new, config->cfi, LT_TITLE,
4092      config->primaryIndent, newKernelTitle);
4093     }
4094     needs &= ~NEED_TITLE;
4095     break;
4096    
4097   newLine = addLine(new, config->cfi, type, config->primaryIndent, chptr);      default:
4098   new->lines = newLine;   abort();
4099     }
4100      }      }
4101    
4102      if (new->multiboot) {      struct singleLine *endLine = NULL;
4103          if (needs & KERNEL_MB)      endLine = getLineByType(LT_ENTRY_END, new->lines);
4104              newLine = addLine(new, config->cfi, LT_KERNEL,      if (endLine) {
4105                                config->secondaryIndent,      removeLine(new, endLine);
4106                                newMBKernel + strlen(prefix));      needs |= NEED_END;
4107          if (needs & KERNEL_KERNEL)      }
4108              newLine = addLine(new, config->cfi, LT_MBMODULE,  
4109                                config->secondaryIndent,      /* add the remainder of the lines, i.e. those that either
4110                                newKernelPath + strlen(prefix));       * weren't present in the template, or in the case of no template,
4111          /* don't need to check for title as it's guaranteed to have been       * all the lines following the entryStart.
4112           * done as we only do multiboot with grub which uses title as       */
4113           * a separator */      if (needs & NEED_TITLE) {
4114          if (needs & KERNEL_INITRD && newKernelInitrd)   newLine = addLine(new, config->cfi, LT_TITLE,
4115              newLine = addLine(new, config->cfi, LT_MBMODULE,    config->secondaryIndent,
4116                                config->secondaryIndent,    newKernelTitle);
4117                                newKernelInitrd + strlen(prefix));   needs &= ~NEED_TITLE;
4118      } else {      }
4119          if (needs & KERNEL_KERNEL)      if ((needs & NEED_MB) && config->cfi->mbHyperFirst) {
4120              newLine = addLine(new, config->cfi, LT_KERNEL,   newLine = addLine(new, config->cfi, LT_HYPER,
4121                                config->secondaryIndent,    config->secondaryIndent,
4122                                newKernelPath + strlen(prefix));    newMBKernel + strlen(prefix));
4123          if (needs & KERNEL_TITLE)   needs &= ~NEED_MB;
4124              newLine = addLine(new, config->cfi, LT_TITLE,      }
4125                                config->secondaryIndent,      if (needs & NEED_KERNEL) {
4126                                newKernelTitle);   newLine = addLine(new, config->cfi,
4127          if (needs & KERNEL_INITRD && newKernelInitrd)    (new->multiboot && getKeywordByType(LT_MBMODULE,
4128              newLine = addLine(new, config->cfi, LT_INITRD,        config->cfi))
4129                                config->secondaryIndent,     ? LT_MBMODULE
4130                                newKernelInitrd + strlen(prefix));   : preferredLineType(LT_KERNEL, config->cfi),
4131      config->secondaryIndent,
4132      newKernelPath + strlen(prefix));
4133     needs &= ~NEED_KERNEL;
4134        }
4135        if (needs & NEED_MB) {
4136     newLine = addLine(new, config->cfi, LT_HYPER,
4137      config->secondaryIndent,
4138      newMBKernel + strlen(prefix));
4139     needs &= ~NEED_MB;
4140        }
4141        if (needs & NEED_INITRD) {
4142     char *initrdVal;
4143     initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);
4144     newLine = addLine(new, config->cfi,
4145      (new->multiboot && getKeywordByType(LT_MBMODULE,
4146          config->cfi))
4147       ? LT_MBMODULE
4148       : preferredLineType(LT_INITRD, config->cfi),
4149      config->secondaryIndent,
4150      initrdVal);
4151     free(initrdVal);
4152     needs &= ~NEED_INITRD;
4153        }
4154        if (needs & NEED_DEVTREE) {
4155     newLine = addLine(new, config->cfi, LT_DEVTREE,
4156      config->secondaryIndent,
4157      newDevTreePath);
4158     needs &= ~NEED_DEVTREE;
4159        }
4160    
4161        /* NEEDS_END must be last on bootloaders that need it... */
4162        if (needs & NEED_END) {
4163     newLine = addLine(new, config->cfi, LT_ENTRY_END,
4164     config->secondaryIndent, NULL);
4165     needs &= ~NEED_END;
4166        }
4167    
4168        if (needs) {
4169     printf(_("grubby: needs=%d, aborting\n"), needs);
4170     abort();
4171      }      }
4172    
4173      if (updateImage(config, "0", prefix, newKernelArgs, NULL,      if (updateImage(config, "0", prefix, newKernelArgs, NULL,
# Line 2274  int addNewKernel(struct grubConfig * con Line 4176  int addNewKernel(struct grubConfig * con
4176      return 0;      return 0;
4177  }  }
4178    
4179    static void traceback(int signum)
4180    {
4181        void *array[40];
4182        size_t size;
4183    
4184        signal(SIGSEGV, SIG_DFL);
4185        memset(array, '\0', sizeof (array));
4186        size = backtrace(array, 40);
4187    
4188        fprintf(stderr, "grubby received SIGSEGV!  Backtrace (%ld):\n",
4189                (unsigned long)size);
4190        backtrace_symbols_fd(array, size, STDERR_FILENO);
4191        exit(1);
4192    }
4193    
4194  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
4195      poptContext optCon;      poptContext optCon;
4196      char * grubConfig = NULL;      const char * grubConfig = NULL;
4197      char * outputFile = NULL;      char * outputFile = NULL;
4198      int arg = 0;      int arg = 0;
4199      int flags = 0;      int flags = 0;
4200      int badImageOkay = 0;      int badImageOkay = 0;
4201        int configureGrub2 = 0;
4202      int configureLilo = 0, configureELilo = 0, configureGrub = 0;      int configureLilo = 0, configureELilo = 0, configureGrub = 0;
4203      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;
4204        int configureExtLinux = 0;
4205      int bootloaderProbe = 0;      int bootloaderProbe = 0;
4206        int extraInitrdCount = 0;
4207      char * updateKernelPath = NULL;      char * updateKernelPath = NULL;
4208      char * newKernelPath = NULL;      char * newKernelPath = NULL;
4209      char * removeKernelPath = NULL;      char * removeKernelPath = NULL;
4210      char * newKernelArgs = NULL;      char * newKernelArgs = NULL;
4211      char * newKernelInitrd = NULL;      char * newKernelInitrd = NULL;
4212      char * newKernelTitle = NULL;      char * newKernelTitle = NULL;
4213      char * newKernelVersion = NULL;      char * newDevTreePath = NULL;
4214      char * newMBKernel = NULL;      char * newMBKernel = NULL;
4215      char * newMBKernelArgs = NULL;      char * newMBKernelArgs = NULL;
4216      char * removeMBKernelArgs = NULL;      char * removeMBKernelArgs = NULL;
# Line 2299  int main(int argc, const char ** argv) { Line 4219  int main(int argc, const char ** argv) {
4219      char * defaultKernel = NULL;      char * defaultKernel = NULL;
4220      char * removeArgs = NULL;      char * removeArgs = NULL;
4221      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4222        char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4223        char * envPath = NULL;
4224      const char * chptr = NULL;      const char * chptr = NULL;
4225      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4226      struct grubConfig * config;      struct grubConfig * config;
4227      struct singleEntry * template = NULL;      struct singleEntry * template = NULL;
4228      int copyDefault = 0, makeDefault = 0;      int copyDefault = 0, makeDefault = 0;
4229      int displayDefault = 0;      int displayDefault = 0;
4230        int displayDefaultIndex = 0;
4231        int displayDefaultTitle = 0;
4232        int defaultIndex = -1;
4233      struct poptOption options[] = {      struct poptOption options[] = {
4234   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
4235      _("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 4247  int main(int argc, const char ** argv) {
4247   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4248      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
4249      _("bootfs") },      _("bootfs") },
4250  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4251   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
4252      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
4253  #endif  #endif
4254   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
4255      _("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 4260  int main(int argc, const char ** argv) {
4260        "the kernel referenced by the default image does not exist, "        "the kernel referenced by the default image does not exist, "
4261        "the first linux entry whose kernel does exist is used as the "        "the first linux entry whose kernel does exist is used as the "
4262        "template"), NULL },        "template"), NULL },
4263     { "debug", 0, 0, &debug, 0,
4264        _("print debugging information for failures") },
4265   { "default-kernel", 0, 0, &displayDefault, 0,   { "default-kernel", 0, 0, &displayDefault, 0,
4266      _("display the path of the default kernel") },      _("display the path of the default kernel") },
4267     { "default-index", 0, 0, &displayDefaultIndex, 0,
4268        _("display the index of the default kernel") },
4269     { "default-title", 0, 0, &displayDefaultTitle, 0,
4270        _("display the title of the default kernel") },
4271     { "devtree", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4272        _("device tree file for new stanza"), _("dtb-path") },
4273     { "devtreedir", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4274        _("device tree directory for new stanza"), _("dtb-path") },
4275   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4276      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4277     { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4278        _("force grub2 stanzas to use efi") },
4279     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4280        _("path for environment data"),
4281        _("path") },
4282     { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4283        _("configure extlinux bootloader (from syslinux)") },
4284   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
4285      _("configure grub bootloader") },      _("configure grub bootloader") },
4286     { "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,
4287        _("configure grub2 bootloader") },
4288   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,
4289      _("display boot information for specified kernel"),      _("display boot information for specified kernel"),
4290      _("kernel-path") },      _("kernel-path") },
4291   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4292      _("initrd image for the new kernel"), _("initrd-path") },      _("initrd image for the new kernel"), _("initrd-path") },
4293     { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4294        _("auxiliary initrd image for things other than the new kernel"), _("initrd-path") },
4295   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4296      _("configure lilo bootloader") },      _("configure lilo bootloader") },
4297   { "make-default", 0, 0, &makeDefault, 0,   { "make-default", 0, 0, &makeDefault, 0,
# Line 2365  int main(int argc, const char ** argv) { Line 4311  int main(int argc, const char ** argv) {
4311   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
4312      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
4313        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
4314     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4315        _("make the given entry index the default entry"),
4316        _("entry-index") },
4317   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4318      _("configure silo bootloader") },      _("configure silo bootloader") },
4319   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 2382  int main(int argc, const char ** argv) { Line 4331  int main(int argc, const char ** argv) {
4331   { 0, 0, 0, 0, 0 }   { 0, 0, 0, 0, 0 }
4332      };      };
4333    
4334        useextlinuxmenu=0;
4335    
4336        signal(SIGSEGV, traceback);
4337    
4338        int i = 0;
4339        for (int j = 1; j < argc; j++)
4340     i += strlen(argv[j]) + 1;
4341        saved_command_line = malloc(i);
4342        if (!saved_command_line) {
4343     fprintf(stderr, "grubby: %m\n");
4344     exit(1);
4345        }
4346        saved_command_line[0] = '\0';
4347        for (int j = 1; j < argc; j++) {
4348     strcat(saved_command_line, argv[j]);
4349     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4350        }
4351    
4352      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4353      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
4354    
# Line 2391  int main(int argc, const char ** argv) { Line 4358  int main(int argc, const char ** argv) {
4358      printf("grubby version %s\n", VERSION);      printf("grubby version %s\n", VERSION);
4359      exit(0);      exit(0);
4360      break;      break;
4361      case 'i':
4362        if (extraInitrdCount < MAX_EXTRA_INITRDS) {
4363         extraInitrds[extraInitrdCount++] = strdup(poptGetOptArg(optCon));
4364        } else {
4365     fprintf(stderr, _("grubby: extra initrd maximum is %d\n"), extraInitrdCount);
4366     return 1;
4367        }
4368        break;
4369   }   }
4370      }      }
4371    
# Line 2406  int main(int argc, const char ** argv) { Line 4381  int main(int argc, const char ** argv) {
4381   return 1;   return 1;
4382      }      }
4383    
4384      if ((configureLilo + configureGrub + configureELilo +      if ((configureLilo + configureGrub2 + configureGrub + configureELilo +
4385   configureYaboot + configureSilo + configureZipl) > 1) {   configureYaboot + configureSilo + configureZipl +
4386     configureExtLinux ) > 1) {
4387   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));
4388   return 1;   return 1;
4389      } else if (bootloaderProbe && grubConfig) {      } else if (bootloaderProbe && grubConfig) {
4390   fprintf(stderr,   fprintf(stderr,
4391      _("grubby: cannot specify config file with --bootloader-probe\n"));      _("grubby: cannot specify config file with --bootloader-probe\n"));
4392   return 1;   return 1;
4393        } else if (configureGrub2) {
4394     cfi = &grub2ConfigType;
4395     if (envPath)
4396        cfi->envFile = envPath;
4397      } else if (configureLilo) {      } else if (configureLilo) {
4398   cfi = &liloConfigType;   cfi = &liloConfigType;
4399      } else if (configureGrub) {      } else if (configureGrub) {
# Line 2426  int main(int argc, const char ** argv) { Line 4406  int main(int argc, const char ** argv) {
4406          cfi = &siloConfigType;          cfi = &siloConfigType;
4407      } else if (configureZipl) {      } else if (configureZipl) {
4408          cfi = &ziplConfigType;          cfi = &ziplConfigType;
4409        } else if (configureExtLinux) {
4410     cfi = &extlinuxConfigType;
4411     useextlinuxmenu=1;
4412      }      }
4413    
4414      if (!cfi) {      if (!cfi) {
4415            if (grub2FindConfig(&grub2ConfigType))
4416        cfi = &grub2ConfigType;
4417     else
4418        #ifdef __ia64__        #ifdef __ia64__
4419   cfi = &eliloConfigType;      cfi = &eliloConfigType;
4420        #elif __powerpc__        #elif __powerpc__
4421   cfi = &yabootConfigType;      cfi = &yabootConfigType;
4422        #elif __sparc__        #elif __sparc__
4423          cfi = &siloConfigType;              cfi = &siloConfigType;
4424        #elif __s390__        #elif __s390__
4425          cfi = &ziplConfigType;              cfi = &ziplConfigType;
4426        #elif __s390x__        #elif __s390x__
4427          cfi = &ziplConfigtype;              cfi = &ziplConfigtype;
4428        #else        #else
4429   cfi = &grubConfigType;      cfi = &grubConfigType;
4430        #endif        #endif
4431      }      }
4432    
4433      if (!grubConfig)      if (!grubConfig) {
4434   grubConfig = cfi->defaultConfig;   if (cfi->findConfig)
4435        grubConfig = cfi->findConfig(cfi);
4436     if (!grubConfig)
4437        grubConfig = cfi->defaultConfig;
4438        }
4439    
4440      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo ||
4441    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
4442    defaultKernel)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
4443        (defaultIndex >= 0))) {
4444   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
4445    "specified option"));    "specified option"));
4446   return 1;   return 1;
4447      }      }
4448    
4449      if ((displayDefault || kernelInfo) && (newKernelVersion || newKernelPath ||      if ((displayDefault || kernelInfo) && (newKernelPath ||
4450     removeKernelPath)) {     removeKernelPath)) {
4451   fprintf(stderr, _("grubby: --default-kernel and --info may not "   fprintf(stderr, _("grubby: --default-kernel and --info may not "
4452    "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 4456  int main(int argc, const char ** argv) {
4456      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
4457   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
4458   return 1;   return 1;
4459      } else if (!newKernelPath && (newKernelTitle  || newKernelInitrd ||      } else if (!newKernelPath && (copyDefault ||
4460    newKernelInitrd || copyDefault     ||    (newKernelInitrd && !updateKernelPath)||
4461    makeDefault)) {    makeDefault || extraInitrdCount > 0)) {
4462   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
4463   return 1;   return 1;
4464      }      }
# Line 2491  int main(int argc, const char ** argv) { Line 4482  int main(int argc, const char ** argv) {
4482   makeDefault = 1;   makeDefault = 1;
4483   defaultKernel = NULL;   defaultKernel = NULL;
4484      }      }
4485        else if (defaultKernel && (defaultIndex >= 0)) {
4486     fprintf(stderr, _("grubby: --set-default and --set-default-index "
4487      "may not be used together\n"));
4488     return 1;
4489        }
4490    
4491      if (!strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
4492   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
4493   "is used\n"));   "is used\n"));
4494   return 1;   return 1;
4495      }      }
4496    
4497      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
4498   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
4499          && !removeMBKernel) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
4500     && (defaultIndex == -1)) {
4501   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
4502   return 1;   return 1;
4503      }      }
# Line 2520  int main(int argc, const char ** argv) { Line 4517  int main(int argc, const char ** argv) {
4517   bootPrefix = "";   bootPrefix = "";
4518      }      }
4519    
4520        if (!cfi->mbAllowExtraInitRds &&
4521     extraInitrdCount > 0) {
4522     fprintf(stderr, _("grubby: %s doesn't allow multiple initrds\n"), cfi->defaultConfig);
4523     return 1;
4524        }
4525    
4526      if (bootloaderProbe) {      if (bootloaderProbe) {
4527   int lrc = 0, grc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4528   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4529    
4530     const char *grub2config = grub2FindConfig(&grub2ConfigType);
4531     if (grub2config) {
4532        gconfig = readConfig(grub2config, &grub2ConfigType);
4533        if (!gconfig)
4534     gr2c = 1;
4535        else
4536     gr2c = checkForGrub2(gconfig);
4537     }
4538    
4539   if (!access(grubConfigType.defaultConfig, F_OK)) {   const char *grubconfig = grubFindConfig(&grubConfigType);
4540      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);   if (!access(grubconfig, F_OK)) {
4541        gconfig = readConfig(grubconfig, &grubConfigType);
4542      if (!gconfig)      if (!gconfig)
4543   grc = 1;   grc = 1;
4544      else      else
# Line 2540  int main(int argc, const char ** argv) { Line 4553  int main(int argc, const char ** argv) {
4553   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4554   }   }
4555    
4556   if (lrc == 1 || grc == 1) return 1;   if (!access(eliloConfigType.defaultConfig, F_OK)) {
4557        econfig = readConfig(eliloConfigType.defaultConfig,
4558     &eliloConfigType);
4559        if (!econfig)
4560     erc = 1;
4561        else
4562     erc = checkForElilo(econfig);
4563     }
4564    
4565     if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4566        lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4567        if (!lconfig)
4568     extrc = 1;
4569        else
4570     extrc = checkForExtLinux(lconfig);
4571     }
4572    
4573    
4574     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4575        yconfig = readConfig(yabootConfigType.defaultConfig,
4576     &yabootConfigType);
4577        if (!yconfig)
4578     yrc = 1;
4579        else
4580     yrc = checkForYaboot(yconfig);
4581     }
4582    
4583     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4584     erc == 1)
4585        return 1;
4586    
4587   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4588     if (gr2c == 2) printf("grub2\n");
4589   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4590     if (extrc == 2) printf("extlinux\n");
4591     if (yrc == 2) printf("yaboot\n");
4592     if (erc == 2) printf("elilo\n");
4593    
4594   return 0;   return 0;
4595      }      }
4596    
4597        if (grubConfig == NULL) {
4598     printf("Could not find bootloader configuration file.\n");
4599     exit(1);
4600        }
4601    
4602      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4603      if (!config) return 1;      if (!config) return 1;
4604    
# Line 2557  int main(int argc, const char ** argv) { Line 4608  int main(int argc, const char ** argv) {
4608          char * rootspec;          char * rootspec;
4609    
4610   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4611     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4612     cfi->defaultIsSaved)
4613        config->defaultImage = 0;
4614   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4615   if (!entry) return 0;   if (!entry) return 0;
4616   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4617    
4618   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;  
4619   if (!line) return 0;   if (!line) return 0;
4620    
4621          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 2570  int main(int argc, const char ** argv) { Line 4623  int main(int argc, const char ** argv) {
4623                 ((rootspec != NULL) ? strlen(rootspec) : 0));                 ((rootspec != NULL) ? strlen(rootspec) : 0));
4624    
4625   return 0;   return 0;
4626    
4627        } else if (displayDefaultTitle) {
4628     struct singleLine * line;
4629     struct singleEntry * entry;
4630    
4631     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);
4636     if (!entry) return 0;
4637    
4638     if (!configureGrub2) {
4639      line = getLineByType(LT_TITLE, entry->lines);
4640      if (!line) return 0;
4641      printf("%s\n", line->elements[1].item);
4642    
4643     } else {
4644      char * title;
4645    
4646      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
4647      line = getLineByType(LT_MENUENTRY, entry->lines);
4648      if (!line) return 0;
4649      title = grub2ExtractTitle(line);
4650      if (title)
4651        printf("%s\n", title);
4652     }
4653     return 0;
4654    
4655        } else if (displayDefaultIndex) {
4656            if (config->defaultImage == -1) return 0;
4657     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4658     cfi->defaultIsSaved)
4659        config->defaultImage = 0;
4660            printf("%i\n", config->defaultImage);
4661            return 0;
4662    
4663      } else if (kernelInfo)      } else if (kernelInfo)
4664   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
4665    
# Line 2581  int main(int argc, const char ** argv) { Line 4671  int main(int argc, const char ** argv) {
4671      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4672      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4673      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4674      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4675      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4676      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4677                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
4678        if (updateKernelPath && newKernelInitrd) {
4679        if (newMBKernel) {
4680        if (addMBInitrd(config, newMBKernel, updateKernelPath,
4681     bootPrefix, newKernelInitrd,
4682     newKernelTitle))
4683        return 1;
4684        } else {
4685        if (updateInitrd(config, updateKernelPath, bootPrefix,
4686     newKernelInitrd, newKernelTitle))
4687     return 1;
4688        }
4689        }
4690      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
4691                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
4692                       newMBKernel, newMBKernelArgs)) return 1;                       (const char **)extraInitrds, extraInitrdCount,
4693                         newMBKernel, newMBKernelArgs, newDevTreePath)) return 1;
4694            
4695    
4696      if (numEntries(config) == 0) {      if (numEntries(config) == 0) {
# Line 2597  int main(int argc, const char ** argv) { Line 4700  int main(int argc, const char ** argv) {
4700      }      }
4701    
4702      if (!outputFile)      if (!outputFile)
4703   outputFile = grubConfig;   outputFile = (char *)grubConfig;
4704    
4705      return writeConfig(config, outputFile, bootPrefix);      return writeConfig(config, outputFile, bootPrefix);
4706  }  }

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