Magellan Linux

Diff of /trunk/grubby/grubby.c

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

revision 3003 by niro, Tue Jun 27 14:14:00 2017 UTC revision 3019 by niro, Tue Jun 27 14:38:16 2017 UTC
# Line 132  struct singleEntry { Line 132  struct singleEntry {
132  #define NEED_DEVTREE (1 << 6)  #define NEED_DEVTREE (1 << 6)
133    
134  #define MAIN_DEFAULT    (1 << 0)  #define MAIN_DEFAULT    (1 << 0)
135    #define FIRST_ENTRY_INDEX    0 /* boot entry index value begin and increment
136       from this initial value */
137    #define NO_DEFAULT_ENTRY    -1 /* indicates that no specific default boot
138       entry was set or currently exists */
139  #define DEFAULT_SAVED       -2  #define DEFAULT_SAVED       -2
140  #define DEFAULT_SAVED_GRUB2 -3  #define DEFAULT_SAVED_GRUB2 -3
141    
# Line 677  struct grubConfig { Line 681  struct grubConfig {
681   int fallbackImage; /* just like defaultImage */   int fallbackImage; /* just like defaultImage */
682   int flags;   int flags;
683   struct configFileInfo *cfi;   struct configFileInfo *cfi;
684     int isModified; /* assumes only one entry added
685       per invocation of grubby */
686  };  };
687    
688  blkid_cache blkid;  blkid_cache blkid;
# Line 1296  static struct grubConfig *readConfig(con Line 1302  static struct grubConfig *readConfig(con
1302   cfg->theLines = NULL;   cfg->theLines = NULL;
1303   cfg->entries = NULL;   cfg->entries = NULL;
1304   cfg->fallbackImage = 0;   cfg->fallbackImage = 0;
1305     cfg->isModified = 0;
1306    
1307   /* copy everything we have */   /* copy everything we have */
1308   while (*head) {   while (*head) {
# Line 1615  static struct grubConfig *readConfig(con Line 1622  static struct grubConfig *readConfig(con
1622   *end == ' ' || *end == '\t'))   *end == ' ' || *end == '\t'))
1623   end++;   end++;
1624   if (*end)   if (*end)
1625   cfg->defaultImage = -1;   cfg->defaultImage = NO_DEFAULT_ENTRY;
1626   } else if (defaultLine->numElements == 3) {   } else if (defaultLine->numElements == 3) {
1627   char *value = defaultLine->elements[2].item;   char *value = defaultLine->elements[2].item;
1628   while (*value && (*value == '"' ||   while (*value && (*value == '"' ||
# Line 1628  static struct grubConfig *readConfig(con Line 1635  static struct grubConfig *readConfig(con
1635   *end == ' ' || *end == '\t'))   *end == ' ' || *end == '\t'))
1636   end++;   end++;
1637   if (*end)   if (*end)
1638   cfg->defaultImage = -1;   cfg->defaultImage = NO_DEFAULT_ENTRY;
1639   }   }
1640   } else if (cfi->defaultSupportSaved &&   } else if (cfi->defaultSupportSaved &&
1641     !strncmp(defaultLine->elements[1].item, "saved",     !strncmp(defaultLine->elements[1].item, "saved",
# Line 1638  static struct grubConfig *readConfig(con Line 1645  static struct grubConfig *readConfig(con
1645   cfg->defaultImage =   cfg->defaultImage =
1646      strtol(defaultLine->elements[1].item, &end, 10);      strtol(defaultLine->elements[1].item, &end, 10);
1647   if (*end)   if (*end)
1648   cfg->defaultImage = -1;   cfg->defaultImage = NO_DEFAULT_ENTRY;
1649   } else if (defaultLine->numElements >= 2) {   } else if (defaultLine->numElements >= 2) {
1650   int i = 0;   int i = 0;
1651   while ((entry = findEntryByIndex(cfg, i))) {   while ((entry = findEntryByIndex(cfg, i))) {
# Line 1666  static struct grubConfig *readConfig(con Line 1673  static struct grubConfig *readConfig(con
1673   if (entry) {   if (entry) {
1674   cfg->defaultImage = i;   cfg->defaultImage = i;
1675   } else {   } else {
1676   cfg->defaultImage = -1;   cfg->defaultImage = NO_DEFAULT_ENTRY;
1677   }   }
1678   }   }
1679   } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {   } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
# Line 1683  static struct grubConfig *readConfig(con Line 1690  static struct grubConfig *readConfig(con
1690   cfg->defaultImage = index;   cfg->defaultImage = index;
1691   }   }
1692   } else {   } else {
1693   cfg->defaultImage = 0;   cfg->defaultImage = FIRST_ENTRY_INDEX;
1694   }   }
1695    
1696   return cfg;   return cfg;
# Line 1703  static void writeDefault(FILE * out, cha Line 1710  static void writeDefault(FILE * out, cha
1710   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1711   else if (cfg->cfi->defaultIsSaved) {   else if (cfg->cfi->defaultIsSaved) {
1712   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1713   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {   if (cfg->defaultImage >= FIRST_ENTRY_INDEX && cfg->cfi->setEnv) {
1714   char *title;   char *title;
1715   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
1716   line = getLineByType(LT_MENUENTRY, entry->lines);   line = getLineByType(LT_MENUENTRY, entry->lines);
# Line 1716  static void writeDefault(FILE * out, cha Line 1723  static void writeDefault(FILE * out, cha
1723   "saved_entry", title);   "saved_entry", title);
1724   }   }
1725   }   }
1726   } else if (cfg->defaultImage > -1) {   } else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) {
1727   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1728   if (cfg->cfi->defaultIsVariable) {   if (cfg->cfi->defaultIsVariable) {
1729   fprintf(out, "%sset default=\"%d\"\n", indent,   fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 2420  struct singleEntry *findTemplate(struct Line 2427  struct singleEntry *findTemplate(struct
2427   }   }
2428   }   }
2429   }   }
2430   } else if (cfg->defaultImage > -1) {   } else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) {
2431   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2432   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2433   if (indexPtr)   if (indexPtr)
# Line 2494  void markRemovedImage(struct grubConfig Line 2501  void markRemovedImage(struct grubConfig
2501   entry->skip = 1;   entry->skip = 1;
2502  }  }
2503    
2504  void setDefaultImage(struct grubConfig *config, int hasNew,  void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,
2505       const char *defaultKernelPath, int newIsDefault,       const char *defaultKernelPath, int newBootEntryIsDefault,
2506       const char *prefix, int flags, int index)       const char *prefix, int flags, int newDefaultBootEntryIndex)
2507  {  {
2508   struct singleEntry *entry, *entry2, *newDefault;   struct singleEntry *entry, *entry2, *newDefault;
2509   int i, j;   int i, j;
2510    
2511   if (newIsDefault) {   if (newBootEntryIsDefault) {
2512   config->defaultImage = 0;   config->defaultImage = FIRST_ENTRY_INDEX;
2513   return;   return;
2514   } else if ((index >= 0) && config->cfi->defaultIsIndex) {   } else if ((newDefaultBootEntryIndex >= 0) && config->cfi->defaultIsIndex) {
2515   if (findEntryByIndex(config, index))   if (findEntryByIndex(config, newDefaultBootEntryIndex))
2516   config->defaultImage = index;   config->defaultImage = newDefaultBootEntryIndex;
2517   else   else
2518   config->defaultImage = -1;   config->defaultImage = NO_DEFAULT_ENTRY;
2519   return;   return;
2520   } else if (defaultKernelPath) {   } else if (defaultKernelPath) {
2521   i = 0;   i = 0;
2522   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
2523   config->defaultImage = i;   config->defaultImage = i;
2524   } else {   } else {
2525   config->defaultImage = -1;   config->defaultImage = NO_DEFAULT_ENTRY;
2526   return;   return;
2527   }   }
2528   }   }
# Line 2527  void setDefaultImage(struct grubConfig * Line 2534  void setDefaultImage(struct grubConfig *
2534   /* default is set to saved, we don't want to change it */   /* default is set to saved, we don't want to change it */
2535   return;   return;
2536    
2537   if (config->defaultImage > -1)   if (config->defaultImage >= FIRST_ENTRY_INDEX)
2538   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
2539   else   else
2540   entry = NULL;   entry = NULL;
2541    
2542   if (entry && !entry->skip) {   if (entry && !entry->skip) {
2543   /* we can preserve the default */   /* we can preserve the default */
2544   if (hasNew)   if (isUserSpecifiedKernelPath)
2545   config->defaultImage++;   config->defaultImage++;
2546    
2547   /* count the number of entries erased before this one */   /* count the number of entries erased before this one */
# Line 2543  void setDefaultImage(struct grubConfig * Line 2550  void setDefaultImage(struct grubConfig *
2550   if (entry2->skip)   if (entry2->skip)
2551   config->defaultImage--;   config->defaultImage--;
2552   }   }
2553   } else if (hasNew) {   } else if (isUserSpecifiedKernelPath) {
2554   config->defaultImage = 0;   config->defaultImage = FIRST_ENTRY_INDEX;
2555   } else {   } else {
2556   /* Either we just erased the default (or the default line was   /* Either we just erased the default (or the default line was
2557   * bad to begin with) and didn't put a new one in. We'll use   * bad to begin with) and didn't put a new one in. We'll use
# Line 2553  void setDefaultImage(struct grubConfig * Line 2560  void setDefaultImage(struct grubConfig *
2560      findTemplate(config, prefix, &config->defaultImage, 1,      findTemplate(config, prefix, &config->defaultImage, 1,
2561   flags);   flags);
2562   if (!newDefault)   if (!newDefault)
2563   config->defaultImage = -1;   config->defaultImage = NO_DEFAULT_ENTRY;
2564   }   }
2565  }  }
2566    
# Line 4210  int addNewKernel(struct grubConfig *conf Line 4217  int addNewKernel(struct grubConfig *conf
4217   const char *newKernelArgs, const char *newKernelInitrd,   const char *newKernelArgs, const char *newKernelInitrd,
4218   const char **extraInitrds, int extraInitrdCount,   const char **extraInitrds, int extraInitrdCount,
4219   const char *newMBKernel, const char *newMBKernelArgs,   const char *newMBKernel, const char *newMBKernelArgs,
4220   const char *newDevTreePath)   const char *newDevTreePath, int newIndex)
4221  {  {
4222   struct singleEntry *new;   struct singleEntry *new, *entry, *prev = NULL;
4223   struct singleLine *newLine = NULL, *tmplLine = NULL, *masterLine = NULL;   struct singleLine *newLine = NULL, *tmplLine = NULL, *masterLine = NULL;
4224   int needs;   int needs;
4225     char *indexs;
4226   char *chptr;   char *chptr;
4227     int rc;
4228    
4229   if (!newKernelPath)   if (!newKernelPath)
4230   return 0;   return 0;
4231    
4232     rc = asprintf(&indexs, "%d", newIndex);
4233     if (rc < 0)
4234     return 1;
4235    
4236   /* if the newKernelTitle is too long silently munge it into something   /* if the newKernelTitle is too long silently munge it into something
4237   * we can live with. truncating is first check, then we'll just mess with   * we can live with. truncating is first check, then we'll just mess with
4238   * it until it looks better */   * it until it looks better */
# Line 4242  int addNewKernel(struct grubConfig *conf Line 4255  int addNewKernel(struct grubConfig *conf
4255   new = malloc(sizeof(*new));   new = malloc(sizeof(*new));
4256   new->skip = 0;   new->skip = 0;
4257   new->multiboot = 0;   new->multiboot = 0;
  new->next = config->entries;  
4258   new->lines = NULL;   new->lines = NULL;
4259   config->entries = new;   entry = config->entries;
4260     for (unsigned int i = 0; i < newIndex; i++) {
4261     if (!entry)
4262     break;
4263     prev = entry;
4264     entry = entry->next;
4265     }
4266     new->next = entry;
4267    
4268     if (prev)
4269     prev->next = new;
4270     else
4271     config->entries = new;
4272    
4273   /* copy/update from the template */   /* copy/update from the template */
4274   needs = NEED_KERNEL | NEED_TITLE;   needs = NEED_KERNEL | NEED_TITLE;
# Line 4707  int addNewKernel(struct grubConfig *conf Line 4731  int addNewKernel(struct grubConfig *conf
4731   abort();   abort();
4732   }   }
4733    
4734   if (updateImage(config, "0", prefix, newKernelArgs, NULL,   if (updateImage(config, indexs, prefix, newKernelArgs, NULL,
4735   newMBKernelArgs, NULL))   newMBKernelArgs, NULL)) {
4736     config->isModified = 1;
4737   return 1;   return 1;
4738     }
4739    
4740   return 0;   return 0;
4741  }  }
# Line 4737  int main(int argc, const char **argv) Line 4763  int main(int argc, const char **argv)
4763   char *newDevTreePath = NULL;   char *newDevTreePath = NULL;
4764   char *newMBKernel = NULL;   char *newMBKernel = NULL;
4765   char *newMBKernelArgs = NULL;   char *newMBKernelArgs = NULL;
4766     int newIndex = 0;
4767   char *removeMBKernelArgs = NULL;   char *removeMBKernelArgs = NULL;
4768   char *removeMBKernel = NULL;   char *removeMBKernel = NULL;
4769   char *bootPrefix = NULL;   char *bootPrefix = NULL;
# Line 4771  int main(int argc, const char **argv) Line 4798  int main(int argc, const char **argv)
4798   NULL},   NULL},
4799   {"boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   {"boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4800   _   _
4801   ("filestystem which contains /boot directory (for testing only)"),   ("filesystem which contains /boot directory (for testing only)"),
4802   _("bootfs")},   _("bootfs")},
4803  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4804   {"bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   {"bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
# Line 4843  int main(int argc, const char **argv) Line 4870  int main(int argc, const char **argv)
4870   {"set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,   {"set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4871   _("make the given entry index the default entry"),   _("make the given entry index the default entry"),
4872   _("entry-index")},   _("entry-index")},
4873     {"set-index", 0, POPT_ARG_INT, &newIndex, 0,
4874     _("use the given index when creating a new entry"),
4875     _("entry-index")},
4876   {"silo", 0, POPT_ARG_NONE, &configureSilo, 0,   {"silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4877   _("configure silo bootloader")},   _("configure silo bootloader")},
4878   {"title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   {"title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 5157  int main(int argc, const char **argv) Line 5187  int main(int argc, const char **argv)
5187   struct singleEntry *entry;   struct singleEntry *entry;
5188   char *rootspec;   char *rootspec;
5189    
5190   if (config->defaultImage == -1)   if (config->defaultImage == NO_DEFAULT_ENTRY)
5191   return 0;   return 0;
5192   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
5193      cfi->defaultIsSaved)      cfi->defaultIsSaved)
5194   config->defaultImage = 0;   config->defaultImage = FIRST_ENTRY_INDEX;
5195   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
5196   if (!entry)   if (!entry)
5197   return 0;   return 0;
# Line 5184  int main(int argc, const char **argv) Line 5214  int main(int argc, const char **argv)
5214   struct singleLine *line;   struct singleLine *line;
5215   struct singleEntry *entry;   struct singleEntry *entry;
5216    
5217   if (config->defaultImage == -1)   if (config->defaultImage == NO_DEFAULT_ENTRY)
5218   return 0;   return 0;
5219   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
5220      cfi->defaultIsSaved)      cfi->defaultIsSaved)
5221   config->defaultImage = 0;   config->defaultImage = FIRST_ENTRY_INDEX;
5222   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
5223   if (!entry)   if (!entry)
5224   return 0;   return 0;
# Line 5218  int main(int argc, const char **argv) Line 5248  int main(int argc, const char **argv)
5248   return 0;   return 0;
5249    
5250   } else if (displayDefaultIndex) {   } else if (displayDefaultIndex) {
5251   if (config->defaultImage == -1)   if (config->defaultImage == NO_DEFAULT_ENTRY)
5252   return 0;   return 0;
5253   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
5254      cfi->defaultIsSaved)      cfi->defaultIsSaved)
5255   config->defaultImage = 0;   config->defaultImage = FIRST_ENTRY_INDEX;
5256   printf("%i\n", config->defaultImage);   printf("%i\n", config->defaultImage);
5257   return 0;   return 0;
5258    
# Line 5258  int main(int argc, const char **argv) Line 5288  int main(int argc, const char **argv)
5288   if (addNewKernel(config, template, bootPrefix, newKernelPath,   if (addNewKernel(config, template, bootPrefix, newKernelPath,
5289   newKernelTitle, newKernelArgs, newKernelInitrd,   newKernelTitle, newKernelArgs, newKernelInitrd,
5290   (const char **)extraInitrds, extraInitrdCount,   (const char **)extraInitrds, extraInitrdCount,
5291   newMBKernel, newMBKernelArgs, newDevTreePath))   newMBKernel, newMBKernelArgs, newDevTreePath,
5292     newIndex))
5293   return 1;   return 1;
5294    
5295   if (numEntries(config) == 0) {   if (numEntries(config) == 0) {

Legend:
Removed from v.3003  
changed lines
  Added in v.3019