--- trunk/grubby/grubby.c 2014/07/16 10:42:34 2688 +++ trunk/grubby/grubby.c 2016/06/30 10:26:25 2980 @@ -60,7 +60,7 @@ int isEfi = 0; -#if defined(__arch64__) +#if defined(__aarch64__) #define isEfiOnly 1 #else #define isEfiOnly 0 @@ -161,6 +161,7 @@ int defaultIsVariable; int defaultSupportSaved; int defaultIsSaved; + int defaultIsUnquoted; enum lineType_e entryStart; enum lineType_e entryEnd; int needsBootPrefix; @@ -221,6 +222,7 @@ .mbHyperFirst = 1, .mbInitRdIsModule = 1, .mbAllowExtraInitRds = 1, + .titlePosition = 1, }; struct keywordTypes grub2Keywords[] = { @@ -581,6 +583,8 @@ { "initrd", LT_INITRD, ' ', ',' }, { "append", LT_KERNELARGS, ' ' }, { "prompt", LT_UNKNOWN, ' ' }, + { "fdt", LT_DEVTREE, ' ' }, + { "fdtdir", LT_DEVTREE, ' ' }, { NULL, 0, 0 }, }; int useextlinuxmenu; @@ -591,6 +595,7 @@ .needsBootPrefix = 1, .argsInQuotes = 1, .mbConcatArgs = 1, + .titlePosition = 1, }; struct configFileInfo liloConfigType = { @@ -599,6 +604,7 @@ .entryStart = LT_KERNEL, .argsInQuotes = 1, .maxTitleLength = 15, + .titlePosition = 1, }; struct configFileInfo yabootConfigType = { @@ -609,6 +615,7 @@ .argsInQuotes = 1, .maxTitleLength = 15, .mbAllowExtraInitRds = 1, + .titlePosition = 1, }; struct configFileInfo siloConfigType = { @@ -618,6 +625,7 @@ .needsBootPrefix = 1, .argsInQuotes = 1, .maxTitleLength = 15, + .titlePosition = 1, }; struct configFileInfo ziplConfigType = { @@ -636,6 +644,8 @@ .needsBootPrefix = 1, .maxTitleLength = 255, .mbAllowExtraInitRds = 1, + .defaultIsUnquoted = 1, + .titlePosition = 1, }; struct grubConfig { @@ -824,13 +834,20 @@ } /* extract the title from within brackets (for zipl) */ -static char * extractTitle(struct singleLine * line) { - /* bracketed title... let's extract it (leaks a byte) */ +static char * extractTitle(struct grubConfig *cfg, struct singleLine * line) { + /* bracketed title... let's extract it */ char * title = NULL; + if (cfg->cfi == &grub2ConfigType) + return grub2ExtractTitle(line); if (line->type == LT_TITLE) { - title = strdup(line->elements[0].item); - title++; - *(title + strlen(title) - 1) = '\0'; + char *tmp = line->elements[cfg->cfi->titlePosition].item; + if (cfg->cfi->titleBracketed) { + tmp++; + title = strdup(tmp); + *(title + strlen(title) - 1) = '\0'; + } else { + title = strdup(tmp); + } } else if (line->type == LT_MENUENTRY) title = strdup(line->elements[1].item); else @@ -1191,16 +1208,13 @@ cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; defaultLine = line; } - } else if (line->type == LT_DEFAULT && line->numElements == 2) { - cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; - defaultLine = line; } else if (iskernel(line->type)) { /* if by some freak chance this is multiboot and the "module" * lines came earlier in the template, make sure to use LT_HYPER * instead of LT_KERNEL now */ - if (entry->multiboot) + if (entry && entry->multiboot) line->type = LT_HYPER; } else if (line->type == LT_MBMODULE) { @@ -1226,8 +1240,9 @@ cfg->fallbackImage = strtol(line->elements[1].item, &end, 10); if (*end) cfg->fallbackImage = -1; - } else if (line->type == LT_TITLE && line->numElements > 1) { - /* make the title a single argument (undoing our parsing) */ + } else if ((line->type == LT_DEFAULT && cfi->defaultIsUnquoted) || + (line->type == LT_TITLE && line->numElements > 1)) { + /* make the title/default a single argument (undoing our parsing) */ len = 0; for (int i = 1; i < line->numElements; i++) { len += strlen(line->elements[i].item); @@ -1334,6 +1349,11 @@ } } + if (line->type == LT_DEFAULT && line->numElements == 2) { + cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT; + defaultLine = line; + } + /* If we find a generic config option which should live at the top of the file, move it there. Old versions of grubby were probably responsible for putting new images in the wrong @@ -1435,7 +1455,7 @@ line->elements[1].item)) break; } else if (line) { if (!strcmp(defaultLine->elements[1].item, - extractTitle(line))) break; + extractTitle(cfg, line))) break; } i++; entry = NULL; @@ -1486,7 +1506,7 @@ if (!line) line = getLineByType(LT_TITLE, entry->lines); if (line) { - title = extractTitle(line); + title = extractTitle(cfg, line); if (title) cfg->cfi->setEnv(cfg->cfi, "saved_entry", title); } @@ -1524,7 +1544,7 @@ else if (line && (line->numElements == 1) && cfg->cfi->titleBracketed) { fprintf(out, "%sdefault%s%s\n", indent, separator, - extractTitle(line)); + extractTitle(cfg, line)); } } } @@ -1955,11 +1975,13 @@ } indexVars[i + 1] = -1; - + i = 0; if (index) { - while (i < *index) i++; - if (indexVars[i] == -1) return NULL; + while (i < *index) { + i++; + if (indexVars[i] == -1) return NULL; + } } entry = findEntryByIndex(config, indexVars[i]); @@ -2113,8 +2135,12 @@ } else { entry = findEntryByTitle(cfg, defTitle, &index); } - if (entry) + if (entry && suitableImage(entry, prefix, skipRemoved, flags)) { cfg->defaultImage = index; + if (indexPtr) + *indexPtr = index; + return entry; + } } } } else if (cfg->defaultImage > -1) { @@ -2271,6 +2297,7 @@ struct singleLine * line; char * root = NULL; int i; + int j; printf("index=%d\n", index); @@ -2358,9 +2385,25 @@ } else { char * title; line = getLineByType(LT_MENUENTRY, entry->lines); - title = grub2ExtractTitle(line); - if (title) - printf("title=%s\n", title); + if (line) { + title = grub2ExtractTitle(line); + if (title) + printf("title=%s\n", title); + } + } + + for (j = 0, line = entry->lines; line; line = line->next) { + if ((line->type & LT_MBMODULE) && line->numElements >= 2) { + if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) + printf("mbmodule%d=", j); + else + printf("mbmodule%d=%s", j, prefix); + + for (i = 1; i < line->numElements; i++) + printf("%s%s", line->elements[i].item, line->elements[i].indent); + printf("\n"); + j++; + } } } @@ -3283,17 +3326,36 @@ } int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel, - const char * image, const char * prefix, const char * initrd) { + const char * image, const char * prefix, const char * initrd, + const char * title) { struct singleEntry * entry; struct singleLine * line, * kernelLine, *endLine = NULL; int index = 0; if (!image) return 0; - for (; (entry = findEntryByPath(cfg, newMBKernel, prefix, &index)); index++) { + for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) { kernelLine = getLineByType(LT_MBMODULE, entry->lines); if (!kernelLine) continue; + /* if title is supplied, the entry's title must match it. */ + if (title) { + char *linetitle; + + line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines); + if (!line) + continue; + + linetitle = extractTitle(cfg, line); + if (!linetitle) + continue; + if (strcmp(title, linetitle)) { + free(linetitle); + continue; + } + free(linetitle); + } + if (prefix) { int prefixLen = strlen(prefix); if (!strncmp(initrd, prefix, prefixLen)) @@ -3319,7 +3381,7 @@ } int updateInitrd(struct grubConfig * cfg, const char * image, - const char * prefix, const char * initrd) { + const char * prefix, const char * initrd, const char * title) { struct singleEntry * entry; struct singleLine * line, * kernelLine, *endLine = NULL; int index = 0; @@ -3330,6 +3392,24 @@ kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines); if (!kernelLine) continue; + /* if title is supplied, the entry's title must match it. */ + if (title) { + char *linetitle; + + line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines); + if (!line) + continue; + + linetitle = extractTitle(cfg, line); + if (!linetitle) + continue; + if (strcmp(title, linetitle)) { + free(linetitle); + continue; + } + free(linetitle); + } + line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines); if (line) removeLine(entry, line); @@ -4033,6 +4113,13 @@ } } + struct singleLine *endLine = NULL; + endLine = getLineByType(LT_ENTRY_END, new->lines); + if (endLine) { + removeLine(new, endLine); + needs |= NEED_END; + } + /* add the remainder of the lines, i.e. those that either * weren't present in the template, or in the case of no template, * all the lines following the entryStart. @@ -4091,6 +4178,7 @@ config->secondaryIndent, NULL); needs &= ~NEED_END; } + if (needs) { printf(_("grubby: needs=%d, aborting\n"), needs); abort(); @@ -4196,6 +4284,8 @@ _("display the title of the default kernel") }, { "devtree", 0, POPT_ARG_STRING, &newDevTreePath, 0, _("device tree file for new stanza"), _("dtb-path") }, + { "devtreedir", 0, POPT_ARG_STRING, &newDevTreePath, 0, + _("device tree directory for new stanza"), _("dtb-path") }, { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, _("configure elilo bootloader") }, { "efi", 0, POPT_ARG_NONE, &isEfi, 0, @@ -4380,7 +4470,7 @@ if (newKernelPath && !newKernelTitle) { fprintf(stderr, _("grubby: kernel title must be specified\n")); return 1; - } else if (!newKernelPath && (newKernelTitle || copyDefault || + } else if (!newKernelPath && (copyDefault || (newKernelInitrd && !updateKernelPath)|| makeDefault || extraInitrdCount > 0)) { fprintf(stderr, _("grubby: kernel path expected\n")); @@ -4602,11 +4692,12 @@ if (updateKernelPath && newKernelInitrd) { if (newMBKernel) { if (addMBInitrd(config, newMBKernel, updateKernelPath, - bootPrefix, newKernelInitrd)) + bootPrefix, newKernelInitrd, + newKernelTitle)) return 1; } else { if (updateInitrd(config, updateKernelPath, bootPrefix, - newKernelInitrd)) + newKernelInitrd, newKernelTitle)) return 1; } }