Magellan Linux

Diff of /trunk/grubby/grubby.c

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

revision 3014 by niro, Tue Jun 27 14:34:11 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 4725  int addNewKernel(struct grubConfig *conf Line 4732  int addNewKernel(struct grubConfig *conf
4732   }   }
4733    
4734   if (updateImage(config, indexs, 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 5178  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 5205  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 5239  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    

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