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 |
|
int isEfi = 0; |
60 |
|
|
61 |
/* comments get lumped in with indention */ |
/* comments get lumped in with indention */ |
62 |
struct lineElement { |
struct lineElement { |
63 |
char * item; |
char * item; |
84 |
LT_MENUENTRY = 1 << 17, |
LT_MENUENTRY = 1 << 17, |
85 |
LT_ENTRY_END = 1 << 18, |
LT_ENTRY_END = 1 << 18, |
86 |
LT_SET_VARIABLE = 1 << 19, |
LT_SET_VARIABLE = 1 << 19, |
87 |
LT_UNKNOWN = 1 << 20, |
LT_KERNEL_EFI = 1 << 20, |
88 |
|
LT_INITRD_EFI = 1 << 21, |
89 |
|
LT_UNKNOWN = 1 << 22, |
90 |
}; |
}; |
91 |
|
|
92 |
struct singleLine { |
struct singleLine { |
118 |
|
|
119 |
#define MAIN_DEFAULT (1 << 0) |
#define MAIN_DEFAULT (1 << 0) |
120 |
#define DEFAULT_SAVED -2 |
#define DEFAULT_SAVED -2 |
121 |
|
#define DEFAULT_SAVED_GRUB2 -3 |
122 |
|
|
123 |
struct keywordTypes { |
struct keywordTypes { |
124 |
char * key; |
char * key; |
168 |
|
|
169 |
const char *grubFindConfig(struct configFileInfo *cfi) { |
const char *grubFindConfig(struct configFileInfo *cfi) { |
170 |
static const char *configFiles[] = { |
static const char *configFiles[] = { |
|
"/etc/grub.conf", |
|
171 |
"/boot/grub/grub.conf", |
"/boot/grub/grub.conf", |
172 |
"/boot/grub/menu.lst", |
"/boot/grub/menu.lst", |
173 |
|
"/etc/grub.conf", |
174 |
NULL |
NULL |
175 |
}; |
}; |
176 |
static int i = -1; |
static int i = -1; |
209 |
{ "default", LT_DEFAULT, ' ' }, |
{ "default", LT_DEFAULT, ' ' }, |
210 |
{ "fallback", LT_FALLBACK, ' ' }, |
{ "fallback", LT_FALLBACK, ' ' }, |
211 |
{ "linux", LT_KERNEL, ' ' }, |
{ "linux", LT_KERNEL, ' ' }, |
212 |
|
{ "linuxefi", LT_KERNEL_EFI, ' ' }, |
213 |
{ "initrd", LT_INITRD, ' ', ' ' }, |
{ "initrd", LT_INITRD, ' ', ' ' }, |
214 |
|
{ "initrdefi", LT_INITRD_EFI, ' ', ' ' }, |
215 |
{ "module", LT_MBMODULE, ' ' }, |
{ "module", LT_MBMODULE, ' ' }, |
216 |
{ "kernel", LT_HYPER, ' ' }, |
{ "kernel", LT_HYPER, ' ' }, |
217 |
{ NULL, 0, 0 }, |
{ NULL, 0, 0 }, |
248 |
return configFiles[i]; |
return configFiles[i]; |
249 |
} |
} |
250 |
|
|
251 |
|
int sizeOfSingleLine(struct singleLine * line) { |
252 |
|
int count = 0; |
253 |
|
|
254 |
|
for (int i = 0; i < line->numElements; i++) { |
255 |
|
int indentSize = 0; |
256 |
|
|
257 |
|
count = count + strlen(line->elements[i].item); |
258 |
|
|
259 |
|
indentSize = strlen(line->elements[i].indent); |
260 |
|
if (indentSize > 0) |
261 |
|
count = count + indentSize; |
262 |
|
else |
263 |
|
/* be extra safe and add room for whitespaces */ |
264 |
|
count = count + 1; |
265 |
|
} |
266 |
|
|
267 |
|
/* room for trailing terminator */ |
268 |
|
count = count + 1; |
269 |
|
|
270 |
|
return count; |
271 |
|
} |
272 |
|
|
273 |
|
static int isquote(char q) |
274 |
|
{ |
275 |
|
if (q == '\'' || q == '\"') |
276 |
|
return 1; |
277 |
|
return 0; |
278 |
|
} |
279 |
|
|
280 |
|
static int iskernel(enum lineType_e type) { |
281 |
|
return (type == LT_KERNEL || type == LT_KERNEL_EFI); |
282 |
|
} |
283 |
|
|
284 |
|
static int isinitrd(enum lineType_e type) { |
285 |
|
return (type == LT_INITRD || type == LT_INITRD_EFI); |
286 |
|
} |
287 |
|
|
288 |
|
char *grub2ExtractTitle(struct singleLine * line) { |
289 |
|
char * current; |
290 |
|
char * current_indent; |
291 |
|
int current_len; |
292 |
|
int current_indent_len; |
293 |
|
int i; |
294 |
|
|
295 |
|
/* bail out if line does not start with menuentry */ |
296 |
|
if (strcmp(line->elements[0].item, "menuentry")) |
297 |
|
return NULL; |
298 |
|
|
299 |
|
i = 1; |
300 |
|
current = line->elements[i].item; |
301 |
|
current_len = strlen(current); |
302 |
|
|
303 |
|
/* if second word is quoted, strip the quotes and return single word */ |
304 |
|
if (isquote(*current) && isquote(current[current_len - 1])) { |
305 |
|
char *tmp; |
306 |
|
|
307 |
|
tmp = strdup(current); |
308 |
|
*(tmp + current_len - 1) = '\0'; |
309 |
|
return ++tmp; |
310 |
|
} |
311 |
|
|
312 |
|
/* if no quotes, return second word verbatim */ |
313 |
|
if (!isquote(*current)) |
314 |
|
return current; |
315 |
|
|
316 |
|
/* second element start with a quote, so we have to find the element |
317 |
|
* whose last character is also quote (assuming it's the closing one) */ |
318 |
|
int resultMaxSize; |
319 |
|
char * result; |
320 |
|
|
321 |
|
resultMaxSize = sizeOfSingleLine(line); |
322 |
|
result = malloc(resultMaxSize); |
323 |
|
snprintf(result, resultMaxSize, "%s", ++current); |
324 |
|
|
325 |
|
i++; |
326 |
|
for (; i < line->numElements; ++i) { |
327 |
|
current = line->elements[i].item; |
328 |
|
current_len = strlen(current); |
329 |
|
current_indent = line->elements[i].indent; |
330 |
|
current_indent_len = strlen(current_indent); |
331 |
|
|
332 |
|
strncat(result, current_indent, current_indent_len); |
333 |
|
if (!isquote(current[current_len-1])) { |
334 |
|
strncat(result, current, current_len); |
335 |
|
} else { |
336 |
|
strncat(result, current, current_len - 1); |
337 |
|
break; |
338 |
|
} |
339 |
|
} |
340 |
|
return result; |
341 |
|
} |
342 |
|
|
343 |
struct configFileInfo grub2ConfigType = { |
struct configFileInfo grub2ConfigType = { |
344 |
.findConfig = grub2FindConfig, |
.findConfig = grub2FindConfig, |
345 |
.keywords = grub2Keywords, |
.keywords = grub2Keywords, |
346 |
.defaultIsIndex = 1, |
.defaultIsIndex = 1, |
347 |
.defaultSupportSaved = 0, |
.defaultSupportSaved = 1, |
348 |
.defaultIsVariable = 1, |
.defaultIsVariable = 1, |
349 |
.entryStart = LT_MENUENTRY, |
.entryStart = LT_MENUENTRY, |
350 |
.entryEnd = LT_ENTRY_END, |
.entryEnd = LT_ENTRY_END, |
584 |
return buf; |
return buf; |
585 |
} |
} |
586 |
|
|
587 |
|
static enum lineType_e preferredLineType(enum lineType_e type, |
588 |
|
struct configFileInfo *cfi) { |
589 |
|
if (isEfi && cfi == &grub2ConfigType) { |
590 |
|
switch (type) { |
591 |
|
case LT_KERNEL: |
592 |
|
return LT_KERNEL_EFI; |
593 |
|
case LT_INITRD: |
594 |
|
return LT_INITRD_EFI; |
595 |
|
default: |
596 |
|
return type; |
597 |
|
} |
598 |
|
} |
599 |
|
return type; |
600 |
|
} |
601 |
|
|
602 |
static struct keywordTypes * getKeywordByType(enum lineType_e type, |
static struct keywordTypes * getKeywordByType(enum lineType_e type, |
603 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
604 |
struct keywordTypes * kw; |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
|
for (kw = cfi->keywords; kw->key; kw++) { |
|
605 |
if (kw->type == type) |
if (kw->type == type) |
606 |
return kw; |
return kw; |
607 |
} |
} |
631 |
|
|
632 |
static enum lineType_e getTypeByKeyword(char * keyword, |
static enum lineType_e getTypeByKeyword(char * keyword, |
633 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
634 |
struct keywordTypes * kw; |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
|
for (kw = cfi->keywords; kw->key; kw++) { |
|
635 |
if (!strcmp(keyword, kw->key)) |
if (!strcmp(keyword, kw->key)) |
636 |
return kw->type; |
return kw->type; |
637 |
} |
} |
718 |
} |
} |
719 |
|
|
720 |
struct singleLine * lineDup(struct singleLine * line) { |
struct singleLine * lineDup(struct singleLine * line) { |
|
int i; |
|
721 |
struct singleLine * newLine = malloc(sizeof(*newLine)); |
struct singleLine * newLine = malloc(sizeof(*newLine)); |
722 |
|
|
723 |
newLine->indent = strdup(line->indent); |
newLine->indent = strdup(line->indent); |
727 |
newLine->elements = malloc(sizeof(*newLine->elements) * |
newLine->elements = malloc(sizeof(*newLine->elements) * |
728 |
newLine->numElements); |
newLine->numElements); |
729 |
|
|
730 |
for (i = 0; i < newLine->numElements; i++) { |
for (int i = 0; i < newLine->numElements; i++) { |
731 |
newLine->elements[i].indent = strdup(line->elements[i].indent); |
newLine->elements[i].indent = strdup(line->elements[i].indent); |
732 |
newLine->elements[i].item = strdup(line->elements[i].item); |
newLine->elements[i].item = strdup(line->elements[i].item); |
733 |
} |
} |
736 |
} |
} |
737 |
|
|
738 |
static void lineFree(struct singleLine * line) { |
static void lineFree(struct singleLine * line) { |
|
int i; |
|
|
|
|
739 |
if (line->indent) free(line->indent); |
if (line->indent) free(line->indent); |
740 |
|
|
741 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
742 |
free(line->elements[i].item); |
free(line->elements[i].item); |
743 |
free(line->elements[i].indent); |
free(line->elements[i].indent); |
744 |
} |
} |
749 |
|
|
750 |
static int lineWrite(FILE * out, struct singleLine * line, |
static int lineWrite(FILE * out, struct singleLine * line, |
751 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
|
int i; |
|
|
|
|
752 |
if (fprintf(out, "%s", line->indent) == -1) return -1; |
if (fprintf(out, "%s", line->indent) == -1) return -1; |
753 |
|
|
754 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
755 |
|
/* Need to handle this, because we strip the quotes from |
756 |
|
* menuentry when read it. */ |
757 |
|
if (line->type == LT_MENUENTRY && i == 1) { |
758 |
|
if(!isquote(*line->elements[i].item)) |
759 |
|
fprintf(out, "\'%s\'", line->elements[i].item); |
760 |
|
else |
761 |
|
fprintf(out, "%s", line->elements[i].item); |
762 |
|
fprintf(out, "%s", line->elements[i].indent); |
763 |
|
|
764 |
|
continue; |
765 |
|
} |
766 |
|
|
767 |
if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes) |
if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes) |
768 |
if (fputc('"', out) == EOF) return -1; |
if (fputc('"', out) == EOF) return -1; |
769 |
|
|
857 |
if (*line->elements[0].item == '#') { |
if (*line->elements[0].item == '#') { |
858 |
char * fullLine; |
char * fullLine; |
859 |
int len; |
int len; |
|
int i; |
|
860 |
|
|
861 |
len = strlen(line->indent); |
len = strlen(line->indent); |
862 |
for (i = 0; i < line->numElements; i++) |
for (int i = 0; i < line->numElements; i++) |
863 |
len += strlen(line->elements[i].item) + |
len += strlen(line->elements[i].item) + |
864 |
strlen(line->elements[i].indent); |
strlen(line->elements[i].indent); |
865 |
|
|
868 |
free(line->indent); |
free(line->indent); |
869 |
line->indent = fullLine; |
line->indent = fullLine; |
870 |
|
|
871 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
872 |
strcat(fullLine, line->elements[i].item); |
strcat(fullLine, line->elements[i].item); |
873 |
strcat(fullLine, line->elements[i].indent); |
strcat(fullLine, line->elements[i].indent); |
874 |
free(line->elements[i].item); |
free(line->elements[i].item); |
887 |
* elements up more |
* elements up more |
888 |
*/ |
*/ |
889 |
if (!isspace(kw->separatorChar)) { |
if (!isspace(kw->separatorChar)) { |
|
int i; |
|
890 |
char indent[2] = ""; |
char indent[2] = ""; |
891 |
indent[0] = kw->separatorChar; |
indent[0] = kw->separatorChar; |
892 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
893 |
char *p; |
char *p; |
|
int j; |
|
894 |
int numNewElements; |
int numNewElements; |
895 |
|
|
896 |
numNewElements = 0; |
numNewElements = 0; |
906 |
sizeof(*line->elements) * elementsAlloced); |
sizeof(*line->elements) * elementsAlloced); |
907 |
} |
} |
908 |
|
|
909 |
for (j = line->numElements; j > i; j--) { |
for (int j = line->numElements; j > i; j--) { |
910 |
line->elements[j + numNewElements] = line->elements[j]; |
line->elements[j + numNewElements] = line->elements[j]; |
911 |
} |
} |
912 |
line->numElements += numNewElements; |
line->numElements += numNewElements; |
919 |
break; |
break; |
920 |
} |
} |
921 |
|
|
922 |
free(line->elements[i].indent); |
line->elements[i + 1].indent = line->elements[i].indent; |
923 |
line->elements[i].indent = strdup(indent); |
line->elements[i].indent = strdup(indent); |
924 |
*p++ = '\0'; |
*p++ = '\0'; |
925 |
i++; |
i++; |
926 |
line->elements[i].item = strdup(p); |
line->elements[i].item = strdup(p); |
|
line->elements[i].indent = strdup(""); |
|
|
p = line->elements[i].item; |
|
927 |
} |
} |
928 |
} |
} |
929 |
} |
} |
944 |
struct singleLine * last = NULL, * line, * defaultLine = NULL; |
struct singleLine * last = NULL, * line, * defaultLine = NULL; |
945 |
char * end; |
char * end; |
946 |
struct singleEntry * entry = NULL; |
struct singleEntry * entry = NULL; |
947 |
int i, len; |
int len; |
948 |
char * buf; |
char * buf; |
949 |
|
|
950 |
if (!strcmp(inName, "-")) { |
if (!strcmp(inName, "-")) { |
990 |
cfg->secondaryIndent = strdup(line->indent); |
cfg->secondaryIndent = strdup(line->indent); |
991 |
} |
} |
992 |
|
|
993 |
if (isEntryStart(line, cfi)) { |
if (isEntryStart(line, cfi) || (cfg->entries && !sawEntry)) { |
994 |
sawEntry = 1; |
sawEntry = 1; |
995 |
if (!entry) { |
if (!entry) { |
996 |
cfg->entries = malloc(sizeof(*entry)); |
cfg->entries = malloc(sizeof(*entry)); |
1007 |
} |
} |
1008 |
|
|
1009 |
if (line->type == LT_SET_VARIABLE) { |
if (line->type == LT_SET_VARIABLE) { |
|
int i; |
|
1010 |
dbgPrintf("found 'set' command (%d elements): ", line->numElements); |
dbgPrintf("found 'set' command (%d elements): ", line->numElements); |
1011 |
dbgPrintf("%s", line->indent); |
dbgPrintf("%s", line->indent); |
1012 |
for (i = 0; i < line->numElements; i++) |
for (int i = 0; i < line->numElements; i++) |
1013 |
dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item); |
dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent); |
1014 |
dbgPrintf("\n"); |
dbgPrintf("\n"); |
1015 |
struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi); |
struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi); |
1016 |
if (kwType && line->numElements == 3 && |
if (kwType && line->numElements == 3 && |
1023 |
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
1024 |
defaultLine = line; |
defaultLine = line; |
1025 |
|
|
1026 |
} else if (line->type == LT_KERNEL) { |
} else if (iskernel(line->type)) { |
1027 |
/* if by some freak chance this is multiboot and the "module" |
/* if by some freak chance this is multiboot and the "module" |
1028 |
* lines came earlier in the template, make sure to use LT_HYPER |
* lines came earlier in the template, make sure to use LT_HYPER |
1029 |
* instead of LT_KERNEL now |
* instead of LT_KERNEL now |
1037 |
* This only applies to grub, but that's the only place we |
* This only applies to grub, but that's the only place we |
1038 |
* should find LT_MBMODULE lines anyway. |
* should find LT_MBMODULE lines anyway. |
1039 |
*/ |
*/ |
1040 |
struct singleLine * l; |
for (struct singleLine *l = entry->lines; l; l = l->next) { |
|
for (l = entry->lines; l; l = l->next) { |
|
1041 |
if (l->type == LT_HYPER) |
if (l->type == LT_HYPER) |
1042 |
break; |
break; |
1043 |
else if (l->type == LT_KERNEL) { |
else if (iskernel(l->type)) { |
1044 |
l->type = LT_HYPER; |
l->type = LT_HYPER; |
1045 |
break; |
break; |
1046 |
} |
} |
1057 |
} else if (line->type == LT_TITLE && line->numElements > 1) { |
} else if (line->type == LT_TITLE && line->numElements > 1) { |
1058 |
/* make the title a single argument (undoing our parsing) */ |
/* make the title a single argument (undoing our parsing) */ |
1059 |
len = 0; |
len = 0; |
1060 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
1061 |
len += strlen(line->elements[i].item); |
len += strlen(line->elements[i].item); |
1062 |
len += strlen(line->elements[i].indent); |
len += strlen(line->elements[i].indent); |
1063 |
} |
} |
1064 |
buf = malloc(len + 1); |
buf = malloc(len + 1); |
1065 |
*buf = '\0'; |
*buf = '\0'; |
1066 |
|
|
1067 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
1068 |
strcat(buf, line->elements[i].item); |
strcat(buf, line->elements[i].item); |
1069 |
free(line->elements[i].item); |
free(line->elements[i].item); |
1070 |
|
|
1078 |
line->elements[line->numElements - 1].indent; |
line->elements[line->numElements - 1].indent; |
1079 |
line->elements[1].item = buf; |
line->elements[1].item = buf; |
1080 |
line->numElements = 2; |
line->numElements = 2; |
1081 |
|
} else if (line->type == LT_MENUENTRY && line->numElements > 3) { |
1082 |
|
/* let --remove-kernel="TITLE=what" work */ |
1083 |
|
len = 0; |
1084 |
|
char *extras; |
1085 |
|
char *title; |
1086 |
|
|
1087 |
|
for (int i = 1; i < line->numElements; i++) { |
1088 |
|
len += strlen(line->elements[i].item); |
1089 |
|
len += strlen(line->elements[i].indent); |
1090 |
|
} |
1091 |
|
buf = malloc(len + 1); |
1092 |
|
*buf = '\0'; |
1093 |
|
|
1094 |
|
/* allocate mem for extra flags. */ |
1095 |
|
extras = malloc(len + 1); |
1096 |
|
*extras = '\0'; |
1097 |
|
|
1098 |
|
/* get title. */ |
1099 |
|
for (int i = 0; i < line->numElements; i++) { |
1100 |
|
if (!strcmp(line->elements[i].item, "menuentry")) |
1101 |
|
continue; |
1102 |
|
if (isquote(*line->elements[i].item)) |
1103 |
|
title = line->elements[i].item + 1; |
1104 |
|
else |
1105 |
|
title = line->elements[i].item; |
1106 |
|
|
1107 |
|
len = strlen(title); |
1108 |
|
if (isquote(title[len-1])) { |
1109 |
|
strncat(buf, title,len-1); |
1110 |
|
break; |
1111 |
|
} else { |
1112 |
|
strcat(buf, title); |
1113 |
|
strcat(buf, line->elements[i].indent); |
1114 |
|
} |
1115 |
|
} |
1116 |
|
|
1117 |
|
/* get extras */ |
1118 |
|
int count = 0; |
1119 |
|
for (int i = 0; i < line->numElements; i++) { |
1120 |
|
if (count >= 2) { |
1121 |
|
strcat(extras, line->elements[i].item); |
1122 |
|
strcat(extras, line->elements[i].indent); |
1123 |
|
} |
1124 |
|
|
1125 |
|
if (!strcmp(line->elements[i].item, "menuentry")) |
1126 |
|
continue; |
1127 |
|
|
1128 |
|
/* count ' or ", there should be two in menuentry line. */ |
1129 |
|
if (isquote(*line->elements[i].item)) |
1130 |
|
count++; |
1131 |
|
|
1132 |
|
len = strlen(line->elements[i].item); |
1133 |
|
|
1134 |
|
if (isquote(line->elements[i].item[len -1])) |
1135 |
|
count++; |
1136 |
|
|
1137 |
|
/* ok, we get the final ' or ", others are extras. */ |
1138 |
|
} |
1139 |
|
line->elements[1].indent = |
1140 |
|
line->elements[line->numElements - 2].indent; |
1141 |
|
line->elements[1].item = buf; |
1142 |
|
line->elements[2].indent = |
1143 |
|
line->elements[line->numElements - 2].indent; |
1144 |
|
line->elements[2].item = extras; |
1145 |
|
line->numElements = 3; |
1146 |
} else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) { |
} else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) { |
1147 |
/* Strip off any " which may be present; they'll be put back |
/* Strip off any " which may be present; they'll be put back |
1148 |
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 |
1151 |
if (line->numElements >= 2) { |
if (line->numElements >= 2) { |
1152 |
int last, len; |
int last, len; |
1153 |
|
|
1154 |
if (*line->elements[1].item == '"') |
if (isquote(*line->elements[1].item)) |
1155 |
memmove(line->elements[1].item, line->elements[1].item + 1, |
memmove(line->elements[1].item, line->elements[1].item + 1, |
1156 |
strlen(line->elements[1].item + 1) + 1); |
strlen(line->elements[1].item + 1) + 1); |
1157 |
|
|
1158 |
last = line->numElements - 1; |
last = line->numElements - 1; |
1159 |
len = strlen(line->elements[last].item) - 1; |
len = strlen(line->elements[last].item) - 1; |
1160 |
if (line->elements[last].item[len] == '"') |
if (isquote(line->elements[last].item[len])) |
1161 |
line->elements[last].item[len] = '\0'; |
line->elements[last].item[len] = '\0'; |
1162 |
} |
} |
1163 |
} |
} |
1216 |
|
|
1217 |
dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset"); |
dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset"); |
1218 |
if (defaultLine) { |
if (defaultLine) { |
1219 |
if (cfi->defaultIsVariable) { |
if (defaultLine->numElements > 2 && |
1220 |
|
cfi->defaultSupportSaved && |
1221 |
|
!strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) { |
1222 |
|
cfg->defaultImage = DEFAULT_SAVED_GRUB2; |
1223 |
|
} else if (cfi->defaultIsVariable) { |
1224 |
char *value = defaultLine->elements[2].item; |
char *value = defaultLine->elements[2].item; |
1225 |
while (*value && (*value == '"' || *value == '\'' || |
while (*value && (*value == '"' || *value == '\'' || |
1226 |
*value == ' ' || *value == '\t')) |
*value == ' ' || *value == '\t')) |
1237 |
cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10); |
cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10); |
1238 |
if (*end) cfg->defaultImage = -1; |
if (*end) cfg->defaultImage = -1; |
1239 |
} else if (defaultLine->numElements >= 2) { |
} else if (defaultLine->numElements >= 2) { |
1240 |
i = 0; |
int i = 0; |
1241 |
while ((entry = findEntryByIndex(cfg, i))) { |
while ((entry = findEntryByIndex(cfg, i))) { |
1242 |
for (line = entry->lines; line; line = line->next) |
for (line = entry->lines; line; line = line->next) |
1243 |
if (line->type == LT_TITLE) break; |
if (line->type == LT_TITLE) break; |
1277 |
|
|
1278 |
if (cfg->defaultImage == DEFAULT_SAVED) |
if (cfg->defaultImage == DEFAULT_SAVED) |
1279 |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
1280 |
|
else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2) |
1281 |
|
fprintf(out, "%sset default=\"${saved_entry}\"\n", indent); |
1282 |
else if (cfg->defaultImage > -1) { |
else if (cfg->defaultImage > -1) { |
1283 |
if (cfg->cfi->defaultIsIndex) { |
if (cfg->cfi->defaultIsIndex) { |
1284 |
if (cfg->cfi->defaultIsVariable) { |
if (cfg->cfi->defaultIsVariable) { |
1339 |
|
|
1340 |
/* most likely the symlink is relative, so change our |
/* most likely the symlink is relative, so change our |
1341 |
directory to the dir of the symlink */ |
directory to the dir of the symlink */ |
1342 |
rc = chdir(dirname(strdupa(outName))); |
char *dir = strdupa(outName); |
1343 |
|
rc = chdir(dirname(dir)); |
1344 |
do { |
do { |
1345 |
buf = alloca(len + 1); |
buf = alloca(len + 1); |
1346 |
rc = readlink(basename(outName), buf, len); |
rc = readlink(basename(outName), buf, len); |
1478 |
buf[rc] = '\0'; |
buf[rc] = '\0'; |
1479 |
chptr = buf; |
chptr = buf; |
1480 |
|
|
1481 |
|
char *foundanswer = NULL; |
1482 |
|
|
1483 |
while (chptr && chptr != buf+rc) { |
while (chptr && chptr != buf+rc) { |
1484 |
devname = chptr; |
devname = chptr; |
1485 |
|
|
1507 |
* for '/' obviously. |
* for '/' obviously. |
1508 |
*/ |
*/ |
1509 |
if (*(++chptr) == '/' && *(++chptr) == ' ') { |
if (*(++chptr) == '/' && *(++chptr) == ' ') { |
1510 |
/* |
/* remember the last / entry in mtab */ |
1511 |
* 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); |
|
1512 |
} |
} |
1513 |
|
|
1514 |
/* Next line */ |
/* Next line */ |
1517 |
chptr++; |
chptr++; |
1518 |
} |
} |
1519 |
|
|
1520 |
|
/* Return the last / entry found */ |
1521 |
|
if (foundanswer) { |
1522 |
|
chptr = strchr(foundanswer, ' '); |
1523 |
|
*chptr = '\0'; |
1524 |
|
return strdup(foundanswer); |
1525 |
|
} |
1526 |
|
|
1527 |
return NULL; |
return NULL; |
1528 |
} |
} |
1529 |
|
|
1530 |
|
void printEntry(struct singleEntry * entry) { |
1531 |
|
int i; |
1532 |
|
struct singleLine * line; |
1533 |
|
|
1534 |
|
for (line = entry->lines; line; line = line->next) { |
1535 |
|
fprintf(stderr, "DBG: %s", line->indent); |
1536 |
|
for (i = 0; i < line->numElements; i++) { |
1537 |
|
/* Need to handle this, because we strip the quotes from |
1538 |
|
* menuentry when read it. */ |
1539 |
|
if (line->type == LT_MENUENTRY && i == 1) { |
1540 |
|
if(!isquote(*line->elements[i].item)) |
1541 |
|
fprintf(stderr, "\'%s\'", line->elements[i].item); |
1542 |
|
else |
1543 |
|
fprintf(stderr, "%s", line->elements[i].item); |
1544 |
|
fprintf(stderr, "%s", line->elements[i].indent); |
1545 |
|
|
1546 |
|
continue; |
1547 |
|
} |
1548 |
|
|
1549 |
|
fprintf(stderr, "%s%s", |
1550 |
|
line->elements[i].item, line->elements[i].indent); |
1551 |
|
} |
1552 |
|
fprintf(stderr, "\n"); |
1553 |
|
} |
1554 |
|
} |
1555 |
|
|
1556 |
|
void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...) |
1557 |
|
{ |
1558 |
|
va_list argp; |
1559 |
|
|
1560 |
|
if (!debug) |
1561 |
|
return; |
1562 |
|
|
1563 |
|
va_start(argp, fmt); |
1564 |
|
fprintf(stderr, "DBG: Image entry failed: "); |
1565 |
|
vfprintf(stderr, fmt, argp); |
1566 |
|
printEntry(entry); |
1567 |
|
va_end(argp); |
1568 |
|
} |
1569 |
|
|
1570 |
|
#define beginswith(s, c) ((s) && (s)[0] == (c)) |
1571 |
|
|
1572 |
|
static int endswith(const char *s, char c) |
1573 |
|
{ |
1574 |
|
int slen; |
1575 |
|
|
1576 |
|
if (!s || !s[0]) |
1577 |
|
return 0; |
1578 |
|
slen = strlen(s) - 1; |
1579 |
|
|
1580 |
|
return s[slen] == c; |
1581 |
|
} |
1582 |
|
|
1583 |
int suitableImage(struct singleEntry * entry, const char * bootPrefix, |
int suitableImage(struct singleEntry * entry, const char * bootPrefix, |
1584 |
int skipRemoved, int flags) { |
int skipRemoved, int flags) { |
1585 |
struct singleLine * line; |
struct singleLine * line; |
1589 |
char * rootspec; |
char * rootspec; |
1590 |
char * rootdev; |
char * rootdev; |
1591 |
|
|
1592 |
if (skipRemoved && entry->skip) return 0; |
if (skipRemoved && entry->skip) { |
1593 |
|
notSuitablePrintf(entry, "marked to skip\n"); |
1594 |
|
return 0; |
1595 |
|
} |
1596 |
|
|
1597 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
1598 |
if (!line || line->numElements < 2) return 0; |
if (!line) { |
1599 |
|
notSuitablePrintf(entry, "no line found\n"); |
1600 |
|
return 0; |
1601 |
|
} |
1602 |
|
if (line->numElements < 2) { |
1603 |
|
notSuitablePrintf(entry, "line has only %d elements\n", |
1604 |
|
line->numElements); |
1605 |
|
return 0; |
1606 |
|
} |
1607 |
|
|
1608 |
if (flags & GRUBBY_BADIMAGE_OKAY) return 1; |
if (flags & GRUBBY_BADIMAGE_OKAY) return 1; |
1609 |
|
|
1610 |
fullName = alloca(strlen(bootPrefix) + |
fullName = alloca(strlen(bootPrefix) + |
1611 |
strlen(line->elements[1].item) + 1); |
strlen(line->elements[1].item) + 1); |
1612 |
rootspec = getRootSpecifier(line->elements[1].item); |
rootspec = getRootSpecifier(line->elements[1].item); |
1613 |
sprintf(fullName, "%s%s", bootPrefix, |
int rootspec_offset = rootspec ? strlen(rootspec) : 0; |
1614 |
line->elements[1].item + (rootspec ? strlen(rootspec) : 0)); |
int hasslash = endswith(bootPrefix, '/') || |
1615 |
if (access(fullName, R_OK)) return 0; |
beginswith(line->elements[1].item + rootspec_offset, '/'); |
1616 |
|
sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/", |
1617 |
|
line->elements[1].item + rootspec_offset); |
1618 |
|
if (access(fullName, R_OK)) { |
1619 |
|
notSuitablePrintf(entry, "access to %s failed\n", fullName); |
1620 |
|
return 0; |
1621 |
|
} |
1622 |
for (i = 2; i < line->numElements; i++) |
for (i = 2; i < line->numElements; i++) |
1623 |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
1624 |
if (i < line->numElements) { |
if (i < line->numElements) { |
1636 |
line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines); |
line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines); |
1637 |
|
|
1638 |
/* failed to find one */ |
/* failed to find one */ |
1639 |
if (!line) return 0; |
if (!line) { |
1640 |
|
notSuitablePrintf(entry, "no line found\n"); |
1641 |
|
return 0; |
1642 |
|
} |
1643 |
|
|
1644 |
for (i = 1; i < line->numElements; i++) |
for (i = 1; i < line->numElements; i++) |
1645 |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
1646 |
if (i < line->numElements) |
if (i < line->numElements) |
1647 |
dev = line->elements[i].item + 5; |
dev = line->elements[i].item + 5; |
1648 |
else { |
else { |
1649 |
|
notSuitablePrintf(entry, "no root= entry found\n"); |
1650 |
/* it failed too... can't find root= */ |
/* it failed too... can't find root= */ |
1651 |
return 0; |
return 0; |
1652 |
} |
} |
1654 |
} |
} |
1655 |
|
|
1656 |
dev = getpathbyspec(dev); |
dev = getpathbyspec(dev); |
1657 |
if (!dev) |
if (!getpathbyspec(dev)) { |
1658 |
|
notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev); |
1659 |
return 0; |
return 0; |
1660 |
|
} else |
1661 |
|
dev = getpathbyspec(dev); |
1662 |
|
|
1663 |
rootdev = findDiskForRoot(); |
rootdev = findDiskForRoot(); |
1664 |
if (!rootdev) |
if (!rootdev) { |
1665 |
|
notSuitablePrintf(entry, "can't find root device\n"); |
1666 |
return 0; |
return 0; |
1667 |
|
} |
1668 |
|
|
1669 |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
1670 |
|
notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n", |
1671 |
|
getuuidbydev(rootdev), getuuidbydev(dev)); |
1672 |
free(rootdev); |
free(rootdev); |
1673 |
return 0; |
return 0; |
1674 |
} |
} |
1675 |
|
|
1676 |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
1677 |
|
notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n", |
1678 |
|
getuuidbydev(rootdev), getuuidbydev(dev)); |
1679 |
free(rootdev); |
free(rootdev); |
1680 |
return 0; |
return 0; |
1681 |
} |
} |
1724 |
entry = findEntryByIndex(config, indexVars[i]); |
entry = findEntryByIndex(config, indexVars[i]); |
1725 |
if (!entry) return NULL; |
if (!entry) return NULL; |
1726 |
|
|
1727 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
1728 |
if (!line) return NULL; |
if (!line) return NULL; |
1729 |
|
|
1730 |
if (index) *index = indexVars[i]; |
if (index) *index = indexVars[i]; |
1762 |
|
|
1763 |
if (!strncmp(kernel, "TITLE=", 6)) { |
if (!strncmp(kernel, "TITLE=", 6)) { |
1764 |
prefix = ""; |
prefix = ""; |
1765 |
checkType = LT_TITLE; |
checkType = LT_TITLE|LT_MENUENTRY; |
1766 |
kernel += 6; |
kernel += 6; |
1767 |
} |
} |
1768 |
|
|
1773 |
|
|
1774 |
/* check all the lines matching checkType */ |
/* check all the lines matching checkType */ |
1775 |
for (line = entry->lines; line; line = line->next) { |
for (line = entry->lines; line; line = line->next) { |
1776 |
line = getLineByType(entry->multiboot && checkType == LT_KERNEL ? |
line = getLineByType(entry->multiboot && checkType == LT_KERNEL |
1777 |
LT_KERNEL|LT_MBMODULE|LT_HYPER : |
? LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER |
1778 |
checkType, line); |
: checkType, line); |
1779 |
if (!line) break; /* not found in this entry */ |
if (!line) break; /* not found in this entry */ |
1780 |
|
|
1781 |
if (line && line->numElements >= 2) { |
if (line && line->type != LT_MENUENTRY && |
1782 |
|
line->numElements >= 2) { |
1783 |
rootspec = getRootSpecifier(line->elements[1].item); |
rootspec = getRootSpecifier(line->elements[1].item); |
1784 |
if (!strcmp(line->elements[1].item + |
if (!strcmp(line->elements[1].item + |
1785 |
((rootspec != NULL) ? strlen(rootspec) : 0), |
((rootspec != NULL) ? strlen(rootspec) : 0), |
1786 |
kernel + strlen(prefix))) |
kernel + strlen(prefix))) |
1787 |
break; |
break; |
1788 |
} |
} |
1789 |
|
if(line->type == LT_MENUENTRY && |
1790 |
|
!strcmp(line->elements[1].item, kernel)) |
1791 |
|
break; |
1792 |
} |
} |
1793 |
|
|
1794 |
/* make sure this entry has a kernel identifier; this skips |
/* make sure this entry has a kernel identifier; this skips |
1795 |
* non-Linux boot entries (could find netbsd etc, though, which is |
* non-Linux boot entries (could find netbsd etc, though, which is |
1796 |
* unfortunate) |
* unfortunate) |
1797 |
*/ |
*/ |
1798 |
if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines)) |
if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines)) |
1799 |
break; /* found 'im! */ |
break; /* found 'im! */ |
1800 |
} |
} |
1801 |
|
|
1880 |
const char * prefix) { |
const char * prefix) { |
1881 |
struct singleEntry * entry; |
struct singleEntry * entry; |
1882 |
|
|
1883 |
if (!image) return; |
if (!image) |
1884 |
|
return; |
1885 |
|
|
1886 |
|
/* check and see if we're removing the default image */ |
1887 |
|
if (isdigit(*image)) { |
1888 |
|
entry = findEntryByPath(cfg, image, prefix, NULL); |
1889 |
|
if(entry) |
1890 |
|
entry->skip = 1; |
1891 |
|
return; |
1892 |
|
} |
1893 |
|
|
1894 |
while ((entry = findEntryByPath(cfg, image, prefix, NULL))) |
while ((entry = findEntryByPath(cfg, image, prefix, NULL))) |
1895 |
entry->skip = 1; |
entry->skip = 1; |
1897 |
|
|
1898 |
void setDefaultImage(struct grubConfig * config, int hasNew, |
void setDefaultImage(struct grubConfig * config, int hasNew, |
1899 |
const char * defaultKernelPath, int newIsDefault, |
const char * defaultKernelPath, int newIsDefault, |
1900 |
const char * prefix, int flags) { |
const char * prefix, int flags, int index) { |
1901 |
struct singleEntry * entry, * entry2, * newDefault; |
struct singleEntry * entry, * entry2, * newDefault; |
1902 |
int i, j; |
int i, j; |
1903 |
|
|
1904 |
if (newIsDefault) { |
if (newIsDefault) { |
1905 |
config->defaultImage = 0; |
config->defaultImage = 0; |
1906 |
return; |
return; |
1907 |
|
} else if ((index >= 0) && config->cfi->defaultIsIndex) { |
1908 |
|
if (findEntryByIndex(config, index)) |
1909 |
|
config->defaultImage = index; |
1910 |
|
else |
1911 |
|
config->defaultImage = -1; |
1912 |
|
return; |
1913 |
} else if (defaultKernelPath) { |
} else if (defaultKernelPath) { |
1914 |
i = 0; |
i = 0; |
1915 |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
1922 |
|
|
1923 |
/* 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 |
1924 |
changes */ |
changes */ |
1925 |
if (config->defaultImage == DEFAULT_SAVED) |
if ((config->defaultImage == DEFAULT_SAVED) || |
1926 |
|
(config->defaultImage == DEFAULT_SAVED_GRUB2)) |
1927 |
/* default is set to saved, we don't want to change it */ |
/* default is set to saved, we don't want to change it */ |
1928 |
return; |
return; |
1929 |
|
|
1984 |
|
|
1985 |
printf("index=%d\n", index); |
printf("index=%d\n", index); |
1986 |
|
|
1987 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
1988 |
if (!line) { |
if (!line) { |
1989 |
printf("non linux entry\n"); |
printf("non linux entry\n"); |
1990 |
return; |
return; |
1991 |
} |
} |
1992 |
|
|
1993 |
printf("kernel=%s\n", line->elements[1].item); |
if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) |
1994 |
|
printf("kernel=%s\n", line->elements[1].item); |
1995 |
|
else |
1996 |
|
printf("kernel=%s%s\n", prefix, line->elements[1].item); |
1997 |
|
|
1998 |
if (line->numElements >= 3) { |
if (line->numElements >= 3) { |
1999 |
printf("args=\""); |
printf("args=\""); |
2049 |
printf("root=%s\n", s); |
printf("root=%s\n", s); |
2050 |
} |
} |
2051 |
|
|
2052 |
line = getLineByType(LT_INITRD, entry->lines); |
line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines); |
2053 |
|
|
2054 |
if (line && line->numElements >= 2) { |
if (line && line->numElements >= 2) { |
2055 |
printf("initrd=%s", prefix); |
if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) |
2056 |
|
printf("initrd="); |
2057 |
|
else |
2058 |
|
printf("initrd=%s", prefix); |
2059 |
|
|
2060 |
for (i = 1; i < line->numElements; i++) |
for (i = 1; i < line->numElements; i++) |
2061 |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
2062 |
printf("\n"); |
printf("\n"); |
2063 |
} |
} |
2064 |
|
|
2065 |
|
line = getLineByType(LT_TITLE, entry->lines); |
2066 |
|
if (line) { |
2067 |
|
printf("title=%s\n", line->elements[1].item); |
2068 |
|
} else { |
2069 |
|
char * title; |
2070 |
|
line = getLineByType(LT_MENUENTRY, entry->lines); |
2071 |
|
title = grub2ExtractTitle(line); |
2072 |
|
if (title) |
2073 |
|
printf("title=%s\n", title); |
2074 |
|
} |
2075 |
|
} |
2076 |
|
|
2077 |
|
int isSuseSystem(void) { |
2078 |
|
const char * path; |
2079 |
|
const static char default_path[] = "/etc/SuSE-release"; |
2080 |
|
|
2081 |
|
if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL) |
2082 |
|
path = default_path; |
2083 |
|
|
2084 |
|
if (!access(path, R_OK)) |
2085 |
|
return 1; |
2086 |
|
return 0; |
2087 |
|
} |
2088 |
|
|
2089 |
|
int isSuseGrubConf(const char * path) { |
2090 |
|
FILE * grubConf; |
2091 |
|
char * line = NULL; |
2092 |
|
size_t len = 0, res = 0; |
2093 |
|
|
2094 |
|
grubConf = fopen(path, "r"); |
2095 |
|
if (!grubConf) { |
2096 |
|
dbgPrintf("Could not open SuSE configuration file '%s'\n", path); |
2097 |
|
return 0; |
2098 |
|
} |
2099 |
|
|
2100 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2101 |
|
if (!strncmp(line, "setup", 5)) { |
2102 |
|
fclose(grubConf); |
2103 |
|
free(line); |
2104 |
|
return 1; |
2105 |
|
} |
2106 |
|
} |
2107 |
|
|
2108 |
|
dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n", |
2109 |
|
path); |
2110 |
|
|
2111 |
|
fclose(grubConf); |
2112 |
|
free(line); |
2113 |
|
return 0; |
2114 |
|
} |
2115 |
|
|
2116 |
|
int suseGrubConfGetLba(const char * path, int * lbaPtr) { |
2117 |
|
FILE * grubConf; |
2118 |
|
char * line = NULL; |
2119 |
|
size_t res = 0, len = 0; |
2120 |
|
|
2121 |
|
if (!path) return 1; |
2122 |
|
if (!lbaPtr) return 1; |
2123 |
|
|
2124 |
|
grubConf = fopen(path, "r"); |
2125 |
|
if (!grubConf) return 1; |
2126 |
|
|
2127 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2128 |
|
if (line[res - 1] == '\n') |
2129 |
|
line[res - 1] = '\0'; |
2130 |
|
else if (len > res) |
2131 |
|
line[res] = '\0'; |
2132 |
|
else { |
2133 |
|
line = realloc(line, res + 1); |
2134 |
|
line[res] = '\0'; |
2135 |
|
} |
2136 |
|
|
2137 |
|
if (!strncmp(line, "setup", 5)) { |
2138 |
|
if (strstr(line, "--force-lba")) { |
2139 |
|
*lbaPtr = 1; |
2140 |
|
} else { |
2141 |
|
*lbaPtr = 0; |
2142 |
|
} |
2143 |
|
dbgPrintf("lba: %i\n", *lbaPtr); |
2144 |
|
break; |
2145 |
|
} |
2146 |
|
} |
2147 |
|
|
2148 |
|
free(line); |
2149 |
|
fclose(grubConf); |
2150 |
|
return 0; |
2151 |
|
} |
2152 |
|
|
2153 |
|
int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) { |
2154 |
|
FILE * grubConf; |
2155 |
|
char * line = NULL; |
2156 |
|
size_t res = 0, len = 0; |
2157 |
|
char * lastParamPtr = NULL; |
2158 |
|
char * secLastParamPtr = NULL; |
2159 |
|
char installDeviceNumber = '\0'; |
2160 |
|
char * bounds = NULL; |
2161 |
|
|
2162 |
|
if (!path) return 1; |
2163 |
|
if (!devicePtr) return 1; |
2164 |
|
|
2165 |
|
grubConf = fopen(path, "r"); |
2166 |
|
if (!grubConf) return 1; |
2167 |
|
|
2168 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2169 |
|
if (strncmp(line, "setup", 5)) |
2170 |
|
continue; |
2171 |
|
|
2172 |
|
if (line[res - 1] == '\n') |
2173 |
|
line[res - 1] = '\0'; |
2174 |
|
else if (len > res) |
2175 |
|
line[res] = '\0'; |
2176 |
|
else { |
2177 |
|
line = realloc(line, res + 1); |
2178 |
|
line[res] = '\0'; |
2179 |
|
} |
2180 |
|
|
2181 |
|
lastParamPtr = bounds = line + res; |
2182 |
|
|
2183 |
|
/* Last parameter in grub may be an optional IMAGE_DEVICE */ |
2184 |
|
while (!isspace(*lastParamPtr)) |
2185 |
|
lastParamPtr--; |
2186 |
|
lastParamPtr++; |
2187 |
|
|
2188 |
|
secLastParamPtr = lastParamPtr - 2; |
2189 |
|
dbgPrintf("lastParamPtr: %s\n", lastParamPtr); |
2190 |
|
|
2191 |
|
if (lastParamPtr + 3 > bounds) { |
2192 |
|
dbgPrintf("lastParamPtr going over boundary"); |
2193 |
|
fclose(grubConf); |
2194 |
|
free(line); |
2195 |
|
return 1; |
2196 |
|
} |
2197 |
|
if (!strncmp(lastParamPtr, "(hd", 3)) |
2198 |
|
lastParamPtr += 3; |
2199 |
|
dbgPrintf("lastParamPtr: %c\n", *lastParamPtr); |
2200 |
|
|
2201 |
|
/* |
2202 |
|
* Second last parameter will decide wether last parameter is |
2203 |
|
* an IMAGE_DEVICE or INSTALL_DEVICE |
2204 |
|
*/ |
2205 |
|
while (!isspace(*secLastParamPtr)) |
2206 |
|
secLastParamPtr--; |
2207 |
|
secLastParamPtr++; |
2208 |
|
|
2209 |
|
if (secLastParamPtr + 3 > bounds) { |
2210 |
|
dbgPrintf("secLastParamPtr going over boundary"); |
2211 |
|
fclose(grubConf); |
2212 |
|
free(line); |
2213 |
|
return 1; |
2214 |
|
} |
2215 |
|
dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr); |
2216 |
|
if (!strncmp(secLastParamPtr, "(hd", 3)) { |
2217 |
|
secLastParamPtr += 3; |
2218 |
|
dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr); |
2219 |
|
installDeviceNumber = *secLastParamPtr; |
2220 |
|
} else { |
2221 |
|
installDeviceNumber = *lastParamPtr; |
2222 |
|
} |
2223 |
|
|
2224 |
|
*devicePtr = malloc(6); |
2225 |
|
snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber); |
2226 |
|
dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber); |
2227 |
|
fclose(grubConf); |
2228 |
|
free(line); |
2229 |
|
return 0; |
2230 |
|
} |
2231 |
|
|
2232 |
|
free(line); |
2233 |
|
fclose(grubConf); |
2234 |
|
return 1; |
2235 |
|
} |
2236 |
|
|
2237 |
|
int grubGetBootFromDeviceMap(const char * device, |
2238 |
|
char ** bootPtr) { |
2239 |
|
FILE * deviceMap; |
2240 |
|
char * line = NULL; |
2241 |
|
size_t res = 0, len = 0; |
2242 |
|
char * devicePtr; |
2243 |
|
char * bounds = NULL; |
2244 |
|
const char * path; |
2245 |
|
const static char default_path[] = "/boot/grub/device.map"; |
2246 |
|
|
2247 |
|
if (!device) return 1; |
2248 |
|
if (!bootPtr) return 1; |
2249 |
|
|
2250 |
|
if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL) |
2251 |
|
path = default_path; |
2252 |
|
|
2253 |
|
dbgPrintf("opening grub device.map file from: %s\n", path); |
2254 |
|
deviceMap = fopen(path, "r"); |
2255 |
|
if (!deviceMap) |
2256 |
|
return 1; |
2257 |
|
|
2258 |
|
while ((res = getline(&line, &len, deviceMap)) != -1) { |
2259 |
|
if (!strncmp(line, "#", 1)) |
2260 |
|
continue; |
2261 |
|
|
2262 |
|
if (line[res - 1] == '\n') |
2263 |
|
line[res - 1] = '\0'; |
2264 |
|
else if (len > res) |
2265 |
|
line[res] = '\0'; |
2266 |
|
else { |
2267 |
|
line = realloc(line, res + 1); |
2268 |
|
line[res] = '\0'; |
2269 |
|
} |
2270 |
|
|
2271 |
|
devicePtr = line; |
2272 |
|
bounds = line + res; |
2273 |
|
|
2274 |
|
while ((isspace(*line) && ((devicePtr + 1) <= bounds))) |
2275 |
|
devicePtr++; |
2276 |
|
dbgPrintf("device: %s\n", devicePtr); |
2277 |
|
|
2278 |
|
if (!strncmp(devicePtr, device, strlen(device))) { |
2279 |
|
devicePtr += strlen(device); |
2280 |
|
while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds)) |
2281 |
|
devicePtr++; |
2282 |
|
|
2283 |
|
*bootPtr = strdup(devicePtr); |
2284 |
|
break; |
2285 |
|
} |
2286 |
|
} |
2287 |
|
|
2288 |
|
free(line); |
2289 |
|
fclose(deviceMap); |
2290 |
|
return 0; |
2291 |
|
} |
2292 |
|
|
2293 |
|
int suseGrubConfGetBoot(const char * path, char ** bootPtr) { |
2294 |
|
char * grubDevice; |
2295 |
|
|
2296 |
|
if (suseGrubConfGetInstallDevice(path, &grubDevice)) |
2297 |
|
dbgPrintf("error looking for grub installation device\n"); |
2298 |
|
else |
2299 |
|
dbgPrintf("grubby installation device: %s\n", grubDevice); |
2300 |
|
|
2301 |
|
if (grubGetBootFromDeviceMap(grubDevice, bootPtr)) |
2302 |
|
dbgPrintf("error looking for grub boot device\n"); |
2303 |
|
else |
2304 |
|
dbgPrintf("grubby boot device: %s\n", *bootPtr); |
2305 |
|
|
2306 |
|
free(grubDevice); |
2307 |
|
return 0; |
2308 |
|
} |
2309 |
|
|
2310 |
|
int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) { |
2311 |
|
/* |
2312 |
|
* This SuSE grub configuration file at this location is not your average |
2313 |
|
* grub configuration file, but instead the grub commands used to setup |
2314 |
|
* grub on that system. |
2315 |
|
*/ |
2316 |
|
const char * path; |
2317 |
|
const static char default_path[] = "/etc/grub.conf"; |
2318 |
|
|
2319 |
|
if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL) |
2320 |
|
path = default_path; |
2321 |
|
|
2322 |
|
if (!isSuseGrubConf(path)) return 1; |
2323 |
|
|
2324 |
|
if (lbaPtr) { |
2325 |
|
*lbaPtr = 0; |
2326 |
|
if (suseGrubConfGetLba(path, lbaPtr)) |
2327 |
|
return 1; |
2328 |
|
} |
2329 |
|
|
2330 |
|
if (bootPtr) { |
2331 |
|
*bootPtr = NULL; |
2332 |
|
suseGrubConfGetBoot(path, bootPtr); |
2333 |
|
} |
2334 |
|
|
2335 |
|
return 0; |
2336 |
} |
} |
2337 |
|
|
2338 |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
2383 |
} |
} |
2384 |
|
|
2385 |
void dumpSysconfigGrub(void) { |
void dumpSysconfigGrub(void) { |
2386 |
char * boot; |
char * boot = NULL; |
2387 |
int lba; |
int lba; |
2388 |
|
|
2389 |
if (!parseSysconfigGrub(&lba, &boot)) { |
if (isSuseSystem()) { |
2390 |
if (lba) printf("lba\n"); |
if (parseSuseGrubConf(&lba, &boot)) { |
2391 |
if (boot) printf("boot=%s\n", boot); |
free(boot); |
2392 |
|
return; |
2393 |
|
} |
2394 |
|
} else { |
2395 |
|
if (parseSysconfigGrub(&lba, &boot)) { |
2396 |
|
free(boot); |
2397 |
|
return; |
2398 |
|
} |
2399 |
|
} |
2400 |
|
|
2401 |
|
if (lba) printf("lba\n"); |
2402 |
|
if (boot) { |
2403 |
|
printf("boot=%s\n", boot); |
2404 |
|
free(boot); |
2405 |
} |
} |
2406 |
} |
} |
2407 |
|
|
2450 |
{ |
{ |
2451 |
struct singleLine * newLine = lineDup(tmplLine); |
struct singleLine * newLine = lineDup(tmplLine); |
2452 |
|
|
2453 |
|
if (isEfi && cfi == &grub2ConfigType) { |
2454 |
|
enum lineType_e old = newLine->type; |
2455 |
|
newLine->type = preferredLineType(newLine->type, cfi); |
2456 |
|
if (old != newLine->type) |
2457 |
|
newLine->elements[0].item = getKeyByType(newLine->type, cfi); |
2458 |
|
} |
2459 |
|
|
2460 |
if (val) { |
if (val) { |
2461 |
/* override the inherited value with our own. |
/* override the inherited value with our own. |
2462 |
* This is a little weak because it only applies to elements[1] |
* This is a little weak because it only applies to elements[1] |
2466 |
insertElement(newLine, val, 1, cfi); |
insertElement(newLine, val, 1, cfi); |
2467 |
|
|
2468 |
/* but try to keep the rootspec from the template... sigh */ |
/* but try to keep the rootspec from the template... sigh */ |
2469 |
if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD)) { |
if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI)) { |
2470 |
char * rootspec = getRootSpecifier(tmplLine->elements[1].item); |
char * rootspec = getRootSpecifier(tmplLine->elements[1].item); |
2471 |
if (rootspec != NULL) { |
if (rootspec != NULL) { |
2472 |
free(newLine->elements[1].item); |
free(newLine->elements[1].item); |
2503 |
/* NB: This function shouldn't allocate items on the heap, rather on the |
/* NB: This function shouldn't allocate items on the heap, rather on the |
2504 |
* stack since it calls addLineTmpl which will make copies. |
* stack since it calls addLineTmpl which will make copies. |
2505 |
*/ |
*/ |
|
|
|
2506 |
if (type == LT_TITLE && cfi->titleBracketed) { |
if (type == LT_TITLE && cfi->titleBracketed) { |
2507 |
/* we're doing a bracketed title (zipl) */ |
/* we're doing a bracketed title (zipl) */ |
2508 |
tmpl.type = type; |
tmpl.type = type; |
2606 |
free(line); |
free(line); |
2607 |
} |
} |
2608 |
|
|
|
static int isquote(char q) |
|
|
{ |
|
|
if (q == '\'' || q == '\"') |
|
|
return 1; |
|
|
return 0; |
|
|
} |
|
|
|
|
2609 |
static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi) |
static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi) |
2610 |
{ |
{ |
2611 |
struct singleLine newLine = { |
struct singleLine newLine = { |
2836 |
firstElement = 2; |
firstElement = 2; |
2837 |
|
|
2838 |
} else { |
} else { |
2839 |
line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines); |
line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines); |
2840 |
if (!line) { |
if (!line) { |
2841 |
/* no LT_KERNEL or LT_MBMODULE in this entry? */ |
/* no LT_KERNEL or LT_MBMODULE in this entry? */ |
2842 |
continue; |
continue; |
3001 |
if (!image) return 0; |
if (!image) return 0; |
3002 |
|
|
3003 |
for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { |
for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { |
3004 |
kernelLine = getLineByType(LT_KERNEL, entry->lines); |
kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines); |
3005 |
if (!kernelLine) continue; |
if (!kernelLine) continue; |
3006 |
|
|
3007 |
line = getLineByType(LT_INITRD, entry->lines); |
line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines); |
3008 |
if (line) |
if (line) |
3009 |
removeLine(entry, line); |
removeLine(entry, line); |
3010 |
if (prefix) { |
if (prefix) { |
3015 |
endLine = getLineByType(LT_ENTRY_END, entry->lines); |
endLine = getLineByType(LT_ENTRY_END, entry->lines); |
3016 |
if (endLine) |
if (endLine) |
3017 |
removeLine(entry, endLine); |
removeLine(entry, endLine); |
3018 |
line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd); |
line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi), |
3019 |
|
kernelLine->indent, initrd); |
3020 |
if (!line) |
if (!line) |
3021 |
return 1; |
return 1; |
3022 |
if (endLine) { |
if (endLine) { |
3054 |
if (memcmp(boot, bootSect, 3)) |
if (memcmp(boot, bootSect, 3)) |
3055 |
return 0; |
return 0; |
3056 |
|
|
3057 |
if (boot[1] == 0xeb) { |
if (boot[1] == JMP_SHORT_OPCODE) { |
3058 |
offset = boot[2] + 2; |
offset = boot[2] + 2; |
3059 |
} else if (boot[1] == 0xe8 || boot[1] == 0xe9) { |
} else if (boot[1] == 0xe8 || boot[1] == 0xe9) { |
3060 |
offset = (boot[3] << 8) + boot[2] + 2; |
offset = (boot[3] << 8) + boot[2] + 2; |
3061 |
} else if (boot[0] == 0xeb) { |
} else if (boot[0] == JMP_SHORT_OPCODE) { |
3062 |
offset = boot[1] + 2; |
offset = boot[1] + 2; |
3063 |
|
/* |
3064 |
|
* it looks like grub, when copying stage1 into the mbr, patches stage1 |
3065 |
|
* right after the JMP location, replacing other instructions such as |
3066 |
|
* JMPs for NOOPs. So, relax the check a little bit by skipping those |
3067 |
|
* different bytes. |
3068 |
|
*/ |
3069 |
|
if ((bootSect[offset + 1] == NOOP_OPCODE) |
3070 |
|
&& (bootSect[offset + 2] == NOOP_OPCODE)) { |
3071 |
|
offset = offset + 3; |
3072 |
|
} |
3073 |
} else if (boot[0] == 0xe8 || boot[0] == 0xe9) { |
} else if (boot[0] == 0xe8 || boot[0] == 0xe9) { |
3074 |
offset = (boot[2] << 8) + boot[1] + 2; |
offset = (boot[2] << 8) + boot[1] + 2; |
3075 |
} else { |
} else { |
3222 |
int fd; |
int fd; |
3223 |
unsigned char bootSect[512]; |
unsigned char bootSect[512]; |
3224 |
char * boot; |
char * boot; |
3225 |
|
int onSuse = isSuseSystem(); |
3226 |
|
|
3227 |
if (parseSysconfigGrub(NULL, &boot)) |
|
3228 |
return 0; |
if (onSuse) { |
3229 |
|
if (parseSuseGrubConf(NULL, &boot)) |
3230 |
|
return 0; |
3231 |
|
} else { |
3232 |
|
if (parseSysconfigGrub(NULL, &boot)) |
3233 |
|
return 0; |
3234 |
|
} |
3235 |
|
|
3236 |
/* assume grub is not installed -- not an error condition */ |
/* assume grub is not installed -- not an error condition */ |
3237 |
if (!boot) |
if (!boot) |
3250 |
} |
} |
3251 |
close(fd); |
close(fd); |
3252 |
|
|
3253 |
|
/* The more elaborate checks do not work on SuSE. The checks done |
3254 |
|
* seem to be reasonble (at least for now), so just return success |
3255 |
|
*/ |
3256 |
|
if (onSuse) |
3257 |
|
return 2; |
3258 |
|
|
3259 |
return checkDeviceBootloader(boot, bootSect); |
return checkDeviceBootloader(boot, bootSect); |
3260 |
} |
} |
3261 |
|
|
3289 |
return checkDeviceBootloader(boot, bootSect); |
return checkDeviceBootloader(boot, bootSect); |
3290 |
} |
} |
3291 |
|
|
3292 |
|
int checkForYaboot(struct grubConfig * config) { |
3293 |
|
/* |
3294 |
|
* This is a simplistic check that we consider good enough for own puporses |
3295 |
|
* |
3296 |
|
* If we were to properly check if yaboot is *installed* we'd need to: |
3297 |
|
* 1) get the system boot device (LT_BOOT) |
3298 |
|
* 2) considering it's a raw filesystem, check if the yaboot binary matches |
3299 |
|
* the content on the boot device |
3300 |
|
* 3) if not, copy the binary to a temporary file and run "addnote" on it |
3301 |
|
* 4) check again if binary and boot device contents match |
3302 |
|
*/ |
3303 |
|
if (!access("/etc/yaboot.conf", R_OK)) |
3304 |
|
return 2; |
3305 |
|
|
3306 |
|
return 1; |
3307 |
|
} |
3308 |
|
|
3309 |
|
int checkForElilo(struct grubConfig * config) { |
3310 |
|
if (!access("/etc/elilo.conf", R_OK)) |
3311 |
|
return 2; |
3312 |
|
|
3313 |
|
return 1; |
3314 |
|
} |
3315 |
|
|
3316 |
static char * getRootSpecifier(char * str) { |
static char * getRootSpecifier(char * str) { |
3317 |
char * idx, * rootspec = NULL; |
char * idx, * rootspec = NULL; |
3318 |
|
|
3327 |
static char * getInitrdVal(struct grubConfig * config, |
static char * getInitrdVal(struct grubConfig * config, |
3328 |
const char * prefix, struct singleLine *tmplLine, |
const char * prefix, struct singleLine *tmplLine, |
3329 |
const char * newKernelInitrd, |
const char * newKernelInitrd, |
3330 |
char ** extraInitrds, int extraInitrdCount) |
const char ** extraInitrds, int extraInitrdCount) |
3331 |
{ |
{ |
3332 |
char *initrdVal, *end; |
char *initrdVal, *end; |
3333 |
int i; |
int i; |
3372 |
|
|
3373 |
int addNewKernel(struct grubConfig * config, struct singleEntry * template, |
int addNewKernel(struct grubConfig * config, struct singleEntry * template, |
3374 |
const char * prefix, |
const char * prefix, |
3375 |
char * newKernelPath, char * newKernelTitle, |
const char * newKernelPath, const char * newKernelTitle, |
3376 |
char * newKernelArgs, char * newKernelInitrd, |
const char * newKernelArgs, const char * newKernelInitrd, |
3377 |
char ** extraInitrds, int extraInitrdCount, |
const char ** extraInitrds, int extraInitrdCount, |
3378 |
char * newMBKernel, char * newMBKernelArgs) { |
const char * newMBKernel, const char * newMBKernelArgs) { |
3379 |
struct singleEntry * new; |
struct singleEntry * new; |
3380 |
struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL; |
struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL; |
3381 |
int needs; |
int needs; |
3429 |
while (*chptr && isspace(*chptr)) chptr++; |
while (*chptr && isspace(*chptr)) chptr++; |
3430 |
if (*chptr == '#') continue; |
if (*chptr == '#') continue; |
3431 |
|
|
3432 |
if (tmplLine->type == LT_KERNEL && |
if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) { |
|
tmplLine->numElements >= 2) { |
|
3433 |
if (!template->multiboot && (needs & NEED_MB)) { |
if (!template->multiboot && (needs & NEED_MB)) { |
3434 |
/* it's not a multiboot template and this is the kernel |
/* it's not a multiboot template and this is the kernel |
3435 |
* line. Try to be intelligent about inserting the |
* line. Try to be intelligent about inserting the |
3506 |
/* template is multi but new is not, |
/* template is multi but new is not, |
3507 |
* insert the kernel in the first module slot |
* insert the kernel in the first module slot |
3508 |
*/ |
*/ |
3509 |
tmplLine->type = LT_KERNEL; |
tmplLine->type = preferredLineType(LT_KERNEL, config->cfi); |
3510 |
free(tmplLine->elements[0].item); |
free(tmplLine->elements[0].item); |
3511 |
tmplLine->elements[0].item = |
tmplLine->elements[0].item = |
3512 |
strdup(getKeywordByType(LT_KERNEL, config->cfi)->key); |
strdup(getKeywordByType(tmplLine->type, |
3513 |
|
config->cfi)->key); |
3514 |
newLine = addLineTmpl(new, tmplLine, newLine, |
newLine = addLineTmpl(new, tmplLine, newLine, |
3515 |
newKernelPath + strlen(prefix), config->cfi); |
newKernelPath + strlen(prefix), |
3516 |
|
config->cfi); |
3517 |
needs &= ~NEED_KERNEL; |
needs &= ~NEED_KERNEL; |
3518 |
} else if (needs & NEED_INITRD) { |
} else if (needs & NEED_INITRD) { |
3519 |
char *initrdVal; |
char *initrdVal; |
3520 |
/* template is multi but new is not, |
/* template is multi but new is not, |
3521 |
* insert the initrd in the second module slot |
* insert the initrd in the second module slot |
3522 |
*/ |
*/ |
3523 |
tmplLine->type = LT_INITRD; |
tmplLine->type = preferredLineType(LT_INITRD, config->cfi); |
3524 |
free(tmplLine->elements[0].item); |
free(tmplLine->elements[0].item); |
3525 |
tmplLine->elements[0].item = |
tmplLine->elements[0].item = |
3526 |
strdup(getKeywordByType(LT_INITRD, config->cfi)->key); |
strdup(getKeywordByType(tmplLine->type, |
3527 |
|
config->cfi)->key); |
3528 |
initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount); |
initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount); |
3529 |
newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi); |
newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi); |
3530 |
free(initrdVal); |
free(initrdVal); |
3531 |
needs &= ~NEED_INITRD; |
needs &= ~NEED_INITRD; |
3532 |
} |
} |
3533 |
|
|
3534 |
} else if (tmplLine->type == LT_INITRD && |
} else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) { |
|
tmplLine->numElements >= 2) { |
|
3535 |
if (needs & NEED_INITRD && |
if (needs & NEED_INITRD && |
3536 |
new->multiboot && !template->multiboot && |
new->multiboot && !template->multiboot && |
3537 |
config->cfi->mbInitRdIsModule) { |
config->cfi->mbInitRdIsModule) { |
3582 |
} |
} |
3583 |
} else if (tmplLine->type == LT_ECHO) { |
} else if (tmplLine->type == LT_ECHO) { |
3584 |
requote(tmplLine, config->cfi); |
requote(tmplLine, config->cfi); |
3585 |
|
static const char *prefix = "'Loading "; |
3586 |
if (tmplLine->numElements > 1 && |
if (tmplLine->numElements > 1 && |
3587 |
strstr(tmplLine->elements[1].item, "'Loading Linux ")) { |
strstr(tmplLine->elements[1].item, prefix) && |
3588 |
char *prefix = "'Loading "; |
masterLine->next && |
3589 |
|
iskernel(masterLine->next->type)) { |
3590 |
char *newTitle = malloc(strlen(prefix) + |
char *newTitle = malloc(strlen(prefix) + |
3591 |
strlen(newKernelTitle) + 2); |
strlen(newKernelTitle) + 2); |
3592 |
|
|
3613 |
*/ |
*/ |
3614 |
switch (config->cfi->entryStart) { |
switch (config->cfi->entryStart) { |
3615 |
case LT_KERNEL: |
case LT_KERNEL: |
3616 |
|
case LT_KERNEL_EFI: |
3617 |
if (new->multiboot && config->cfi->mbHyperFirst) { |
if (new->multiboot && config->cfi->mbHyperFirst) { |
3618 |
/* fall through to LT_HYPER */ |
/* fall through to LT_HYPER */ |
3619 |
} else { |
} else { |
3620 |
newLine = addLine(new, config->cfi, LT_KERNEL, |
newLine = addLine(new, config->cfi, |
3621 |
|
preferredLineType(LT_KERNEL, config->cfi), |
3622 |
config->primaryIndent, |
config->primaryIndent, |
3623 |
newKernelPath + strlen(prefix)); |
newKernelPath + strlen(prefix)); |
3624 |
needs &= ~NEED_KERNEL; |
needs &= ~NEED_KERNEL; |
3694 |
if (needs & NEED_KERNEL) { |
if (needs & NEED_KERNEL) { |
3695 |
newLine = addLine(new, config->cfi, |
newLine = addLine(new, config->cfi, |
3696 |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
3697 |
config->cfi)) ? |
config->cfi)) |
3698 |
LT_MBMODULE : LT_KERNEL, |
? LT_MBMODULE |
3699 |
|
: preferredLineType(LT_KERNEL, config->cfi), |
3700 |
config->secondaryIndent, |
config->secondaryIndent, |
3701 |
newKernelPath + strlen(prefix)); |
newKernelPath + strlen(prefix)); |
3702 |
needs &= ~NEED_KERNEL; |
needs &= ~NEED_KERNEL; |
3712 |
initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount); |
initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount); |
3713 |
newLine = addLine(new, config->cfi, |
newLine = addLine(new, config->cfi, |
3714 |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
3715 |
config->cfi)) ? |
config->cfi)) |
3716 |
LT_MBMODULE : LT_INITRD, |
? LT_MBMODULE |
3717 |
|
: preferredLineType(LT_INITRD, config->cfi), |
3718 |
config->secondaryIndent, |
config->secondaryIndent, |
3719 |
initrdVal); |
initrdVal); |
3720 |
free(initrdVal); |
free(initrdVal); |
3787 |
struct singleEntry * template = NULL; |
struct singleEntry * template = NULL; |
3788 |
int copyDefault = 0, makeDefault = 0; |
int copyDefault = 0, makeDefault = 0; |
3789 |
int displayDefault = 0; |
int displayDefault = 0; |
3790 |
|
int displayDefaultIndex = 0; |
3791 |
|
int displayDefaultTitle = 0; |
3792 |
|
int defaultIndex = -1; |
3793 |
struct poptOption options[] = { |
struct poptOption options[] = { |
3794 |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
3795 |
_("add an entry for the specified kernel"), _("kernel-path") }, |
_("add an entry for the specified kernel"), _("kernel-path") }, |
3807 |
{ "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0, |
{ "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0, |
3808 |
_("filestystem which contains /boot directory (for testing only)"), |
_("filestystem which contains /boot directory (for testing only)"), |
3809 |
_("bootfs") }, |
_("bootfs") }, |
3810 |
#if defined(__i386__) || defined(__x86_64__) |
#if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__) |
3811 |
{ "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0, |
{ "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0, |
3812 |
_("check if lilo is installed on lilo.conf boot sector") }, |
_("check which bootloader is installed on boot sector") }, |
3813 |
#endif |
#endif |
3814 |
{ "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0, |
{ "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0, |
3815 |
_("path to grub config file to update (\"-\" for stdin)"), |
_("path to grub config file to update (\"-\" for stdin)"), |
3820 |
"the kernel referenced by the default image does not exist, " |
"the kernel referenced by the default image does not exist, " |
3821 |
"the first linux entry whose kernel does exist is used as the " |
"the first linux entry whose kernel does exist is used as the " |
3822 |
"template"), NULL }, |
"template"), NULL }, |
3823 |
|
{ "debug", 0, 0, &debug, 0, |
3824 |
|
_("print debugging information for failures") }, |
3825 |
{ "default-kernel", 0, 0, &displayDefault, 0, |
{ "default-kernel", 0, 0, &displayDefault, 0, |
3826 |
_("display the path of the default kernel") }, |
_("display the path of the default kernel") }, |
3827 |
|
{ "default-index", 0, 0, &displayDefaultIndex, 0, |
3828 |
|
_("display the index of the default kernel") }, |
3829 |
|
{ "default-title", 0, 0, &displayDefaultTitle, 0, |
3830 |
|
_("display the title of the default kernel") }, |
3831 |
{ "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, |
{ "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, |
3832 |
_("configure elilo bootloader") }, |
_("configure elilo bootloader") }, |
3833 |
|
{ "efi", 0, POPT_ARG_NONE, &isEfi, 0, |
3834 |
|
_("force grub2 stanzas to use efi") }, |
3835 |
{ "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0, |
{ "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0, |
3836 |
_("configure extlinux bootloader (from syslinux)") }, |
_("configure extlinux bootloader (from syslinux)") }, |
3837 |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
3864 |
{ "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0, |
{ "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0, |
3865 |
_("make the first entry referencing the specified kernel " |
_("make the first entry referencing the specified kernel " |
3866 |
"the default"), _("kernel-path") }, |
"the default"), _("kernel-path") }, |
3867 |
|
{ "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0, |
3868 |
|
_("make the given entry index the default entry"), |
3869 |
|
_("entry-index") }, |
3870 |
{ "silo", 0, POPT_ARG_NONE, &configureSilo, 0, |
{ "silo", 0, POPT_ARG_NONE, &configureSilo, 0, |
3871 |
_("configure silo bootloader") }, |
_("configure silo bootloader") }, |
3872 |
{ "title", 0, POPT_ARG_STRING, &newKernelTitle, 0, |
{ "title", 0, POPT_ARG_STRING, &newKernelTitle, 0, |
3949 |
} |
} |
3950 |
|
|
3951 |
if (!cfi) { |
if (!cfi) { |
3952 |
|
if (grub2FindConfig(&grub2ConfigType)) |
3953 |
|
cfi = &grub2ConfigType; |
3954 |
|
else |
3955 |
#ifdef __ia64__ |
#ifdef __ia64__ |
3956 |
cfi = &eliloConfigType; |
cfi = &eliloConfigType; |
3957 |
#elif __powerpc__ |
#elif __powerpc__ |
3958 |
cfi = &yabootConfigType; |
cfi = &yabootConfigType; |
3959 |
#elif __sparc__ |
#elif __sparc__ |
3960 |
cfi = &siloConfigType; |
cfi = &siloConfigType; |
3961 |
#elif __s390__ |
#elif __s390__ |
3962 |
cfi = &ziplConfigType; |
cfi = &ziplConfigType; |
3963 |
#elif __s390x__ |
#elif __s390x__ |
3964 |
cfi = &ziplConfigtype; |
cfi = &ziplConfigtype; |
3965 |
#else |
#else |
|
if (grub2FindConfig(&grub2ConfigType)) |
|
|
cfi = &grub2ConfigType; |
|
|
else |
|
3966 |
cfi = &grubConfigType; |
cfi = &grubConfigType; |
3967 |
#endif |
#endif |
3968 |
} |
} |
3975 |
} |
} |
3976 |
|
|
3977 |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
3978 |
newKernelPath || removeKernelPath || makeDefault || |
newKernelPath || removeKernelPath || makeDefault || |
3979 |
defaultKernel)) { |
defaultKernel || displayDefaultIndex || displayDefaultTitle || |
3980 |
|
(defaultIndex >= 0))) { |
3981 |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
3982 |
"specified option")); |
"specified option")); |
3983 |
return 1; |
return 1; |
4019 |
makeDefault = 1; |
makeDefault = 1; |
4020 |
defaultKernel = NULL; |
defaultKernel = NULL; |
4021 |
} |
} |
4022 |
|
else if (defaultKernel && (defaultIndex >= 0)) { |
4023 |
|
fprintf(stderr, _("grubby: --set-default and --set-default-index " |
4024 |
|
"may not be used together\n")); |
4025 |
|
return 1; |
4026 |
|
} |
4027 |
|
|
4028 |
if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) { |
if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) { |
4029 |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
4032 |
} |
} |
4033 |
|
|
4034 |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
4035 |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
4036 |
&& !removeMBKernel) { |
&& !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle |
4037 |
|
&& (defaultIndex == -1)) { |
4038 |
fprintf(stderr, _("grubby: no action specified\n")); |
fprintf(stderr, _("grubby: no action specified\n")); |
4039 |
return 1; |
return 1; |
4040 |
} |
} |
4061 |
} |
} |
4062 |
|
|
4063 |
if (bootloaderProbe) { |
if (bootloaderProbe) { |
4064 |
int lrc = 0, grc = 0, gr2c = 0, erc = 0; |
int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0; |
4065 |
struct grubConfig * lconfig, * gconfig; |
struct grubConfig * lconfig, * gconfig, * yconfig, * econfig; |
4066 |
|
|
4067 |
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
4068 |
if (grub2config) { |
if (grub2config) { |
4090 |
lrc = checkForLilo(lconfig); |
lrc = checkForLilo(lconfig); |
4091 |
} |
} |
4092 |
|
|
4093 |
|
if (!access(eliloConfigType.defaultConfig, F_OK)) { |
4094 |
|
econfig = readConfig(eliloConfigType.defaultConfig, |
4095 |
|
&eliloConfigType); |
4096 |
|
if (!econfig) |
4097 |
|
erc = 1; |
4098 |
|
else |
4099 |
|
erc = checkForElilo(econfig); |
4100 |
|
} |
4101 |
|
|
4102 |
if (!access(extlinuxConfigType.defaultConfig, F_OK)) { |
if (!access(extlinuxConfigType.defaultConfig, F_OK)) { |
4103 |
lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType); |
lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType); |
4104 |
if (!lconfig) |
if (!lconfig) |
4105 |
erc = 1; |
extrc = 1; |
4106 |
else |
else |
4107 |
erc = checkForExtLinux(lconfig); |
extrc = checkForExtLinux(lconfig); |
4108 |
} |
} |
4109 |
|
|
4110 |
if (lrc == 1 || grc == 1 || gr2c == 1) return 1; |
|
4111 |
|
if (!access(yabootConfigType.defaultConfig, F_OK)) { |
4112 |
|
yconfig = readConfig(yabootConfigType.defaultConfig, |
4113 |
|
&yabootConfigType); |
4114 |
|
if (!yconfig) |
4115 |
|
yrc = 1; |
4116 |
|
else |
4117 |
|
yrc = checkForYaboot(yconfig); |
4118 |
|
} |
4119 |
|
|
4120 |
|
if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 || |
4121 |
|
erc == 1) |
4122 |
|
return 1; |
4123 |
|
|
4124 |
if (lrc == 2) printf("lilo\n"); |
if (lrc == 2) printf("lilo\n"); |
4125 |
if (gr2c == 2) printf("grub2\n"); |
if (gr2c == 2) printf("grub2\n"); |
4126 |
if (grc == 2) printf("grub\n"); |
if (grc == 2) printf("grub\n"); |
4127 |
if (erc == 2) printf("extlinux\n"); |
if (extrc == 2) printf("extlinux\n"); |
4128 |
|
if (yrc == 2) printf("yaboot\n"); |
4129 |
|
if (erc == 2) printf("elilo\n"); |
4130 |
|
|
4131 |
return 0; |
return 0; |
4132 |
} |
} |
4144 |
if (!entry) return 0; |
if (!entry) return 0; |
4145 |
if (!suitableImage(entry, bootPrefix, 0, flags)) return 0; |
if (!suitableImage(entry, bootPrefix, 0, flags)) return 0; |
4146 |
|
|
4147 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
4148 |
if (!line) return 0; |
if (!line) return 0; |
4149 |
|
|
4150 |
rootspec = getRootSpecifier(line->elements[1].item); |
rootspec = getRootSpecifier(line->elements[1].item); |
4152 |
((rootspec != NULL) ? strlen(rootspec) : 0)); |
((rootspec != NULL) ? strlen(rootspec) : 0)); |
4153 |
|
|
4154 |
return 0; |
return 0; |
4155 |
|
|
4156 |
|
} else if (displayDefaultTitle) { |
4157 |
|
struct singleLine * line; |
4158 |
|
struct singleEntry * entry; |
4159 |
|
|
4160 |
|
if (config->defaultImage == -1) return 0; |
4161 |
|
entry = findEntryByIndex(config, config->defaultImage); |
4162 |
|
if (!entry) return 0; |
4163 |
|
|
4164 |
|
if (!configureGrub2) { |
4165 |
|
line = getLineByType(LT_TITLE, entry->lines); |
4166 |
|
if (!line) return 0; |
4167 |
|
printf("%s\n", line->elements[1].item); |
4168 |
|
|
4169 |
|
} else { |
4170 |
|
char * title; |
4171 |
|
|
4172 |
|
dbgPrintf("This is GRUB2, default title is embeded in menuentry\n"); |
4173 |
|
line = getLineByType(LT_MENUENTRY, entry->lines); |
4174 |
|
if (!line) return 0; |
4175 |
|
title = grub2ExtractTitle(line); |
4176 |
|
if (title) |
4177 |
|
printf("%s\n", title); |
4178 |
|
} |
4179 |
|
return 0; |
4180 |
|
|
4181 |
|
} else if (displayDefaultIndex) { |
4182 |
|
if (config->defaultImage == -1) return 0; |
4183 |
|
printf("%i\n", config->defaultImage); |
4184 |
|
|
4185 |
} else if (kernelInfo) |
} else if (kernelInfo) |
4186 |
return displayInfo(config, kernelInfo, bootPrefix); |
return displayInfo(config, kernelInfo, bootPrefix); |
4187 |
|
|
4193 |
markRemovedImage(config, removeKernelPath, bootPrefix); |
markRemovedImage(config, removeKernelPath, bootPrefix); |
4194 |
markRemovedImage(config, removeMBKernel, bootPrefix); |
markRemovedImage(config, removeMBKernel, bootPrefix); |
4195 |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault, |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault, |
4196 |
bootPrefix, flags); |
bootPrefix, flags, defaultIndex); |
4197 |
setFallbackImage(config, newKernelPath != NULL); |
setFallbackImage(config, newKernelPath != NULL); |
4198 |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
4199 |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; |
4203 |
} |
} |
4204 |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
4205 |
newKernelTitle, newKernelArgs, newKernelInitrd, |
newKernelTitle, newKernelArgs, newKernelInitrd, |
4206 |
extraInitrds, extraInitrdCount, |
(const char **)extraInitrds, extraInitrdCount, |
4207 |
newMBKernel, newMBKernelArgs)) return 1; |
newMBKernel, newMBKernelArgs)) return 1; |
4208 |
|
|
4209 |
|
|