--- trunk/grubby/grubby.c 2013/10/21 14:01:48 2257 +++ trunk/grubby/grubby.c 2014/07/16 09:51:14 2683 @@ -90,7 +90,9 @@ LT_SET_VARIABLE = 1 << 19, LT_KERNEL_EFI = 1 << 20, LT_INITRD_EFI = 1 << 21, - LT_UNKNOWN = 1 << 22, + LT_KERNEL_16 = 1 << 22, + LT_INITRD_16 = 1 << 23, + LT_UNKNOWN = 1 << 24, }; struct singleLine { @@ -182,6 +184,8 @@ "/boot/grub/grub.conf", "/boot/grub/menu.lst", "/etc/grub.conf", + "/boot/grub2/grub.cfg", + "/boot/grub2-efi/grub.cfg", NULL }; static int i = -1; @@ -221,8 +225,10 @@ { "fallback", LT_FALLBACK, ' ' }, { "linux", LT_KERNEL, ' ' }, { "linuxefi", LT_KERNEL_EFI, ' ' }, + { "linux16", LT_KERNEL_16, ' ' }, { "initrd", LT_INITRD, ' ', ' ' }, { "initrdefi", LT_INITRD_EFI, ' ', ' ' }, + { "initrd16", LT_INITRD_16, ' ', ' ' }, { "module", LT_MBMODULE, ' ' }, { "kernel", LT_HYPER, ' ' }, { NULL, 0, 0 }, @@ -273,8 +279,8 @@ static char buf[1025]; char *s = NULL; char *ret = NULL; - char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv"; - int rc = asprintf(&s, "grub2-editenv %s list | grep '^%s='", envFile, name); + char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv"; + int rc = asprintf(&s, "grub-editenv %s list | grep '^%s='", envFile, name); if (rc < 0) return NULL; @@ -338,14 +344,14 @@ { char *s = NULL; int rc = 0; - char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv"; + char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv"; unquote(value); value = shellEscape(value); if (!value) return -1; - rc = asprintf(&s, "grub2-editenv %s set '%s=%s'", envFile, name, value); + rc = asprintf(&s, "grub-editenv %s set '%s=%s'", envFile, name, value); free(value); if (rc <0) return -1; @@ -396,11 +402,11 @@ } static int iskernel(enum lineType_e type) { - return (type == LT_KERNEL || type == LT_KERNEL_EFI); + return (type == LT_KERNEL || type == LT_KERNEL_EFI || type == LT_KERNEL_16); } static int isinitrd(enum lineType_e type) { - return (type == LT_INITRD || type == LT_INITRD_EFI); + return (type == LT_INITRD || type == LT_INITRD_EFI || type == LT_INITRD_16); } char *grub2ExtractTitle(struct singleLine * line) { @@ -718,6 +724,17 @@ default: return type; } +#if defined(__i386__) || defined(__x86_64__) + } else if (cfi == &grub2ConfigType) { + switch (type) { + case LT_KERNEL: + return LT_KERNEL_16; + case LT_INITRD: + return LT_INITRD_16; + default: + return type; + } +#endif } return type; } @@ -1066,6 +1083,15 @@ return 0; } +static int isnumber(const char *s) +{ + int i; + for (i = 0; s[i] != '\0'; i++) + if (s[i] < '0' || s[i] > '9') + return 0; + return i; +} + static struct grubConfig * readConfig(const char * inName, struct configFileInfo * cfi) { int in; @@ -1362,7 +1388,12 @@ char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry"); if (defTitle) { int index = 0; - entry = findEntryByTitle(cfg, defTitle, &index); + if (isnumber(defTitle)) { + index = atoi(defTitle); + entry = findEntryByIndex(cfg, index); + } else { + entry = findEntryByTitle(cfg, defTitle, &index); + } if (entry) cfg->defaultImage = index; } @@ -1411,7 +1442,12 @@ char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry"); if (defTitle) { int index = 0; - entry = findEntryByTitle(cfg, defTitle, &index); + if (isnumber(defTitle)) { + index = atoi(defTitle); + entry = findEntryByIndex(cfg, index); + } else { + entry = findEntryByTitle(cfg, defTitle, &index); + } if (entry) cfg->defaultImage = index; } @@ -1786,7 +1822,7 @@ return 0; } - line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); + line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines); if (!line) { notSuitablePrintf(entry, 0, "no line found\n"); return 0; @@ -1920,7 +1956,7 @@ entry = findEntryByIndex(config, indexVars[i]); if (!entry) return NULL; - line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); + line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines); if (!line) return NULL; if (index) *index = indexVars[i]; @@ -1971,9 +2007,9 @@ for (line = entry->lines; line; line = line->next) { enum lineType_e ct = checkType; if (entry->multiboot && checkType == LT_KERNEL) - ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER; + ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER|LT_KERNEL_16; else if (checkType & LT_KERNEL) - ct = checkType | LT_KERNEL_EFI; + ct = checkType | LT_KERNEL_EFI | LT_KERNEL_16; line = getLineByType(ct, line); if (!line) break; /* not found in this entry */ @@ -1995,7 +2031,7 @@ * non-Linux boot entries (could find netbsd etc, though, which is * unfortunate) */ - if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines)) + if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines)) break; /* found 'im! */ } @@ -2062,7 +2098,14 @@ char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry"); if (defTitle) { int index = 0; - entry = findEntryByTitle(cfg, defTitle, &index); + if (isnumber(defTitle)) { + index = atoi(defTitle); + entry = findEntryByIndex(cfg, index); + } else { + entry = findEntryByTitle(cfg, defTitle, &index); + } + if (entry) + cfg->defaultImage = index; } } } else if (cfg->defaultImage > -1) { @@ -2222,7 +2265,7 @@ printf("index=%d\n", index); - line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); + line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines); if (!line) { printf("non linux entry\n"); return; @@ -2287,7 +2330,7 @@ printf("root=%s\n", s); } - line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines); + line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines); if (line && line->numElements >= 2) { if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) @@ -2704,7 +2747,7 @@ insertElement(newLine, val, 1, cfi); /* but try to keep the rootspec from the template... sigh */ - if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI)) { + if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI|LT_KERNEL_16|LT_INITRD_16)) { char * rootspec = getRootSpecifier(tmplLine->elements[1].item); if (rootspec != NULL) { free(newLine->elements[1].item); @@ -3074,7 +3117,7 @@ firstElement = 2; } else { - line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines); + line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines); if (!line) { /* no LT_KERNEL or LT_MBMODULE in this entry? */ continue; @@ -3230,6 +3273,42 @@ return rc; } +int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel, + const char * image, const char * prefix, const char * initrd) { + struct singleEntry * entry; + struct singleLine * line, * kernelLine, *endLine = NULL; + int index = 0; + + if (!image) return 0; + + for (; (entry = findEntryByPath(cfg, newMBKernel, prefix, &index)); index++) { + kernelLine = getLineByType(LT_MBMODULE, entry->lines); + if (!kernelLine) continue; + + if (prefix) { + int prefixLen = strlen(prefix); + if (!strncmp(initrd, prefix, prefixLen)) + initrd += prefixLen; + } + endLine = getLineByType(LT_ENTRY_END, entry->lines); + if (endLine) + removeLine(entry, endLine); + line = addLine(entry, cfg->cfi, preferredLineType(LT_MBMODULE,cfg->cfi), + kernelLine->indent, initrd); + if (!line) + return 1; + if (endLine) { + line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL); + if (!line) + return 1; + } + + break; + } + + return 0; +} + int updateInitrd(struct grubConfig * cfg, const char * image, const char * prefix, const char * initrd) { struct singleEntry * entry; @@ -3239,10 +3318,10 @@ if (!image) return 0; for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { - kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines); + kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines); if (!kernelLine) continue; - line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines); + line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines); if (line) removeLine(entry, line); if (prefix) { @@ -3253,8 +3332,21 @@ endLine = getLineByType(LT_ENTRY_END, entry->lines); if (endLine) removeLine(entry, endLine); - line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi), - kernelLine->indent, initrd); + enum lineType_e lt; + switch(kernelLine->type) { + case LT_KERNEL: + lt = LT_INITRD; + break; + case LT_KERNEL_EFI: + lt = LT_INITRD_EFI; + break; + case LT_KERNEL_16: + lt = LT_INITRD_16; + break; + default: + lt = preferredLineType(LT_INITRD, cfg->cfi); + } + line = addLine(entry, cfg->cfi, lt, kernelLine->indent, initrd); if (!line) return 1; if (endLine) { @@ -3852,6 +3944,7 @@ switch (config->cfi->entryStart) { case LT_KERNEL: case LT_KERNEL_EFI: + case LT_KERNEL_16: if (new->multiboot && config->cfi->mbHyperFirst) { /* fall through to LT_HYPER */ } else { @@ -4410,7 +4503,7 @@ if (!entry) return 0; if (!suitableImage(entry, bootPrefix, 0, flags)) return 0; - line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); + line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines); if (!line) return 0; rootspec = getRootSpecifier(line->elements[1].item); @@ -4471,8 +4564,15 @@ if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; if (updateKernelPath && newKernelInitrd) { - if (updateInitrd(config, updateKernelPath, bootPrefix, - newKernelInitrd)) return 1; + if (newMBKernel) { + if (addMBInitrd(config, newMBKernel, updateKernelPath, + bootPrefix, newKernelInitrd)) + return 1; + } else { + if (updateInitrd(config, updateKernelPath, bootPrefix, + newKernelInitrd)) + return 1; + } } if (addNewKernel(config, template, bootPrefix, newKernelPath, newKernelTitle, newKernelArgs, newKernelInitrd,