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 { |
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 }, |
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 |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
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 |
1040 |
for (struct singleLine *l = entry->lines; l; l = l->next) { |
for (struct singleLine *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 |
} |
} |
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); |
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 |
|
|
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; |
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 |
if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) |
if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) |
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) { |
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); |
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, |
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); |