56 |
#define NOOP_OPCODE 0x90 |
#define NOOP_OPCODE 0x90 |
57 |
#define JMP_SHORT_OPCODE 0xeb |
#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 { |
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 }, |
249 |
} |
} |
250 |
|
|
251 |
int sizeOfSingleLine(struct singleLine * line) { |
int sizeOfSingleLine(struct singleLine * line) { |
|
int i; |
|
252 |
int count = 0; |
int count = 0; |
253 |
|
|
254 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
255 |
int indentSize = 0; |
int indentSize = 0; |
256 |
|
|
257 |
count = count + strlen(line->elements[i].item); |
count = count + strlen(line->elements[i].item); |
277 |
return 0; |
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) { |
char *grub2ExtractTitle(struct singleLine * line) { |
289 |
char * current; |
char * current; |
290 |
char * current_indent; |
char * current_indent; |
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 |
/* Need to handle this, because we strip the quotes from |
756 |
* menuentry when read it. */ |
* menuentry when read it. */ |
757 |
if (line->type == LT_MENUENTRY && i == 1) { |
if (line->type == LT_MENUENTRY && i == 1) { |
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; |
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, "-")) { |
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].item, line->elements[i].indent); |
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); |
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 |
|
|
1084 |
char *extras; |
char *extras; |
1085 |
char *title; |
char *title; |
1086 |
|
|
1087 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
1088 |
len += strlen(line->elements[i].item); |
len += strlen(line->elements[i].item); |
1089 |
len += strlen(line->elements[i].indent); |
len += strlen(line->elements[i].indent); |
1090 |
} |
} |
1096 |
*extras = '\0'; |
*extras = '\0'; |
1097 |
|
|
1098 |
/* get title. */ |
/* get title. */ |
1099 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
1100 |
if (!strcmp(line->elements[i].item, "menuentry")) |
if (!strcmp(line->elements[i].item, "menuentry")) |
1101 |
continue; |
continue; |
1102 |
if (isquote(*line->elements[i].item)) |
if (isquote(*line->elements[i].item)) |
1116 |
|
|
1117 |
/* get extras */ |
/* get extras */ |
1118 |
int count = 0; |
int count = 0; |
1119 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
1120 |
if (count >= 2) { |
if (count >= 2) { |
1121 |
strcat(extras, line->elements[i].item); |
strcat(extras, line->elements[i].item); |
1122 |
strcat(extras, line->elements[i].indent); |
strcat(extras, line->elements[i].indent); |
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; |
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 |
|
|
1594 |
return 0; |
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) { |
if (!line) { |
1599 |
notSuitablePrintf(entry, "no line found\n"); |
notSuitablePrintf(entry, "no line found\n"); |
1600 |
return 0; |
return 0; |
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]; |
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 ? |
enum lineType_e ct = checkType; |
1777 |
LT_KERNEL|LT_MBMODULE|LT_HYPER : |
if (entry->multiboot && checkType == LT_KERNEL) |
1778 |
checkType, line); |
ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER; |
1779 |
if (!line) break; /* not found in this entry */ |
else if (checkType & LT_KERNEL) |
1780 |
|
ct = checkType | LT_KERNEL_EFI; |
1781 |
|
line = getLineByType(ct, line); |
1782 |
|
if (!line) |
1783 |
|
break; /* not found in this entry */ |
1784 |
|
|
1785 |
if (line && line->type != LT_MENUENTRY && |
if (line && line->type != LT_MENUENTRY && |
1786 |
line->numElements >= 2) { |
line->numElements >= 2) { |
1799 |
* non-Linux boot entries (could find netbsd etc, though, which is |
* non-Linux boot entries (could find netbsd etc, though, which is |
1800 |
* unfortunate) |
* unfortunate) |
1801 |
*/ |
*/ |
1802 |
if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines)) |
if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines)) |
1803 |
break; /* found 'im! */ |
break; /* found 'im! */ |
1804 |
} |
} |
1805 |
|
|
1901 |
|
|
1902 |
void setDefaultImage(struct grubConfig * config, int hasNew, |
void setDefaultImage(struct grubConfig * config, int hasNew, |
1903 |
const char * defaultKernelPath, int newIsDefault, |
const char * defaultKernelPath, int newIsDefault, |
1904 |
const char * prefix, int flags) { |
const char * prefix, int flags, int index) { |
1905 |
struct singleEntry * entry, * entry2, * newDefault; |
struct singleEntry * entry, * entry2, * newDefault; |
1906 |
int i, j; |
int i, j; |
1907 |
|
|
1908 |
if (newIsDefault) { |
if (newIsDefault) { |
1909 |
config->defaultImage = 0; |
config->defaultImage = 0; |
1910 |
return; |
return; |
1911 |
|
} else if ((index >= 0) && config->cfi->defaultIsIndex) { |
1912 |
|
if (findEntryByIndex(config, index)) |
1913 |
|
config->defaultImage = index; |
1914 |
|
else |
1915 |
|
config->defaultImage = -1; |
1916 |
|
return; |
1917 |
} else if (defaultKernelPath) { |
} else if (defaultKernelPath) { |
1918 |
i = 0; |
i = 0; |
1919 |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
1988 |
|
|
1989 |
printf("index=%d\n", index); |
printf("index=%d\n", index); |
1990 |
|
|
1991 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
1992 |
if (!line) { |
if (!line) { |
1993 |
printf("non linux entry\n"); |
printf("non linux entry\n"); |
1994 |
return; |
return; |
1995 |
} |
} |
1996 |
|
|
1997 |
printf("kernel=%s%s\n", prefix, line->elements[1].item); |
if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) |
1998 |
|
printf("kernel=%s\n", line->elements[1].item); |
1999 |
|
else |
2000 |
|
printf("kernel=%s%s\n", prefix, line->elements[1].item); |
2001 |
|
|
2002 |
if (line->numElements >= 3) { |
if (line->numElements >= 3) { |
2003 |
printf("args=\""); |
printf("args=\""); |
2053 |
printf("root=%s\n", s); |
printf("root=%s\n", s); |
2054 |
} |
} |
2055 |
|
|
2056 |
line = getLineByType(LT_INITRD, entry->lines); |
line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines); |
2057 |
|
|
2058 |
if (line && line->numElements >= 2) { |
if (line && line->numElements >= 2) { |
2059 |
printf("initrd=%s", prefix); |
if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) |
2060 |
|
printf("initrd="); |
2061 |
|
else |
2062 |
|
printf("initrd=%s", prefix); |
2063 |
|
|
2064 |
for (i = 1; i < line->numElements; i++) |
for (i = 1; i < line->numElements; i++) |
2065 |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
2066 |
printf("\n"); |
printf("\n"); |
2078 |
} |
} |
2079 |
} |
} |
2080 |
|
|
2081 |
|
int isSuseSystem(void) { |
2082 |
|
const char * path; |
2083 |
|
const static char default_path[] = "/etc/SuSE-release"; |
2084 |
|
|
2085 |
|
if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL) |
2086 |
|
path = default_path; |
2087 |
|
|
2088 |
|
if (!access(path, R_OK)) |
2089 |
|
return 1; |
2090 |
|
return 0; |
2091 |
|
} |
2092 |
|
|
2093 |
|
int isSuseGrubConf(const char * path) { |
2094 |
|
FILE * grubConf; |
2095 |
|
char * line = NULL; |
2096 |
|
size_t len = 0, res = 0; |
2097 |
|
|
2098 |
|
grubConf = fopen(path, "r"); |
2099 |
|
if (!grubConf) { |
2100 |
|
dbgPrintf("Could not open SuSE configuration file '%s'\n", path); |
2101 |
|
return 0; |
2102 |
|
} |
2103 |
|
|
2104 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2105 |
|
if (!strncmp(line, "setup", 5)) { |
2106 |
|
fclose(grubConf); |
2107 |
|
free(line); |
2108 |
|
return 1; |
2109 |
|
} |
2110 |
|
} |
2111 |
|
|
2112 |
|
dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n", |
2113 |
|
path); |
2114 |
|
|
2115 |
|
fclose(grubConf); |
2116 |
|
free(line); |
2117 |
|
return 0; |
2118 |
|
} |
2119 |
|
|
2120 |
|
int suseGrubConfGetLba(const char * path, int * lbaPtr) { |
2121 |
|
FILE * grubConf; |
2122 |
|
char * line = NULL; |
2123 |
|
size_t res = 0, len = 0; |
2124 |
|
|
2125 |
|
if (!path) return 1; |
2126 |
|
if (!lbaPtr) return 1; |
2127 |
|
|
2128 |
|
grubConf = fopen(path, "r"); |
2129 |
|
if (!grubConf) return 1; |
2130 |
|
|
2131 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2132 |
|
if (line[res - 1] == '\n') |
2133 |
|
line[res - 1] = '\0'; |
2134 |
|
else if (len > res) |
2135 |
|
line[res] = '\0'; |
2136 |
|
else { |
2137 |
|
line = realloc(line, res + 1); |
2138 |
|
line[res] = '\0'; |
2139 |
|
} |
2140 |
|
|
2141 |
|
if (!strncmp(line, "setup", 5)) { |
2142 |
|
if (strstr(line, "--force-lba")) { |
2143 |
|
*lbaPtr = 1; |
2144 |
|
} else { |
2145 |
|
*lbaPtr = 0; |
2146 |
|
} |
2147 |
|
dbgPrintf("lba: %i\n", *lbaPtr); |
2148 |
|
break; |
2149 |
|
} |
2150 |
|
} |
2151 |
|
|
2152 |
|
free(line); |
2153 |
|
fclose(grubConf); |
2154 |
|
return 0; |
2155 |
|
} |
2156 |
|
|
2157 |
|
int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) { |
2158 |
|
FILE * grubConf; |
2159 |
|
char * line = NULL; |
2160 |
|
size_t res = 0, len = 0; |
2161 |
|
char * lastParamPtr = NULL; |
2162 |
|
char * secLastParamPtr = NULL; |
2163 |
|
char installDeviceNumber = '\0'; |
2164 |
|
char * bounds = NULL; |
2165 |
|
|
2166 |
|
if (!path) return 1; |
2167 |
|
if (!devicePtr) return 1; |
2168 |
|
|
2169 |
|
grubConf = fopen(path, "r"); |
2170 |
|
if (!grubConf) return 1; |
2171 |
|
|
2172 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2173 |
|
if (strncmp(line, "setup", 5)) |
2174 |
|
continue; |
2175 |
|
|
2176 |
|
if (line[res - 1] == '\n') |
2177 |
|
line[res - 1] = '\0'; |
2178 |
|
else if (len > res) |
2179 |
|
line[res] = '\0'; |
2180 |
|
else { |
2181 |
|
line = realloc(line, res + 1); |
2182 |
|
line[res] = '\0'; |
2183 |
|
} |
2184 |
|
|
2185 |
|
lastParamPtr = bounds = line + res; |
2186 |
|
|
2187 |
|
/* Last parameter in grub may be an optional IMAGE_DEVICE */ |
2188 |
|
while (!isspace(*lastParamPtr)) |
2189 |
|
lastParamPtr--; |
2190 |
|
lastParamPtr++; |
2191 |
|
|
2192 |
|
secLastParamPtr = lastParamPtr - 2; |
2193 |
|
dbgPrintf("lastParamPtr: %s\n", lastParamPtr); |
2194 |
|
|
2195 |
|
if (lastParamPtr + 3 > bounds) { |
2196 |
|
dbgPrintf("lastParamPtr going over boundary"); |
2197 |
|
fclose(grubConf); |
2198 |
|
free(line); |
2199 |
|
return 1; |
2200 |
|
} |
2201 |
|
if (!strncmp(lastParamPtr, "(hd", 3)) |
2202 |
|
lastParamPtr += 3; |
2203 |
|
dbgPrintf("lastParamPtr: %c\n", *lastParamPtr); |
2204 |
|
|
2205 |
|
/* |
2206 |
|
* Second last parameter will decide wether last parameter is |
2207 |
|
* an IMAGE_DEVICE or INSTALL_DEVICE |
2208 |
|
*/ |
2209 |
|
while (!isspace(*secLastParamPtr)) |
2210 |
|
secLastParamPtr--; |
2211 |
|
secLastParamPtr++; |
2212 |
|
|
2213 |
|
if (secLastParamPtr + 3 > bounds) { |
2214 |
|
dbgPrintf("secLastParamPtr going over boundary"); |
2215 |
|
fclose(grubConf); |
2216 |
|
free(line); |
2217 |
|
return 1; |
2218 |
|
} |
2219 |
|
dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr); |
2220 |
|
if (!strncmp(secLastParamPtr, "(hd", 3)) { |
2221 |
|
secLastParamPtr += 3; |
2222 |
|
dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr); |
2223 |
|
installDeviceNumber = *secLastParamPtr; |
2224 |
|
} else { |
2225 |
|
installDeviceNumber = *lastParamPtr; |
2226 |
|
} |
2227 |
|
|
2228 |
|
*devicePtr = malloc(6); |
2229 |
|
snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber); |
2230 |
|
dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber); |
2231 |
|
fclose(grubConf); |
2232 |
|
free(line); |
2233 |
|
return 0; |
2234 |
|
} |
2235 |
|
|
2236 |
|
free(line); |
2237 |
|
fclose(grubConf); |
2238 |
|
return 1; |
2239 |
|
} |
2240 |
|
|
2241 |
|
int grubGetBootFromDeviceMap(const char * device, |
2242 |
|
char ** bootPtr) { |
2243 |
|
FILE * deviceMap; |
2244 |
|
char * line = NULL; |
2245 |
|
size_t res = 0, len = 0; |
2246 |
|
char * devicePtr; |
2247 |
|
char * bounds = NULL; |
2248 |
|
const char * path; |
2249 |
|
const static char default_path[] = "/boot/grub/device.map"; |
2250 |
|
|
2251 |
|
if (!device) return 1; |
2252 |
|
if (!bootPtr) return 1; |
2253 |
|
|
2254 |
|
if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL) |
2255 |
|
path = default_path; |
2256 |
|
|
2257 |
|
dbgPrintf("opening grub device.map file from: %s\n", path); |
2258 |
|
deviceMap = fopen(path, "r"); |
2259 |
|
if (!deviceMap) |
2260 |
|
return 1; |
2261 |
|
|
2262 |
|
while ((res = getline(&line, &len, deviceMap)) != -1) { |
2263 |
|
if (!strncmp(line, "#", 1)) |
2264 |
|
continue; |
2265 |
|
|
2266 |
|
if (line[res - 1] == '\n') |
2267 |
|
line[res - 1] = '\0'; |
2268 |
|
else if (len > res) |
2269 |
|
line[res] = '\0'; |
2270 |
|
else { |
2271 |
|
line = realloc(line, res + 1); |
2272 |
|
line[res] = '\0'; |
2273 |
|
} |
2274 |
|
|
2275 |
|
devicePtr = line; |
2276 |
|
bounds = line + res; |
2277 |
|
|
2278 |
|
while ((isspace(*line) && ((devicePtr + 1) <= bounds))) |
2279 |
|
devicePtr++; |
2280 |
|
dbgPrintf("device: %s\n", devicePtr); |
2281 |
|
|
2282 |
|
if (!strncmp(devicePtr, device, strlen(device))) { |
2283 |
|
devicePtr += strlen(device); |
2284 |
|
while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds)) |
2285 |
|
devicePtr++; |
2286 |
|
|
2287 |
|
*bootPtr = strdup(devicePtr); |
2288 |
|
break; |
2289 |
|
} |
2290 |
|
} |
2291 |
|
|
2292 |
|
free(line); |
2293 |
|
fclose(deviceMap); |
2294 |
|
return 0; |
2295 |
|
} |
2296 |
|
|
2297 |
|
int suseGrubConfGetBoot(const char * path, char ** bootPtr) { |
2298 |
|
char * grubDevice; |
2299 |
|
|
2300 |
|
if (suseGrubConfGetInstallDevice(path, &grubDevice)) |
2301 |
|
dbgPrintf("error looking for grub installation device\n"); |
2302 |
|
else |
2303 |
|
dbgPrintf("grubby installation device: %s\n", grubDevice); |
2304 |
|
|
2305 |
|
if (grubGetBootFromDeviceMap(grubDevice, bootPtr)) |
2306 |
|
dbgPrintf("error looking for grub boot device\n"); |
2307 |
|
else |
2308 |
|
dbgPrintf("grubby boot device: %s\n", *bootPtr); |
2309 |
|
|
2310 |
|
free(grubDevice); |
2311 |
|
return 0; |
2312 |
|
} |
2313 |
|
|
2314 |
|
int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) { |
2315 |
|
/* |
2316 |
|
* This SuSE grub configuration file at this location is not your average |
2317 |
|
* grub configuration file, but instead the grub commands used to setup |
2318 |
|
* grub on that system. |
2319 |
|
*/ |
2320 |
|
const char * path; |
2321 |
|
const static char default_path[] = "/etc/grub.conf"; |
2322 |
|
|
2323 |
|
if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL) |
2324 |
|
path = default_path; |
2325 |
|
|
2326 |
|
if (!isSuseGrubConf(path)) return 1; |
2327 |
|
|
2328 |
|
if (lbaPtr) { |
2329 |
|
*lbaPtr = 0; |
2330 |
|
if (suseGrubConfGetLba(path, lbaPtr)) |
2331 |
|
return 1; |
2332 |
|
} |
2333 |
|
|
2334 |
|
if (bootPtr) { |
2335 |
|
*bootPtr = NULL; |
2336 |
|
suseGrubConfGetBoot(path, bootPtr); |
2337 |
|
} |
2338 |
|
|
2339 |
|
return 0; |
2340 |
|
} |
2341 |
|
|
2342 |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
2343 |
FILE * in; |
FILE * in; |
2344 |
char buf[1024]; |
char buf[1024]; |
2387 |
} |
} |
2388 |
|
|
2389 |
void dumpSysconfigGrub(void) { |
void dumpSysconfigGrub(void) { |
2390 |
char * boot; |
char * boot = NULL; |
2391 |
int lba; |
int lba; |
2392 |
|
|
2393 |
if (!parseSysconfigGrub(&lba, &boot)) { |
if (isSuseSystem()) { |
2394 |
if (lba) printf("lba\n"); |
if (parseSuseGrubConf(&lba, &boot)) { |
2395 |
if (boot) printf("boot=%s\n", boot); |
free(boot); |
2396 |
|
return; |
2397 |
|
} |
2398 |
|
} else { |
2399 |
|
if (parseSysconfigGrub(&lba, &boot)) { |
2400 |
|
free(boot); |
2401 |
|
return; |
2402 |
|
} |
2403 |
|
} |
2404 |
|
|
2405 |
|
if (lba) printf("lba\n"); |
2406 |
|
if (boot) { |
2407 |
|
printf("boot=%s\n", boot); |
2408 |
|
free(boot); |
2409 |
} |
} |
2410 |
} |
} |
2411 |
|
|
2454 |
{ |
{ |
2455 |
struct singleLine * newLine = lineDup(tmplLine); |
struct singleLine * newLine = lineDup(tmplLine); |
2456 |
|
|
2457 |
|
if (isEfi && cfi == &grub2ConfigType) { |
2458 |
|
enum lineType_e old = newLine->type; |
2459 |
|
newLine->type = preferredLineType(newLine->type, cfi); |
2460 |
|
if (old != newLine->type) |
2461 |
|
newLine->elements[0].item = getKeyByType(newLine->type, cfi); |
2462 |
|
} |
2463 |
|
|
2464 |
if (val) { |
if (val) { |
2465 |
/* override the inherited value with our own. |
/* override the inherited value with our own. |
2466 |
* This is a little weak because it only applies to elements[1] |
* This is a little weak because it only applies to elements[1] |
2470 |
insertElement(newLine, val, 1, cfi); |
insertElement(newLine, val, 1, cfi); |
2471 |
|
|
2472 |
/* but try to keep the rootspec from the template... sigh */ |
/* but try to keep the rootspec from the template... sigh */ |
2473 |
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)) { |
2474 |
char * rootspec = getRootSpecifier(tmplLine->elements[1].item); |
char * rootspec = getRootSpecifier(tmplLine->elements[1].item); |
2475 |
if (rootspec != NULL) { |
if (rootspec != NULL) { |
2476 |
free(newLine->elements[1].item); |
free(newLine->elements[1].item); |
2507 |
/* 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 |
2508 |
* stack since it calls addLineTmpl which will make copies. |
* stack since it calls addLineTmpl which will make copies. |
2509 |
*/ |
*/ |
|
|
|
2510 |
if (type == LT_TITLE && cfi->titleBracketed) { |
if (type == LT_TITLE && cfi->titleBracketed) { |
2511 |
/* we're doing a bracketed title (zipl) */ |
/* we're doing a bracketed title (zipl) */ |
2512 |
tmpl.type = type; |
tmpl.type = type; |
2840 |
firstElement = 2; |
firstElement = 2; |
2841 |
|
|
2842 |
} else { |
} else { |
2843 |
line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines); |
line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines); |
2844 |
if (!line) { |
if (!line) { |
2845 |
/* no LT_KERNEL or LT_MBMODULE in this entry? */ |
/* no LT_KERNEL or LT_MBMODULE in this entry? */ |
2846 |
continue; |
continue; |
3005 |
if (!image) return 0; |
if (!image) return 0; |
3006 |
|
|
3007 |
for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { |
for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { |
3008 |
kernelLine = getLineByType(LT_KERNEL, entry->lines); |
kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines); |
3009 |
if (!kernelLine) continue; |
if (!kernelLine) continue; |
3010 |
|
|
3011 |
line = getLineByType(LT_INITRD, entry->lines); |
line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines); |
3012 |
if (line) |
if (line) |
3013 |
removeLine(entry, line); |
removeLine(entry, line); |
3014 |
if (prefix) { |
if (prefix) { |
3019 |
endLine = getLineByType(LT_ENTRY_END, entry->lines); |
endLine = getLineByType(LT_ENTRY_END, entry->lines); |
3020 |
if (endLine) |
if (endLine) |
3021 |
removeLine(entry, endLine); |
removeLine(entry, endLine); |
3022 |
line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd); |
line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi), |
3023 |
|
kernelLine->indent, initrd); |
3024 |
if (!line) |
if (!line) |
3025 |
return 1; |
return 1; |
3026 |
if (endLine) { |
if (endLine) { |
3226 |
int fd; |
int fd; |
3227 |
unsigned char bootSect[512]; |
unsigned char bootSect[512]; |
3228 |
char * boot; |
char * boot; |
3229 |
|
int onSuse = isSuseSystem(); |
3230 |
|
|
3231 |
if (parseSysconfigGrub(NULL, &boot)) |
|
3232 |
return 0; |
if (onSuse) { |
3233 |
|
if (parseSuseGrubConf(NULL, &boot)) |
3234 |
|
return 0; |
3235 |
|
} else { |
3236 |
|
if (parseSysconfigGrub(NULL, &boot)) |
3237 |
|
return 0; |
3238 |
|
} |
3239 |
|
|
3240 |
/* assume grub is not installed -- not an error condition */ |
/* assume grub is not installed -- not an error condition */ |
3241 |
if (!boot) |
if (!boot) |
3254 |
} |
} |
3255 |
close(fd); |
close(fd); |
3256 |
|
|
3257 |
|
/* The more elaborate checks do not work on SuSE. The checks done |
3258 |
|
* seem to be reasonble (at least for now), so just return success |
3259 |
|
*/ |
3260 |
|
if (onSuse) |
3261 |
|
return 2; |
3262 |
|
|
3263 |
return checkDeviceBootloader(boot, bootSect); |
return checkDeviceBootloader(boot, bootSect); |
3264 |
} |
} |
3265 |
|
|
3293 |
return checkDeviceBootloader(boot, bootSect); |
return checkDeviceBootloader(boot, bootSect); |
3294 |
} |
} |
3295 |
|
|
3296 |
|
int checkForYaboot(struct grubConfig * config) { |
3297 |
|
/* |
3298 |
|
* This is a simplistic check that we consider good enough for own puporses |
3299 |
|
* |
3300 |
|
* If we were to properly check if yaboot is *installed* we'd need to: |
3301 |
|
* 1) get the system boot device (LT_BOOT) |
3302 |
|
* 2) considering it's a raw filesystem, check if the yaboot binary matches |
3303 |
|
* the content on the boot device |
3304 |
|
* 3) if not, copy the binary to a temporary file and run "addnote" on it |
3305 |
|
* 4) check again if binary and boot device contents match |
3306 |
|
*/ |
3307 |
|
if (!access("/etc/yaboot.conf", R_OK)) |
3308 |
|
return 2; |
3309 |
|
|
3310 |
|
return 1; |
3311 |
|
} |
3312 |
|
|
3313 |
|
int checkForElilo(struct grubConfig * config) { |
3314 |
|
if (!access("/etc/elilo.conf", R_OK)) |
3315 |
|
return 2; |
3316 |
|
|
3317 |
|
return 1; |
3318 |
|
} |
3319 |
|
|
3320 |
static char * getRootSpecifier(char * str) { |
static char * getRootSpecifier(char * str) { |
3321 |
char * idx, * rootspec = NULL; |
char * idx, * rootspec = NULL; |
3322 |
|
|
3331 |
static char * getInitrdVal(struct grubConfig * config, |
static char * getInitrdVal(struct grubConfig * config, |
3332 |
const char * prefix, struct singleLine *tmplLine, |
const char * prefix, struct singleLine *tmplLine, |
3333 |
const char * newKernelInitrd, |
const char * newKernelInitrd, |
3334 |
char ** extraInitrds, int extraInitrdCount) |
const char ** extraInitrds, int extraInitrdCount) |
3335 |
{ |
{ |
3336 |
char *initrdVal, *end; |
char *initrdVal, *end; |
3337 |
int i; |
int i; |
3376 |
|
|
3377 |
int addNewKernel(struct grubConfig * config, struct singleEntry * template, |
int addNewKernel(struct grubConfig * config, struct singleEntry * template, |
3378 |
const char * prefix, |
const char * prefix, |
3379 |
char * newKernelPath, char * newKernelTitle, |
const char * newKernelPath, const char * newKernelTitle, |
3380 |
char * newKernelArgs, char * newKernelInitrd, |
const char * newKernelArgs, const char * newKernelInitrd, |
3381 |
char ** extraInitrds, int extraInitrdCount, |
const char ** extraInitrds, int extraInitrdCount, |
3382 |
char * newMBKernel, char * newMBKernelArgs) { |
const char * newMBKernel, const char * newMBKernelArgs) { |
3383 |
struct singleEntry * new; |
struct singleEntry * new; |
3384 |
struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL; |
struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL; |
3385 |
int needs; |
int needs; |
3433 |
while (*chptr && isspace(*chptr)) chptr++; |
while (*chptr && isspace(*chptr)) chptr++; |
3434 |
if (*chptr == '#') continue; |
if (*chptr == '#') continue; |
3435 |
|
|
3436 |
if (tmplLine->type == LT_KERNEL && |
if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) { |
|
tmplLine->numElements >= 2) { |
|
3437 |
if (!template->multiboot && (needs & NEED_MB)) { |
if (!template->multiboot && (needs & NEED_MB)) { |
3438 |
/* it's not a multiboot template and this is the kernel |
/* it's not a multiboot template and this is the kernel |
3439 |
* line. Try to be intelligent about inserting the |
* line. Try to be intelligent about inserting the |
3510 |
/* template is multi but new is not, |
/* template is multi but new is not, |
3511 |
* insert the kernel in the first module slot |
* insert the kernel in the first module slot |
3512 |
*/ |
*/ |
3513 |
tmplLine->type = LT_KERNEL; |
tmplLine->type = preferredLineType(LT_KERNEL, config->cfi); |
3514 |
free(tmplLine->elements[0].item); |
free(tmplLine->elements[0].item); |
3515 |
tmplLine->elements[0].item = |
tmplLine->elements[0].item = |
3516 |
strdup(getKeywordByType(LT_KERNEL, config->cfi)->key); |
strdup(getKeywordByType(tmplLine->type, |
3517 |
|
config->cfi)->key); |
3518 |
newLine = addLineTmpl(new, tmplLine, newLine, |
newLine = addLineTmpl(new, tmplLine, newLine, |
3519 |
newKernelPath + strlen(prefix), config->cfi); |
newKernelPath + strlen(prefix), |
3520 |
|
config->cfi); |
3521 |
needs &= ~NEED_KERNEL; |
needs &= ~NEED_KERNEL; |
3522 |
} else if (needs & NEED_INITRD) { |
} else if (needs & NEED_INITRD) { |
3523 |
char *initrdVal; |
char *initrdVal; |
3524 |
/* template is multi but new is not, |
/* template is multi but new is not, |
3525 |
* insert the initrd in the second module slot |
* insert the initrd in the second module slot |
3526 |
*/ |
*/ |
3527 |
tmplLine->type = LT_INITRD; |
tmplLine->type = preferredLineType(LT_INITRD, config->cfi); |
3528 |
free(tmplLine->elements[0].item); |
free(tmplLine->elements[0].item); |
3529 |
tmplLine->elements[0].item = |
tmplLine->elements[0].item = |
3530 |
strdup(getKeywordByType(LT_INITRD, config->cfi)->key); |
strdup(getKeywordByType(tmplLine->type, |
3531 |
|
config->cfi)->key); |
3532 |
initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount); |
initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount); |
3533 |
newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi); |
newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi); |
3534 |
free(initrdVal); |
free(initrdVal); |
3535 |
needs &= ~NEED_INITRD; |
needs &= ~NEED_INITRD; |
3536 |
} |
} |
3537 |
|
|
3538 |
} else if (tmplLine->type == LT_INITRD && |
} else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) { |
|
tmplLine->numElements >= 2) { |
|
3539 |
if (needs & NEED_INITRD && |
if (needs & NEED_INITRD && |
3540 |
new->multiboot && !template->multiboot && |
new->multiboot && !template->multiboot && |
3541 |
config->cfi->mbInitRdIsModule) { |
config->cfi->mbInitRdIsModule) { |
3589 |
static const char *prefix = "'Loading "; |
static const char *prefix = "'Loading "; |
3590 |
if (tmplLine->numElements > 1 && |
if (tmplLine->numElements > 1 && |
3591 |
strstr(tmplLine->elements[1].item, prefix) && |
strstr(tmplLine->elements[1].item, prefix) && |
3592 |
masterLine->next && masterLine->next->type == LT_KERNEL) { |
masterLine->next && |
3593 |
|
iskernel(masterLine->next->type)) { |
3594 |
char *newTitle = malloc(strlen(prefix) + |
char *newTitle = malloc(strlen(prefix) + |
3595 |
strlen(newKernelTitle) + 2); |
strlen(newKernelTitle) + 2); |
3596 |
|
|
3617 |
*/ |
*/ |
3618 |
switch (config->cfi->entryStart) { |
switch (config->cfi->entryStart) { |
3619 |
case LT_KERNEL: |
case LT_KERNEL: |
3620 |
|
case LT_KERNEL_EFI: |
3621 |
if (new->multiboot && config->cfi->mbHyperFirst) { |
if (new->multiboot && config->cfi->mbHyperFirst) { |
3622 |
/* fall through to LT_HYPER */ |
/* fall through to LT_HYPER */ |
3623 |
} else { |
} else { |
3624 |
newLine = addLine(new, config->cfi, LT_KERNEL, |
newLine = addLine(new, config->cfi, |
3625 |
|
preferredLineType(LT_KERNEL, config->cfi), |
3626 |
config->primaryIndent, |
config->primaryIndent, |
3627 |
newKernelPath + strlen(prefix)); |
newKernelPath + strlen(prefix)); |
3628 |
needs &= ~NEED_KERNEL; |
needs &= ~NEED_KERNEL; |
3698 |
if (needs & NEED_KERNEL) { |
if (needs & NEED_KERNEL) { |
3699 |
newLine = addLine(new, config->cfi, |
newLine = addLine(new, config->cfi, |
3700 |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
3701 |
config->cfi)) ? |
config->cfi)) |
3702 |
LT_MBMODULE : LT_KERNEL, |
? LT_MBMODULE |
3703 |
|
: preferredLineType(LT_KERNEL, config->cfi), |
3704 |
config->secondaryIndent, |
config->secondaryIndent, |
3705 |
newKernelPath + strlen(prefix)); |
newKernelPath + strlen(prefix)); |
3706 |
needs &= ~NEED_KERNEL; |
needs &= ~NEED_KERNEL; |
3716 |
initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount); |
initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount); |
3717 |
newLine = addLine(new, config->cfi, |
newLine = addLine(new, config->cfi, |
3718 |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
(new->multiboot && getKeywordByType(LT_MBMODULE, |
3719 |
config->cfi)) ? |
config->cfi)) |
3720 |
LT_MBMODULE : LT_INITRD, |
? LT_MBMODULE |
3721 |
|
: preferredLineType(LT_INITRD, config->cfi), |
3722 |
config->secondaryIndent, |
config->secondaryIndent, |
3723 |
initrdVal); |
initrdVal); |
3724 |
free(initrdVal); |
free(initrdVal); |
3793 |
int displayDefault = 0; |
int displayDefault = 0; |
3794 |
int displayDefaultIndex = 0; |
int displayDefaultIndex = 0; |
3795 |
int displayDefaultTitle = 0; |
int displayDefaultTitle = 0; |
3796 |
|
int defaultIndex = -1; |
3797 |
struct poptOption options[] = { |
struct poptOption options[] = { |
3798 |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
3799 |
_("add an entry for the specified kernel"), _("kernel-path") }, |
_("add an entry for the specified kernel"), _("kernel-path") }, |
3811 |
{ "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0, |
{ "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0, |
3812 |
_("filestystem which contains /boot directory (for testing only)"), |
_("filestystem which contains /boot directory (for testing only)"), |
3813 |
_("bootfs") }, |
_("bootfs") }, |
3814 |
#if defined(__i386__) || defined(__x86_64__) |
#if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__) |
3815 |
{ "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0, |
{ "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0, |
3816 |
_("check if lilo is installed on lilo.conf boot sector") }, |
_("check which bootloader is installed on boot sector") }, |
3817 |
#endif |
#endif |
3818 |
{ "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0, |
{ "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0, |
3819 |
_("path to grub config file to update (\"-\" for stdin)"), |
_("path to grub config file to update (\"-\" for stdin)"), |
3834 |
_("display the title of the default kernel") }, |
_("display the title of the default kernel") }, |
3835 |
{ "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, |
{ "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, |
3836 |
_("configure elilo bootloader") }, |
_("configure elilo bootloader") }, |
3837 |
|
{ "efi", 0, POPT_ARG_NONE, &isEfi, 0, |
3838 |
|
_("force grub2 stanzas to use efi") }, |
3839 |
{ "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0, |
{ "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0, |
3840 |
_("configure extlinux bootloader (from syslinux)") }, |
_("configure extlinux bootloader (from syslinux)") }, |
3841 |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
{ "grub", 0, POPT_ARG_NONE, &configureGrub, 0, |
3868 |
{ "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0, |
{ "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0, |
3869 |
_("make the first entry referencing the specified kernel " |
_("make the first entry referencing the specified kernel " |
3870 |
"the default"), _("kernel-path") }, |
"the default"), _("kernel-path") }, |
3871 |
|
{ "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0, |
3872 |
|
_("make the given entry index the default entry"), |
3873 |
|
_("entry-index") }, |
3874 |
{ "silo", 0, POPT_ARG_NONE, &configureSilo, 0, |
{ "silo", 0, POPT_ARG_NONE, &configureSilo, 0, |
3875 |
_("configure silo bootloader") }, |
_("configure silo bootloader") }, |
3876 |
{ "title", 0, POPT_ARG_STRING, &newKernelTitle, 0, |
{ "title", 0, POPT_ARG_STRING, &newKernelTitle, 0, |
3979 |
} |
} |
3980 |
|
|
3981 |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
3982 |
newKernelPath || removeKernelPath || makeDefault || |
newKernelPath || removeKernelPath || makeDefault || |
3983 |
defaultKernel || displayDefaultIndex || displayDefaultTitle)) { |
defaultKernel || displayDefaultIndex || displayDefaultTitle || |
3984 |
|
(defaultIndex >= 0))) { |
3985 |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
3986 |
"specified option")); |
"specified option")); |
3987 |
return 1; |
return 1; |
4023 |
makeDefault = 1; |
makeDefault = 1; |
4024 |
defaultKernel = NULL; |
defaultKernel = NULL; |
4025 |
} |
} |
4026 |
|
else if (defaultKernel && (defaultIndex >= 0)) { |
4027 |
|
fprintf(stderr, _("grubby: --set-default and --set-default-index " |
4028 |
|
"may not be used together\n")); |
4029 |
|
return 1; |
4030 |
|
} |
4031 |
|
|
4032 |
if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) { |
if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) { |
4033 |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
4036 |
} |
} |
4037 |
|
|
4038 |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
4039 |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
4040 |
&& !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) { |
&& !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle |
4041 |
|
&& (defaultIndex == -1)) { |
4042 |
fprintf(stderr, _("grubby: no action specified\n")); |
fprintf(stderr, _("grubby: no action specified\n")); |
4043 |
return 1; |
return 1; |
4044 |
} |
} |
4065 |
} |
} |
4066 |
|
|
4067 |
if (bootloaderProbe) { |
if (bootloaderProbe) { |
4068 |
int lrc = 0, grc = 0, gr2c = 0, erc = 0; |
int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0; |
4069 |
struct grubConfig * lconfig, * gconfig; |
struct grubConfig * lconfig, * gconfig, * yconfig, * econfig; |
4070 |
|
|
4071 |
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
4072 |
if (grub2config) { |
if (grub2config) { |
4094 |
lrc = checkForLilo(lconfig); |
lrc = checkForLilo(lconfig); |
4095 |
} |
} |
4096 |
|
|
4097 |
|
if (!access(eliloConfigType.defaultConfig, F_OK)) { |
4098 |
|
econfig = readConfig(eliloConfigType.defaultConfig, |
4099 |
|
&eliloConfigType); |
4100 |
|
if (!econfig) |
4101 |
|
erc = 1; |
4102 |
|
else |
4103 |
|
erc = checkForElilo(econfig); |
4104 |
|
} |
4105 |
|
|
4106 |
if (!access(extlinuxConfigType.defaultConfig, F_OK)) { |
if (!access(extlinuxConfigType.defaultConfig, F_OK)) { |
4107 |
lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType); |
lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType); |
4108 |
if (!lconfig) |
if (!lconfig) |
4109 |
erc = 1; |
extrc = 1; |
4110 |
else |
else |
4111 |
erc = checkForExtLinux(lconfig); |
extrc = checkForExtLinux(lconfig); |
4112 |
} |
} |
4113 |
|
|
4114 |
if (lrc == 1 || grc == 1 || gr2c == 1) return 1; |
|
4115 |
|
if (!access(yabootConfigType.defaultConfig, F_OK)) { |
4116 |
|
yconfig = readConfig(yabootConfigType.defaultConfig, |
4117 |
|
&yabootConfigType); |
4118 |
|
if (!yconfig) |
4119 |
|
yrc = 1; |
4120 |
|
else |
4121 |
|
yrc = checkForYaboot(yconfig); |
4122 |
|
} |
4123 |
|
|
4124 |
|
if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 || |
4125 |
|
erc == 1) |
4126 |
|
return 1; |
4127 |
|
|
4128 |
if (lrc == 2) printf("lilo\n"); |
if (lrc == 2) printf("lilo\n"); |
4129 |
if (gr2c == 2) printf("grub2\n"); |
if (gr2c == 2) printf("grub2\n"); |
4130 |
if (grc == 2) printf("grub\n"); |
if (grc == 2) printf("grub\n"); |
4131 |
if (erc == 2) printf("extlinux\n"); |
if (extrc == 2) printf("extlinux\n"); |
4132 |
|
if (yrc == 2) printf("yaboot\n"); |
4133 |
|
if (erc == 2) printf("elilo\n"); |
4134 |
|
|
4135 |
return 0; |
return 0; |
4136 |
} |
} |
4148 |
if (!entry) return 0; |
if (!entry) return 0; |
4149 |
if (!suitableImage(entry, bootPrefix, 0, flags)) return 0; |
if (!suitableImage(entry, bootPrefix, 0, flags)) return 0; |
4150 |
|
|
4151 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
4152 |
if (!line) return 0; |
if (!line) return 0; |
4153 |
|
|
4154 |
rootspec = getRootSpecifier(line->elements[1].item); |
rootspec = getRootSpecifier(line->elements[1].item); |
4197 |
markRemovedImage(config, removeKernelPath, bootPrefix); |
markRemovedImage(config, removeKernelPath, bootPrefix); |
4198 |
markRemovedImage(config, removeMBKernel, bootPrefix); |
markRemovedImage(config, removeMBKernel, bootPrefix); |
4199 |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault, |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault, |
4200 |
bootPrefix, flags); |
bootPrefix, flags, defaultIndex); |
4201 |
setFallbackImage(config, newKernelPath != NULL); |
setFallbackImage(config, newKernelPath != NULL); |
4202 |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
4203 |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; |
4207 |
} |
} |
4208 |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
4209 |
newKernelTitle, newKernelArgs, newKernelInitrd, |
newKernelTitle, newKernelArgs, newKernelInitrd, |
4210 |
extraInitrds, extraInitrdCount, |
(const char **)extraInitrds, extraInitrdCount, |
4211 |
newMBKernel, newMBKernelArgs)) return 1; |
newMBKernel, newMBKernelArgs)) return 1; |
4212 |
|
|
4213 |
|
|