36 |
#include <signal.h> |
#include <signal.h> |
37 |
#include <blkid/blkid.h> |
#include <blkid/blkid.h> |
38 |
|
|
39 |
|
#ifndef DEBUG |
40 |
#define DEBUG 0 |
#define DEBUG 0 |
41 |
|
#endif |
42 |
|
|
43 |
#if DEBUG |
#if DEBUG |
44 |
#define dbgPrintf(format, args...) fprintf(stderr, format , ## args) |
#define dbgPrintf(format, args...) fprintf(stderr, format , ## args) |
58 |
}; |
}; |
59 |
|
|
60 |
enum lineType_e { |
enum lineType_e { |
61 |
LT_WHITESPACE = 1 << 0, |
LT_WHITESPACE = 1 << 0, |
62 |
LT_TITLE = 1 << 1, |
LT_TITLE = 1 << 1, |
63 |
LT_KERNEL = 1 << 2, |
LT_KERNEL = 1 << 2, |
64 |
LT_INITRD = 1 << 3, |
LT_INITRD = 1 << 3, |
65 |
LT_HYPER = 1 << 4, |
LT_HYPER = 1 << 4, |
66 |
LT_DEFAULT = 1 << 5, |
LT_DEFAULT = 1 << 5, |
67 |
LT_MBMODULE = 1 << 6, |
LT_MBMODULE = 1 << 6, |
68 |
LT_ROOT = 1 << 7, |
LT_ROOT = 1 << 7, |
69 |
LT_FALLBACK = 1 << 8, |
LT_FALLBACK = 1 << 8, |
70 |
LT_KERNELARGS = 1 << 9, |
LT_KERNELARGS = 1 << 9, |
71 |
LT_BOOT = 1 << 10, |
LT_BOOT = 1 << 10, |
72 |
LT_BOOTROOT = 1 << 11, |
LT_BOOTROOT = 1 << 11, |
73 |
LT_LBA = 1 << 12, |
LT_LBA = 1 << 12, |
74 |
LT_OTHER = 1 << 13, |
LT_OTHER = 1 << 13, |
75 |
LT_GENERIC = 1 << 14, |
LT_GENERIC = 1 << 14, |
76 |
LT_UNKNOWN = 1 << 15, |
LT_ECHO = 1 << 16, |
77 |
|
LT_MENUENTRY = 1 << 17, |
78 |
|
LT_ENTRY_END = 1 << 18, |
79 |
|
LT_SET_VARIABLE = 1 << 19, |
80 |
|
LT_UNKNOWN = 1 << 20, |
81 |
}; |
}; |
82 |
|
|
83 |
struct singleLine { |
struct singleLine { |
105 |
#define NEED_TITLE (1 << 2) |
#define NEED_TITLE (1 << 2) |
106 |
#define NEED_ARGS (1 << 3) |
#define NEED_ARGS (1 << 3) |
107 |
#define NEED_MB (1 << 4) |
#define NEED_MB (1 << 4) |
108 |
|
#define NEED_END (1 << 5) |
109 |
|
|
110 |
#define MAIN_DEFAULT (1 << 0) |
#define MAIN_DEFAULT (1 << 0) |
111 |
#define DEFAULT_SAVED -2 |
#define DEFAULT_SAVED -2 |
117 |
char separatorChar; |
char separatorChar; |
118 |
}; |
}; |
119 |
|
|
120 |
|
struct configFileInfo; |
121 |
|
|
122 |
|
typedef const char *(*findConfigFunc)(struct configFileInfo *); |
123 |
|
typedef const int (*writeLineFunc)(struct configFileInfo *, |
124 |
|
struct singleLine *line); |
125 |
|
|
126 |
struct configFileInfo { |
struct configFileInfo { |
127 |
char * defaultConfig; |
char * defaultConfig; |
128 |
|
findConfigFunc findConfig; |
129 |
|
writeLineFunc writeLine; |
130 |
struct keywordTypes * keywords; |
struct keywordTypes * keywords; |
131 |
int defaultIsIndex; |
int defaultIsIndex; |
132 |
|
int defaultIsVariable; |
133 |
int defaultSupportSaved; |
int defaultSupportSaved; |
134 |
enum lineType_e entrySeparator; |
enum lineType_e entryStart; |
135 |
|
enum lineType_e entryEnd; |
136 |
int needsBootPrefix; |
int needsBootPrefix; |
137 |
int argsInQuotes; |
int argsInQuotes; |
138 |
int maxTitleLength; |
int maxTitleLength; |
139 |
int titleBracketed; |
int titleBracketed; |
140 |
|
int titlePosition; |
141 |
int mbHyperFirst; |
int mbHyperFirst; |
142 |
int mbInitRdIsModule; |
int mbInitRdIsModule; |
143 |
int mbConcatArgs; |
int mbConcatArgs; |
157 |
}; |
}; |
158 |
|
|
159 |
struct configFileInfo grubConfigType = { |
struct configFileInfo grubConfigType = { |
160 |
"/boot/grub/grub.conf", /* defaultConfig */ |
.defaultConfig = "/boot/grub/grub.conf", |
161 |
grubKeywords, /* keywords */ |
.keywords = grubKeywords, |
162 |
1, /* defaultIsIndex */ |
.defaultIsIndex = 1, |
163 |
1, /* defaultSupportSaved */ |
.defaultSupportSaved = 1, |
164 |
LT_TITLE, /* entrySeparator */ |
.entryStart = LT_TITLE, |
165 |
1, /* needsBootPrefix */ |
.needsBootPrefix = 1, |
166 |
0, /* argsInQuotes */ |
.mbHyperFirst = 1, |
167 |
0, /* maxTitleLength */ |
.mbInitRdIsModule = 1, |
168 |
0, /* titleBracketed */ |
.mbAllowExtraInitRds = 1, |
169 |
1, /* mbHyperFirst */ |
}; |
170 |
1, /* mbInitRdIsModule */ |
|
171 |
0, /* mbConcatArgs */ |
struct keywordTypes grub2Keywords[] = { |
172 |
1, /* mbAllowExtraInitRds */ |
{ "menuentry", LT_MENUENTRY, ' ' }, |
173 |
|
{ "}", LT_ENTRY_END, ' ' }, |
174 |
|
{ "echo", LT_ECHO, ' ' }, |
175 |
|
{ "set", LT_SET_VARIABLE,' ', '=' }, |
176 |
|
{ "root", LT_BOOTROOT, ' ' }, |
177 |
|
{ "default", LT_DEFAULT, ' ' }, |
178 |
|
{ "fallback", LT_FALLBACK, ' ' }, |
179 |
|
{ "linux", LT_KERNEL, ' ' }, |
180 |
|
{ "initrd", LT_INITRD, ' ', ' ' }, |
181 |
|
{ "module", LT_MBMODULE, ' ' }, |
182 |
|
{ "kernel", LT_HYPER, ' ' }, |
183 |
|
{ NULL, 0, 0 }, |
184 |
|
}; |
185 |
|
|
186 |
|
const char *grub2FindConfig(struct configFileInfo *cfi) { |
187 |
|
static const char *configFiles[] = { |
188 |
|
"/boot/grub/grub-efi.cfg", |
189 |
|
"/boot/grub/grub.cfg", |
190 |
|
NULL |
191 |
|
}; |
192 |
|
static int i = -1; |
193 |
|
|
194 |
|
if (i == -1) { |
195 |
|
for (i = 0; configFiles[i] != NULL; i++) { |
196 |
|
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
197 |
|
if (!access(configFiles[i], R_OK)) { |
198 |
|
dbgPrintf("found\n"); |
199 |
|
return configFiles[i]; |
200 |
|
} |
201 |
|
dbgPrintf("not found\n"); |
202 |
|
} |
203 |
|
} |
204 |
|
return configFiles[i]; |
205 |
|
} |
206 |
|
|
207 |
|
struct configFileInfo grub2ConfigType = { |
208 |
|
.findConfig = grub2FindConfig, |
209 |
|
.keywords = grub2Keywords, |
210 |
|
.defaultIsIndex = 1, |
211 |
|
.defaultSupportSaved = 0, |
212 |
|
.defaultIsVariable = 1, |
213 |
|
.entryStart = LT_MENUENTRY, |
214 |
|
.entryEnd = LT_ENTRY_END, |
215 |
|
.titlePosition = 1, |
216 |
|
.needsBootPrefix = 1, |
217 |
|
.mbHyperFirst = 1, |
218 |
|
.mbInitRdIsModule = 1, |
219 |
|
.mbAllowExtraInitRds = 1, |
220 |
}; |
}; |
221 |
|
|
222 |
struct keywordTypes yabootKeywords[] = { |
struct keywordTypes yabootKeywords[] = { |
314 |
}; |
}; |
315 |
int useextlinuxmenu; |
int useextlinuxmenu; |
316 |
struct configFileInfo eliloConfigType = { |
struct configFileInfo eliloConfigType = { |
317 |
"/boot/efi/EFI/redhat/elilo.conf", /* defaultConfig */ |
.defaultConfig = "/boot/efi/EFI/redhat/elilo.conf", |
318 |
eliloKeywords, /* keywords */ |
.keywords = eliloKeywords, |
319 |
0, /* defaultIsIndex */ |
.entryStart = LT_KERNEL, |
320 |
0, /* defaultSupportSaved */ |
.needsBootPrefix = 1, |
321 |
LT_KERNEL, /* entrySeparator */ |
.argsInQuotes = 1, |
322 |
1, /* needsBootPrefix */ |
.mbConcatArgs = 1, |
|
1, /* argsInQuotes */ |
|
|
0, /* maxTitleLength */ |
|
|
0, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
1, /* mbConcatArgs */ |
|
|
0, /* mbAllowExtraInitRds */ |
|
323 |
}; |
}; |
324 |
|
|
325 |
struct configFileInfo liloConfigType = { |
struct configFileInfo liloConfigType = { |
326 |
"/etc/lilo.conf", /* defaultConfig */ |
.defaultConfig = "/etc/lilo.conf", |
327 |
liloKeywords, /* keywords */ |
.keywords = liloKeywords, |
328 |
0, /* defaultIsIndex */ |
.entryStart = LT_KERNEL, |
329 |
0, /* defaultSupportSaved */ |
.argsInQuotes = 1, |
330 |
LT_KERNEL, /* entrySeparator */ |
.maxTitleLength = 15, |
|
0, /* needsBootPrefix */ |
|
|
1, /* argsInQuotes */ |
|
|
15, /* maxTitleLength */ |
|
|
0, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
0, /* mbConcatArgs */ |
|
|
0, /* mbAllowExtraInitRds */ |
|
331 |
}; |
}; |
332 |
|
|
333 |
struct configFileInfo yabootConfigType = { |
struct configFileInfo yabootConfigType = { |
334 |
"/etc/yaboot.conf", /* defaultConfig */ |
.defaultConfig = "/etc/yaboot.conf", |
335 |
yabootKeywords, /* keywords */ |
.keywords = yabootKeywords, |
336 |
0, /* defaultIsIndex */ |
.entryStart = LT_KERNEL, |
337 |
0, /* defaultSupportSaved */ |
.needsBootPrefix = 1, |
338 |
LT_KERNEL, /* entrySeparator */ |
.argsInQuotes = 1, |
339 |
1, /* needsBootPrefix */ |
.maxTitleLength = 15, |
340 |
1, /* argsInQuotes */ |
.mbAllowExtraInitRds = 1, |
|
15, /* maxTitleLength */ |
|
|
0, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
0, /* mbConcatArgs */ |
|
|
1, /* mbAllowExtraInitRds */ |
|
341 |
}; |
}; |
342 |
|
|
343 |
struct configFileInfo siloConfigType = { |
struct configFileInfo siloConfigType = { |
344 |
"/etc/silo.conf", /* defaultConfig */ |
.defaultConfig = "/etc/silo.conf", |
345 |
siloKeywords, /* keywords */ |
.keywords = siloKeywords, |
346 |
0, /* defaultIsIndex */ |
.entryStart = LT_KERNEL, |
347 |
0, /* defaultSupportSaved */ |
.needsBootPrefix = 1, |
348 |
LT_KERNEL, /* entrySeparator */ |
.argsInQuotes = 1, |
349 |
1, /* needsBootPrefix */ |
.maxTitleLength = 15, |
|
1, /* argsInQuotes */ |
|
|
15, /* maxTitleLength */ |
|
|
0, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
0, /* mbConcatArgs */ |
|
|
0, /* mbAllowExtraInitRds */ |
|
350 |
}; |
}; |
351 |
|
|
352 |
struct configFileInfo ziplConfigType = { |
struct configFileInfo ziplConfigType = { |
353 |
"/etc/zipl.conf", /* defaultConfig */ |
.defaultConfig = "/etc/zipl.conf", |
354 |
ziplKeywords, /* keywords */ |
.keywords = ziplKeywords, |
355 |
0, /* defaultIsIndex */ |
.entryStart = LT_TITLE, |
356 |
0, /* defaultSupportSaved */ |
.argsInQuotes = 1, |
357 |
LT_TITLE, /* entrySeparator */ |
.titleBracketed = 1, |
|
0, /* needsBootPrefix */ |
|
|
1, /* argsInQuotes */ |
|
|
0, /* maxTitleLength */ |
|
|
1, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
0, /* mbConcatArgs */ |
|
|
0, /* mbAllowExtraInitRds */ |
|
358 |
}; |
}; |
359 |
|
|
360 |
struct configFileInfo extlinuxConfigType = { |
struct configFileInfo extlinuxConfigType = { |
361 |
"/boot/extlinux/extlinux.conf", /* defaultConfig */ |
.defaultConfig = "/boot/extlinux/extlinux.conf", |
362 |
extlinuxKeywords, /* keywords */ |
.keywords = extlinuxKeywords, |
363 |
0, /* defaultIsIndex */ |
.entryStart = LT_TITLE, |
364 |
0, /* defaultSupportSaved */ |
.needsBootPrefix = 1, |
365 |
LT_TITLE, /* entrySeparator */ |
.maxTitleLength = 255, |
366 |
1, /* needsBootPrefix */ |
.mbAllowExtraInitRds = 1, |
|
0, /* argsInQuotes */ |
|
|
255, /* maxTitleLength */ |
|
|
0, /* titleBracketed */ |
|
|
0, /* mbHyperFirst */ |
|
|
0, /* mbInitRdIsModule */ |
|
|
0, /* mbConcatArgs */ |
|
|
1, /* mbAllowExtraInitRds */ |
|
367 |
}; |
}; |
368 |
|
|
369 |
struct grubConfig { |
struct grubConfig { |
393 |
static int getNextLine(char ** bufPtr, struct singleLine * line, |
static int getNextLine(char ** bufPtr, struct singleLine * line, |
394 |
struct configFileInfo * cfi); |
struct configFileInfo * cfi); |
395 |
static char * getRootSpecifier(char * str); |
static char * getRootSpecifier(char * str); |
396 |
|
static void requote(struct singleLine *line, struct configFileInfo * cfi); |
397 |
static void insertElement(struct singleLine * line, |
static void insertElement(struct singleLine * line, |
398 |
const char * item, int insertHere, |
const char * item, int insertHere, |
399 |
struct configFileInfo * cfi); |
struct configFileInfo * cfi); |
458 |
return NULL; |
return NULL; |
459 |
} |
} |
460 |
|
|
461 |
|
static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) { |
462 |
|
struct keywordTypes *kt = getKeywordByType(type, cfi); |
463 |
|
if (kt) |
464 |
|
return kt->key; |
465 |
|
return "unknown"; |
466 |
|
} |
467 |
|
|
468 |
static char * getpathbyspec(char *device) { |
static char * getpathbyspec(char *device) { |
469 |
if (!blkid) |
if (!blkid) |
470 |
blkid_get_cache(&blkid, NULL); |
blkid_get_cache(&blkid, NULL); |
514 |
return 0; |
return 0; |
515 |
} |
} |
516 |
|
|
517 |
static int isEntrySeparator(struct singleLine * line, |
static int isEntryStart(struct singleLine * line, |
518 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
519 |
return line->type == cfi->entrySeparator || line->type == LT_OTHER || |
return line->type == cfi->entryStart || line->type == LT_OTHER || |
520 |
(cfi->titleBracketed && isBracketedTitle(line)); |
(cfi->titleBracketed && isBracketedTitle(line)); |
521 |
} |
} |
522 |
|
|
839 |
cfg->secondaryIndent = strdup(line->indent); |
cfg->secondaryIndent = strdup(line->indent); |
840 |
} |
} |
841 |
|
|
842 |
if (isEntrySeparator(line, cfi)) { |
if (isEntryStart(line, cfi)) { |
843 |
sawEntry = 1; |
sawEntry = 1; |
844 |
if (!entry) { |
if (!entry) { |
845 |
cfg->entries = malloc(sizeof(*entry)); |
cfg->entries = malloc(sizeof(*entry)); |
855 |
entry->next = NULL; |
entry->next = NULL; |
856 |
} |
} |
857 |
|
|
858 |
if (line->type == LT_DEFAULT && line->numElements == 2) { |
if (line->type == LT_SET_VARIABLE) { |
859 |
|
int i; |
860 |
|
dbgPrintf("found 'set' command (%d elements): ", line->numElements); |
861 |
|
dbgPrintf("%s", line->indent); |
862 |
|
for (i = 0; i < line->numElements; i++) |
863 |
|
dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item); |
864 |
|
dbgPrintf("\n"); |
865 |
|
struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi); |
866 |
|
if (kwType && line->numElements == 3 && |
867 |
|
!strcmp(line->elements[1].item, kwType->key)) { |
868 |
|
dbgPrintf("Line sets default config\n"); |
869 |
|
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
870 |
|
defaultLine = line; |
871 |
|
} |
872 |
|
} else if (line->type == LT_DEFAULT && line->numElements == 2) { |
873 |
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
874 |
defaultLine = line; |
defaultLine = line; |
875 |
|
|
982 |
entry->lines = line; |
entry->lines = line; |
983 |
else |
else |
984 |
last->next = line; |
last->next = line; |
985 |
dbgPrintf("readConfig added %d to %p\n", line->type, entry); |
dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry); |
986 |
|
|
987 |
|
/* we could have seen this outside of an entry... if so, we |
988 |
|
* ignore it like any other line we don't grok */ |
989 |
|
if (line->type == LT_ENTRY_END && sawEntry) |
990 |
|
sawEntry = 0; |
991 |
} else { |
} else { |
992 |
if (!cfg->theLines) |
if (!cfg->theLines) |
993 |
cfg->theLines = line; |
cfg->theLines = line; |
994 |
else |
else |
995 |
last->next = line; |
last->next = line; |
996 |
dbgPrintf("readConfig added %d to cfg\n", line->type); |
dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi)); |
997 |
} |
} |
998 |
|
|
999 |
last = line; |
last = line; |
1001 |
|
|
1002 |
free(incoming); |
free(incoming); |
1003 |
|
|
1004 |
|
dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset"); |
1005 |
if (defaultLine) { |
if (defaultLine) { |
1006 |
if (cfi->defaultSupportSaved && |
if (cfi->defaultIsVariable) { |
1007 |
|
char *value = defaultLine->elements[2].item; |
1008 |
|
while (*value && (*value == '"' || *value == '\'' || |
1009 |
|
*value == ' ' || *value == '\t')) |
1010 |
|
value++; |
1011 |
|
cfg->defaultImage = strtol(value, &end, 10); |
1012 |
|
while (*end && (*end == '"' || *end == '\'' || |
1013 |
|
*end == ' ' || *end == '\t')) |
1014 |
|
end++; |
1015 |
|
if (*end) cfg->defaultImage = -1; |
1016 |
|
} else if (cfi->defaultSupportSaved && |
1017 |
!strncmp(defaultLine->elements[1].item, "saved", 5)) { |
!strncmp(defaultLine->elements[1].item, "saved", 5)) { |
1018 |
cfg->defaultImage = DEFAULT_SAVED; |
cfg->defaultImage = DEFAULT_SAVED; |
1019 |
} else if (cfi->defaultIsIndex) { |
} else if (cfi->defaultIsIndex) { |
1062 |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
1063 |
else if (cfg->defaultImage > -1) { |
else if (cfg->defaultImage > -1) { |
1064 |
if (cfg->cfi->defaultIsIndex) { |
if (cfg->cfi->defaultIsIndex) { |
1065 |
fprintf(out, "%sdefault%s%d\n", indent, separator, |
if (cfg->cfi->defaultIsVariable) { |
1066 |
cfg->defaultImage); |
fprintf(out, "%sset default=\"%d\"\n", indent, |
1067 |
|
cfg->defaultImage); |
1068 |
|
} else { |
1069 |
|
fprintf(out, "%sdefault%s%d\n", indent, separator, |
1070 |
|
cfg->defaultImage); |
1071 |
|
} |
1072 |
} else { |
} else { |
1073 |
int image = cfg->defaultImage; |
int image = cfg->defaultImage; |
1074 |
|
|
1158 |
} |
} |
1159 |
|
|
1160 |
line = cfg->theLines; |
line = cfg->theLines; |
1161 |
|
struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi); |
1162 |
while (line) { |
while (line) { |
1163 |
if (line->type == LT_DEFAULT) { |
if (line->type == LT_SET_VARIABLE && defaultKw && |
1164 |
|
line->numElements == 3 && |
1165 |
|
!strcmp(line->elements[1].item, defaultKw->key)) { |
1166 |
|
writeDefault(out, line->indent, line->elements[0].indent, cfg); |
1167 |
|
needs &= ~MAIN_DEFAULT; |
1168 |
|
} else if (line->type == LT_DEFAULT) { |
1169 |
writeDefault(out, line->indent, line->elements[0].indent, cfg); |
writeDefault(out, line->indent, line->elements[0].indent, cfg); |
1170 |
needs &= ~MAIN_DEFAULT; |
needs &= ~MAIN_DEFAULT; |
1171 |
} else if (line->type == LT_FALLBACK) { |
} else if (line->type == LT_FALLBACK) { |
1888 |
sprintf(tmpl.elements[0].item, "[%s]", val); |
sprintf(tmpl.elements[0].item, "[%s]", val); |
1889 |
tmpl.elements[0].indent = ""; |
tmpl.elements[0].indent = ""; |
1890 |
val = NULL; |
val = NULL; |
1891 |
|
} else if (type == LT_MENUENTRY) { |
1892 |
|
char *lineend = "--class gnu-linux --class gnu --class os {"; |
1893 |
|
if (!val) { |
1894 |
|
fprintf(stderr, "Line type LT_MENUENTRY requires a value\n"); |
1895 |
|
abort(); |
1896 |
|
} |
1897 |
|
kw = getKeywordByType(type, cfi); |
1898 |
|
if (!kw) { |
1899 |
|
fprintf(stderr, "Looking up keyword for unknown type %d\n", type); |
1900 |
|
abort(); |
1901 |
|
} |
1902 |
|
tmpl.indent = ""; |
1903 |
|
tmpl.type = type; |
1904 |
|
tmpl.numElements = 3; |
1905 |
|
tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements); |
1906 |
|
tmpl.elements[0].item = kw->key; |
1907 |
|
tmpl.elements[0].indent = alloca(2); |
1908 |
|
sprintf(tmpl.elements[0].indent, "%c", kw->nextChar); |
1909 |
|
tmpl.elements[1].item = (char *)val; |
1910 |
|
tmpl.elements[1].indent = alloca(2); |
1911 |
|
sprintf(tmpl.elements[1].indent, "%c", kw->nextChar); |
1912 |
|
tmpl.elements[2].item = alloca(strlen(lineend)+1); |
1913 |
|
strcpy(tmpl.elements[2].item, lineend); |
1914 |
|
tmpl.elements[2].indent = ""; |
1915 |
} else { |
} else { |
1916 |
kw = getKeywordByType(type, cfi); |
kw = getKeywordByType(type, cfi); |
1917 |
if (!kw) abort(); |
if (!kw) { |
1918 |
|
fprintf(stderr, "Looking up keyword for unknown type %d\n", type); |
1919 |
|
abort(); |
1920 |
|
} |
1921 |
tmpl.type = type; |
tmpl.type = type; |
1922 |
tmpl.numElements = val ? 2 : 1; |
tmpl.numElements = val ? 2 : 1; |
1923 |
tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements); |
tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements); |
1941 |
if (!line->next && !prev) prev = line; |
if (!line->next && !prev) prev = line; |
1942 |
} |
} |
1943 |
|
|
1944 |
if (prev == entry->lines) |
struct singleLine *menuEntry; |
1945 |
tmpl.indent = defaultIndent ?: ""; |
menuEntry = getLineByType(LT_MENUENTRY, entry->lines); |
1946 |
else |
if (tmpl.type == LT_ENTRY_END) { |
1947 |
tmpl.indent = prev->indent; |
if (menuEntry) |
1948 |
|
tmpl.indent = menuEntry->indent; |
1949 |
|
else |
1950 |
|
tmpl.indent = defaultIndent ?: ""; |
1951 |
|
} else if (tmpl.type != LT_MENUENTRY) { |
1952 |
|
if (menuEntry) |
1953 |
|
tmpl.indent = "\t"; |
1954 |
|
else if (prev == entry->lines) |
1955 |
|
tmpl.indent = defaultIndent ?: ""; |
1956 |
|
else |
1957 |
|
tmpl.indent = prev->indent; |
1958 |
|
} |
1959 |
|
|
1960 |
return addLineTmpl(entry, &tmpl, prev, val, cfi); |
return addLineTmpl(entry, &tmpl, prev, val, cfi); |
1961 |
} |
} |
1982 |
free(line); |
free(line); |
1983 |
} |
} |
1984 |
|
|
1985 |
|
static int isquote(char q) |
1986 |
|
{ |
1987 |
|
if (q == '\'' || q == '\"') |
1988 |
|
return 1; |
1989 |
|
return 0; |
1990 |
|
} |
1991 |
|
|
1992 |
|
static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi) |
1993 |
|
{ |
1994 |
|
struct singleLine newLine = { |
1995 |
|
.indent = tmplLine->indent, |
1996 |
|
.type = tmplLine->type, |
1997 |
|
.next = tmplLine->next, |
1998 |
|
}; |
1999 |
|
int firstQuotedItem = -1; |
2000 |
|
int quoteLen = 0; |
2001 |
|
int j; |
2002 |
|
int element = 0; |
2003 |
|
char *c; |
2004 |
|
|
2005 |
|
c = malloc(strlen(tmplLine->elements[0].item) + 1); |
2006 |
|
strcpy(c, tmplLine->elements[0].item); |
2007 |
|
insertElement(&newLine, c, element++, cfi); |
2008 |
|
free(c); |
2009 |
|
c = NULL; |
2010 |
|
|
2011 |
|
for (j = 1; j < tmplLine->numElements; j++) { |
2012 |
|
if (firstQuotedItem == -1) { |
2013 |
|
quoteLen += strlen(tmplLine->elements[j].item); |
2014 |
|
|
2015 |
|
if (isquote(tmplLine->elements[j].item[0])) { |
2016 |
|
firstQuotedItem = j; |
2017 |
|
quoteLen += strlen(tmplLine->elements[j].indent); |
2018 |
|
} else { |
2019 |
|
c = malloc(quoteLen + 1); |
2020 |
|
strcpy(c, tmplLine->elements[j].item); |
2021 |
|
insertElement(&newLine, c, element++, cfi); |
2022 |
|
free(c); |
2023 |
|
quoteLen = 0; |
2024 |
|
} |
2025 |
|
} else { |
2026 |
|
int itemlen = strlen(tmplLine->elements[j].item); |
2027 |
|
quoteLen += itemlen; |
2028 |
|
quoteLen += strlen(tmplLine->elements[j].indent); |
2029 |
|
|
2030 |
|
if (isquote(tmplLine->elements[j].item[itemlen - 1])) { |
2031 |
|
c = malloc(quoteLen + 1); |
2032 |
|
c[0] = '\0'; |
2033 |
|
for (int i = firstQuotedItem; i < j+1; i++) { |
2034 |
|
strcat(c, tmplLine->elements[i].item); |
2035 |
|
strcat(c, tmplLine->elements[i].indent); |
2036 |
|
} |
2037 |
|
insertElement(&newLine, c, element++, cfi); |
2038 |
|
free(c); |
2039 |
|
|
2040 |
|
firstQuotedItem = -1; |
2041 |
|
quoteLen = 0; |
2042 |
|
} |
2043 |
|
} |
2044 |
|
} |
2045 |
|
while (tmplLine->numElements) |
2046 |
|
removeElement(tmplLine, 0); |
2047 |
|
if (tmplLine->elements) |
2048 |
|
free(tmplLine->elements); |
2049 |
|
|
2050 |
|
tmplLine->numElements = newLine.numElements; |
2051 |
|
tmplLine->elements = newLine.elements; |
2052 |
|
} |
2053 |
|
|
2054 |
static void insertElement(struct singleLine * line, |
static void insertElement(struct singleLine * line, |
2055 |
const char * item, int insertHere, |
const char * item, int insertHere, |
2056 |
struct configFileInfo * cfi) |
struct configFileInfo * cfi) |
2378 |
int updateInitrd(struct grubConfig * cfg, const char * image, |
int updateInitrd(struct grubConfig * cfg, const char * image, |
2379 |
const char * prefix, const char * initrd) { |
const char * prefix, const char * initrd) { |
2380 |
struct singleEntry * entry; |
struct singleEntry * entry; |
2381 |
struct singleLine * line, * kernelLine; |
struct singleLine * line, * kernelLine, *endLine = NULL; |
2382 |
int index = 0; |
int index = 0; |
2383 |
|
|
2384 |
if (!image) return 0; |
if (!image) return 0; |
2395 |
if (!strncmp(initrd, prefix, prefixLen)) |
if (!strncmp(initrd, prefix, prefixLen)) |
2396 |
initrd += prefixLen; |
initrd += prefixLen; |
2397 |
} |
} |
2398 |
|
endLine = getLineByType(LT_ENTRY_END, entry->lines); |
2399 |
|
if (endLine) |
2400 |
|
removeLine(entry, endLine); |
2401 |
line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd); |
line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd); |
2402 |
if (!line) return 1; |
if (!line) |
2403 |
|
return 1; |
2404 |
|
if (endLine) { |
2405 |
|
line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL); |
2406 |
|
if (!line) |
2407 |
|
return 1; |
2408 |
|
} |
2409 |
|
|
2410 |
break; |
break; |
2411 |
} |
} |
2412 |
|
|
2583 |
return checkDeviceBootloader(line->elements[1].item, boot); |
return checkDeviceBootloader(line->elements[1].item, boot); |
2584 |
} |
} |
2585 |
|
|
2586 |
|
int checkForGrub2(struct grubConfig * config) { |
2587 |
|
if (!access("/boot/grub2", R_OK)) |
2588 |
|
return 2; |
2589 |
|
|
2590 |
|
return 1; |
2591 |
|
} |
2592 |
|
|
2593 |
int checkForGrub(struct grubConfig * config) { |
int checkForGrub(struct grubConfig * config) { |
2594 |
int fd; |
int fd; |
2595 |
unsigned char bootSect[512]; |
unsigned char bootSect[512]; |
2765 |
if (*chptr == '#') continue; |
if (*chptr == '#') continue; |
2766 |
|
|
2767 |
if (tmplLine->type == LT_KERNEL && |
if (tmplLine->type == LT_KERNEL && |
2768 |
tmplLine->numElements >= 2) { |
tmplLine->numElements >= 2) { |
2769 |
if (!template->multiboot && (needs & NEED_MB)) { |
if (!template->multiboot && (needs & NEED_MB)) { |
2770 |
/* it's not a multiboot template and this is the kernel |
/* it's not a multiboot template and this is the kernel |
2771 |
* line. Try to be intelligent about inserting the |
* line. Try to be intelligent about inserting the |
2891 |
needs &= ~NEED_INITRD; |
needs &= ~NEED_INITRD; |
2892 |
} |
} |
2893 |
|
|
2894 |
|
} else if (tmplLine->type == LT_MENUENTRY && |
2895 |
|
(needs & NEED_TITLE)) { |
2896 |
|
requote(tmplLine, config->cfi); |
2897 |
|
char *nkt = malloc(strlen(newKernelTitle)+3); |
2898 |
|
strcpy(nkt, "'"); |
2899 |
|
strcat(nkt, newKernelTitle); |
2900 |
|
strcat(nkt, "'"); |
2901 |
|
newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi); |
2902 |
|
free(nkt); |
2903 |
|
needs &= ~NEED_TITLE; |
2904 |
} else if (tmplLine->type == LT_TITLE && |
} else if (tmplLine->type == LT_TITLE && |
2905 |
(needs & NEED_TITLE)) { |
(needs & NEED_TITLE)) { |
2906 |
if (tmplLine->numElements >= 2) { |
if (tmplLine->numElements >= 2) { |
2914 |
tmplLine->indent, newKernelTitle); |
tmplLine->indent, newKernelTitle); |
2915 |
needs &= ~NEED_TITLE; |
needs &= ~NEED_TITLE; |
2916 |
} |
} |
2917 |
|
} else if (tmplLine->type == LT_ECHO) { |
2918 |
|
requote(tmplLine, config->cfi); |
2919 |
|
if (tmplLine->numElements > 1 && |
2920 |
|
strstr(tmplLine->elements[1].item, "'Loading Linux ")) { |
2921 |
|
char *prefix = "'Loading "; |
2922 |
|
char *newTitle = malloc(strlen(prefix) + |
2923 |
|
strlen(newKernelTitle) + 2); |
2924 |
|
|
2925 |
|
strcpy(newTitle, prefix); |
2926 |
|
strcat(newTitle, newKernelTitle); |
2927 |
|
strcat(newTitle, "'"); |
2928 |
|
newLine = addLine(new, config->cfi, LT_ECHO, |
2929 |
|
tmplLine->indent, newTitle); |
2930 |
|
free(newTitle); |
2931 |
|
} else { |
2932 |
|
/* pass through other lines from the template */ |
2933 |
|
newLine = addLineTmpl(new, tmplLine, newLine, NULL, |
2934 |
|
config->cfi); |
2935 |
|
} |
2936 |
} else { |
} else { |
2937 |
/* pass through other lines from the template */ |
/* pass through other lines from the template */ |
2938 |
newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi); |
newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi); |
2943 |
/* don't have a template, so start the entry with the |
/* don't have a template, so start the entry with the |
2944 |
* appropriate starting line |
* appropriate starting line |
2945 |
*/ |
*/ |
2946 |
switch (config->cfi->entrySeparator) { |
switch (config->cfi->entryStart) { |
2947 |
case LT_KERNEL: |
case LT_KERNEL: |
2948 |
if (new->multiboot && config->cfi->mbHyperFirst) { |
if (new->multiboot && config->cfi->mbHyperFirst) { |
2949 |
/* fall through to LT_HYPER */ |
/* fall through to LT_HYPER */ |
2962 |
needs &= ~NEED_MB; |
needs &= ~NEED_MB; |
2963 |
break; |
break; |
2964 |
|
|
2965 |
|
case LT_MENUENTRY: { |
2966 |
|
char *nkt = malloc(strlen(newKernelTitle)+3); |
2967 |
|
strcpy(nkt, "'"); |
2968 |
|
strcat(nkt, newKernelTitle); |
2969 |
|
strcat(nkt, "'"); |
2970 |
|
newLine = addLine(new, config->cfi, LT_MENUENTRY, |
2971 |
|
config->primaryIndent, nkt); |
2972 |
|
free(nkt); |
2973 |
|
needs &= ~NEED_TITLE; |
2974 |
|
needs |= NEED_END; |
2975 |
|
break; |
2976 |
|
} |
2977 |
case LT_TITLE: |
case LT_TITLE: |
2978 |
if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above) |
if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above) |
2979 |
char * templabel; |
char * templabel; |
3007 |
|
|
3008 |
/* add the remainder of the lines, i.e. those that either |
/* add the remainder of the lines, i.e. those that either |
3009 |
* weren't present in the template, or in the case of no template, |
* weren't present in the template, or in the case of no template, |
3010 |
* all the lines following the entrySeparator. |
* all the lines following the entryStart. |
3011 |
*/ |
*/ |
3012 |
if (needs & NEED_TITLE) { |
if (needs & NEED_TITLE) { |
3013 |
newLine = addLine(new, config->cfi, LT_TITLE, |
newLine = addLine(new, config->cfi, LT_TITLE, |
3048 |
free(initrdVal); |
free(initrdVal); |
3049 |
needs &= ~NEED_INITRD; |
needs &= ~NEED_INITRD; |
3050 |
} |
} |
3051 |
|
if (needs & NEED_END) { |
3052 |
|
newLine = addLine(new, config->cfi, LT_ENTRY_END, |
3053 |
|
config->secondaryIndent, NULL); |
3054 |
|
needs &= ~NEED_END; |
3055 |
|
} |
3056 |
|
|
3057 |
if (needs) { |
if (needs) { |
3058 |
printf(_("grubby: needs=%d, aborting\n"), needs); |
printf(_("grubby: needs=%d, aborting\n"), needs); |
3082 |
|
|
3083 |
int main(int argc, const char ** argv) { |
int main(int argc, const char ** argv) { |
3084 |
poptContext optCon; |
poptContext optCon; |
3085 |
char * grubConfig = NULL; |
const char * grubConfig = NULL; |
3086 |
char * outputFile = NULL; |
char * outputFile = NULL; |
3087 |
int arg = 0; |
int arg = 0; |
3088 |
int flags = 0; |
int flags = 0; |
3089 |
int badImageOkay = 0; |
int badImageOkay = 0; |
3090 |
|
int configureGrub2 = 0; |
3091 |
int configureLilo = 0, configureELilo = 0, configureGrub = 0; |
int configureLilo = 0, configureELilo = 0, configureGrub = 0; |
3092 |
int configureYaboot = 0, configureSilo = 0, configureZipl = 0; |
int configureYaboot = 0, configureSilo = 0, configureZipl = 0; |
3093 |
int configureExtLinux = 0; |
int configureExtLinux = 0; |
3153 |
_("configure extlinux bootloader (from syslinux)") }, |
_("configure extlinux bootloader (from syslinux)") }, |
3154 |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
3155 |
_("configure grub bootloader") }, |
_("configure grub bootloader") }, |
3156 |
|
{ "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0, |
3157 |
|
_("configure grub2 bootloader") }, |
3158 |
{ "info", 0, POPT_ARG_STRING, &kernelInfo, 0, |
{ "info", 0, POPT_ARG_STRING, &kernelInfo, 0, |
3159 |
_("display boot information for specified kernel"), |
_("display boot information for specified kernel"), |
3160 |
_("kernel-path") }, |
_("kernel-path") }, |
3234 |
return 1; |
return 1; |
3235 |
} |
} |
3236 |
|
|
3237 |
if ((configureLilo + configureGrub + configureELilo + |
if ((configureLilo + configureGrub2 + configureGrub + configureELilo + |
3238 |
configureYaboot + configureSilo + configureZipl + |
configureYaboot + configureSilo + configureZipl + |
3239 |
configureExtLinux ) > 1) { |
configureExtLinux ) > 1) { |
3240 |
fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n")); |
fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n")); |
3243 |
fprintf(stderr, |
fprintf(stderr, |
3244 |
_("grubby: cannot specify config file with --bootloader-probe\n")); |
_("grubby: cannot specify config file with --bootloader-probe\n")); |
3245 |
return 1; |
return 1; |
3246 |
|
} else if (configureGrub2) { |
3247 |
|
cfi = &grub2ConfigType; |
3248 |
} else if (configureLilo) { |
} else if (configureLilo) { |
3249 |
cfi = &liloConfigType; |
cfi = &liloConfigType; |
3250 |
} else if (configureGrub) { |
} else if (configureGrub) { |
3274 |
#elif __s390x__ |
#elif __s390x__ |
3275 |
cfi = &ziplConfigtype; |
cfi = &ziplConfigtype; |
3276 |
#else |
#else |
3277 |
cfi = &grubConfigType; |
if (grub2FindConfig(&grub2ConfigType)) |
3278 |
|
cfi = &grub2ConfigType; |
3279 |
|
else |
3280 |
|
cfi = &grubConfigType; |
3281 |
#endif |
#endif |
3282 |
} |
} |
3283 |
|
|
3284 |
if (!grubConfig) |
if (!grubConfig) { |
3285 |
grubConfig = cfi->defaultConfig; |
if (cfi->findConfig) |
3286 |
|
grubConfig = cfi->findConfig(cfi); |
3287 |
|
if (!grubConfig) |
3288 |
|
grubConfig = cfi->defaultConfig; |
3289 |
|
} |
3290 |
|
|
3291 |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
3292 |
newKernelPath || removeKernelPath || makeDefault || |
newKernelPath || removeKernelPath || makeDefault || |
3368 |
} |
} |
3369 |
|
|
3370 |
if (bootloaderProbe) { |
if (bootloaderProbe) { |
3371 |
int lrc = 0, grc = 0, erc = 0; |
int lrc = 0, grc = 0, gr2c = 0, erc = 0; |
3372 |
struct grubConfig * lconfig, * gconfig; |
struct grubConfig * lconfig, * gconfig; |
3373 |
|
|
3374 |
|
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
3375 |
|
if (grub2config) { |
3376 |
|
gconfig = readConfig(grub2config, &grub2ConfigType); |
3377 |
|
if (!gconfig) |
3378 |
|
gr2c = 1; |
3379 |
|
else |
3380 |
|
gr2c = checkForGrub2(gconfig); |
3381 |
|
} |
3382 |
|
|
3383 |
if (!access(grubConfigType.defaultConfig, F_OK)) { |
if (!access(grubConfigType.defaultConfig, F_OK)) { |
3384 |
gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType); |
gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType); |
3385 |
if (!gconfig) |
if (!gconfig) |
3404 |
erc = checkForExtLinux(lconfig); |
erc = checkForExtLinux(lconfig); |
3405 |
} |
} |
3406 |
|
|
3407 |
if (lrc == 1 || grc == 1) return 1; |
if (lrc == 1 || grc == 1 || gr2c == 1) return 1; |
3408 |
|
|
3409 |
if (lrc == 2) printf("lilo\n"); |
if (lrc == 2) printf("lilo\n"); |
3410 |
|
if (gr2c == 2) printf("grub2\n"); |
3411 |
if (grc == 2) printf("grub\n"); |
if (grc == 2) printf("grub\n"); |
3412 |
if (erc == 2) printf("extlinux\n"); |
if (erc == 2) printf("extlinux\n"); |
3413 |
|
|
3467 |
} |
} |
3468 |
|
|
3469 |
if (!outputFile) |
if (!outputFile) |
3470 |
outputFile = grubConfig; |
outputFile = (char *)grubConfig; |
3471 |
|
|
3472 |
return writeConfig(config, outputFile, bootPrefix); |
return writeConfig(config, outputFile, bootPrefix); |
3473 |
} |
} |