36 |
#include <signal.h> |
#include <signal.h> |
37 |
#include <blkid/blkid.h> |
#include <blkid/blkid.h> |
38 |
|
|
39 |
|
#include "log.h" |
40 |
|
|
41 |
#ifndef DEBUG |
#ifndef DEBUG |
42 |
#define DEBUG 0 |
#define DEBUG 0 |
43 |
#endif |
#endif |
58 |
#define NOOP_OPCODE 0x90 |
#define NOOP_OPCODE 0x90 |
59 |
#define JMP_SHORT_OPCODE 0xeb |
#define JMP_SHORT_OPCODE 0xeb |
60 |
|
|
61 |
|
int isEfi = 0; |
62 |
|
|
63 |
|
char *saved_command_line = NULL; |
64 |
|
|
65 |
/* comments get lumped in with indention */ |
/* comments get lumped in with indention */ |
66 |
struct lineElement { |
struct lineElement { |
67 |
char * item; |
char * item; |
88 |
LT_MENUENTRY = 1 << 17, |
LT_MENUENTRY = 1 << 17, |
89 |
LT_ENTRY_END = 1 << 18, |
LT_ENTRY_END = 1 << 18, |
90 |
LT_SET_VARIABLE = 1 << 19, |
LT_SET_VARIABLE = 1 << 19, |
91 |
LT_UNKNOWN = 1 << 20, |
LT_KERNEL_EFI = 1 << 20, |
92 |
|
LT_INITRD_EFI = 1 << 21, |
93 |
|
LT_UNKNOWN = 1 << 22, |
94 |
}; |
}; |
95 |
|
|
96 |
struct singleLine { |
struct singleLine { |
142 |
findConfigFunc findConfig; |
findConfigFunc findConfig; |
143 |
writeLineFunc writeLine; |
writeLineFunc writeLine; |
144 |
struct keywordTypes * keywords; |
struct keywordTypes * keywords; |
145 |
|
int caseInsensitive; |
146 |
int defaultIsIndex; |
int defaultIsIndex; |
147 |
int defaultIsVariable; |
int defaultIsVariable; |
148 |
int defaultSupportSaved; |
int defaultSupportSaved; |
173 |
|
|
174 |
const char *grubFindConfig(struct configFileInfo *cfi) { |
const char *grubFindConfig(struct configFileInfo *cfi) { |
175 |
static const char *configFiles[] = { |
static const char *configFiles[] = { |
|
"/etc/grub.conf", |
|
176 |
"/boot/grub/grub.conf", |
"/boot/grub/grub.conf", |
177 |
"/boot/grub/menu.lst", |
"/boot/grub/menu.lst", |
178 |
|
"/etc/grub.conf", |
179 |
NULL |
NULL |
180 |
}; |
}; |
181 |
static int i = -1; |
static int i = -1; |
214 |
{ "default", LT_DEFAULT, ' ' }, |
{ "default", LT_DEFAULT, ' ' }, |
215 |
{ "fallback", LT_FALLBACK, ' ' }, |
{ "fallback", LT_FALLBACK, ' ' }, |
216 |
{ "linux", LT_KERNEL, ' ' }, |
{ "linux", LT_KERNEL, ' ' }, |
217 |
|
{ "linuxefi", LT_KERNEL_EFI, ' ' }, |
218 |
{ "initrd", LT_INITRD, ' ', ' ' }, |
{ "initrd", LT_INITRD, ' ', ' ' }, |
219 |
|
{ "initrdefi", LT_INITRD_EFI, ' ', ' ' }, |
220 |
{ "module", LT_MBMODULE, ' ' }, |
{ "module", LT_MBMODULE, ' ' }, |
221 |
{ "kernel", LT_HYPER, ' ' }, |
{ "kernel", LT_HYPER, ' ' }, |
222 |
{ NULL, 0, 0 }, |
{ NULL, 0, 0 }, |
230 |
}; |
}; |
231 |
static int i = -1; |
static int i = -1; |
232 |
static const char *grub_cfg = "/boot/grub/grub.cfg"; |
static const char *grub_cfg = "/boot/grub/grub.cfg"; |
233 |
|
int rc = -1; |
234 |
|
|
235 |
if (i == -1) { |
if (i == -1) { |
236 |
for (i = 0; configFiles[i] != NULL; i++) { |
for (i = 0; configFiles[i] != NULL; i++) { |
237 |
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
238 |
if (!access(configFiles[i], R_OK)) { |
if ((rc = access(configFiles[i], R_OK))) { |
239 |
|
if (errno == EACCES) { |
240 |
|
printf("Unable to access bootloader configuration file " |
241 |
|
"\"%s\": %m\n", configFiles[i]); |
242 |
|
exit(1); |
243 |
|
} |
244 |
|
continue; |
245 |
|
} else { |
246 |
dbgPrintf("found\n"); |
dbgPrintf("found\n"); |
247 |
return configFiles[i]; |
return configFiles[i]; |
248 |
} |
} |
262 |
} |
} |
263 |
|
|
264 |
int sizeOfSingleLine(struct singleLine * line) { |
int sizeOfSingleLine(struct singleLine * line) { |
|
int i; |
|
265 |
int count = 0; |
int count = 0; |
266 |
|
|
267 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
268 |
int indentSize = 0; |
int indentSize = 0; |
269 |
|
|
270 |
count = count + strlen(line->elements[i].item); |
count = count + strlen(line->elements[i].item); |
290 |
return 0; |
return 0; |
291 |
} |
} |
292 |
|
|
293 |
|
static int iskernel(enum lineType_e type) { |
294 |
|
return (type == LT_KERNEL || type == LT_KERNEL_EFI); |
295 |
|
} |
296 |
|
|
297 |
|
static int isinitrd(enum lineType_e type) { |
298 |
|
return (type == LT_INITRD || type == LT_INITRD_EFI); |
299 |
|
} |
300 |
|
|
301 |
char *grub2ExtractTitle(struct singleLine * line) { |
char *grub2ExtractTitle(struct singleLine * line) { |
302 |
char * current; |
char * current; |
303 |
char * current_indent; |
char * current_indent; |
509 |
struct configFileInfo extlinuxConfigType = { |
struct configFileInfo extlinuxConfigType = { |
510 |
.defaultConfig = "/boot/extlinux/extlinux.conf", |
.defaultConfig = "/boot/extlinux/extlinux.conf", |
511 |
.keywords = extlinuxKeywords, |
.keywords = extlinuxKeywords, |
512 |
|
.caseInsensitive = 1, |
513 |
.entryStart = LT_TITLE, |
.entryStart = LT_TITLE, |
514 |
.needsBootPrefix = 1, |
.needsBootPrefix = 1, |
515 |
.maxTitleLength = 255, |
.maxTitleLength = 255, |
598 |
return buf; |
return buf; |
599 |
} |
} |
600 |
|
|
601 |
|
static enum lineType_e preferredLineType(enum lineType_e type, |
602 |
|
struct configFileInfo *cfi) { |
603 |
|
if (isEfi && cfi == &grub2ConfigType) { |
604 |
|
switch (type) { |
605 |
|
case LT_KERNEL: |
606 |
|
return LT_KERNEL_EFI; |
607 |
|
case LT_INITRD: |
608 |
|
return LT_INITRD_EFI; |
609 |
|
default: |
610 |
|
return type; |
611 |
|
} |
612 |
|
} |
613 |
|
return type; |
614 |
|
} |
615 |
|
|
616 |
static struct keywordTypes * getKeywordByType(enum lineType_e type, |
static struct keywordTypes * getKeywordByType(enum lineType_e type, |
617 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
618 |
struct keywordTypes * kw; |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
|
for (kw = cfi->keywords; kw->key; kw++) { |
|
619 |
if (kw->type == type) |
if (kw->type == type) |
620 |
return kw; |
return kw; |
621 |
} |
} |
645 |
|
|
646 |
static enum lineType_e getTypeByKeyword(char * keyword, |
static enum lineType_e getTypeByKeyword(char * keyword, |
647 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
648 |
struct keywordTypes * kw; |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
649 |
for (kw = cfi->keywords; kw->key; kw++) { |
if (cfi->caseInsensitive) { |
650 |
if (!strcmp(keyword, kw->key)) |
if (!strcasecmp(keyword, kw->key)) |
651 |
return kw->type; |
return kw->type; |
652 |
|
} else { |
653 |
|
if (!strcmp(keyword, kw->key)) |
654 |
|
return kw->type; |
655 |
|
} |
656 |
} |
} |
657 |
return LT_UNKNOWN; |
return LT_UNKNOWN; |
658 |
} |
} |
737 |
} |
} |
738 |
|
|
739 |
struct singleLine * lineDup(struct singleLine * line) { |
struct singleLine * lineDup(struct singleLine * line) { |
|
int i; |
|
740 |
struct singleLine * newLine = malloc(sizeof(*newLine)); |
struct singleLine * newLine = malloc(sizeof(*newLine)); |
741 |
|
|
742 |
newLine->indent = strdup(line->indent); |
newLine->indent = strdup(line->indent); |
746 |
newLine->elements = malloc(sizeof(*newLine->elements) * |
newLine->elements = malloc(sizeof(*newLine->elements) * |
747 |
newLine->numElements); |
newLine->numElements); |
748 |
|
|
749 |
for (i = 0; i < newLine->numElements; i++) { |
for (int i = 0; i < newLine->numElements; i++) { |
750 |
newLine->elements[i].indent = strdup(line->elements[i].indent); |
newLine->elements[i].indent = strdup(line->elements[i].indent); |
751 |
newLine->elements[i].item = strdup(line->elements[i].item); |
newLine->elements[i].item = strdup(line->elements[i].item); |
752 |
} |
} |
755 |
} |
} |
756 |
|
|
757 |
static void lineFree(struct singleLine * line) { |
static void lineFree(struct singleLine * line) { |
|
int i; |
|
|
|
|
758 |
if (line->indent) free(line->indent); |
if (line->indent) free(line->indent); |
759 |
|
|
760 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
761 |
free(line->elements[i].item); |
free(line->elements[i].item); |
762 |
free(line->elements[i].indent); |
free(line->elements[i].indent); |
763 |
} |
} |
768 |
|
|
769 |
static int lineWrite(FILE * out, struct singleLine * line, |
static int lineWrite(FILE * out, struct singleLine * line, |
770 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
|
int i; |
|
|
|
|
771 |
if (fprintf(out, "%s", line->indent) == -1) return -1; |
if (fprintf(out, "%s", line->indent) == -1) return -1; |
772 |
|
|
773 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
774 |
/* Need to handle this, because we strip the quotes from |
/* Need to handle this, because we strip the quotes from |
775 |
* menuentry when read it. */ |
* menuentry when read it. */ |
776 |
if (line->type == LT_MENUENTRY && i == 1) { |
if (line->type == LT_MENUENTRY && i == 1) { |
876 |
if (*line->elements[0].item == '#') { |
if (*line->elements[0].item == '#') { |
877 |
char * fullLine; |
char * fullLine; |
878 |
int len; |
int len; |
|
int i; |
|
879 |
|
|
880 |
len = strlen(line->indent); |
len = strlen(line->indent); |
881 |
for (i = 0; i < line->numElements; i++) |
for (int i = 0; i < line->numElements; i++) |
882 |
len += strlen(line->elements[i].item) + |
len += strlen(line->elements[i].item) + |
883 |
strlen(line->elements[i].indent); |
strlen(line->elements[i].indent); |
884 |
|
|
887 |
free(line->indent); |
free(line->indent); |
888 |
line->indent = fullLine; |
line->indent = fullLine; |
889 |
|
|
890 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
891 |
strcat(fullLine, line->elements[i].item); |
strcat(fullLine, line->elements[i].item); |
892 |
strcat(fullLine, line->elements[i].indent); |
strcat(fullLine, line->elements[i].indent); |
893 |
free(line->elements[i].item); |
free(line->elements[i].item); |
906 |
* elements up more |
* elements up more |
907 |
*/ |
*/ |
908 |
if (!isspace(kw->separatorChar)) { |
if (!isspace(kw->separatorChar)) { |
|
int i; |
|
909 |
char indent[2] = ""; |
char indent[2] = ""; |
910 |
indent[0] = kw->separatorChar; |
indent[0] = kw->separatorChar; |
911 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
912 |
char *p; |
char *p; |
|
int j; |
|
913 |
int numNewElements; |
int numNewElements; |
914 |
|
|
915 |
numNewElements = 0; |
numNewElements = 0; |
925 |
sizeof(*line->elements) * elementsAlloced); |
sizeof(*line->elements) * elementsAlloced); |
926 |
} |
} |
927 |
|
|
928 |
for (j = line->numElements; j > i; j--) { |
for (int j = line->numElements; j > i; j--) { |
929 |
line->elements[j + numNewElements] = line->elements[j]; |
line->elements[j + numNewElements] = line->elements[j]; |
930 |
} |
} |
931 |
line->numElements += numNewElements; |
line->numElements += numNewElements; |
938 |
break; |
break; |
939 |
} |
} |
940 |
|
|
941 |
free(line->elements[i].indent); |
line->elements[i + 1].indent = line->elements[i].indent; |
942 |
line->elements[i].indent = strdup(indent); |
line->elements[i].indent = strdup(indent); |
943 |
*p++ = '\0'; |
*p++ = '\0'; |
944 |
i++; |
i++; |
945 |
line->elements[i].item = strdup(p); |
line->elements[i].item = strdup(p); |
|
line->elements[i].indent = strdup(""); |
|
|
p = line->elements[i].item; |
|
946 |
} |
} |
947 |
} |
} |
948 |
} |
} |
963 |
struct singleLine * last = NULL, * line, * defaultLine = NULL; |
struct singleLine * last = NULL, * line, * defaultLine = NULL; |
964 |
char * end; |
char * end; |
965 |
struct singleEntry * entry = NULL; |
struct singleEntry * entry = NULL; |
966 |
int i, len; |
int len; |
967 |
char * buf; |
char * buf; |
968 |
|
|
969 |
if (!strcmp(inName, "-")) { |
if (inName == NULL) { |
970 |
|
printf("Could not find bootloader configuration\n"); |
971 |
|
exit(1); |
972 |
|
} else if (!strcmp(inName, "-")) { |
973 |
in = 0; |
in = 0; |
974 |
} else { |
} else { |
975 |
if ((in = open(inName, O_RDONLY)) < 0) { |
if ((in = open(inName, O_RDONLY)) < 0) { |
1029 |
} |
} |
1030 |
|
|
1031 |
if (line->type == LT_SET_VARIABLE) { |
if (line->type == LT_SET_VARIABLE) { |
|
int i; |
|
1032 |
dbgPrintf("found 'set' command (%d elements): ", line->numElements); |
dbgPrintf("found 'set' command (%d elements): ", line->numElements); |
1033 |
dbgPrintf("%s", line->indent); |
dbgPrintf("%s", line->indent); |
1034 |
for (i = 0; i < line->numElements; i++) |
for (int i = 0; i < line->numElements; i++) |
1035 |
dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item); |
dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent); |
1036 |
dbgPrintf("\n"); |
dbgPrintf("\n"); |
1037 |
struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi); |
struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi); |
1038 |
if (kwType && line->numElements == 3 && |
if (kwType && line->numElements == 3 && |
1045 |
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; |
1046 |
defaultLine = line; |
defaultLine = line; |
1047 |
|
|
1048 |
} else if (line->type == LT_KERNEL) { |
} else if (iskernel(line->type)) { |
1049 |
/* if by some freak chance this is multiboot and the "module" |
/* if by some freak chance this is multiboot and the "module" |
1050 |
* lines came earlier in the template, make sure to use LT_HYPER |
* lines came earlier in the template, make sure to use LT_HYPER |
1051 |
* instead of LT_KERNEL now |
* instead of LT_KERNEL now |
1059 |
* This only applies to grub, but that's the only place we |
* This only applies to grub, but that's the only place we |
1060 |
* should find LT_MBMODULE lines anyway. |
* should find LT_MBMODULE lines anyway. |
1061 |
*/ |
*/ |
1062 |
struct singleLine * l; |
for (struct singleLine *l = entry->lines; l; l = l->next) { |
|
for (l = entry->lines; l; l = l->next) { |
|
1063 |
if (l->type == LT_HYPER) |
if (l->type == LT_HYPER) |
1064 |
break; |
break; |
1065 |
else if (l->type == LT_KERNEL) { |
else if (iskernel(l->type)) { |
1066 |
l->type = LT_HYPER; |
l->type = LT_HYPER; |
1067 |
break; |
break; |
1068 |
} |
} |
1079 |
} else if (line->type == LT_TITLE && line->numElements > 1) { |
} else if (line->type == LT_TITLE && line->numElements > 1) { |
1080 |
/* make the title a single argument (undoing our parsing) */ |
/* make the title a single argument (undoing our parsing) */ |
1081 |
len = 0; |
len = 0; |
1082 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
1083 |
len += strlen(line->elements[i].item); |
len += strlen(line->elements[i].item); |
1084 |
len += strlen(line->elements[i].indent); |
len += strlen(line->elements[i].indent); |
1085 |
} |
} |
1086 |
buf = malloc(len + 1); |
buf = malloc(len + 1); |
1087 |
*buf = '\0'; |
*buf = '\0'; |
1088 |
|
|
1089 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
1090 |
strcat(buf, line->elements[i].item); |
strcat(buf, line->elements[i].item); |
1091 |
free(line->elements[i].item); |
free(line->elements[i].item); |
1092 |
|
|
1106 |
char *extras; |
char *extras; |
1107 |
char *title; |
char *title; |
1108 |
|
|
1109 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
1110 |
len += strlen(line->elements[i].item); |
len += strlen(line->elements[i].item); |
1111 |
len += strlen(line->elements[i].indent); |
len += strlen(line->elements[i].indent); |
1112 |
} |
} |
1118 |
*extras = '\0'; |
*extras = '\0'; |
1119 |
|
|
1120 |
/* get title. */ |
/* get title. */ |
1121 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
1122 |
if (!strcmp(line->elements[i].item, "menuentry")) |
if (!strcmp(line->elements[i].item, "menuentry")) |
1123 |
continue; |
continue; |
1124 |
if (isquote(*line->elements[i].item)) |
if (isquote(*line->elements[i].item)) |
1138 |
|
|
1139 |
/* get extras */ |
/* get extras */ |
1140 |
int count = 0; |
int count = 0; |
1141 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
1142 |
if (count == 2) { |
if (count >= 2) { |
1143 |
strcat(extras, line->elements[i].item); |
strcat(extras, line->elements[i].item); |
1144 |
strcat(extras, line->elements[i].indent); |
strcat(extras, line->elements[i].indent); |
1145 |
} |
} |
1259 |
cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10); |
cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10); |
1260 |
if (*end) cfg->defaultImage = -1; |
if (*end) cfg->defaultImage = -1; |
1261 |
} else if (defaultLine->numElements >= 2) { |
} else if (defaultLine->numElements >= 2) { |
1262 |
i = 0; |
int i = 0; |
1263 |
while ((entry = findEntryByIndex(cfg, i))) { |
while ((entry = findEntryByIndex(cfg, i))) { |
1264 |
for (line = entry->lines; line; line = line->next) |
for (line = entry->lines; line; line = line->next) |
1265 |
if (line->type == LT_TITLE) break; |
if (line->type == LT_TITLE) break; |
1361 |
|
|
1362 |
/* most likely the symlink is relative, so change our |
/* most likely the symlink is relative, so change our |
1363 |
directory to the dir of the symlink */ |
directory to the dir of the symlink */ |
1364 |
rc = chdir(dirname(strdupa(outName))); |
char *dir = strdupa(outName); |
1365 |
|
rc = chdir(dirname(dir)); |
1366 |
do { |
do { |
1367 |
buf = alloca(len + 1); |
buf = alloca(len + 1); |
1368 |
rc = readlink(basename(outName), buf, len); |
rc = readlink(basename(outName), buf, len); |
1500 |
buf[rc] = '\0'; |
buf[rc] = '\0'; |
1501 |
chptr = buf; |
chptr = buf; |
1502 |
|
|
1503 |
|
char *foundanswer = NULL; |
1504 |
|
|
1505 |
while (chptr && chptr != buf+rc) { |
while (chptr && chptr != buf+rc) { |
1506 |
devname = chptr; |
devname = chptr; |
1507 |
|
|
1529 |
* for '/' obviously. |
* for '/' obviously. |
1530 |
*/ |
*/ |
1531 |
if (*(++chptr) == '/' && *(++chptr) == ' ') { |
if (*(++chptr) == '/' && *(++chptr) == ' ') { |
1532 |
/* |
/* remember the last / entry in mtab */ |
1533 |
* 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); |
|
1534 |
} |
} |
1535 |
|
|
1536 |
/* Next line */ |
/* Next line */ |
1539 |
chptr++; |
chptr++; |
1540 |
} |
} |
1541 |
|
|
1542 |
|
/* Return the last / entry found */ |
1543 |
|
if (foundanswer) { |
1544 |
|
chptr = strchr(foundanswer, ' '); |
1545 |
|
*chptr = '\0'; |
1546 |
|
return strdup(foundanswer); |
1547 |
|
} |
1548 |
|
|
1549 |
return NULL; |
return NULL; |
1550 |
} |
} |
1551 |
|
|
1552 |
void printEntry(struct singleEntry * entry) { |
void printEntry(struct singleEntry * entry, FILE *f) { |
1553 |
int i; |
int i; |
1554 |
struct singleLine * line; |
struct singleLine * line; |
1555 |
|
|
1556 |
for (line = entry->lines; line; line = line->next) { |
for (line = entry->lines; line; line = line->next) { |
1557 |
fprintf(stderr, "DBG: %s", line->indent); |
log_message(f, "DBG: %s", line->indent); |
1558 |
for (i = 0; i < line->numElements; i++) { |
for (i = 0; i < line->numElements; i++) { |
1559 |
/* Need to handle this, because we strip the quotes from |
/* Need to handle this, because we strip the quotes from |
1560 |
* menuentry when read it. */ |
* menuentry when read it. */ |
1561 |
if (line->type == LT_MENUENTRY && i == 1) { |
if (line->type == LT_MENUENTRY && i == 1) { |
1562 |
if(!isquote(*line->elements[i].item)) |
if(!isquote(*line->elements[i].item)) |
1563 |
fprintf(stderr, "\'%s\'", line->elements[i].item); |
log_message(f, "\'%s\'", line->elements[i].item); |
1564 |
else |
else |
1565 |
fprintf(stderr, "%s", line->elements[i].item); |
log_message(f, "%s", line->elements[i].item); |
1566 |
fprintf(stderr, "%s", line->elements[i].indent); |
log_message(f, "%s", line->elements[i].indent); |
1567 |
|
|
1568 |
continue; |
continue; |
1569 |
} |
} |
1570 |
|
|
1571 |
fprintf(stderr, "%s%s", |
log_message(f, "%s%s", |
1572 |
line->elements[i].item, line->elements[i].indent); |
line->elements[i].item, line->elements[i].indent); |
1573 |
} |
} |
1574 |
fprintf(stderr, "\n"); |
log_message(f, "\n"); |
1575 |
} |
} |
1576 |
} |
} |
1577 |
|
|
1578 |
void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...) |
void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...) |
1579 |
{ |
{ |
1580 |
va_list argp; |
static int once; |
1581 |
|
va_list argp, argq; |
1582 |
|
|
1583 |
|
va_start(argp, fmt); |
1584 |
|
|
1585 |
|
va_copy(argq, argp); |
1586 |
|
if (!once) { |
1587 |
|
log_time(NULL); |
1588 |
|
log_message(NULL, "command line: %s\n", saved_command_line); |
1589 |
|
} |
1590 |
|
log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed"); |
1591 |
|
log_vmessage(NULL, fmt, argq); |
1592 |
|
|
1593 |
|
printEntry(entry, NULL); |
1594 |
|
va_end(argq); |
1595 |
|
|
1596 |
if (!debug) |
if (!debug) { |
1597 |
|
once = 1; |
1598 |
|
va_end(argp); |
1599 |
return; |
return; |
1600 |
|
} |
1601 |
|
|
1602 |
va_start(argp, fmt); |
if (okay) { |
1603 |
|
va_end(argp); |
1604 |
|
return; |
1605 |
|
} |
1606 |
|
|
1607 |
|
if (!once) |
1608 |
|
log_message(stderr, "DBG: command line: %s\n", saved_command_line); |
1609 |
|
once = 1; |
1610 |
fprintf(stderr, "DBG: Image entry failed: "); |
fprintf(stderr, "DBG: Image entry failed: "); |
1611 |
vfprintf(stderr, fmt, argp); |
vfprintf(stderr, fmt, argp); |
1612 |
printEntry(entry); |
printEntry(entry, stderr); |
1613 |
va_end(argp); |
va_end(argp); |
1614 |
} |
} |
1615 |
|
|
1636 |
char * rootdev; |
char * rootdev; |
1637 |
|
|
1638 |
if (skipRemoved && entry->skip) { |
if (skipRemoved && entry->skip) { |
1639 |
notSuitablePrintf(entry, "marked to skip\n"); |
notSuitablePrintf(entry, 0, "marked to skip\n"); |
1640 |
return 0; |
return 0; |
1641 |
} |
} |
1642 |
|
|
1643 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
1644 |
if (!line) { |
if (!line) { |
1645 |
notSuitablePrintf(entry, "no line found\n"); |
notSuitablePrintf(entry, 0, "no line found\n"); |
1646 |
return 0; |
return 0; |
1647 |
} |
} |
1648 |
if (line->numElements < 2) { |
if (line->numElements < 2) { |
1649 |
notSuitablePrintf(entry, "line has only %d elements\n", |
notSuitablePrintf(entry, 0, "line has only %d elements\n", |
1650 |
line->numElements); |
line->numElements); |
1651 |
return 0; |
return 0; |
1652 |
} |
} |
1653 |
|
|
1654 |
if (flags & GRUBBY_BADIMAGE_OKAY) return 1; |
if (flags & GRUBBY_BADIMAGE_OKAY) { |
1655 |
|
notSuitablePrintf(entry, 1, "\n"); |
1656 |
|
return 1; |
1657 |
|
} |
1658 |
|
|
1659 |
fullName = alloca(strlen(bootPrefix) + |
fullName = alloca(strlen(bootPrefix) + |
1660 |
strlen(line->elements[1].item) + 1); |
strlen(line->elements[1].item) + 1); |
1665 |
sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/", |
sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/", |
1666 |
line->elements[1].item + rootspec_offset); |
line->elements[1].item + rootspec_offset); |
1667 |
if (access(fullName, R_OK)) { |
if (access(fullName, R_OK)) { |
1668 |
notSuitablePrintf(entry, "access to %s failed\n", fullName); |
notSuitablePrintf(entry, 0, "access to %s failed\n", fullName); |
1669 |
return 0; |
return 0; |
1670 |
} |
} |
1671 |
for (i = 2; i < line->numElements; i++) |
for (i = 2; i < line->numElements; i++) |
1686 |
|
|
1687 |
/* failed to find one */ |
/* failed to find one */ |
1688 |
if (!line) { |
if (!line) { |
1689 |
notSuitablePrintf(entry, "no line found\n"); |
notSuitablePrintf(entry, 0, "no line found\n"); |
1690 |
return 0; |
return 0; |
1691 |
} |
} |
1692 |
|
|
1695 |
if (i < line->numElements) |
if (i < line->numElements) |
1696 |
dev = line->elements[i].item + 5; |
dev = line->elements[i].item + 5; |
1697 |
else { |
else { |
1698 |
notSuitablePrintf(entry, "no root= entry found\n"); |
notSuitablePrintf(entry, 0, "no root= entry found\n"); |
1699 |
/* it failed too... can't find root= */ |
/* it failed too... can't find root= */ |
1700 |
return 0; |
return 0; |
1701 |
} |
} |
1704 |
|
|
1705 |
dev = getpathbyspec(dev); |
dev = getpathbyspec(dev); |
1706 |
if (!getpathbyspec(dev)) { |
if (!getpathbyspec(dev)) { |
1707 |
notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev); |
notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev); |
1708 |
return 0; |
return 0; |
1709 |
} else |
} else |
1710 |
dev = getpathbyspec(dev); |
dev = getpathbyspec(dev); |
1711 |
|
|
1712 |
rootdev = findDiskForRoot(); |
rootdev = findDiskForRoot(); |
1713 |
if (!rootdev) { |
if (!rootdev) { |
1714 |
notSuitablePrintf(entry, "can't find root device\n"); |
notSuitablePrintf(entry, 0, "can't find root device\n"); |
1715 |
return 0; |
return 0; |
1716 |
} |
} |
1717 |
|
|
1718 |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
1719 |
notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n", |
notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n", |
1720 |
getuuidbydev(rootdev), getuuidbydev(dev)); |
getuuidbydev(rootdev), getuuidbydev(dev)); |
1721 |
free(rootdev); |
free(rootdev); |
1722 |
return 0; |
return 0; |
1723 |
} |
} |
1724 |
|
|
1725 |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
1726 |
notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n", |
notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n", |
1727 |
getuuidbydev(rootdev), getuuidbydev(dev)); |
getuuidbydev(rootdev), getuuidbydev(dev)); |
1728 |
free(rootdev); |
free(rootdev); |
1729 |
return 0; |
return 0; |
1730 |
} |
} |
1731 |
|
|
1732 |
free(rootdev); |
free(rootdev); |
1733 |
|
notSuitablePrintf(entry, 1, "\n"); |
1734 |
|
|
1735 |
return 1; |
return 1; |
1736 |
} |
} |
1774 |
entry = findEntryByIndex(config, indexVars[i]); |
entry = findEntryByIndex(config, indexVars[i]); |
1775 |
if (!entry) return NULL; |
if (!entry) return NULL; |
1776 |
|
|
1777 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
1778 |
if (!line) return NULL; |
if (!line) return NULL; |
1779 |
|
|
1780 |
if (index) *index = indexVars[i]; |
if (index) *index = indexVars[i]; |
1823 |
|
|
1824 |
/* check all the lines matching checkType */ |
/* check all the lines matching checkType */ |
1825 |
for (line = entry->lines; line; line = line->next) { |
for (line = entry->lines; line; line = line->next) { |
1826 |
line = getLineByType(entry->multiboot && checkType == LT_KERNEL ? |
enum lineType_e ct = checkType; |
1827 |
LT_KERNEL|LT_MBMODULE|LT_HYPER : |
if (entry->multiboot && checkType == LT_KERNEL) |
1828 |
checkType, line); |
ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER; |
1829 |
if (!line) break; /* not found in this entry */ |
else if (checkType & LT_KERNEL) |
1830 |
|
ct = checkType | LT_KERNEL_EFI; |
1831 |
|
line = getLineByType(ct, line); |
1832 |
|
if (!line) |
1833 |
|
break; /* not found in this entry */ |
1834 |
|
|
1835 |
if (line && line->type != LT_MENUENTRY && |
if (line && line->type != LT_MENUENTRY && |
1836 |
line->numElements >= 2) { |
line->numElements >= 2) { |
1849 |
* non-Linux boot entries (could find netbsd etc, though, which is |
* non-Linux boot entries (could find netbsd etc, though, which is |
1850 |
* unfortunate) |
* unfortunate) |
1851 |
*/ |
*/ |
1852 |
if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines)) |
if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines)) |
1853 |
break; /* found 'im! */ |
break; /* found 'im! */ |
1854 |
} |
} |
1855 |
|
|
1951 |
|
|
1952 |
void setDefaultImage(struct grubConfig * config, int hasNew, |
void setDefaultImage(struct grubConfig * config, int hasNew, |
1953 |
const char * defaultKernelPath, int newIsDefault, |
const char * defaultKernelPath, int newIsDefault, |
1954 |
const char * prefix, int flags) { |
const char * prefix, int flags, int index) { |
1955 |
struct singleEntry * entry, * entry2, * newDefault; |
struct singleEntry * entry, * entry2, * newDefault; |
1956 |
int i, j; |
int i, j; |
1957 |
|
|
1958 |
if (newIsDefault) { |
if (newIsDefault) { |
1959 |
config->defaultImage = 0; |
config->defaultImage = 0; |
1960 |
return; |
return; |
1961 |
|
} else if ((index >= 0) && config->cfi->defaultIsIndex) { |
1962 |
|
if (findEntryByIndex(config, index)) |
1963 |
|
config->defaultImage = index; |
1964 |
|
else |
1965 |
|
config->defaultImage = -1; |
1966 |
|
return; |
1967 |
} else if (defaultKernelPath) { |
} else if (defaultKernelPath) { |
1968 |
i = 0; |
i = 0; |
1969 |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
2038 |
|
|
2039 |
printf("index=%d\n", index); |
printf("index=%d\n", index); |
2040 |
|
|
2041 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
2042 |
if (!line) { |
if (!line) { |
2043 |
printf("non linux entry\n"); |
printf("non linux entry\n"); |
2044 |
return; |
return; |
2045 |
} |
} |
2046 |
|
|
2047 |
printf("kernel=%s%s\n", prefix, line->elements[1].item); |
if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) |
2048 |
|
printf("kernel=%s\n", line->elements[1].item); |
2049 |
|
else |
2050 |
|
printf("kernel=%s%s\n", prefix, line->elements[1].item); |
2051 |
|
|
2052 |
if (line->numElements >= 3) { |
if (line->numElements >= 3) { |
2053 |
printf("args=\""); |
printf("args=\""); |
2103 |
printf("root=%s\n", s); |
printf("root=%s\n", s); |
2104 |
} |
} |
2105 |
|
|
2106 |
line = getLineByType(LT_INITRD, entry->lines); |
line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines); |
2107 |
|
|
2108 |
if (line && line->numElements >= 2) { |
if (line && line->numElements >= 2) { |
2109 |
printf("initrd=%s", prefix); |
if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) |
2110 |
|
printf("initrd="); |
2111 |
|
else |
2112 |
|
printf("initrd=%s", prefix); |
2113 |
|
|
2114 |
for (i = 1; i < line->numElements; i++) |
for (i = 1; i < line->numElements; i++) |
2115 |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
2116 |
printf("\n"); |
printf("\n"); |
2128 |
} |
} |
2129 |
} |
} |
2130 |
|
|
2131 |
|
int isSuseSystem(void) { |
2132 |
|
const char * path; |
2133 |
|
const static char default_path[] = "/etc/SuSE-release"; |
2134 |
|
|
2135 |
|
if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL) |
2136 |
|
path = default_path; |
2137 |
|
|
2138 |
|
if (!access(path, R_OK)) |
2139 |
|
return 1; |
2140 |
|
return 0; |
2141 |
|
} |
2142 |
|
|
2143 |
|
int isSuseGrubConf(const char * path) { |
2144 |
|
FILE * grubConf; |
2145 |
|
char * line = NULL; |
2146 |
|
size_t len = 0, res = 0; |
2147 |
|
|
2148 |
|
grubConf = fopen(path, "r"); |
2149 |
|
if (!grubConf) { |
2150 |
|
dbgPrintf("Could not open SuSE configuration file '%s'\n", path); |
2151 |
|
return 0; |
2152 |
|
} |
2153 |
|
|
2154 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2155 |
|
if (!strncmp(line, "setup", 5)) { |
2156 |
|
fclose(grubConf); |
2157 |
|
free(line); |
2158 |
|
return 1; |
2159 |
|
} |
2160 |
|
} |
2161 |
|
|
2162 |
|
dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n", |
2163 |
|
path); |
2164 |
|
|
2165 |
|
fclose(grubConf); |
2166 |
|
free(line); |
2167 |
|
return 0; |
2168 |
|
} |
2169 |
|
|
2170 |
|
int suseGrubConfGetLba(const char * path, int * lbaPtr) { |
2171 |
|
FILE * grubConf; |
2172 |
|
char * line = NULL; |
2173 |
|
size_t res = 0, len = 0; |
2174 |
|
|
2175 |
|
if (!path) return 1; |
2176 |
|
if (!lbaPtr) return 1; |
2177 |
|
|
2178 |
|
grubConf = fopen(path, "r"); |
2179 |
|
if (!grubConf) return 1; |
2180 |
|
|
2181 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2182 |
|
if (line[res - 1] == '\n') |
2183 |
|
line[res - 1] = '\0'; |
2184 |
|
else if (len > res) |
2185 |
|
line[res] = '\0'; |
2186 |
|
else { |
2187 |
|
line = realloc(line, res + 1); |
2188 |
|
line[res] = '\0'; |
2189 |
|
} |
2190 |
|
|
2191 |
|
if (!strncmp(line, "setup", 5)) { |
2192 |
|
if (strstr(line, "--force-lba")) { |
2193 |
|
*lbaPtr = 1; |
2194 |
|
} else { |
2195 |
|
*lbaPtr = 0; |
2196 |
|
} |
2197 |
|
dbgPrintf("lba: %i\n", *lbaPtr); |
2198 |
|
break; |
2199 |
|
} |
2200 |
|
} |
2201 |
|
|
2202 |
|
free(line); |
2203 |
|
fclose(grubConf); |
2204 |
|
return 0; |
2205 |
|
} |
2206 |
|
|
2207 |
|
int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) { |
2208 |
|
FILE * grubConf; |
2209 |
|
char * line = NULL; |
2210 |
|
size_t res = 0, len = 0; |
2211 |
|
char * lastParamPtr = NULL; |
2212 |
|
char * secLastParamPtr = NULL; |
2213 |
|
char installDeviceNumber = '\0'; |
2214 |
|
char * bounds = NULL; |
2215 |
|
|
2216 |
|
if (!path) return 1; |
2217 |
|
if (!devicePtr) return 1; |
2218 |
|
|
2219 |
|
grubConf = fopen(path, "r"); |
2220 |
|
if (!grubConf) return 1; |
2221 |
|
|
2222 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2223 |
|
if (strncmp(line, "setup", 5)) |
2224 |
|
continue; |
2225 |
|
|
2226 |
|
if (line[res - 1] == '\n') |
2227 |
|
line[res - 1] = '\0'; |
2228 |
|
else if (len > res) |
2229 |
|
line[res] = '\0'; |
2230 |
|
else { |
2231 |
|
line = realloc(line, res + 1); |
2232 |
|
line[res] = '\0'; |
2233 |
|
} |
2234 |
|
|
2235 |
|
lastParamPtr = bounds = line + res; |
2236 |
|
|
2237 |
|
/* Last parameter in grub may be an optional IMAGE_DEVICE */ |
2238 |
|
while (!isspace(*lastParamPtr)) |
2239 |
|
lastParamPtr--; |
2240 |
|
lastParamPtr++; |
2241 |
|
|
2242 |
|
secLastParamPtr = lastParamPtr - 2; |
2243 |
|
dbgPrintf("lastParamPtr: %s\n", lastParamPtr); |
2244 |
|
|
2245 |
|
if (lastParamPtr + 3 > bounds) { |
2246 |
|
dbgPrintf("lastParamPtr going over boundary"); |
2247 |
|
fclose(grubConf); |
2248 |
|
free(line); |
2249 |
|
return 1; |
2250 |
|
} |
2251 |
|
if (!strncmp(lastParamPtr, "(hd", 3)) |
2252 |
|
lastParamPtr += 3; |
2253 |
|
dbgPrintf("lastParamPtr: %c\n", *lastParamPtr); |
2254 |
|
|
2255 |
|
/* |
2256 |
|
* Second last parameter will decide wether last parameter is |
2257 |
|
* an IMAGE_DEVICE or INSTALL_DEVICE |
2258 |
|
*/ |
2259 |
|
while (!isspace(*secLastParamPtr)) |
2260 |
|
secLastParamPtr--; |
2261 |
|
secLastParamPtr++; |
2262 |
|
|
2263 |
|
if (secLastParamPtr + 3 > bounds) { |
2264 |
|
dbgPrintf("secLastParamPtr going over boundary"); |
2265 |
|
fclose(grubConf); |
2266 |
|
free(line); |
2267 |
|
return 1; |
2268 |
|
} |
2269 |
|
dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr); |
2270 |
|
if (!strncmp(secLastParamPtr, "(hd", 3)) { |
2271 |
|
secLastParamPtr += 3; |
2272 |
|
dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr); |
2273 |
|
installDeviceNumber = *secLastParamPtr; |
2274 |
|
} else { |
2275 |
|
installDeviceNumber = *lastParamPtr; |
2276 |
|
} |
2277 |
|
|
2278 |
|
*devicePtr = malloc(6); |
2279 |
|
snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber); |
2280 |
|
dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber); |
2281 |
|
fclose(grubConf); |
2282 |
|
free(line); |
2283 |
|
return 0; |
2284 |
|
} |
2285 |
|
|
2286 |
|
free(line); |
2287 |
|
fclose(grubConf); |
2288 |
|
return 1; |
2289 |
|
} |
2290 |
|
|
2291 |
|
int grubGetBootFromDeviceMap(const char * device, |
2292 |
|
char ** bootPtr) { |
2293 |
|
FILE * deviceMap; |
2294 |
|
char * line = NULL; |
2295 |
|
size_t res = 0, len = 0; |
2296 |
|
char * devicePtr; |
2297 |
|
char * bounds = NULL; |
2298 |
|
const char * path; |
2299 |
|
const static char default_path[] = "/boot/grub/device.map"; |
2300 |
|
|
2301 |
|
if (!device) return 1; |
2302 |
|
if (!bootPtr) return 1; |
2303 |
|
|
2304 |
|
if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL) |
2305 |
|
path = default_path; |
2306 |
|
|
2307 |
|
dbgPrintf("opening grub device.map file from: %s\n", path); |
2308 |
|
deviceMap = fopen(path, "r"); |
2309 |
|
if (!deviceMap) |
2310 |
|
return 1; |
2311 |
|
|
2312 |
|
while ((res = getline(&line, &len, deviceMap)) != -1) { |
2313 |
|
if (!strncmp(line, "#", 1)) |
2314 |
|
continue; |
2315 |
|
|
2316 |
|
if (line[res - 1] == '\n') |
2317 |
|
line[res - 1] = '\0'; |
2318 |
|
else if (len > res) |
2319 |
|
line[res] = '\0'; |
2320 |
|
else { |
2321 |
|
line = realloc(line, res + 1); |
2322 |
|
line[res] = '\0'; |
2323 |
|
} |
2324 |
|
|
2325 |
|
devicePtr = line; |
2326 |
|
bounds = line + res; |
2327 |
|
|
2328 |
|
while ((isspace(*line) && ((devicePtr + 1) <= bounds))) |
2329 |
|
devicePtr++; |
2330 |
|
dbgPrintf("device: %s\n", devicePtr); |
2331 |
|
|
2332 |
|
if (!strncmp(devicePtr, device, strlen(device))) { |
2333 |
|
devicePtr += strlen(device); |
2334 |
|
while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds)) |
2335 |
|
devicePtr++; |
2336 |
|
|
2337 |
|
*bootPtr = strdup(devicePtr); |
2338 |
|
break; |
2339 |
|
} |
2340 |
|
} |
2341 |
|
|
2342 |
|
free(line); |
2343 |
|
fclose(deviceMap); |
2344 |
|
return 0; |
2345 |
|
} |
2346 |
|
|
2347 |
|
int suseGrubConfGetBoot(const char * path, char ** bootPtr) { |
2348 |
|
char * grubDevice; |
2349 |
|
|
2350 |
|
if (suseGrubConfGetInstallDevice(path, &grubDevice)) |
2351 |
|
dbgPrintf("error looking for grub installation device\n"); |
2352 |
|
else |
2353 |
|
dbgPrintf("grubby installation device: %s\n", grubDevice); |
2354 |
|
|
2355 |
|
if (grubGetBootFromDeviceMap(grubDevice, bootPtr)) |
2356 |
|
dbgPrintf("error looking for grub boot device\n"); |
2357 |
|
else |
2358 |
|
dbgPrintf("grubby boot device: %s\n", *bootPtr); |
2359 |
|
|
2360 |
|
free(grubDevice); |
2361 |
|
return 0; |
2362 |
|
} |
2363 |
|
|
2364 |
|
int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) { |
2365 |
|
/* |
2366 |
|
* This SuSE grub configuration file at this location is not your average |
2367 |
|
* grub configuration file, but instead the grub commands used to setup |
2368 |
|
* grub on that system. |
2369 |
|
*/ |
2370 |
|
const char * path; |
2371 |
|
const static char default_path[] = "/etc/grub.conf"; |
2372 |
|
|
2373 |
|
if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL) |
2374 |
|
path = default_path; |
2375 |
|
|
2376 |
|
if (!isSuseGrubConf(path)) return 1; |
2377 |
|
|
2378 |
|
if (lbaPtr) { |
2379 |
|
*lbaPtr = 0; |
2380 |
|
if (suseGrubConfGetLba(path, lbaPtr)) |
2381 |
|
return 1; |
2382 |
|
} |
2383 |
|
|
2384 |
|
if (bootPtr) { |
2385 |
|
*bootPtr = NULL; |
2386 |
|
suseGrubConfGetBoot(path, bootPtr); |
2387 |
|
} |
2388 |
|
|
2389 |
|
return 0; |
2390 |
|
} |
2391 |
|
|
2392 |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
2393 |
FILE * in; |
FILE * in; |
2394 |
char buf[1024]; |
char buf[1024]; |
2437 |
} |
} |
2438 |
|
|
2439 |
void dumpSysconfigGrub(void) { |
void dumpSysconfigGrub(void) { |
2440 |
char * boot; |
char * boot = NULL; |
2441 |
int lba; |
int lba; |
2442 |
|
|
2443 |
if (!parseSysconfigGrub(&lba, &boot)) { |
if (isSuseSystem()) { |
2444 |
if (lba) printf("lba\n"); |
if (parseSuseGrubConf(&lba, &boot)) { |
2445 |
if (boot) printf("boot=%s\n", boot); |
free(boot); |
2446 |
|
return; |
2447 |
|
} |
2448 |
|
} else { |
2449 |
|
if (parseSysconfigGrub(&lba, &boot)) { |
2450 |
|
free(boot); |
2451 |
|
return; |
2452 |
|
} |
2453 |
|
} |
2454 |
|
|
2455 |
|
if (lba) printf("lba\n"); |
2456 |
|
if (boot) { |
2457 |
|
printf("boot=%s\n", boot); |
2458 |
|
free(boot); |
2459 |
} |
} |
2460 |
} |
} |
2461 |
|
|
2504 |
{ |
{ |
2505 |
struct singleLine * newLine = lineDup(tmplLine); |
struct singleLine * newLine = lineDup(tmplLine); |
2506 |
|
|
2507 |
|
if (isEfi && cfi == &grub2ConfigType) { |
2508 |
|
enum lineType_e old = newLine->type; |
2509 |
|
newLine->type = preferredLineType(newLine->type, cfi); |
2510 |
|
if (old != newLine->type) |
2511 |
|
newLine->elements[0].item = getKeyByType(newLine->type, cfi); |
2512 |
|
} |
2513 |
|
|
2514 |
if (val) { |
if (val) { |
2515 |
/* override the inherited value with our own. |
/* override the inherited value with our own. |
2516 |
* This is a little weak because it only applies to elements[1] |
* This is a little weak because it only applies to elements[1] |
2520 |
insertElement(newLine, val, 1, cfi); |
insertElement(newLine, val, 1, cfi); |
2521 |
|
|
2522 |
/* but try to keep the rootspec from the template... sigh */ |
/* but try to keep the rootspec from the template... sigh */ |
2523 |
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)) { |
2524 |
char * rootspec = getRootSpecifier(tmplLine->elements[1].item); |
char * rootspec = getRootSpecifier(tmplLine->elements[1].item); |
2525 |
if (rootspec != NULL) { |
if (rootspec != NULL) { |
2526 |
free(newLine->elements[1].item); |
free(newLine->elements[1].item); |
2557 |
/* 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 |
2558 |
* stack since it calls addLineTmpl which will make copies. |
* stack since it calls addLineTmpl which will make copies. |
2559 |
*/ |
*/ |
|
|
|
2560 |
if (type == LT_TITLE && cfi->titleBracketed) { |
if (type == LT_TITLE && cfi->titleBracketed) { |
2561 |
/* we're doing a bracketed title (zipl) */ |
/* we're doing a bracketed title (zipl) */ |
2562 |
tmpl.type = type; |
tmpl.type = type; |
2890 |
firstElement = 2; |
firstElement = 2; |
2891 |
|
|
2892 |
} else { |
} else { |
2893 |
line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines); |
line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines); |
2894 |
if (!line) { |
if (!line) { |
2895 |
/* no LT_KERNEL or LT_MBMODULE in this entry? */ |
/* no LT_KERNEL or LT_MBMODULE in this entry? */ |
2896 |
continue; |
continue; |
3055 |
if (!image) return 0; |
if (!image) return 0; |
3056 |
|
|
3057 |
for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { |
for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { |
3058 |
kernelLine = getLineByType(LT_KERNEL, entry->lines); |
kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines); |
3059 |
if (!kernelLine) continue; |
if (!kernelLine) continue; |
3060 |
|
|
3061 |
line = getLineByType(LT_INITRD, entry->lines); |
line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines); |
3062 |
if (line) |
if (line) |
3063 |
removeLine(entry, line); |
removeLine(entry, line); |
3064 |
if (prefix) { |
if (prefix) { |
3069 |
endLine = getLineByType(LT_ENTRY_END, entry->lines); |
endLine = getLineByType(LT_ENTRY_END, entry->lines); |
3070 |
if (endLine) |
if (endLine) |
3071 |
removeLine(entry, endLine); |
removeLine(entry, endLine); |
3072 |
line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd); |
line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi), |
3073 |
|
kernelLine->indent, initrd); |
3074 |
if (!line) |
if (!line) |
3075 |
return 1; |
return 1; |
3076 |
if (endLine) { |
if (endLine) { |
3276 |
int fd; |
int fd; |
3277 |
unsigned char bootSect[512]; |
unsigned char bootSect[512]; |
3278 |
char * boot; |
char * boot; |
3279 |
|
int onSuse = isSuseSystem(); |
3280 |
|
|
3281 |
if (parseSysconfigGrub(NULL, &boot)) |
|
3282 |
return 0; |
if (onSuse) { |
3283 |
|
if (parseSuseGrubConf(NULL, &boot)) |
3284 |
|
return 0; |
3285 |
|
} else { |
3286 |
|
if (parseSysconfigGrub(NULL, &boot)) |
3287 |
|
return 0; |
3288 |
|
} |
3289 |
|
|
3290 |
/* assume grub is not installed -- not an error condition */ |
/* assume grub is not installed -- not an error condition */ |
3291 |
if (!boot) |
if (!boot) |
3304 |
} |
} |
3305 |
close(fd); |
close(fd); |
3306 |
|
|
3307 |
|
/* The more elaborate checks do not work on SuSE. The checks done |
3308 |
|
* seem to be reasonble (at least for now), so just return success |
3309 |
|
*/ |
3310 |
|
if (onSuse) |
3311 |
|
return 2; |
3312 |
|
|
3313 |
return checkDeviceBootloader(boot, bootSect); |
return checkDeviceBootloader(boot, bootSect); |
3314 |
} |
} |
3315 |
|
|
3343 |
return checkDeviceBootloader(boot, bootSect); |
return checkDeviceBootloader(boot, bootSect); |
3344 |
} |
} |
3345 |
|
|
3346 |
|
int checkForYaboot(struct grubConfig * config) { |
3347 |
|
/* |
3348 |
|
* This is a simplistic check that we consider good enough for own puporses |
3349 |
|
* |
3350 |
|
* If we were to properly check if yaboot is *installed* we'd need to: |
3351 |
|
* 1) get the system boot device (LT_BOOT) |
3352 |
|
* 2) considering it's a raw filesystem, check if the yaboot binary matches |
3353 |
|
* the content on the boot device |
3354 |
|
* 3) if not, copy the binary to a temporary file and run "addnote" on it |
3355 |
|
* 4) check again if binary and boot device contents match |
3356 |
|
*/ |
3357 |
|
if (!access("/etc/yaboot.conf", R_OK)) |
3358 |
|
return 2; |
3359 |
|
|
3360 |
|
return 1; |
3361 |
|
} |
3362 |
|
|
3363 |
|
int checkForElilo(struct grubConfig * config) { |
3364 |
|
if (!access("/etc/elilo.conf", R_OK)) |
3365 |
|
return 2; |
3366 |
|
|
3367 |
|
return 1; |
3368 |
|
} |
3369 |
|
|
3370 |
static char * getRootSpecifier(char * str) { |
static char * getRootSpecifier(char * str) { |
3371 |
char * idx, * rootspec = NULL; |
char * idx, * rootspec = NULL; |
3372 |
|
|
3381 |
static char * getInitrdVal(struct grubConfig * config, |
static char * getInitrdVal(struct grubConfig * config, |
3382 |
const char * prefix, struct singleLine *tmplLine, |
const char * prefix, struct singleLine *tmplLine, |
3383 |
const char * newKernelInitrd, |
const char * newKernelInitrd, |
3384 |
char ** extraInitrds, int extraInitrdCount) |
const char ** extraInitrds, int extraInitrdCount) |
3385 |
{ |
{ |
3386 |
char *initrdVal, *end; |
char *initrdVal, *end; |
3387 |
int i; |
int i; |
3426 |
|
|
3427 |
int addNewKernel(struct grubConfig * config, struct singleEntry * template, |
int addNewKernel(struct grubConfig * config, struct singleEntry * template, |
3428 |
const char * prefix, |
const char * prefix, |
3429 |
char * newKernelPath, char * newKernelTitle, |
const char * newKernelPath, const char * newKernelTitle, |
3430 |
char * newKernelArgs, char * newKernelInitrd, |
const char * newKernelArgs, const char * newKernelInitrd, |
3431 |
char ** extraInitrds, int extraInitrdCount, |
const char ** extraInitrds, int extraInitrdCount, |
3432 |
char * newMBKernel, char * newMBKernelArgs) { |
const char * newMBKernel, const char * newMBKernelArgs) { |
3433 |
struct singleEntry * new; |
struct singleEntry * new; |
3434 |
struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL; |
struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL; |
3435 |
int needs; |
int needs; |
3483 |
while (*chptr && isspace(*chptr)) chptr++; |
while (*chptr && isspace(*chptr)) chptr++; |
3484 |
if (*chptr == '#') continue; |
if (*chptr == '#') continue; |
3485 |
|
|
3486 |
if (tmplLine->type == LT_KERNEL && |
if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) { |
|
tmplLine->numElements >= 2) { |
|
3487 |
if (!template->multiboot && (needs & NEED_MB)) { |
if (!template->multiboot && (needs & NEED_MB)) { |
3488 |
/* it's not a multiboot template and this is the kernel |
/* it's not a multiboot template and this is the kernel |
3489 |
* line. Try to be intelligent about inserting the |
* line. Try to be intelligent about inserting the |
3560 |
/* template is multi but new is not, |
/* template is multi but new is not, |
3561 |
* insert the kernel in the first module slot |
* insert the kernel in the first module slot |
3562 |
*/ |
*/ |
3563 |
tmplLine->type = LT_KERNEL; |
tmplLine->type = preferredLineType(LT_KERNEL, config->cfi); |
3564 |
free(tmplLine->elements[0].item); |
free(tmplLine->elements[0].item); |
3565 |
tmplLine->elements[0].item = |
tmplLine->elements[0].item = |
3566 |
strdup(getKeywordByType(LT_KERNEL, config->cfi)->key); |
strdup(getKeywordByType(tmplLine->type, |
3567 |
|
config->cfi)->key); |
3568 |
newLine = addLineTmpl(new, tmplLine, newLine, |
newLine = addLineTmpl(new, tmplLine, newLine, |
3569 |
newKernelPath + strlen(prefix), config->cfi); |
newKernelPath + strlen(prefix), |
3570 |
|
config->cfi); |
3571 |
needs &= ~NEED_KERNEL; |
needs &= ~NEED_KERNEL; |
3572 |
} else if (needs & NEED_INITRD) { |
} else if (needs & NEED_INITRD) { |
3573 |
char *initrdVal; |
char *initrdVal; |
3574 |
/* template is multi but new is not, |
/* template is multi but new is not, |
3575 |
* insert the initrd in the second module slot |
* insert the initrd in the second module slot |
3576 |
*/ |
*/ |
3577 |
tmplLine->type = LT_INITRD; |
tmplLine->type = preferredLineType(LT_INITRD, config->cfi); |
3578 |
free(tmplLine->elements[0].item); |
free(tmplLine->elements[0].item); |
3579 |
tmplLine->elements[0].item = |
tmplLine->elements[0].item = |
3580 |
strdup(getKeywordByType(LT_INITRD, config->cfi)->key); |
strdup(getKeywordByType(tmplLine->type, |
3581 |
|
config->cfi)->key); |
3582 |
initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount); |
initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount); |
3583 |
newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi); |
newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi); |
3584 |
free(initrdVal); |
free(initrdVal); |
3585 |
needs &= ~NEED_INITRD; |
needs &= ~NEED_INITRD; |
3586 |
} |
} |
3587 |
|
|
3588 |
} else if (tmplLine->type == LT_INITRD && |
} else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) { |
|
tmplLine->numElements >= 2) { |
|
3589 |
if (needs & NEED_INITRD && |
if (needs & NEED_INITRD && |
3590 |
new->multiboot && !template->multiboot && |
new->multiboot && !template->multiboot && |
3591 |
config->cfi->mbInitRdIsModule) { |
config->cfi->mbInitRdIsModule) { |
3639 |
static const char *prefix = "'Loading "; |
static const char *prefix = "'Loading "; |
3640 |
if (tmplLine->numElements > 1 && |
if (tmplLine->numElements > 1 && |
3641 |
strstr(tmplLine->elements[1].item, prefix) && |
strstr(tmplLine->elements[1].item, prefix) && |
3642 |
masterLine->next && masterLine->next->type == LT_KERNEL) { |
masterLine->next && |
3643 |
|
iskernel(masterLine->next->type)) { |
3644 |
char *newTitle = malloc(strlen(prefix) + |
char *newTitle = malloc(strlen(prefix) + |
3645 |
strlen(newKernelTitle) + 2); |
strlen(newKernelTitle) + 2); |
3646 |
|
|
3667 |
*/ |
*/ |
3668 |
switch (config->cfi->entryStart) { |
switch (config->cfi->entryStart) { |
3669 |
case LT_KERNEL: |
case LT_KERNEL: |
3670 |
|
case LT_KERNEL_EFI: |
3671 |
if (new->multiboot && config->cfi->mbHyperFirst) { |
if (new->multiboot && config->cfi->mbHyperFirst) { |
3672 |
/* fall through to LT_HYPER */ |
/* fall through to LT_HYPER */ |
3673 |
} else { |
} else { |
3674 |
newLine = addLine(new, config->cfi, LT_KERNEL, |
newLine = addLine(new, config->cfi, |
3675 |
|
preferredLineType(LT_KERNEL, config->cfi), |
3676 |
config->primaryIndent, |
config->primaryIndent, |
3677 |
newKernelPath + strlen(prefix)); |
newKernelPath + strlen(prefix)); |
3678 |
needs &= ~NEED_KERNEL; |
needs &= ~NEED_KERNEL; |
3748 |
if (needs & NEED_KERNEL) { |
if (needs & NEED_KERNEL) { |
3749 |
newLine = addLine(new, config->cfi, |
newLine = addLine(new, config->cfi, |
3750 |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
3751 |
config->cfi)) ? |
config->cfi)) |
3752 |
LT_MBMODULE : LT_KERNEL, |
? LT_MBMODULE |
3753 |
|
: preferredLineType(LT_KERNEL, config->cfi), |
3754 |
config->secondaryIndent, |
config->secondaryIndent, |
3755 |
newKernelPath + strlen(prefix)); |
newKernelPath + strlen(prefix)); |
3756 |
needs &= ~NEED_KERNEL; |
needs &= ~NEED_KERNEL; |
3766 |
initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount); |
initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount); |
3767 |
newLine = addLine(new, config->cfi, |
newLine = addLine(new, config->cfi, |
3768 |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
3769 |
config->cfi)) ? |
config->cfi)) |
3770 |
LT_MBMODULE : LT_INITRD, |
? LT_MBMODULE |
3771 |
|
: preferredLineType(LT_INITRD, config->cfi), |
3772 |
config->secondaryIndent, |
config->secondaryIndent, |
3773 |
initrdVal); |
initrdVal); |
3774 |
free(initrdVal); |
free(initrdVal); |
3800 |
memset(array, '\0', sizeof (array)); |
memset(array, '\0', sizeof (array)); |
3801 |
size = backtrace(array, 40); |
size = backtrace(array, 40); |
3802 |
|
|
3803 |
fprintf(stderr, "grubby recieved SIGSEGV! Backtrace (%ld):\n", |
fprintf(stderr, "grubby received SIGSEGV! Backtrace (%ld):\n", |
3804 |
(unsigned long)size); |
(unsigned long)size); |
3805 |
backtrace_symbols_fd(array, size, STDERR_FILENO); |
backtrace_symbols_fd(array, size, STDERR_FILENO); |
3806 |
exit(1); |
exit(1); |
3843 |
int displayDefault = 0; |
int displayDefault = 0; |
3844 |
int displayDefaultIndex = 0; |
int displayDefaultIndex = 0; |
3845 |
int displayDefaultTitle = 0; |
int displayDefaultTitle = 0; |
3846 |
|
int defaultIndex = -1; |
3847 |
struct poptOption options[] = { |
struct poptOption options[] = { |
3848 |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
3849 |
_("add an entry for the specified kernel"), _("kernel-path") }, |
_("add an entry for the specified kernel"), _("kernel-path") }, |
3861 |
{ "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0, |
{ "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0, |
3862 |
_("filestystem which contains /boot directory (for testing only)"), |
_("filestystem which contains /boot directory (for testing only)"), |
3863 |
_("bootfs") }, |
_("bootfs") }, |
3864 |
#if defined(__i386__) || defined(__x86_64__) |
#if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__) |
3865 |
{ "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0, |
{ "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0, |
3866 |
_("check if lilo is installed on lilo.conf boot sector") }, |
_("check which bootloader is installed on boot sector") }, |
3867 |
#endif |
#endif |
3868 |
{ "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0, |
{ "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0, |
3869 |
_("path to grub config file to update (\"-\" for stdin)"), |
_("path to grub config file to update (\"-\" for stdin)"), |
3884 |
_("display the title of the default kernel") }, |
_("display the title of the default kernel") }, |
3885 |
{ "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, |
{ "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, |
3886 |
_("configure elilo bootloader") }, |
_("configure elilo bootloader") }, |
3887 |
|
{ "efi", 0, POPT_ARG_NONE, &isEfi, 0, |
3888 |
|
_("force grub2 stanzas to use efi") }, |
3889 |
{ "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0, |
{ "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0, |
3890 |
_("configure extlinux bootloader (from syslinux)") }, |
_("configure extlinux bootloader (from syslinux)") }, |
3891 |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
3898 |
{ "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0, |
{ "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0, |
3899 |
_("initrd image for the new kernel"), _("initrd-path") }, |
_("initrd image for the new kernel"), _("initrd-path") }, |
3900 |
{ "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i', |
{ "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i', |
3901 |
_("auxilliary initrd image for things other than the new kernel"), _("initrd-path") }, |
_("auxiliary initrd image for things other than the new kernel"), _("initrd-path") }, |
3902 |
{ "lilo", 0, POPT_ARG_NONE, &configureLilo, 0, |
{ "lilo", 0, POPT_ARG_NONE, &configureLilo, 0, |
3903 |
_("configure lilo bootloader") }, |
_("configure lilo bootloader") }, |
3904 |
{ "make-default", 0, 0, &makeDefault, 0, |
{ "make-default", 0, 0, &makeDefault, 0, |
3918 |
{ "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0, |
{ "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0, |
3919 |
_("make the first entry referencing the specified kernel " |
_("make the first entry referencing the specified kernel " |
3920 |
"the default"), _("kernel-path") }, |
"the default"), _("kernel-path") }, |
3921 |
|
{ "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0, |
3922 |
|
_("make the given entry index the default entry"), |
3923 |
|
_("entry-index") }, |
3924 |
{ "silo", 0, POPT_ARG_NONE, &configureSilo, 0, |
{ "silo", 0, POPT_ARG_NONE, &configureSilo, 0, |
3925 |
_("configure silo bootloader") }, |
_("configure silo bootloader") }, |
3926 |
{ "title", 0, POPT_ARG_STRING, &newKernelTitle, 0, |
{ "title", 0, POPT_ARG_STRING, &newKernelTitle, 0, |
3942 |
|
|
3943 |
signal(SIGSEGV, traceback); |
signal(SIGSEGV, traceback); |
3944 |
|
|
3945 |
|
int i = 0; |
3946 |
|
for (int j = 1; j < argc; j++) |
3947 |
|
i += strlen(argv[j]) + 1; |
3948 |
|
saved_command_line = malloc(i); |
3949 |
|
if (!saved_command_line) { |
3950 |
|
fprintf(stderr, "grubby: %m\n"); |
3951 |
|
exit(1); |
3952 |
|
} |
3953 |
|
saved_command_line[0] = '\0'; |
3954 |
|
for (int j = 1; j < argc; j++) { |
3955 |
|
strcat(saved_command_line, argv[j]); |
3956 |
|
strncat(saved_command_line, j == argc -1 ? "" : " ", 1); |
3957 |
|
} |
3958 |
|
|
3959 |
optCon = poptGetContext("grubby", argc, argv, options, 0); |
optCon = poptGetContext("grubby", argc, argv, options, 0); |
3960 |
poptReadDefaultConfig(optCon, 1); |
poptReadDefaultConfig(optCon, 1); |
3961 |
|
|
4017 |
} |
} |
4018 |
|
|
4019 |
if (!cfi) { |
if (!cfi) { |
4020 |
|
if (grub2FindConfig(&grub2ConfigType)) |
4021 |
|
cfi = &grub2ConfigType; |
4022 |
|
else |
4023 |
#ifdef __ia64__ |
#ifdef __ia64__ |
4024 |
cfi = &eliloConfigType; |
cfi = &eliloConfigType; |
4025 |
#elif __powerpc__ |
#elif __powerpc__ |
4026 |
cfi = &yabootConfigType; |
cfi = &yabootConfigType; |
4027 |
#elif __sparc__ |
#elif __sparc__ |
4028 |
cfi = &siloConfigType; |
cfi = &siloConfigType; |
4029 |
#elif __s390__ |
#elif __s390__ |
4030 |
cfi = &ziplConfigType; |
cfi = &ziplConfigType; |
4031 |
#elif __s390x__ |
#elif __s390x__ |
4032 |
cfi = &ziplConfigtype; |
cfi = &ziplConfigtype; |
4033 |
#else |
#else |
|
if (grub2FindConfig(&grub2ConfigType)) |
|
|
cfi = &grub2ConfigType; |
|
|
else |
|
4034 |
cfi = &grubConfigType; |
cfi = &grubConfigType; |
4035 |
#endif |
#endif |
4036 |
} |
} |
4043 |
} |
} |
4044 |
|
|
4045 |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
4046 |
newKernelPath || removeKernelPath || makeDefault || |
newKernelPath || removeKernelPath || makeDefault || |
4047 |
defaultKernel || displayDefaultIndex || displayDefaultTitle)) { |
defaultKernel || displayDefaultIndex || displayDefaultTitle || |
4048 |
|
(defaultIndex >= 0))) { |
4049 |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
4050 |
"specified option")); |
"specified option")); |
4051 |
return 1; |
return 1; |
4087 |
makeDefault = 1; |
makeDefault = 1; |
4088 |
defaultKernel = NULL; |
defaultKernel = NULL; |
4089 |
} |
} |
4090 |
|
else if (defaultKernel && (defaultIndex >= 0)) { |
4091 |
|
fprintf(stderr, _("grubby: --set-default and --set-default-index " |
4092 |
|
"may not be used together\n")); |
4093 |
|
return 1; |
4094 |
|
} |
4095 |
|
|
4096 |
if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) { |
if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) { |
4097 |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
4100 |
} |
} |
4101 |
|
|
4102 |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
4103 |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
4104 |
&& !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) { |
&& !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle |
4105 |
|
&& (defaultIndex == -1)) { |
4106 |
fprintf(stderr, _("grubby: no action specified\n")); |
fprintf(stderr, _("grubby: no action specified\n")); |
4107 |
return 1; |
return 1; |
4108 |
} |
} |
4129 |
} |
} |
4130 |
|
|
4131 |
if (bootloaderProbe) { |
if (bootloaderProbe) { |
4132 |
int lrc = 0, grc = 0, gr2c = 0, erc = 0; |
int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0; |
4133 |
struct grubConfig * lconfig, * gconfig; |
struct grubConfig * lconfig, * gconfig, * yconfig, * econfig; |
4134 |
|
|
4135 |
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
4136 |
if (grub2config) { |
if (grub2config) { |
4158 |
lrc = checkForLilo(lconfig); |
lrc = checkForLilo(lconfig); |
4159 |
} |
} |
4160 |
|
|
4161 |
|
if (!access(eliloConfigType.defaultConfig, F_OK)) { |
4162 |
|
econfig = readConfig(eliloConfigType.defaultConfig, |
4163 |
|
&eliloConfigType); |
4164 |
|
if (!econfig) |
4165 |
|
erc = 1; |
4166 |
|
else |
4167 |
|
erc = checkForElilo(econfig); |
4168 |
|
} |
4169 |
|
|
4170 |
if (!access(extlinuxConfigType.defaultConfig, F_OK)) { |
if (!access(extlinuxConfigType.defaultConfig, F_OK)) { |
4171 |
lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType); |
lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType); |
4172 |
if (!lconfig) |
if (!lconfig) |
4173 |
erc = 1; |
extrc = 1; |
4174 |
else |
else |
4175 |
erc = checkForExtLinux(lconfig); |
extrc = checkForExtLinux(lconfig); |
4176 |
} |
} |
4177 |
|
|
4178 |
if (lrc == 1 || grc == 1 || gr2c == 1) return 1; |
|
4179 |
|
if (!access(yabootConfigType.defaultConfig, F_OK)) { |
4180 |
|
yconfig = readConfig(yabootConfigType.defaultConfig, |
4181 |
|
&yabootConfigType); |
4182 |
|
if (!yconfig) |
4183 |
|
yrc = 1; |
4184 |
|
else |
4185 |
|
yrc = checkForYaboot(yconfig); |
4186 |
|
} |
4187 |
|
|
4188 |
|
if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 || |
4189 |
|
erc == 1) |
4190 |
|
return 1; |
4191 |
|
|
4192 |
if (lrc == 2) printf("lilo\n"); |
if (lrc == 2) printf("lilo\n"); |
4193 |
if (gr2c == 2) printf("grub2\n"); |
if (gr2c == 2) printf("grub2\n"); |
4194 |
if (grc == 2) printf("grub\n"); |
if (grc == 2) printf("grub\n"); |
4195 |
if (erc == 2) printf("extlinux\n"); |
if (extrc == 2) printf("extlinux\n"); |
4196 |
|
if (yrc == 2) printf("yaboot\n"); |
4197 |
|
if (erc == 2) printf("elilo\n"); |
4198 |
|
|
4199 |
return 0; |
return 0; |
4200 |
} |
} |
4201 |
|
|
4202 |
|
if (grubConfig == NULL) { |
4203 |
|
printf("Could not find bootloader configuration file.\n"); |
4204 |
|
exit(1); |
4205 |
|
} |
4206 |
|
|
4207 |
config = readConfig(grubConfig, cfi); |
config = readConfig(grubConfig, cfi); |
4208 |
if (!config) return 1; |
if (!config) return 1; |
4209 |
|
|
4217 |
if (!entry) return 0; |
if (!entry) return 0; |
4218 |
if (!suitableImage(entry, bootPrefix, 0, flags)) return 0; |
if (!suitableImage(entry, bootPrefix, 0, flags)) return 0; |
4219 |
|
|
4220 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
4221 |
if (!line) return 0; |
if (!line) return 0; |
4222 |
|
|
4223 |
rootspec = getRootSpecifier(line->elements[1].item); |
rootspec = getRootSpecifier(line->elements[1].item); |
4254 |
} else if (displayDefaultIndex) { |
} else if (displayDefaultIndex) { |
4255 |
if (config->defaultImage == -1) return 0; |
if (config->defaultImage == -1) return 0; |
4256 |
printf("%i\n", config->defaultImage); |
printf("%i\n", config->defaultImage); |
4257 |
|
return 0; |
4258 |
|
|
4259 |
} else if (kernelInfo) |
} else if (kernelInfo) |
4260 |
return displayInfo(config, kernelInfo, bootPrefix); |
return displayInfo(config, kernelInfo, bootPrefix); |
4267 |
markRemovedImage(config, removeKernelPath, bootPrefix); |
markRemovedImage(config, removeKernelPath, bootPrefix); |
4268 |
markRemovedImage(config, removeMBKernel, bootPrefix); |
markRemovedImage(config, removeMBKernel, bootPrefix); |
4269 |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault, |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault, |
4270 |
bootPrefix, flags); |
bootPrefix, flags, defaultIndex); |
4271 |
setFallbackImage(config, newKernelPath != NULL); |
setFallbackImage(config, newKernelPath != NULL); |
4272 |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
4273 |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; |
4277 |
} |
} |
4278 |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
4279 |
newKernelTitle, newKernelArgs, newKernelInitrd, |
newKernelTitle, newKernelArgs, newKernelInitrd, |
4280 |
extraInitrds, extraInitrdCount, |
(const char **)extraInitrds, extraInitrdCount, |
4281 |
newMBKernel, newMBKernelArgs)) return 1; |
newMBKernel, newMBKernelArgs)) return 1; |
4282 |
|
|
4283 |
|
|