46 |
#define dbgPrintf(format, args...) |
#define dbgPrintf(format, args...) |
47 |
#endif |
#endif |
48 |
|
|
49 |
|
int debug = 0; /* Currently just for template debugging */ |
50 |
|
|
51 |
#define _(A) (A) |
#define _(A) (A) |
52 |
|
|
53 |
#define MAX_EXTRA_INITRDS 16 /* code segment checked by --bootloader-probe */ |
#define MAX_EXTRA_INITRDS 16 /* code segment checked by --bootloader-probe */ |
54 |
#define CODE_SEG_SIZE 128 /* code segment checked by --bootloader-probe */ |
#define CODE_SEG_SIZE 128 /* code segment checked by --bootloader-probe */ |
55 |
|
|
56 |
|
#define NOOP_OPCODE 0x90 |
57 |
|
#define JMP_SHORT_OPCODE 0xeb |
58 |
|
|
59 |
/* comments get lumped in with indention */ |
/* comments get lumped in with indention */ |
60 |
struct lineElement { |
struct lineElement { |
61 |
char * item; |
char * item; |
63 |
}; |
}; |
64 |
|
|
65 |
enum lineType_e { |
enum lineType_e { |
66 |
LT_WHITESPACE = 1 << 0, |
LT_WHITESPACE = 1 << 0, |
67 |
LT_TITLE = 1 << 1, |
LT_TITLE = 1 << 1, |
68 |
LT_KERNEL = 1 << 2, |
LT_KERNEL = 1 << 2, |
69 |
LT_INITRD = 1 << 3, |
LT_INITRD = 1 << 3, |
70 |
LT_HYPER = 1 << 4, |
LT_HYPER = 1 << 4, |
71 |
LT_DEFAULT = 1 << 5, |
LT_DEFAULT = 1 << 5, |
72 |
LT_MBMODULE = 1 << 6, |
LT_MBMODULE = 1 << 6, |
73 |
LT_ROOT = 1 << 7, |
LT_ROOT = 1 << 7, |
74 |
LT_FALLBACK = 1 << 8, |
LT_FALLBACK = 1 << 8, |
75 |
LT_KERNELARGS = 1 << 9, |
LT_KERNELARGS = 1 << 9, |
76 |
LT_BOOT = 1 << 10, |
LT_BOOT = 1 << 10, |
77 |
LT_BOOTROOT = 1 << 11, |
LT_BOOTROOT = 1 << 11, |
78 |
LT_LBA = 1 << 12, |
LT_LBA = 1 << 12, |
79 |
LT_OTHER = 1 << 13, |
LT_OTHER = 1 << 13, |
80 |
LT_GENERIC = 1 << 14, |
LT_GENERIC = 1 << 14, |
81 |
LT_UNKNOWN = 1 << 15, |
LT_ECHO = 1 << 16, |
82 |
|
LT_MENUENTRY = 1 << 17, |
83 |
|
LT_ENTRY_END = 1 << 18, |
84 |
|
LT_SET_VARIABLE = 1 << 19, |
85 |
|
LT_UNKNOWN = 1 << 20, |
86 |
}; |
}; |
87 |
|
|
88 |
struct singleLine { |
struct singleLine { |
110 |
#define NEED_TITLE (1 << 2) |
#define NEED_TITLE (1 << 2) |
111 |
#define NEED_ARGS (1 << 3) |
#define NEED_ARGS (1 << 3) |
112 |
#define NEED_MB (1 << 4) |
#define NEED_MB (1 << 4) |
113 |
|
#define NEED_END (1 << 5) |
114 |
|
|
115 |
#define MAIN_DEFAULT (1 << 0) |
#define MAIN_DEFAULT (1 << 0) |
116 |
#define DEFAULT_SAVED -2 |
#define DEFAULT_SAVED -2 |
117 |
|
#define DEFAULT_SAVED_GRUB2 -3 |
118 |
|
|
119 |
struct keywordTypes { |
struct keywordTypes { |
120 |
char * key; |
char * key; |
123 |
char separatorChar; |
char separatorChar; |
124 |
}; |
}; |
125 |
|
|
126 |
|
struct configFileInfo; |
127 |
|
|
128 |
|
typedef const char *(*findConfigFunc)(struct configFileInfo *); |
129 |
|
typedef const int (*writeLineFunc)(struct configFileInfo *, |
130 |
|
struct singleLine *line); |
131 |
|
|
132 |
struct configFileInfo { |
struct configFileInfo { |
133 |
char * defaultConfig; |
char * defaultConfig; |
134 |
|
findConfigFunc findConfig; |
135 |
|
writeLineFunc writeLine; |
136 |
struct keywordTypes * keywords; |
struct keywordTypes * keywords; |
137 |
int defaultIsIndex; |
int defaultIsIndex; |
138 |
|
int defaultIsVariable; |
139 |
int defaultSupportSaved; |
int defaultSupportSaved; |
140 |
enum lineType_e entryStart; |
enum lineType_e entryStart; |
141 |
|
enum lineType_e entryEnd; |
142 |
int needsBootPrefix; |
int needsBootPrefix; |
143 |
int argsInQuotes; |
int argsInQuotes; |
144 |
int maxTitleLength; |
int maxTitleLength; |
145 |
int titleBracketed; |
int titleBracketed; |
146 |
|
int titlePosition; |
147 |
int mbHyperFirst; |
int mbHyperFirst; |
148 |
int mbInitRdIsModule; |
int mbInitRdIsModule; |
149 |
int mbConcatArgs; |
int mbConcatArgs; |
162 |
{ NULL, 0, 0 }, |
{ NULL, 0, 0 }, |
163 |
}; |
}; |
164 |
|
|
165 |
|
const char *grubFindConfig(struct configFileInfo *cfi) { |
166 |
|
static const char *configFiles[] = { |
167 |
|
"/etc/grub.conf", |
168 |
|
"/boot/grub/grub.conf", |
169 |
|
"/boot/grub/menu.lst", |
170 |
|
NULL |
171 |
|
}; |
172 |
|
static int i = -1; |
173 |
|
|
174 |
|
if (i == -1) { |
175 |
|
for (i = 0; configFiles[i] != NULL; i++) { |
176 |
|
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
177 |
|
if (!access(configFiles[i], R_OK)) { |
178 |
|
dbgPrintf("found\n"); |
179 |
|
return configFiles[i]; |
180 |
|
} |
181 |
|
dbgPrintf("not found\n"); |
182 |
|
} |
183 |
|
} |
184 |
|
return configFiles[i]; |
185 |
|
} |
186 |
|
|
187 |
struct configFileInfo grubConfigType = { |
struct configFileInfo grubConfigType = { |
188 |
.defaultConfig = "/boot/grub/grub.conf", |
.findConfig = grubFindConfig, |
189 |
.keywords = grubKeywords, |
.keywords = grubKeywords, |
190 |
.defaultIsIndex = 1, |
.defaultIsIndex = 1, |
191 |
.defaultSupportSaved = 1, |
.defaultSupportSaved = 1, |
196 |
.mbAllowExtraInitRds = 1, |
.mbAllowExtraInitRds = 1, |
197 |
}; |
}; |
198 |
|
|
199 |
|
struct keywordTypes grub2Keywords[] = { |
200 |
|
{ "menuentry", LT_MENUENTRY, ' ' }, |
201 |
|
{ "}", LT_ENTRY_END, ' ' }, |
202 |
|
{ "echo", LT_ECHO, ' ' }, |
203 |
|
{ "set", LT_SET_VARIABLE,' ', '=' }, |
204 |
|
{ "root", LT_BOOTROOT, ' ' }, |
205 |
|
{ "default", LT_DEFAULT, ' ' }, |
206 |
|
{ "fallback", LT_FALLBACK, ' ' }, |
207 |
|
{ "linux", LT_KERNEL, ' ' }, |
208 |
|
{ "initrd", LT_INITRD, ' ', ' ' }, |
209 |
|
{ "module", LT_MBMODULE, ' ' }, |
210 |
|
{ "kernel", LT_HYPER, ' ' }, |
211 |
|
{ NULL, 0, 0 }, |
212 |
|
}; |
213 |
|
|
214 |
|
const char *grub2FindConfig(struct configFileInfo *cfi) { |
215 |
|
static const char *configFiles[] = { |
216 |
|
"/boot/grub/grub-efi.cfg", |
217 |
|
"/boot/grub/grub.cfg", |
218 |
|
NULL |
219 |
|
}; |
220 |
|
static int i = -1; |
221 |
|
static const char *grub_cfg = "/boot/grub/grub.cfg"; |
222 |
|
|
223 |
|
if (i == -1) { |
224 |
|
for (i = 0; configFiles[i] != NULL; i++) { |
225 |
|
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
226 |
|
if (!access(configFiles[i], R_OK)) { |
227 |
|
dbgPrintf("found\n"); |
228 |
|
return configFiles[i]; |
229 |
|
} |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
|
/* Ubuntu renames grub2 to grub, so check for the grub.d directory |
234 |
|
* that isn't in grub1, and if it exists, return the config file path |
235 |
|
* that they use. */ |
236 |
|
if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) { |
237 |
|
dbgPrintf("found\n"); |
238 |
|
return grub_cfg; |
239 |
|
} |
240 |
|
|
241 |
|
dbgPrintf("not found\n"); |
242 |
|
return configFiles[i]; |
243 |
|
} |
244 |
|
|
245 |
|
int sizeOfSingleLine(struct singleLine * line) { |
246 |
|
int count = 0; |
247 |
|
|
248 |
|
for (int i = 0; i < line->numElements; i++) { |
249 |
|
int indentSize = 0; |
250 |
|
|
251 |
|
count = count + strlen(line->elements[i].item); |
252 |
|
|
253 |
|
indentSize = strlen(line->elements[i].indent); |
254 |
|
if (indentSize > 0) |
255 |
|
count = count + indentSize; |
256 |
|
else |
257 |
|
/* be extra safe and add room for whitespaces */ |
258 |
|
count = count + 1; |
259 |
|
} |
260 |
|
|
261 |
|
/* room for trailing terminator */ |
262 |
|
count = count + 1; |
263 |
|
|
264 |
|
return count; |
265 |
|
} |
266 |
|
|
267 |
|
static int isquote(char q) |
268 |
|
{ |
269 |
|
if (q == '\'' || q == '\"') |
270 |
|
return 1; |
271 |
|
return 0; |
272 |
|
} |
273 |
|
|
274 |
|
char *grub2ExtractTitle(struct singleLine * line) { |
275 |
|
char * current; |
276 |
|
char * current_indent; |
277 |
|
int current_len; |
278 |
|
int current_indent_len; |
279 |
|
int i; |
280 |
|
|
281 |
|
/* bail out if line does not start with menuentry */ |
282 |
|
if (strcmp(line->elements[0].item, "menuentry")) |
283 |
|
return NULL; |
284 |
|
|
285 |
|
i = 1; |
286 |
|
current = line->elements[i].item; |
287 |
|
current_len = strlen(current); |
288 |
|
|
289 |
|
/* if second word is quoted, strip the quotes and return single word */ |
290 |
|
if (isquote(*current) && isquote(current[current_len - 1])) { |
291 |
|
char *tmp; |
292 |
|
|
293 |
|
tmp = strdup(current); |
294 |
|
*(tmp + current_len - 1) = '\0'; |
295 |
|
return ++tmp; |
296 |
|
} |
297 |
|
|
298 |
|
/* if no quotes, return second word verbatim */ |
299 |
|
if (!isquote(*current)) |
300 |
|
return current; |
301 |
|
|
302 |
|
/* second element start with a quote, so we have to find the element |
303 |
|
* whose last character is also quote (assuming it's the closing one) */ |
304 |
|
int resultMaxSize; |
305 |
|
char * result; |
306 |
|
|
307 |
|
resultMaxSize = sizeOfSingleLine(line); |
308 |
|
result = malloc(resultMaxSize); |
309 |
|
snprintf(result, resultMaxSize, "%s", ++current); |
310 |
|
|
311 |
|
i++; |
312 |
|
for (; i < line->numElements; ++i) { |
313 |
|
current = line->elements[i].item; |
314 |
|
current_len = strlen(current); |
315 |
|
current_indent = line->elements[i].indent; |
316 |
|
current_indent_len = strlen(current_indent); |
317 |
|
|
318 |
|
strncat(result, current_indent, current_indent_len); |
319 |
|
if (!isquote(current[current_len-1])) { |
320 |
|
strncat(result, current, current_len); |
321 |
|
} else { |
322 |
|
strncat(result, current, current_len - 1); |
323 |
|
break; |
324 |
|
} |
325 |
|
} |
326 |
|
return result; |
327 |
|
} |
328 |
|
|
329 |
|
struct configFileInfo grub2ConfigType = { |
330 |
|
.findConfig = grub2FindConfig, |
331 |
|
.keywords = grub2Keywords, |
332 |
|
.defaultIsIndex = 1, |
333 |
|
.defaultSupportSaved = 1, |
334 |
|
.defaultIsVariable = 1, |
335 |
|
.entryStart = LT_MENUENTRY, |
336 |
|
.entryEnd = LT_ENTRY_END, |
337 |
|
.titlePosition = 1, |
338 |
|
.needsBootPrefix = 1, |
339 |
|
.mbHyperFirst = 1, |
340 |
|
.mbInitRdIsModule = 1, |
341 |
|
.mbAllowExtraInitRds = 1, |
342 |
|
}; |
343 |
|
|
344 |
struct keywordTypes yabootKeywords[] = { |
struct keywordTypes yabootKeywords[] = { |
345 |
{ "label", LT_TITLE, '=' }, |
{ "label", LT_TITLE, '=' }, |
346 |
{ "root", LT_ROOT, '=' }, |
{ "root", LT_ROOT, '=' }, |
515 |
static int getNextLine(char ** bufPtr, struct singleLine * line, |
static int getNextLine(char ** bufPtr, struct singleLine * line, |
516 |
struct configFileInfo * cfi); |
struct configFileInfo * cfi); |
517 |
static char * getRootSpecifier(char * str); |
static char * getRootSpecifier(char * str); |
518 |
|
static void requote(struct singleLine *line, struct configFileInfo * cfi); |
519 |
static void insertElement(struct singleLine * line, |
static void insertElement(struct singleLine * line, |
520 |
const char * item, int insertHere, |
const char * item, int insertHere, |
521 |
struct configFileInfo * cfi); |
struct configFileInfo * cfi); |
572 |
|
|
573 |
static struct keywordTypes * getKeywordByType(enum lineType_e type, |
static struct keywordTypes * getKeywordByType(enum lineType_e type, |
574 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
575 |
struct keywordTypes * kw; |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
|
for (kw = cfi->keywords; kw->key; kw++) { |
|
576 |
if (kw->type == type) |
if (kw->type == type) |
577 |
return kw; |
return kw; |
578 |
} |
} |
579 |
return NULL; |
return NULL; |
580 |
} |
} |
581 |
|
|
582 |
|
static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) { |
583 |
|
struct keywordTypes *kt = getKeywordByType(type, cfi); |
584 |
|
if (kt) |
585 |
|
return kt->key; |
586 |
|
return "unknown"; |
587 |
|
} |
588 |
|
|
589 |
static char * getpathbyspec(char *device) { |
static char * getpathbyspec(char *device) { |
590 |
if (!blkid) |
if (!blkid) |
591 |
blkid_get_cache(&blkid, NULL); |
blkid_get_cache(&blkid, NULL); |
602 |
|
|
603 |
static enum lineType_e getTypeByKeyword(char * keyword, |
static enum lineType_e getTypeByKeyword(char * keyword, |
604 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
605 |
struct keywordTypes * kw; |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
|
for (kw = cfi->keywords; kw->key; kw++) { |
|
606 |
if (!strcmp(keyword, kw->key)) |
if (!strcmp(keyword, kw->key)) |
607 |
return kw->type; |
return kw->type; |
608 |
} |
} |
689 |
} |
} |
690 |
|
|
691 |
struct singleLine * lineDup(struct singleLine * line) { |
struct singleLine * lineDup(struct singleLine * line) { |
|
int i; |
|
692 |
struct singleLine * newLine = malloc(sizeof(*newLine)); |
struct singleLine * newLine = malloc(sizeof(*newLine)); |
693 |
|
|
694 |
newLine->indent = strdup(line->indent); |
newLine->indent = strdup(line->indent); |
698 |
newLine->elements = malloc(sizeof(*newLine->elements) * |
newLine->elements = malloc(sizeof(*newLine->elements) * |
699 |
newLine->numElements); |
newLine->numElements); |
700 |
|
|
701 |
for (i = 0; i < newLine->numElements; i++) { |
for (int i = 0; i < newLine->numElements; i++) { |
702 |
newLine->elements[i].indent = strdup(line->elements[i].indent); |
newLine->elements[i].indent = strdup(line->elements[i].indent); |
703 |
newLine->elements[i].item = strdup(line->elements[i].item); |
newLine->elements[i].item = strdup(line->elements[i].item); |
704 |
} |
} |
707 |
} |
} |
708 |
|
|
709 |
static void lineFree(struct singleLine * line) { |
static void lineFree(struct singleLine * line) { |
|
int i; |
|
|
|
|
710 |
if (line->indent) free(line->indent); |
if (line->indent) free(line->indent); |
711 |
|
|
712 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
713 |
free(line->elements[i].item); |
free(line->elements[i].item); |
714 |
free(line->elements[i].indent); |
free(line->elements[i].indent); |
715 |
} |
} |
720 |
|
|
721 |
static int lineWrite(FILE * out, struct singleLine * line, |
static int lineWrite(FILE * out, struct singleLine * line, |
722 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
|
int i; |
|
|
|
|
723 |
if (fprintf(out, "%s", line->indent) == -1) return -1; |
if (fprintf(out, "%s", line->indent) == -1) return -1; |
724 |
|
|
725 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
726 |
|
/* Need to handle this, because we strip the quotes from |
727 |
|
* menuentry when read it. */ |
728 |
|
if (line->type == LT_MENUENTRY && i == 1) { |
729 |
|
if(!isquote(*line->elements[i].item)) |
730 |
|
fprintf(out, "\'%s\'", line->elements[i].item); |
731 |
|
else |
732 |
|
fprintf(out, "%s", line->elements[i].item); |
733 |
|
fprintf(out, "%s", line->elements[i].indent); |
734 |
|
|
735 |
|
continue; |
736 |
|
} |
737 |
|
|
738 |
if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes) |
if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes) |
739 |
if (fputc('"', out) == EOF) return -1; |
if (fputc('"', out) == EOF) return -1; |
740 |
|
|
828 |
if (*line->elements[0].item == '#') { |
if (*line->elements[0].item == '#') { |
829 |
char * fullLine; |
char * fullLine; |
830 |
int len; |
int len; |
|
int i; |
|
831 |
|
|
832 |
len = strlen(line->indent); |
len = strlen(line->indent); |
833 |
for (i = 0; i < line->numElements; i++) |
for (int i = 0; i < line->numElements; i++) |
834 |
len += strlen(line->elements[i].item) + |
len += strlen(line->elements[i].item) + |
835 |
strlen(line->elements[i].indent); |
strlen(line->elements[i].indent); |
836 |
|
|
839 |
free(line->indent); |
free(line->indent); |
840 |
line->indent = fullLine; |
line->indent = fullLine; |
841 |
|
|
842 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
843 |
strcat(fullLine, line->elements[i].item); |
strcat(fullLine, line->elements[i].item); |
844 |
strcat(fullLine, line->elements[i].indent); |
strcat(fullLine, line->elements[i].indent); |
845 |
free(line->elements[i].item); |
free(line->elements[i].item); |
858 |
* elements up more |
* elements up more |
859 |
*/ |
*/ |
860 |
if (!isspace(kw->separatorChar)) { |
if (!isspace(kw->separatorChar)) { |
|
int i; |
|
861 |
char indent[2] = ""; |
char indent[2] = ""; |
862 |
indent[0] = kw->separatorChar; |
indent[0] = kw->separatorChar; |
863 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
864 |
char *p; |
char *p; |
|
int j; |
|
865 |
int numNewElements; |
int numNewElements; |
866 |
|
|
867 |
numNewElements = 0; |
numNewElements = 0; |
877 |
sizeof(*line->elements) * elementsAlloced); |
sizeof(*line->elements) * elementsAlloced); |
878 |
} |
} |
879 |
|
|
880 |
for (j = line->numElements; j > i; j--) { |
for (int j = line->numElements; j > i; j--) { |
881 |
line->elements[j + numNewElements] = line->elements[j]; |
line->elements[j + numNewElements] = line->elements[j]; |
882 |
} |
} |
883 |
line->numElements += numNewElements; |
line->numElements += numNewElements; |
890 |
break; |
break; |
891 |
} |
} |
892 |
|
|
893 |
free(line->elements[i].indent); |
line->elements[i + 1].indent = line->elements[i].indent; |
894 |
line->elements[i].indent = strdup(indent); |
line->elements[i].indent = strdup(indent); |
895 |
*p++ = '\0'; |
*p++ = '\0'; |
896 |
i++; |
i++; |
897 |
line->elements[i].item = strdup(p); |
line->elements[i].item = strdup(p); |
|
line->elements[i].indent = strdup(""); |
|
|
p = line->elements[i].item; |
|
898 |
} |
} |
899 |
} |
} |
900 |
} |
} |
915 |
struct singleLine * last = NULL, * line, * defaultLine = NULL; |
struct singleLine * last = NULL, * line, * defaultLine = NULL; |
916 |
char * end; |
char * end; |
917 |
struct singleEntry * entry = NULL; |
struct singleEntry * entry = NULL; |
918 |
int i, len; |
int len; |
919 |
char * buf; |
char * buf; |
920 |
|
|
921 |
if (!strcmp(inName, "-")) { |
if (!strcmp(inName, "-")) { |
961 |
cfg->secondaryIndent = strdup(line->indent); |
cfg->secondaryIndent = strdup(line->indent); |
962 |
} |
} |
963 |
|
|
964 |
if (isEntryStart(line, cfi)) { |
if (isEntryStart(line, cfi) || (cfg->entries && !sawEntry)) { |
965 |
sawEntry = 1; |
sawEntry = 1; |
966 |
if (!entry) { |
if (!entry) { |
967 |
cfg->entries = malloc(sizeof(*entry)); |
cfg->entries = malloc(sizeof(*entry)); |
977 |
entry->next = NULL; |
entry->next = NULL; |
978 |
} |
} |
979 |
|
|
980 |
if (line->type == LT_DEFAULT && line->numElements == 2) { |
if (line->type == LT_SET_VARIABLE) { |
981 |
|
dbgPrintf("found 'set' command (%d elements): ", line->numElements); |
982 |
|
dbgPrintf("%s", line->indent); |
983 |
|
for (int i = 0; i < line->numElements; i++) |
984 |
|
dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent); |
985 |
|
dbgPrintf("\n"); |
986 |
|
struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi); |
987 |
|
if (kwType && line->numElements == 3 && |
988 |
|
!strcmp(line->elements[1].item, kwType->key)) { |
989 |
|
dbgPrintf("Line sets default config\n"); |
990 |
|
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
991 |
|
defaultLine = line; |
992 |
|
} |
993 |
|
} else if (line->type == LT_DEFAULT && line->numElements == 2) { |
994 |
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
995 |
defaultLine = line; |
defaultLine = line; |
996 |
|
|
1008 |
* This only applies to grub, but that's the only place we |
* This only applies to grub, but that's the only place we |
1009 |
* should find LT_MBMODULE lines anyway. |
* should find LT_MBMODULE lines anyway. |
1010 |
*/ |
*/ |
1011 |
struct singleLine * l; |
for (struct singleLine *l = entry->lines; l; l = l->next) { |
|
for (l = entry->lines; l; l = l->next) { |
|
1012 |
if (l->type == LT_HYPER) |
if (l->type == LT_HYPER) |
1013 |
break; |
break; |
1014 |
else if (l->type == LT_KERNEL) { |
else if (l->type == LT_KERNEL) { |
1028 |
} else if (line->type == LT_TITLE && line->numElements > 1) { |
} else if (line->type == LT_TITLE && line->numElements > 1) { |
1029 |
/* make the title a single argument (undoing our parsing) */ |
/* make the title a single argument (undoing our parsing) */ |
1030 |
len = 0; |
len = 0; |
1031 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
1032 |
len += strlen(line->elements[i].item); |
len += strlen(line->elements[i].item); |
1033 |
len += strlen(line->elements[i].indent); |
len += strlen(line->elements[i].indent); |
1034 |
} |
} |
1035 |
buf = malloc(len + 1); |
buf = malloc(len + 1); |
1036 |
*buf = '\0'; |
*buf = '\0'; |
1037 |
|
|
1038 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
1039 |
strcat(buf, line->elements[i].item); |
strcat(buf, line->elements[i].item); |
1040 |
free(line->elements[i].item); |
free(line->elements[i].item); |
1041 |
|
|
1049 |
line->elements[line->numElements - 1].indent; |
line->elements[line->numElements - 1].indent; |
1050 |
line->elements[1].item = buf; |
line->elements[1].item = buf; |
1051 |
line->numElements = 2; |
line->numElements = 2; |
1052 |
|
} else if (line->type == LT_MENUENTRY && line->numElements > 3) { |
1053 |
|
/* let --remove-kernel="TITLE=what" work */ |
1054 |
|
len = 0; |
1055 |
|
char *extras; |
1056 |
|
char *title; |
1057 |
|
|
1058 |
|
for (int i = 1; i < line->numElements; i++) { |
1059 |
|
len += strlen(line->elements[i].item); |
1060 |
|
len += strlen(line->elements[i].indent); |
1061 |
|
} |
1062 |
|
buf = malloc(len + 1); |
1063 |
|
*buf = '\0'; |
1064 |
|
|
1065 |
|
/* allocate mem for extra flags. */ |
1066 |
|
extras = malloc(len + 1); |
1067 |
|
*extras = '\0'; |
1068 |
|
|
1069 |
|
/* get title. */ |
1070 |
|
for (int i = 0; i < line->numElements; i++) { |
1071 |
|
if (!strcmp(line->elements[i].item, "menuentry")) |
1072 |
|
continue; |
1073 |
|
if (isquote(*line->elements[i].item)) |
1074 |
|
title = line->elements[i].item + 1; |
1075 |
|
else |
1076 |
|
title = line->elements[i].item; |
1077 |
|
|
1078 |
|
len = strlen(title); |
1079 |
|
if (isquote(title[len-1])) { |
1080 |
|
strncat(buf, title,len-1); |
1081 |
|
break; |
1082 |
|
} else { |
1083 |
|
strcat(buf, title); |
1084 |
|
strcat(buf, line->elements[i].indent); |
1085 |
|
} |
1086 |
|
} |
1087 |
|
|
1088 |
|
/* get extras */ |
1089 |
|
int count = 0; |
1090 |
|
for (int i = 0; i < line->numElements; i++) { |
1091 |
|
if (count >= 2) { |
1092 |
|
strcat(extras, line->elements[i].item); |
1093 |
|
strcat(extras, line->elements[i].indent); |
1094 |
|
} |
1095 |
|
|
1096 |
|
if (!strcmp(line->elements[i].item, "menuentry")) |
1097 |
|
continue; |
1098 |
|
|
1099 |
|
/* count ' or ", there should be two in menuentry line. */ |
1100 |
|
if (isquote(*line->elements[i].item)) |
1101 |
|
count++; |
1102 |
|
|
1103 |
|
len = strlen(line->elements[i].item); |
1104 |
|
|
1105 |
|
if (isquote(line->elements[i].item[len -1])) |
1106 |
|
count++; |
1107 |
|
|
1108 |
|
/* ok, we get the final ' or ", others are extras. */ |
1109 |
|
} |
1110 |
|
line->elements[1].indent = |
1111 |
|
line->elements[line->numElements - 2].indent; |
1112 |
|
line->elements[1].item = buf; |
1113 |
|
line->elements[2].indent = |
1114 |
|
line->elements[line->numElements - 2].indent; |
1115 |
|
line->elements[2].item = extras; |
1116 |
|
line->numElements = 3; |
1117 |
} else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) { |
} else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) { |
1118 |
/* Strip off any " which may be present; they'll be put back |
/* Strip off any " which may be present; they'll be put back |
1119 |
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 |
1122 |
if (line->numElements >= 2) { |
if (line->numElements >= 2) { |
1123 |
int last, len; |
int last, len; |
1124 |
|
|
1125 |
if (*line->elements[1].item == '"') |
if (isquote(*line->elements[1].item)) |
1126 |
memmove(line->elements[1].item, line->elements[1].item + 1, |
memmove(line->elements[1].item, line->elements[1].item + 1, |
1127 |
strlen(line->elements[1].item + 1) + 1); |
strlen(line->elements[1].item + 1) + 1); |
1128 |
|
|
1129 |
last = line->numElements - 1; |
last = line->numElements - 1; |
1130 |
len = strlen(line->elements[last].item) - 1; |
len = strlen(line->elements[last].item) - 1; |
1131 |
if (line->elements[last].item[len] == '"') |
if (isquote(line->elements[last].item[len])) |
1132 |
line->elements[last].item[len] = '\0'; |
line->elements[last].item[len] = '\0'; |
1133 |
} |
} |
1134 |
} |
} |
1166 |
entry->lines = line; |
entry->lines = line; |
1167 |
else |
else |
1168 |
last->next = line; |
last->next = line; |
1169 |
dbgPrintf("readConfig added %d to %p\n", line->type, entry); |
dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry); |
1170 |
|
|
1171 |
|
/* we could have seen this outside of an entry... if so, we |
1172 |
|
* ignore it like any other line we don't grok */ |
1173 |
|
if (line->type == LT_ENTRY_END && sawEntry) |
1174 |
|
sawEntry = 0; |
1175 |
} else { |
} else { |
1176 |
if (!cfg->theLines) |
if (!cfg->theLines) |
1177 |
cfg->theLines = line; |
cfg->theLines = line; |
1178 |
else |
else |
1179 |
last->next = line; |
last->next = line; |
1180 |
dbgPrintf("readConfig added %d to cfg\n", line->type); |
dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi)); |
1181 |
} |
} |
1182 |
|
|
1183 |
last = line; |
last = line; |
1185 |
|
|
1186 |
free(incoming); |
free(incoming); |
1187 |
|
|
1188 |
|
dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset"); |
1189 |
if (defaultLine) { |
if (defaultLine) { |
1190 |
if (cfi->defaultSupportSaved && |
if (defaultLine->numElements > 2 && |
1191 |
|
cfi->defaultSupportSaved && |
1192 |
|
!strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) { |
1193 |
|
cfg->defaultImage = DEFAULT_SAVED_GRUB2; |
1194 |
|
} else if (cfi->defaultIsVariable) { |
1195 |
|
char *value = defaultLine->elements[2].item; |
1196 |
|
while (*value && (*value == '"' || *value == '\'' || |
1197 |
|
*value == ' ' || *value == '\t')) |
1198 |
|
value++; |
1199 |
|
cfg->defaultImage = strtol(value, &end, 10); |
1200 |
|
while (*end && (*end == '"' || *end == '\'' || |
1201 |
|
*end == ' ' || *end == '\t')) |
1202 |
|
end++; |
1203 |
|
if (*end) cfg->defaultImage = -1; |
1204 |
|
} else if (cfi->defaultSupportSaved && |
1205 |
!strncmp(defaultLine->elements[1].item, "saved", 5)) { |
!strncmp(defaultLine->elements[1].item, "saved", 5)) { |
1206 |
cfg->defaultImage = DEFAULT_SAVED; |
cfg->defaultImage = DEFAULT_SAVED; |
1207 |
} else if (cfi->defaultIsIndex) { |
} else if (cfi->defaultIsIndex) { |
1208 |
cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10); |
cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10); |
1209 |
if (*end) cfg->defaultImage = -1; |
if (*end) cfg->defaultImage = -1; |
1210 |
} else if (defaultLine->numElements >= 2) { |
} else if (defaultLine->numElements >= 2) { |
1211 |
i = 0; |
int i = 0; |
1212 |
while ((entry = findEntryByIndex(cfg, i))) { |
while ((entry = findEntryByIndex(cfg, i))) { |
1213 |
for (line = entry->lines; line; line = line->next) |
for (line = entry->lines; line; line = line->next) |
1214 |
if (line->type == LT_TITLE) break; |
if (line->type == LT_TITLE) break; |
1248 |
|
|
1249 |
if (cfg->defaultImage == DEFAULT_SAVED) |
if (cfg->defaultImage == DEFAULT_SAVED) |
1250 |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
1251 |
|
else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2) |
1252 |
|
fprintf(out, "%sset default=\"${saved_entry}\"\n", indent); |
1253 |
else if (cfg->defaultImage > -1) { |
else if (cfg->defaultImage > -1) { |
1254 |
if (cfg->cfi->defaultIsIndex) { |
if (cfg->cfi->defaultIsIndex) { |
1255 |
fprintf(out, "%sdefault%s%d\n", indent, separator, |
if (cfg->cfi->defaultIsVariable) { |
1256 |
cfg->defaultImage); |
fprintf(out, "%sset default=\"%d\"\n", indent, |
1257 |
|
cfg->defaultImage); |
1258 |
|
} else { |
1259 |
|
fprintf(out, "%sdefault%s%d\n", indent, separator, |
1260 |
|
cfg->defaultImage); |
1261 |
|
} |
1262 |
} else { |
} else { |
1263 |
int image = cfg->defaultImage; |
int image = cfg->defaultImage; |
1264 |
|
|
1310 |
|
|
1311 |
/* most likely the symlink is relative, so change our |
/* most likely the symlink is relative, so change our |
1312 |
directory to the dir of the symlink */ |
directory to the dir of the symlink */ |
1313 |
rc = chdir(dirname(strdupa(outName))); |
rc = chdir(dirname(outName)); |
1314 |
do { |
do { |
1315 |
buf = alloca(len + 1); |
buf = alloca(len + 1); |
1316 |
rc = readlink(basename(outName), buf, len); |
rc = readlink(basename(outName), buf, len); |
1348 |
} |
} |
1349 |
|
|
1350 |
line = cfg->theLines; |
line = cfg->theLines; |
1351 |
|
struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi); |
1352 |
while (line) { |
while (line) { |
1353 |
if (line->type == LT_DEFAULT) { |
if (line->type == LT_SET_VARIABLE && defaultKw && |
1354 |
|
line->numElements == 3 && |
1355 |
|
!strcmp(line->elements[1].item, defaultKw->key)) { |
1356 |
|
writeDefault(out, line->indent, line->elements[0].indent, cfg); |
1357 |
|
needs &= ~MAIN_DEFAULT; |
1358 |
|
} else if (line->type == LT_DEFAULT) { |
1359 |
writeDefault(out, line->indent, line->elements[0].indent, cfg); |
writeDefault(out, line->indent, line->elements[0].indent, cfg); |
1360 |
needs &= ~MAIN_DEFAULT; |
needs &= ~MAIN_DEFAULT; |
1361 |
} else if (line->type == LT_FALLBACK) { |
} else if (line->type == LT_FALLBACK) { |
1448 |
buf[rc] = '\0'; |
buf[rc] = '\0'; |
1449 |
chptr = buf; |
chptr = buf; |
1450 |
|
|
1451 |
|
char *foundanswer = NULL; |
1452 |
|
|
1453 |
while (chptr && chptr != buf+rc) { |
while (chptr && chptr != buf+rc) { |
1454 |
devname = chptr; |
devname = chptr; |
1455 |
|
|
1477 |
* for '/' obviously. |
* for '/' obviously. |
1478 |
*/ |
*/ |
1479 |
if (*(++chptr) == '/' && *(++chptr) == ' ') { |
if (*(++chptr) == '/' && *(++chptr) == ' ') { |
1480 |
/* |
/* remember the last / entry in mtab */ |
1481 |
* Move back 2, which is the first space after the device name, set |
foundanswer = devname; |
|
* it to \0 so strdup will just get the devicename. |
|
|
*/ |
|
|
chptr -= 2; |
|
|
*chptr = '\0'; |
|
|
return strdup(devname); |
|
1482 |
} |
} |
1483 |
|
|
1484 |
/* Next line */ |
/* Next line */ |
1487 |
chptr++; |
chptr++; |
1488 |
} |
} |
1489 |
|
|
1490 |
|
/* Return the last / entry found */ |
1491 |
|
if (foundanswer) { |
1492 |
|
chptr = strchr(foundanswer, ' '); |
1493 |
|
*chptr = '\0'; |
1494 |
|
return strdup(foundanswer); |
1495 |
|
} |
1496 |
|
|
1497 |
return NULL; |
return NULL; |
1498 |
} |
} |
1499 |
|
|
1500 |
|
void printEntry(struct singleEntry * entry) { |
1501 |
|
int i; |
1502 |
|
struct singleLine * line; |
1503 |
|
|
1504 |
|
for (line = entry->lines; line; line = line->next) { |
1505 |
|
fprintf(stderr, "DBG: %s", line->indent); |
1506 |
|
for (i = 0; i < line->numElements; i++) { |
1507 |
|
/* Need to handle this, because we strip the quotes from |
1508 |
|
* menuentry when read it. */ |
1509 |
|
if (line->type == LT_MENUENTRY && i == 1) { |
1510 |
|
if(!isquote(*line->elements[i].item)) |
1511 |
|
fprintf(stderr, "\'%s\'", line->elements[i].item); |
1512 |
|
else |
1513 |
|
fprintf(stderr, "%s", line->elements[i].item); |
1514 |
|
fprintf(stderr, "%s", line->elements[i].indent); |
1515 |
|
|
1516 |
|
continue; |
1517 |
|
} |
1518 |
|
|
1519 |
|
fprintf(stderr, "%s%s", |
1520 |
|
line->elements[i].item, line->elements[i].indent); |
1521 |
|
} |
1522 |
|
fprintf(stderr, "\n"); |
1523 |
|
} |
1524 |
|
} |
1525 |
|
|
1526 |
|
void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...) |
1527 |
|
{ |
1528 |
|
va_list argp; |
1529 |
|
|
1530 |
|
if (!debug) |
1531 |
|
return; |
1532 |
|
|
1533 |
|
va_start(argp, fmt); |
1534 |
|
fprintf(stderr, "DBG: Image entry failed: "); |
1535 |
|
vfprintf(stderr, fmt, argp); |
1536 |
|
printEntry(entry); |
1537 |
|
va_end(argp); |
1538 |
|
} |
1539 |
|
|
1540 |
|
#define beginswith(s, c) ((s) && (s)[0] == (c)) |
1541 |
|
|
1542 |
|
static int endswith(const char *s, char c) |
1543 |
|
{ |
1544 |
|
int slen; |
1545 |
|
|
1546 |
|
if (!s || !s[0]) |
1547 |
|
return 0; |
1548 |
|
slen = strlen(s) - 1; |
1549 |
|
|
1550 |
|
return s[slen] == c; |
1551 |
|
} |
1552 |
|
|
1553 |
int suitableImage(struct singleEntry * entry, const char * bootPrefix, |
int suitableImage(struct singleEntry * entry, const char * bootPrefix, |
1554 |
int skipRemoved, int flags) { |
int skipRemoved, int flags) { |
1555 |
struct singleLine * line; |
struct singleLine * line; |
1559 |
char * rootspec; |
char * rootspec; |
1560 |
char * rootdev; |
char * rootdev; |
1561 |
|
|
1562 |
if (skipRemoved && entry->skip) return 0; |
if (skipRemoved && entry->skip) { |
1563 |
|
notSuitablePrintf(entry, "marked to skip\n"); |
1564 |
|
return 0; |
1565 |
|
} |
1566 |
|
|
1567 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
1568 |
if (!line || line->numElements < 2) return 0; |
if (!line) { |
1569 |
|
notSuitablePrintf(entry, "no line found\n"); |
1570 |
|
return 0; |
1571 |
|
} |
1572 |
|
if (line->numElements < 2) { |
1573 |
|
notSuitablePrintf(entry, "line has only %d elements\n", |
1574 |
|
line->numElements); |
1575 |
|
return 0; |
1576 |
|
} |
1577 |
|
|
1578 |
if (flags & GRUBBY_BADIMAGE_OKAY) return 1; |
if (flags & GRUBBY_BADIMAGE_OKAY) return 1; |
1579 |
|
|
1580 |
fullName = alloca(strlen(bootPrefix) + |
fullName = alloca(strlen(bootPrefix) + |
1581 |
strlen(line->elements[1].item) + 1); |
strlen(line->elements[1].item) + 1); |
1582 |
rootspec = getRootSpecifier(line->elements[1].item); |
rootspec = getRootSpecifier(line->elements[1].item); |
1583 |
sprintf(fullName, "%s%s", bootPrefix, |
int rootspec_offset = rootspec ? strlen(rootspec) : 0; |
1584 |
line->elements[1].item + (rootspec ? strlen(rootspec) : 0)); |
int hasslash = endswith(bootPrefix, '/') || |
1585 |
if (access(fullName, R_OK)) return 0; |
beginswith(line->elements[1].item + rootspec_offset, '/'); |
1586 |
|
sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/", |
1587 |
|
line->elements[1].item + rootspec_offset); |
1588 |
|
if (access(fullName, R_OK)) { |
1589 |
|
notSuitablePrintf(entry, "access to %s failed\n", fullName); |
1590 |
|
return 0; |
1591 |
|
} |
1592 |
for (i = 2; i < line->numElements; i++) |
for (i = 2; i < line->numElements; i++) |
1593 |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
1594 |
if (i < line->numElements) { |
if (i < line->numElements) { |
1606 |
line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines); |
line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines); |
1607 |
|
|
1608 |
/* failed to find one */ |
/* failed to find one */ |
1609 |
if (!line) return 0; |
if (!line) { |
1610 |
|
notSuitablePrintf(entry, "no line found\n"); |
1611 |
|
return 0; |
1612 |
|
} |
1613 |
|
|
1614 |
for (i = 1; i < line->numElements; i++) |
for (i = 1; i < line->numElements; i++) |
1615 |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
1616 |
if (i < line->numElements) |
if (i < line->numElements) |
1617 |
dev = line->elements[i].item + 5; |
dev = line->elements[i].item + 5; |
1618 |
else { |
else { |
1619 |
|
notSuitablePrintf(entry, "no root= entry found\n"); |
1620 |
/* it failed too... can't find root= */ |
/* it failed too... can't find root= */ |
1621 |
return 0; |
return 0; |
1622 |
} |
} |
1624 |
} |
} |
1625 |
|
|
1626 |
dev = getpathbyspec(dev); |
dev = getpathbyspec(dev); |
1627 |
if (!dev) |
if (!getpathbyspec(dev)) { |
1628 |
|
notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev); |
1629 |
return 0; |
return 0; |
1630 |
|
} else |
1631 |
|
dev = getpathbyspec(dev); |
1632 |
|
|
1633 |
rootdev = findDiskForRoot(); |
rootdev = findDiskForRoot(); |
1634 |
if (!rootdev) |
if (!rootdev) { |
1635 |
|
notSuitablePrintf(entry, "can't find root device\n"); |
1636 |
return 0; |
return 0; |
1637 |
|
} |
1638 |
|
|
1639 |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
1640 |
|
notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n", |
1641 |
|
getuuidbydev(rootdev), getuuidbydev(dev)); |
1642 |
free(rootdev); |
free(rootdev); |
1643 |
return 0; |
return 0; |
1644 |
} |
} |
1645 |
|
|
1646 |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
1647 |
|
notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n", |
1648 |
|
getuuidbydev(rootdev), getuuidbydev(dev)); |
1649 |
free(rootdev); |
free(rootdev); |
1650 |
return 0; |
return 0; |
1651 |
} |
} |
1732 |
|
|
1733 |
if (!strncmp(kernel, "TITLE=", 6)) { |
if (!strncmp(kernel, "TITLE=", 6)) { |
1734 |
prefix = ""; |
prefix = ""; |
1735 |
checkType = LT_TITLE; |
checkType = LT_TITLE|LT_MENUENTRY; |
1736 |
kernel += 6; |
kernel += 6; |
1737 |
} |
} |
1738 |
|
|
1748 |
checkType, line); |
checkType, line); |
1749 |
if (!line) break; /* not found in this entry */ |
if (!line) break; /* not found in this entry */ |
1750 |
|
|
1751 |
if (line && line->numElements >= 2) { |
if (line && line->type != LT_MENUENTRY && |
1752 |
|
line->numElements >= 2) { |
1753 |
rootspec = getRootSpecifier(line->elements[1].item); |
rootspec = getRootSpecifier(line->elements[1].item); |
1754 |
if (!strcmp(line->elements[1].item + |
if (!strcmp(line->elements[1].item + |
1755 |
((rootspec != NULL) ? strlen(rootspec) : 0), |
((rootspec != NULL) ? strlen(rootspec) : 0), |
1756 |
kernel + strlen(prefix))) |
kernel + strlen(prefix))) |
1757 |
break; |
break; |
1758 |
} |
} |
1759 |
|
if(line->type == LT_MENUENTRY && |
1760 |
|
!strcmp(line->elements[1].item, kernel)) |
1761 |
|
break; |
1762 |
} |
} |
1763 |
|
|
1764 |
/* make sure this entry has a kernel identifier; this skips |
/* make sure this entry has a kernel identifier; this skips |
1850 |
const char * prefix) { |
const char * prefix) { |
1851 |
struct singleEntry * entry; |
struct singleEntry * entry; |
1852 |
|
|
1853 |
if (!image) return; |
if (!image) |
1854 |
|
return; |
1855 |
|
|
1856 |
|
/* check and see if we're removing the default image */ |
1857 |
|
if (isdigit(*image)) { |
1858 |
|
entry = findEntryByPath(cfg, image, prefix, NULL); |
1859 |
|
if(entry) |
1860 |
|
entry->skip = 1; |
1861 |
|
return; |
1862 |
|
} |
1863 |
|
|
1864 |
while ((entry = findEntryByPath(cfg, image, prefix, NULL))) |
while ((entry = findEntryByPath(cfg, image, prefix, NULL))) |
1865 |
entry->skip = 1; |
entry->skip = 1; |
1886 |
|
|
1887 |
/* 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 |
1888 |
changes */ |
changes */ |
1889 |
if (config->defaultImage == DEFAULT_SAVED) |
if ((config->defaultImage == DEFAULT_SAVED) || |
1890 |
|
(config->defaultImage == DEFAULT_SAVED_GRUB2)) |
1891 |
/* default is set to saved, we don't want to change it */ |
/* default is set to saved, we don't want to change it */ |
1892 |
return; |
return; |
1893 |
|
|
1954 |
return; |
return; |
1955 |
} |
} |
1956 |
|
|
1957 |
printf("kernel=%s\n", line->elements[1].item); |
printf("kernel=%s%s\n", prefix, line->elements[1].item); |
1958 |
|
|
1959 |
if (line->numElements >= 3) { |
if (line->numElements >= 3) { |
1960 |
printf("args=\""); |
printf("args=\""); |
2018 |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
2019 |
printf("\n"); |
printf("\n"); |
2020 |
} |
} |
2021 |
|
|
2022 |
|
line = getLineByType(LT_TITLE, entry->lines); |
2023 |
|
if (line) { |
2024 |
|
printf("title=%s\n", line->elements[1].item); |
2025 |
|
} else { |
2026 |
|
char * title; |
2027 |
|
line = getLineByType(LT_MENUENTRY, entry->lines); |
2028 |
|
title = grub2ExtractTitle(line); |
2029 |
|
if (title) |
2030 |
|
printf("title=%s\n", title); |
2031 |
|
} |
2032 |
} |
} |
2033 |
|
|
2034 |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
2189 |
sprintf(tmpl.elements[0].item, "[%s]", val); |
sprintf(tmpl.elements[0].item, "[%s]", val); |
2190 |
tmpl.elements[0].indent = ""; |
tmpl.elements[0].indent = ""; |
2191 |
val = NULL; |
val = NULL; |
2192 |
|
} else if (type == LT_MENUENTRY) { |
2193 |
|
char *lineend = "--class gnu-linux --class gnu --class os {"; |
2194 |
|
if (!val) { |
2195 |
|
fprintf(stderr, "Line type LT_MENUENTRY requires a value\n"); |
2196 |
|
abort(); |
2197 |
|
} |
2198 |
|
kw = getKeywordByType(type, cfi); |
2199 |
|
if (!kw) { |
2200 |
|
fprintf(stderr, "Looking up keyword for unknown type %d\n", type); |
2201 |
|
abort(); |
2202 |
|
} |
2203 |
|
tmpl.indent = ""; |
2204 |
|
tmpl.type = type; |
2205 |
|
tmpl.numElements = 3; |
2206 |
|
tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements); |
2207 |
|
tmpl.elements[0].item = kw->key; |
2208 |
|
tmpl.elements[0].indent = alloca(2); |
2209 |
|
sprintf(tmpl.elements[0].indent, "%c", kw->nextChar); |
2210 |
|
tmpl.elements[1].item = (char *)val; |
2211 |
|
tmpl.elements[1].indent = alloca(2); |
2212 |
|
sprintf(tmpl.elements[1].indent, "%c", kw->nextChar); |
2213 |
|
tmpl.elements[2].item = alloca(strlen(lineend)+1); |
2214 |
|
strcpy(tmpl.elements[2].item, lineend); |
2215 |
|
tmpl.elements[2].indent = ""; |
2216 |
} else { |
} else { |
2217 |
kw = getKeywordByType(type, cfi); |
kw = getKeywordByType(type, cfi); |
2218 |
if (!kw) abort(); |
if (!kw) { |
2219 |
|
fprintf(stderr, "Looking up keyword for unknown type %d\n", type); |
2220 |
|
abort(); |
2221 |
|
} |
2222 |
tmpl.type = type; |
tmpl.type = type; |
2223 |
tmpl.numElements = val ? 2 : 1; |
tmpl.numElements = val ? 2 : 1; |
2224 |
tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements); |
tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements); |
2242 |
if (!line->next && !prev) prev = line; |
if (!line->next && !prev) prev = line; |
2243 |
} |
} |
2244 |
|
|
2245 |
if (prev == entry->lines) |
struct singleLine *menuEntry; |
2246 |
tmpl.indent = defaultIndent ?: ""; |
menuEntry = getLineByType(LT_MENUENTRY, entry->lines); |
2247 |
else |
if (tmpl.type == LT_ENTRY_END) { |
2248 |
tmpl.indent = prev->indent; |
if (menuEntry) |
2249 |
|
tmpl.indent = menuEntry->indent; |
2250 |
|
else |
2251 |
|
tmpl.indent = defaultIndent ?: ""; |
2252 |
|
} else if (tmpl.type != LT_MENUENTRY) { |
2253 |
|
if (menuEntry) |
2254 |
|
tmpl.indent = "\t"; |
2255 |
|
else if (prev == entry->lines) |
2256 |
|
tmpl.indent = defaultIndent ?: ""; |
2257 |
|
else |
2258 |
|
tmpl.indent = prev->indent; |
2259 |
|
} |
2260 |
|
|
2261 |
return addLineTmpl(entry, &tmpl, prev, val, cfi); |
return addLineTmpl(entry, &tmpl, prev, val, cfi); |
2262 |
} |
} |
2283 |
free(line); |
free(line); |
2284 |
} |
} |
2285 |
|
|
2286 |
|
static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi) |
2287 |
|
{ |
2288 |
|
struct singleLine newLine = { |
2289 |
|
.indent = tmplLine->indent, |
2290 |
|
.type = tmplLine->type, |
2291 |
|
.next = tmplLine->next, |
2292 |
|
}; |
2293 |
|
int firstQuotedItem = -1; |
2294 |
|
int quoteLen = 0; |
2295 |
|
int j; |
2296 |
|
int element = 0; |
2297 |
|
char *c; |
2298 |
|
|
2299 |
|
c = malloc(strlen(tmplLine->elements[0].item) + 1); |
2300 |
|
strcpy(c, tmplLine->elements[0].item); |
2301 |
|
insertElement(&newLine, c, element++, cfi); |
2302 |
|
free(c); |
2303 |
|
c = NULL; |
2304 |
|
|
2305 |
|
for (j = 1; j < tmplLine->numElements; j++) { |
2306 |
|
if (firstQuotedItem == -1) { |
2307 |
|
quoteLen += strlen(tmplLine->elements[j].item); |
2308 |
|
|
2309 |
|
if (isquote(tmplLine->elements[j].item[0])) { |
2310 |
|
firstQuotedItem = j; |
2311 |
|
quoteLen += strlen(tmplLine->elements[j].indent); |
2312 |
|
} else { |
2313 |
|
c = malloc(quoteLen + 1); |
2314 |
|
strcpy(c, tmplLine->elements[j].item); |
2315 |
|
insertElement(&newLine, c, element++, cfi); |
2316 |
|
free(c); |
2317 |
|
quoteLen = 0; |
2318 |
|
} |
2319 |
|
} else { |
2320 |
|
int itemlen = strlen(tmplLine->elements[j].item); |
2321 |
|
quoteLen += itemlen; |
2322 |
|
quoteLen += strlen(tmplLine->elements[j].indent); |
2323 |
|
|
2324 |
|
if (isquote(tmplLine->elements[j].item[itemlen - 1])) { |
2325 |
|
c = malloc(quoteLen + 1); |
2326 |
|
c[0] = '\0'; |
2327 |
|
for (int i = firstQuotedItem; i < j+1; i++) { |
2328 |
|
strcat(c, tmplLine->elements[i].item); |
2329 |
|
strcat(c, tmplLine->elements[i].indent); |
2330 |
|
} |
2331 |
|
insertElement(&newLine, c, element++, cfi); |
2332 |
|
free(c); |
2333 |
|
|
2334 |
|
firstQuotedItem = -1; |
2335 |
|
quoteLen = 0; |
2336 |
|
} |
2337 |
|
} |
2338 |
|
} |
2339 |
|
while (tmplLine->numElements) |
2340 |
|
removeElement(tmplLine, 0); |
2341 |
|
if (tmplLine->elements) |
2342 |
|
free(tmplLine->elements); |
2343 |
|
|
2344 |
|
tmplLine->numElements = newLine.numElements; |
2345 |
|
tmplLine->elements = newLine.elements; |
2346 |
|
} |
2347 |
|
|
2348 |
static void insertElement(struct singleLine * line, |
static void insertElement(struct singleLine * line, |
2349 |
const char * item, int insertHere, |
const char * item, int insertHere, |
2350 |
struct configFileInfo * cfi) |
struct configFileInfo * cfi) |
2672 |
int updateInitrd(struct grubConfig * cfg, const char * image, |
int updateInitrd(struct grubConfig * cfg, const char * image, |
2673 |
const char * prefix, const char * initrd) { |
const char * prefix, const char * initrd) { |
2674 |
struct singleEntry * entry; |
struct singleEntry * entry; |
2675 |
struct singleLine * line, * kernelLine; |
struct singleLine * line, * kernelLine, *endLine = NULL; |
2676 |
int index = 0; |
int index = 0; |
2677 |
|
|
2678 |
if (!image) return 0; |
if (!image) return 0; |
2689 |
if (!strncmp(initrd, prefix, prefixLen)) |
if (!strncmp(initrd, prefix, prefixLen)) |
2690 |
initrd += prefixLen; |
initrd += prefixLen; |
2691 |
} |
} |
2692 |
|
endLine = getLineByType(LT_ENTRY_END, entry->lines); |
2693 |
|
if (endLine) |
2694 |
|
removeLine(entry, endLine); |
2695 |
line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd); |
line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd); |
2696 |
if (!line) return 1; |
if (!line) |
2697 |
|
return 1; |
2698 |
|
if (endLine) { |
2699 |
|
line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL); |
2700 |
|
if (!line) |
2701 |
|
return 1; |
2702 |
|
} |
2703 |
|
|
2704 |
break; |
break; |
2705 |
} |
} |
2706 |
|
|
2730 |
if (memcmp(boot, bootSect, 3)) |
if (memcmp(boot, bootSect, 3)) |
2731 |
return 0; |
return 0; |
2732 |
|
|
2733 |
if (boot[1] == 0xeb) { |
if (boot[1] == JMP_SHORT_OPCODE) { |
2734 |
offset = boot[2] + 2; |
offset = boot[2] + 2; |
2735 |
} else if (boot[1] == 0xe8 || boot[1] == 0xe9) { |
} else if (boot[1] == 0xe8 || boot[1] == 0xe9) { |
2736 |
offset = (boot[3] << 8) + boot[2] + 2; |
offset = (boot[3] << 8) + boot[2] + 2; |
2737 |
} else if (boot[0] == 0xeb) { |
} else if (boot[0] == JMP_SHORT_OPCODE) { |
2738 |
offset = boot[1] + 2; |
offset = boot[1] + 2; |
2739 |
|
/* |
2740 |
|
* it looks like grub, when copying stage1 into the mbr, patches stage1 |
2741 |
|
* right after the JMP location, replacing other instructions such as |
2742 |
|
* JMPs for NOOPs. So, relax the check a little bit by skipping those |
2743 |
|
* different bytes. |
2744 |
|
*/ |
2745 |
|
if ((bootSect[offset + 1] == NOOP_OPCODE) |
2746 |
|
&& (bootSect[offset + 2] == NOOP_OPCODE)) { |
2747 |
|
offset = offset + 3; |
2748 |
|
} |
2749 |
} else if (boot[0] == 0xe8 || boot[0] == 0xe9) { |
} else if (boot[0] == 0xe8 || boot[0] == 0xe9) { |
2750 |
offset = (boot[2] << 8) + boot[1] + 2; |
offset = (boot[2] << 8) + boot[1] + 2; |
2751 |
} else { |
} else { |
2887 |
return checkDeviceBootloader(line->elements[1].item, boot); |
return checkDeviceBootloader(line->elements[1].item, boot); |
2888 |
} |
} |
2889 |
|
|
2890 |
|
int checkForGrub2(struct grubConfig * config) { |
2891 |
|
if (!access("/etc/grub.d/", R_OK)) |
2892 |
|
return 2; |
2893 |
|
|
2894 |
|
return 1; |
2895 |
|
} |
2896 |
|
|
2897 |
int checkForGrub(struct grubConfig * config) { |
int checkForGrub(struct grubConfig * config) { |
2898 |
int fd; |
int fd; |
2899 |
unsigned char bootSect[512]; |
unsigned char bootSect[512]; |
2966 |
static char * getInitrdVal(struct grubConfig * config, |
static char * getInitrdVal(struct grubConfig * config, |
2967 |
const char * prefix, struct singleLine *tmplLine, |
const char * prefix, struct singleLine *tmplLine, |
2968 |
const char * newKernelInitrd, |
const char * newKernelInitrd, |
2969 |
char ** extraInitrds, int extraInitrdCount) |
const char ** extraInitrds, int extraInitrdCount) |
2970 |
{ |
{ |
2971 |
char *initrdVal, *end; |
char *initrdVal, *end; |
2972 |
int i; |
int i; |
3011 |
|
|
3012 |
int addNewKernel(struct grubConfig * config, struct singleEntry * template, |
int addNewKernel(struct grubConfig * config, struct singleEntry * template, |
3013 |
const char * prefix, |
const char * prefix, |
3014 |
char * newKernelPath, char * newKernelTitle, |
const char * newKernelPath, const char * newKernelTitle, |
3015 |
char * newKernelArgs, char * newKernelInitrd, |
const char * newKernelArgs, const char * newKernelInitrd, |
3016 |
char ** extraInitrds, int extraInitrdCount, |
const char ** extraInitrds, int extraInitrdCount, |
3017 |
char * newMBKernel, char * newMBKernelArgs) { |
const char * newMBKernel, const char * newMBKernelArgs) { |
3018 |
struct singleEntry * new; |
struct singleEntry * new; |
3019 |
struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL; |
struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL; |
3020 |
int needs; |
int needs; |
3069 |
if (*chptr == '#') continue; |
if (*chptr == '#') continue; |
3070 |
|
|
3071 |
if (tmplLine->type == LT_KERNEL && |
if (tmplLine->type == LT_KERNEL && |
3072 |
tmplLine->numElements >= 2) { |
tmplLine->numElements >= 2) { |
3073 |
if (!template->multiboot && (needs & NEED_MB)) { |
if (!template->multiboot && (needs & NEED_MB)) { |
3074 |
/* it's not a multiboot template and this is the kernel |
/* it's not a multiboot template and this is the kernel |
3075 |
* line. Try to be intelligent about inserting the |
* line. Try to be intelligent about inserting the |
3195 |
needs &= ~NEED_INITRD; |
needs &= ~NEED_INITRD; |
3196 |
} |
} |
3197 |
|
|
3198 |
|
} else if (tmplLine->type == LT_MENUENTRY && |
3199 |
|
(needs & NEED_TITLE)) { |
3200 |
|
requote(tmplLine, config->cfi); |
3201 |
|
char *nkt = malloc(strlen(newKernelTitle)+3); |
3202 |
|
strcpy(nkt, "'"); |
3203 |
|
strcat(nkt, newKernelTitle); |
3204 |
|
strcat(nkt, "'"); |
3205 |
|
newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi); |
3206 |
|
free(nkt); |
3207 |
|
needs &= ~NEED_TITLE; |
3208 |
} else if (tmplLine->type == LT_TITLE && |
} else if (tmplLine->type == LT_TITLE && |
3209 |
(needs & NEED_TITLE)) { |
(needs & NEED_TITLE)) { |
3210 |
if (tmplLine->numElements >= 2) { |
if (tmplLine->numElements >= 2) { |
3218 |
tmplLine->indent, newKernelTitle); |
tmplLine->indent, newKernelTitle); |
3219 |
needs &= ~NEED_TITLE; |
needs &= ~NEED_TITLE; |
3220 |
} |
} |
3221 |
|
} else if (tmplLine->type == LT_ECHO) { |
3222 |
|
requote(tmplLine, config->cfi); |
3223 |
|
static const char *prefix = "'Loading "; |
3224 |
|
if (tmplLine->numElements > 1 && |
3225 |
|
strstr(tmplLine->elements[1].item, prefix) && |
3226 |
|
masterLine->next && masterLine->next->type == LT_KERNEL) { |
3227 |
|
char *newTitle = malloc(strlen(prefix) + |
3228 |
|
strlen(newKernelTitle) + 2); |
3229 |
|
|
3230 |
|
strcpy(newTitle, prefix); |
3231 |
|
strcat(newTitle, newKernelTitle); |
3232 |
|
strcat(newTitle, "'"); |
3233 |
|
newLine = addLine(new, config->cfi, LT_ECHO, |
3234 |
|
tmplLine->indent, newTitle); |
3235 |
|
free(newTitle); |
3236 |
|
} else { |
3237 |
|
/* pass through other lines from the template */ |
3238 |
|
newLine = addLineTmpl(new, tmplLine, newLine, NULL, |
3239 |
|
config->cfi); |
3240 |
|
} |
3241 |
} else { |
} else { |
3242 |
/* pass through other lines from the template */ |
/* pass through other lines from the template */ |
3243 |
newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi); |
newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi); |
3267 |
needs &= ~NEED_MB; |
needs &= ~NEED_MB; |
3268 |
break; |
break; |
3269 |
|
|
3270 |
|
case LT_MENUENTRY: { |
3271 |
|
char *nkt = malloc(strlen(newKernelTitle)+3); |
3272 |
|
strcpy(nkt, "'"); |
3273 |
|
strcat(nkt, newKernelTitle); |
3274 |
|
strcat(nkt, "'"); |
3275 |
|
newLine = addLine(new, config->cfi, LT_MENUENTRY, |
3276 |
|
config->primaryIndent, nkt); |
3277 |
|
free(nkt); |
3278 |
|
needs &= ~NEED_TITLE; |
3279 |
|
needs |= NEED_END; |
3280 |
|
break; |
3281 |
|
} |
3282 |
case LT_TITLE: |
case LT_TITLE: |
3283 |
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) |
3284 |
char * templabel; |
char * templabel; |
3353 |
free(initrdVal); |
free(initrdVal); |
3354 |
needs &= ~NEED_INITRD; |
needs &= ~NEED_INITRD; |
3355 |
} |
} |
3356 |
|
if (needs & NEED_END) { |
3357 |
|
newLine = addLine(new, config->cfi, LT_ENTRY_END, |
3358 |
|
config->secondaryIndent, NULL); |
3359 |
|
needs &= ~NEED_END; |
3360 |
|
} |
3361 |
|
|
3362 |
if (needs) { |
if (needs) { |
3363 |
printf(_("grubby: needs=%d, aborting\n"), needs); |
printf(_("grubby: needs=%d, aborting\n"), needs); |
3387 |
|
|
3388 |
int main(int argc, const char ** argv) { |
int main(int argc, const char ** argv) { |
3389 |
poptContext optCon; |
poptContext optCon; |
3390 |
char * grubConfig = NULL; |
const char * grubConfig = NULL; |
3391 |
char * outputFile = NULL; |
char * outputFile = NULL; |
3392 |
int arg = 0; |
int arg = 0; |
3393 |
int flags = 0; |
int flags = 0; |
3394 |
int badImageOkay = 0; |
int badImageOkay = 0; |
3395 |
|
int configureGrub2 = 0; |
3396 |
int configureLilo = 0, configureELilo = 0, configureGrub = 0; |
int configureLilo = 0, configureELilo = 0, configureGrub = 0; |
3397 |
int configureYaboot = 0, configureSilo = 0, configureZipl = 0; |
int configureYaboot = 0, configureSilo = 0, configureZipl = 0; |
3398 |
int configureExtLinux = 0; |
int configureExtLinux = 0; |
3420 |
struct singleEntry * template = NULL; |
struct singleEntry * template = NULL; |
3421 |
int copyDefault = 0, makeDefault = 0; |
int copyDefault = 0, makeDefault = 0; |
3422 |
int displayDefault = 0; |
int displayDefault = 0; |
3423 |
|
int displayDefaultIndex = 0; |
3424 |
|
int displayDefaultTitle = 0; |
3425 |
struct poptOption options[] = { |
struct poptOption options[] = { |
3426 |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
3427 |
_("add an entry for the specified kernel"), _("kernel-path") }, |
_("add an entry for the specified kernel"), _("kernel-path") }, |
3452 |
"the kernel referenced by the default image does not exist, " |
"the kernel referenced by the default image does not exist, " |
3453 |
"the first linux entry whose kernel does exist is used as the " |
"the first linux entry whose kernel does exist is used as the " |
3454 |
"template"), NULL }, |
"template"), NULL }, |
3455 |
|
{ "debug", 0, 0, &debug, 0, |
3456 |
|
_("print debugging information for failures") }, |
3457 |
{ "default-kernel", 0, 0, &displayDefault, 0, |
{ "default-kernel", 0, 0, &displayDefault, 0, |
3458 |
_("display the path of the default kernel") }, |
_("display the path of the default kernel") }, |
3459 |
|
{ "default-index", 0, 0, &displayDefaultIndex, 0, |
3460 |
|
_("display the index of the default kernel") }, |
3461 |
|
{ "default-title", 0, 0, &displayDefaultTitle, 0, |
3462 |
|
_("display the title of the default kernel") }, |
3463 |
{ "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, |
{ "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, |
3464 |
_("configure elilo bootloader") }, |
_("configure elilo bootloader") }, |
3465 |
{ "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0, |
{ "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0, |
3466 |
_("configure extlinux bootloader (from syslinux)") }, |
_("configure extlinux bootloader (from syslinux)") }, |
3467 |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
3468 |
_("configure grub bootloader") }, |
_("configure grub bootloader") }, |
3469 |
|
{ "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0, |
3470 |
|
_("configure grub2 bootloader") }, |
3471 |
{ "info", 0, POPT_ARG_STRING, &kernelInfo, 0, |
{ "info", 0, POPT_ARG_STRING, &kernelInfo, 0, |
3472 |
_("display boot information for specified kernel"), |
_("display boot information for specified kernel"), |
3473 |
_("kernel-path") }, |
_("kernel-path") }, |
3547 |
return 1; |
return 1; |
3548 |
} |
} |
3549 |
|
|
3550 |
if ((configureLilo + configureGrub + configureELilo + |
if ((configureLilo + configureGrub2 + configureGrub + configureELilo + |
3551 |
configureYaboot + configureSilo + configureZipl + |
configureYaboot + configureSilo + configureZipl + |
3552 |
configureExtLinux ) > 1) { |
configureExtLinux ) > 1) { |
3553 |
fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n")); |
fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n")); |
3556 |
fprintf(stderr, |
fprintf(stderr, |
3557 |
_("grubby: cannot specify config file with --bootloader-probe\n")); |
_("grubby: cannot specify config file with --bootloader-probe\n")); |
3558 |
return 1; |
return 1; |
3559 |
|
} else if (configureGrub2) { |
3560 |
|
cfi = &grub2ConfigType; |
3561 |
} else if (configureLilo) { |
} else if (configureLilo) { |
3562 |
cfi = &liloConfigType; |
cfi = &liloConfigType; |
3563 |
} else if (configureGrub) { |
} else if (configureGrub) { |
3576 |
} |
} |
3577 |
|
|
3578 |
if (!cfi) { |
if (!cfi) { |
3579 |
|
if (grub2FindConfig(&grub2ConfigType)) |
3580 |
|
cfi = &grub2ConfigType; |
3581 |
|
else |
3582 |
#ifdef __ia64__ |
#ifdef __ia64__ |
3583 |
cfi = &eliloConfigType; |
cfi = &eliloConfigType; |
3584 |
#elif __powerpc__ |
#elif __powerpc__ |
3585 |
cfi = &yabootConfigType; |
cfi = &yabootConfigType; |
3586 |
#elif __sparc__ |
#elif __sparc__ |
3587 |
cfi = &siloConfigType; |
cfi = &siloConfigType; |
3588 |
#elif __s390__ |
#elif __s390__ |
3589 |
cfi = &ziplConfigType; |
cfi = &ziplConfigType; |
3590 |
#elif __s390x__ |
#elif __s390x__ |
3591 |
cfi = &ziplConfigtype; |
cfi = &ziplConfigtype; |
3592 |
#else |
#else |
3593 |
cfi = &grubConfigType; |
cfi = &grubConfigType; |
3594 |
#endif |
#endif |
3595 |
} |
} |
3596 |
|
|
3597 |
if (!grubConfig) |
if (!grubConfig) { |
3598 |
grubConfig = cfi->defaultConfig; |
if (cfi->findConfig) |
3599 |
|
grubConfig = cfi->findConfig(cfi); |
3600 |
|
if (!grubConfig) |
3601 |
|
grubConfig = cfi->defaultConfig; |
3602 |
|
} |
3603 |
|
|
3604 |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
3605 |
newKernelPath || removeKernelPath || makeDefault || |
newKernelPath || removeKernelPath || makeDefault || |
3606 |
defaultKernel)) { |
defaultKernel || displayDefaultIndex || displayDefaultTitle)) { |
3607 |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
3608 |
"specified option")); |
"specified option")); |
3609 |
return 1; |
return 1; |
3646 |
defaultKernel = NULL; |
defaultKernel = NULL; |
3647 |
} |
} |
3648 |
|
|
3649 |
if (!strcmp(grubConfig, "-") && !outputFile) { |
if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) { |
3650 |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
3651 |
"is used\n")); |
"is used\n")); |
3652 |
return 1; |
return 1; |
3654 |
|
|
3655 |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
3656 |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
3657 |
&& !removeMBKernel) { |
&& !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) { |
3658 |
fprintf(stderr, _("grubby: no action specified\n")); |
fprintf(stderr, _("grubby: no action specified\n")); |
3659 |
return 1; |
return 1; |
3660 |
} |
} |
3681 |
} |
} |
3682 |
|
|
3683 |
if (bootloaderProbe) { |
if (bootloaderProbe) { |
3684 |
int lrc = 0, grc = 0, erc = 0; |
int lrc = 0, grc = 0, gr2c = 0, erc = 0; |
3685 |
struct grubConfig * lconfig, * gconfig; |
struct grubConfig * lconfig, * gconfig; |
3686 |
|
|
3687 |
if (!access(grubConfigType.defaultConfig, F_OK)) { |
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
3688 |
gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType); |
if (grub2config) { |
3689 |
|
gconfig = readConfig(grub2config, &grub2ConfigType); |
3690 |
|
if (!gconfig) |
3691 |
|
gr2c = 1; |
3692 |
|
else |
3693 |
|
gr2c = checkForGrub2(gconfig); |
3694 |
|
} |
3695 |
|
|
3696 |
|
const char *grubconfig = grubFindConfig(&grubConfigType); |
3697 |
|
if (!access(grubconfig, F_OK)) { |
3698 |
|
gconfig = readConfig(grubconfig, &grubConfigType); |
3699 |
if (!gconfig) |
if (!gconfig) |
3700 |
grc = 1; |
grc = 1; |
3701 |
else |
else |
3718 |
erc = checkForExtLinux(lconfig); |
erc = checkForExtLinux(lconfig); |
3719 |
} |
} |
3720 |
|
|
3721 |
if (lrc == 1 || grc == 1) return 1; |
if (lrc == 1 || grc == 1 || gr2c == 1) return 1; |
3722 |
|
|
3723 |
if (lrc == 2) printf("lilo\n"); |
if (lrc == 2) printf("lilo\n"); |
3724 |
|
if (gr2c == 2) printf("grub2\n"); |
3725 |
if (grc == 2) printf("grub\n"); |
if (grc == 2) printf("grub\n"); |
3726 |
if (erc == 2) printf("extlinux\n"); |
if (erc == 2) printf("extlinux\n"); |
3727 |
|
|
3749 |
((rootspec != NULL) ? strlen(rootspec) : 0)); |
((rootspec != NULL) ? strlen(rootspec) : 0)); |
3750 |
|
|
3751 |
return 0; |
return 0; |
3752 |
|
|
3753 |
|
} else if (displayDefaultTitle) { |
3754 |
|
struct singleLine * line; |
3755 |
|
struct singleEntry * entry; |
3756 |
|
|
3757 |
|
if (config->defaultImage == -1) return 0; |
3758 |
|
entry = findEntryByIndex(config, config->defaultImage); |
3759 |
|
if (!entry) return 0; |
3760 |
|
|
3761 |
|
if (!configureGrub2) { |
3762 |
|
line = getLineByType(LT_TITLE, entry->lines); |
3763 |
|
if (!line) return 0; |
3764 |
|
printf("%s\n", line->elements[1].item); |
3765 |
|
|
3766 |
|
} else { |
3767 |
|
char * title; |
3768 |
|
|
3769 |
|
dbgPrintf("This is GRUB2, default title is embeded in menuentry\n"); |
3770 |
|
line = getLineByType(LT_MENUENTRY, entry->lines); |
3771 |
|
if (!line) return 0; |
3772 |
|
title = grub2ExtractTitle(line); |
3773 |
|
if (title) |
3774 |
|
printf("%s\n", title); |
3775 |
|
} |
3776 |
|
return 0; |
3777 |
|
|
3778 |
|
} else if (displayDefaultIndex) { |
3779 |
|
if (config->defaultImage == -1) return 0; |
3780 |
|
printf("%i\n", config->defaultImage); |
3781 |
|
|
3782 |
} else if (kernelInfo) |
} else if (kernelInfo) |
3783 |
return displayInfo(config, kernelInfo, bootPrefix); |
return displayInfo(config, kernelInfo, bootPrefix); |
3784 |
|
|
3800 |
} |
} |
3801 |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
3802 |
newKernelTitle, newKernelArgs, newKernelInitrd, |
newKernelTitle, newKernelArgs, newKernelInitrd, |
3803 |
extraInitrds, extraInitrdCount, |
(const char **)extraInitrds, extraInitrdCount, |
3804 |
newMBKernel, newMBKernelArgs)) return 1; |
newMBKernel, newMBKernelArgs)) return 1; |
3805 |
|
|
3806 |
|
|
3811 |
} |
} |
3812 |
|
|
3813 |
if (!outputFile) |
if (!outputFile) |
3814 |
outputFile = grubConfig; |
outputFile = (char *)grubConfig; |
3815 |
|
|
3816 |
return writeConfig(config, outputFile, bootPrefix); |
return writeConfig(config, outputFile, bootPrefix); |
3817 |
} |
} |