Magellan Linux

Diff of /trunk/grubby/grubby.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2688 by niro, Wed Jul 16 10:42:34 2014 UTC revision 2983 by niro, Thu Jun 30 10:27:31 2016 UTC
# Line 60  int debug = 0; /* Currently just for tem Line 60  int debug = 0; /* Currently just for tem
60    
61  int isEfi = 0;  int isEfi = 0;
62    
63  #if defined(__arch64__)  #if defined(__aarch64__)
64  #define isEfiOnly 1  #define isEfiOnly 1
65  #else  #else
66  #define isEfiOnly 0  #define isEfiOnly 0
# Line 161  struct configFileInfo { Line 161  struct configFileInfo {
161      int defaultIsVariable;      int defaultIsVariable;
162      int defaultSupportSaved;      int defaultSupportSaved;
163      int defaultIsSaved;      int defaultIsSaved;
164        int defaultIsUnquoted;
165      enum lineType_e entryStart;      enum lineType_e entryStart;
166      enum lineType_e entryEnd;      enum lineType_e entryEnd;
167      int needsBootPrefix;      int needsBootPrefix;
# Line 221  struct configFileInfo grubConfigType = { Line 222  struct configFileInfo grubConfigType = {
222      .mbHyperFirst = 1,      .mbHyperFirst = 1,
223      .mbInitRdIsModule = 1,      .mbInitRdIsModule = 1,
224      .mbAllowExtraInitRds = 1,      .mbAllowExtraInitRds = 1,
225        .titlePosition = 1,
226  };  };
227    
228  struct keywordTypes grub2Keywords[] = {  struct keywordTypes grub2Keywords[] = {
# Line 581  struct keywordTypes extlinuxKeywords[] = Line 583  struct keywordTypes extlinuxKeywords[] =
583      { "initrd",    LT_INITRD,      ' ', ',' },      { "initrd",    LT_INITRD,      ' ', ',' },
584      { "append",    LT_KERNELARGS,  ' ' },      { "append",    LT_KERNELARGS,  ' ' },
585      { "prompt",     LT_UNKNOWN,     ' ' },      { "prompt",     LT_UNKNOWN,     ' ' },
586        { "fdt",        LT_DEVTREE,     ' ' },
587        { "fdtdir",     LT_DEVTREE,     ' ' },
588      { NULL,    0, 0 },      { NULL,    0, 0 },
589  };  };
590  int useextlinuxmenu;  int useextlinuxmenu;
# Line 591  struct configFileInfo eliloConfigType = Line 595  struct configFileInfo eliloConfigType =
595      .needsBootPrefix = 1,      .needsBootPrefix = 1,
596      .argsInQuotes = 1,      .argsInQuotes = 1,
597      .mbConcatArgs = 1,      .mbConcatArgs = 1,
598        .titlePosition = 1,
599  };  };
600    
601  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
# Line 599  struct configFileInfo liloConfigType = { Line 604  struct configFileInfo liloConfigType = {
604      .entryStart = LT_KERNEL,      .entryStart = LT_KERNEL,
605      .argsInQuotes = 1,      .argsInQuotes = 1,
606      .maxTitleLength = 15,      .maxTitleLength = 15,
607        .titlePosition = 1,
608  };  };
609    
610  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
# Line 609  struct configFileInfo yabootConfigType = Line 615  struct configFileInfo yabootConfigType =
615      .argsInQuotes = 1,      .argsInQuotes = 1,
616      .maxTitleLength = 15,      .maxTitleLength = 15,
617      .mbAllowExtraInitRds = 1,      .mbAllowExtraInitRds = 1,
618        .titlePosition = 1,
619  };  };
620    
621  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
# Line 618  struct configFileInfo siloConfigType = { Line 625  struct configFileInfo siloConfigType = {
625      .needsBootPrefix = 1,      .needsBootPrefix = 1,
626      .argsInQuotes = 1,      .argsInQuotes = 1,
627      .maxTitleLength = 15,      .maxTitleLength = 15,
628        .titlePosition = 1,
629  };  };
630    
631  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
# Line 636  struct configFileInfo extlinuxConfigType Line 644  struct configFileInfo extlinuxConfigType
644      .needsBootPrefix = 1,      .needsBootPrefix = 1,
645      .maxTitleLength = 255,      .maxTitleLength = 255,
646      .mbAllowExtraInitRds = 1,      .mbAllowExtraInitRds = 1,
647        .defaultIsUnquoted = 1,
648        .titlePosition = 1,
649  };  };
650    
651  struct grubConfig {  struct grubConfig {
# Line 824  static int isEntryStart(struct singleLin Line 834  static int isEntryStart(struct singleLin
834  }  }
835    
836  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
837  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct grubConfig *cfg, struct singleLine * line) {
838      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it */
839      char * title = NULL;      char * title = NULL;
840      if (line->type == LT_TITLE) {      if (line->type == LT_TITLE) {
841   title = strdup(line->elements[0].item);   char *tmp = line->elements[cfg->cfi->titlePosition].item;
842   title++;   if (cfg->cfi->titleBracketed) {
843   *(title + strlen(title) - 1) = '\0';      tmp++;
844        title = strdup(tmp);
845        *(title + strlen(title) - 1) = '\0';
846     } else {
847        title = strdup(tmp);
848     }
849      } else if (line->type == LT_MENUENTRY)      } else if (line->type == LT_MENUENTRY)
850   title = strdup(line->elements[1].item);   title = strdup(line->elements[1].item);
851      else      else
# Line 1191  static struct grubConfig * readConfig(co Line 1206  static struct grubConfig * readConfig(co
1206   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1207   defaultLine = line;   defaultLine = line;
1208      }      }
  } else if (line->type == LT_DEFAULT && line->numElements == 2) {  
     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;  
     defaultLine = line;  
1209    
1210          } else if (iskernel(line->type)) {          } else if (iskernel(line->type)) {
1211      /* if by some freak chance this is multiboot and the "module"      /* if by some freak chance this is multiboot and the "module"
1212       * lines came earlier in the template, make sure to use LT_HYPER       * lines came earlier in the template, make sure to use LT_HYPER
1213       * instead of LT_KERNEL now       * instead of LT_KERNEL now
1214       */       */
1215      if (entry->multiboot)      if (entry && entry->multiboot)
1216   line->type = LT_HYPER;   line->type = LT_HYPER;
1217    
1218          } else if (line->type == LT_MBMODULE) {          } else if (line->type == LT_MBMODULE) {
# Line 1226  static struct grubConfig * readConfig(co Line 1238  static struct grubConfig * readConfig(co
1238      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);      cfg->fallbackImage = strtol(line->elements[1].item, &end, 10);
1239      if (*end) cfg->fallbackImage = -1;      if (*end) cfg->fallbackImage = -1;
1240    
1241   } else if (line->type == LT_TITLE && line->numElements > 1) {   } else if ((line->type == LT_DEFAULT && cfi->defaultIsUnquoted) ||
1242      /* make the title a single argument (undoing our parsing) */                  (line->type == LT_TITLE && line->numElements > 1)) {
1243        /* make the title/default a single argument (undoing our parsing) */
1244      len = 0;      len = 0;
1245      for (int i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1246   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
# Line 1334  static struct grubConfig * readConfig(co Line 1347  static struct grubConfig * readConfig(co
1347      }      }
1348   }   }
1349    
1350     if (line->type == LT_DEFAULT && line->numElements == 2) {
1351        cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1352        defaultLine = line;
1353     }
1354    
1355   /* If we find a generic config option which should live at the   /* If we find a generic config option which should live at the
1356     top of the file, move it there. Old versions of grubby were     top of the file, move it there. Old versions of grubby were
1357     probably responsible for putting new images in the wrong     probably responsible for putting new images in the wrong
# Line 1435  static struct grubConfig * readConfig(co Line 1453  static struct grubConfig * readConfig(co
1453                                  line->elements[1].item)) break;                                  line->elements[1].item)) break;
1454                  } else if (line) {                  } else if (line) {
1455                      if (!strcmp(defaultLine->elements[1].item,                      if (!strcmp(defaultLine->elements[1].item,
1456                                  extractTitle(line))) break;                                  extractTitle(cfg, line))) break;
1457                  }                  }
1458   i++;   i++;
1459   entry = NULL;   entry = NULL;
# Line 1486  static void writeDefault(FILE * out, cha Line 1504  static void writeDefault(FILE * out, cha
1504      if (!line)      if (!line)
1505   line = getLineByType(LT_TITLE, entry->lines);   line = getLineByType(LT_TITLE, entry->lines);
1506      if (line) {      if (line) {
1507   title = extractTitle(line);   title = extractTitle(cfg, line);
1508   if (title)   if (title)
1509      cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);      cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1510      }      }
# Line 1524  static void writeDefault(FILE * out, cha Line 1542  static void writeDefault(FILE * out, cha
1542              else if (line && (line->numElements == 1) &&              else if (line && (line->numElements == 1) &&
1543                       cfg->cfi->titleBracketed) {                       cfg->cfi->titleBracketed) {
1544   fprintf(out, "%sdefault%s%s\n", indent, separator,   fprintf(out, "%sdefault%s%s\n", indent, separator,
1545                          extractTitle(line));                          extractTitle(cfg, line));
1546              }              }
1547   }   }
1548      }      }
# Line 1955  struct singleEntry * findEntryByPath(str Line 1973  struct singleEntry * findEntryByPath(str
1973   }   }
1974    
1975   indexVars[i + 1] = -1;   indexVars[i + 1] = -1;
1976    
1977   i = 0;   i = 0;
1978   if (index) {   if (index) {
1979      while (i < *index) i++;      while (i < *index) {
1980      if (indexVars[i] == -1) return NULL;   i++;
1981     if (indexVars[i] == -1) return NULL;
1982        }
1983   }   }
1984    
1985   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
# Line 2113  struct singleEntry * findTemplate(struct Line 2133  struct singleEntry * findTemplate(struct
2133   } else {   } else {
2134      entry = findEntryByTitle(cfg, defTitle, &index);      entry = findEntryByTitle(cfg, defTitle, &index);
2135   }   }
2136   if (entry)   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2137      cfg->defaultImage = index;      cfg->defaultImage = index;
2138        if (indexPtr)
2139     *indexPtr = index;
2140        return entry;
2141     }
2142      }      }
2143   }   }
2144      } else if (cfg->defaultImage > -1) {      } else if (cfg->defaultImage > -1) {
# Line 2271  void displayEntry(struct singleEntry * e Line 2295  void displayEntry(struct singleEntry * e
2295      struct singleLine * line;      struct singleLine * line;
2296      char * root = NULL;      char * root = NULL;
2297      int i;      int i;
2298        int j;
2299    
2300      printf("index=%d\n", index);      printf("index=%d\n", index);
2301    
# Line 2358  void displayEntry(struct singleEntry * e Line 2383  void displayEntry(struct singleEntry * e
2383      } else {      } else {
2384   char * title;   char * title;
2385   line = getLineByType(LT_MENUENTRY, entry->lines);   line = getLineByType(LT_MENUENTRY, entry->lines);
2386   title = grub2ExtractTitle(line);   if (line) {
2387   if (title)      title = grub2ExtractTitle(line);
2388      printf("title=%s\n", title);      if (title)
2389     printf("title=%s\n", title);
2390     }
2391        }
2392    
2393        for (j = 0, line = entry->lines; line; line = line->next) {
2394     if ((line->type & LT_MBMODULE) && line->numElements >= 2) {
2395        if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2396     printf("mbmodule%d=", j);
2397        else
2398     printf("mbmodule%d=%s", j, prefix);
2399    
2400        for (i = 1; i < line->numElements; i++)
2401     printf("%s%s", line->elements[i].item, line->elements[i].indent);
2402        printf("\n");
2403        j++;
2404     }
2405      }      }
2406  }  }
2407    
# Line 3283  int updateImage(struct grubConfig * cfg, Line 3324  int updateImage(struct grubConfig * cfg,
3324  }  }
3325    
3326  int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,  int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
3327   const char * image, const char * prefix, const char * initrd) {   const char * image, const char * prefix, const char * initrd,
3328     const char * title) {
3329      struct singleEntry * entry;      struct singleEntry * entry;
3330      struct singleLine * line, * kernelLine, *endLine = NULL;      struct singleLine * line, * kernelLine, *endLine = NULL;
3331      int index = 0;      int index = 0;
3332    
3333      if (!image) return 0;      if (!image) return 0;
3334    
3335      for (; (entry = findEntryByPath(cfg, newMBKernel, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3336          kernelLine = getLineByType(LT_MBMODULE, entry->lines);          kernelLine = getLineByType(LT_MBMODULE, entry->lines);
3337          if (!kernelLine) continue;          if (!kernelLine) continue;
3338    
3339     /* if title is supplied, the entry's title must match it. */
3340     if (title) {
3341        char *linetitle;
3342    
3343        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3344        if (!line)
3345     continue;
3346    
3347        linetitle = extractTitle(cfg, line);
3348        if (!linetitle)
3349     continue;
3350        if (strcmp(title, linetitle)) {
3351     free(linetitle);
3352     continue;
3353        }
3354        free(linetitle);
3355     }
3356    
3357          if (prefix) {          if (prefix) {
3358              int prefixLen = strlen(prefix);              int prefixLen = strlen(prefix);
3359              if (!strncmp(initrd, prefix, prefixLen))              if (!strncmp(initrd, prefix, prefixLen))
# Line 3319  int addMBInitrd(struct grubConfig * cfg, Line 3379  int addMBInitrd(struct grubConfig * cfg,
3379  }  }
3380    
3381  int updateInitrd(struct grubConfig * cfg, const char * image,  int updateInitrd(struct grubConfig * cfg, const char * image,
3382                   const char * prefix, const char * initrd) {                   const char * prefix, const char * initrd, const char * title) {
3383      struct singleEntry * entry;      struct singleEntry * entry;
3384      struct singleLine * line, * kernelLine, *endLine = NULL;      struct singleLine * line, * kernelLine, *endLine = NULL;
3385      int index = 0;      int index = 0;
# Line 3330  int updateInitrd(struct grubConfig * cfg Line 3390  int updateInitrd(struct grubConfig * cfg
3390          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3391          if (!kernelLine) continue;          if (!kernelLine) continue;
3392    
3393     /* if title is supplied, the entry's title must match it. */
3394     if (title) {
3395        char *linetitle;
3396    
3397        line = getLineByType(LT_TITLE|LT_MENUENTRY, entry->lines);
3398        if (!line)
3399     continue;
3400    
3401        linetitle = extractTitle(cfg, line);
3402        if (!linetitle)
3403     continue;
3404        if (strcmp(title, linetitle)) {
3405     free(linetitle);
3406     continue;
3407        }
3408        free(linetitle);
3409     }
3410    
3411          line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);          line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
3412          if (line)          if (line)
3413              removeLine(entry, line);              removeLine(entry, line);
# Line 4033  int addNewKernel(struct grubConfig * con Line 4111  int addNewKernel(struct grubConfig * con
4111   }   }
4112      }      }
4113    
4114        struct singleLine *endLine = NULL;
4115        endLine = getLineByType(LT_ENTRY_END, new->lines);
4116        if (endLine) {
4117        removeLine(new, endLine);
4118        needs |= NEED_END;
4119        }
4120    
4121      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
4122       * weren't present in the template, or in the case of no template,       * weren't present in the template, or in the case of no template,
4123       * all the lines following the entryStart.       * all the lines following the entryStart.
# Line 4091  int addNewKernel(struct grubConfig * con Line 4176  int addNewKernel(struct grubConfig * con
4176   config->secondaryIndent, NULL);   config->secondaryIndent, NULL);
4177   needs &= ~NEED_END;   needs &= ~NEED_END;
4178      }      }
4179    
4180      if (needs) {      if (needs) {
4181   printf(_("grubby: needs=%d, aborting\n"), needs);   printf(_("grubby: needs=%d, aborting\n"), needs);
4182   abort();   abort();
# Line 4196  int main(int argc, const char ** argv) { Line 4282  int main(int argc, const char ** argv) {
4282      _("display the title of the default kernel") },      _("display the title of the default kernel") },
4283   { "devtree", 0, POPT_ARG_STRING, &newDevTreePath, 0,   { "devtree", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4284      _("device tree file for new stanza"), _("dtb-path") },      _("device tree file for new stanza"), _("dtb-path") },
4285     { "devtreedir", 0, POPT_ARG_STRING, &newDevTreePath, 0,
4286        _("device tree directory for new stanza"), _("dtb-path") },
4287   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
4288      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4289   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
# Line 4380  int main(int argc, const char ** argv) { Line 4468  int main(int argc, const char ** argv) {
4468      if (newKernelPath && !newKernelTitle) {      if (newKernelPath && !newKernelTitle) {
4469   fprintf(stderr, _("grubby: kernel title must be specified\n"));   fprintf(stderr, _("grubby: kernel title must be specified\n"));
4470   return 1;   return 1;
4471      } else if (!newKernelPath && (newKernelTitle  || copyDefault ||      } else if (!newKernelPath && (copyDefault ||
4472    (newKernelInitrd && !updateKernelPath)||    (newKernelInitrd && !updateKernelPath)||
4473    makeDefault || extraInitrdCount > 0)) {    makeDefault || extraInitrdCount > 0)) {
4474   fprintf(stderr, _("grubby: kernel path expected\n"));   fprintf(stderr, _("grubby: kernel path expected\n"));
# Line 4602  int main(int argc, const char ** argv) { Line 4690  int main(int argc, const char ** argv) {
4690      if (updateKernelPath && newKernelInitrd) {      if (updateKernelPath && newKernelInitrd) {
4691      if (newMBKernel) {      if (newMBKernel) {
4692      if (addMBInitrd(config, newMBKernel, updateKernelPath,      if (addMBInitrd(config, newMBKernel, updateKernelPath,
4693   bootPrefix, newKernelInitrd))   bootPrefix, newKernelInitrd,
4694     newKernelTitle))
4695      return 1;      return 1;
4696      } else {      } else {
4697      if (updateInitrd(config, updateKernelPath, bootPrefix,      if (updateInitrd(config, updateKernelPath, bootPrefix,
4698   newKernelInitrd))   newKernelInitrd, newKernelTitle))
4699   return 1;   return 1;
4700      }      }
4701      }      }

Legend:
Removed from v.2688  
changed lines
  Added in v.2983