Magellan Linux

Diff of /trunk/grubby/grubby.c

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

revision 3010 by niro, Tue Jun 27 14:30:29 2017 UTC revision 3024 by niro, Tue Jun 27 14:41:50 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 2432  struct singleEntry *findTemplate(struct Line 2439  struct singleEntry *findTemplate(struct
2439   index = 0;   index = 0;
2440   while ((entry = findEntryByIndex(cfg, index))) {   while ((entry = findEntryByIndex(cfg, index))) {
2441   if (suitableImage(entry, prefix, skipRemoved, flags)) {   if (suitableImage(entry, prefix, skipRemoved, flags)) {
2442   int j;   int j, unmodifiedIndex;
2443   for (j = 0; j < index; j++) {  
2444     unmodifiedIndex = index;
2445    
2446     for (j = 0; j < unmodifiedIndex; j++) {
2447   entry2 = findEntryByIndex(cfg, j);   entry2 = findEntryByIndex(cfg, j);
2448   if (entry2->skip)   if (entry2->skip)
2449   index--;   index--;
# Line 2494  void markRemovedImage(struct grubConfig Line 2504  void markRemovedImage(struct grubConfig
2504   entry->skip = 1;   entry->skip = 1;
2505  }  }
2506    
2507  void setDefaultImage(struct grubConfig *config, int hasNew,  void setDefaultImage(struct grubConfig *config, int isAddingBootEntry,
2508       const char *defaultKernelPath, int newIsDefault,       const char *defaultKernelPath, int newBootEntryIsDefault,
2509       const char *prefix, int flags, int index)       const char *prefix, int flags,
2510  {       int newDefaultBootEntryIndex, int newBootEntryIndex)
2511   struct singleEntry *entry, *entry2, *newDefault;  {
2512   int i, j;   struct singleEntry *bootEntry, *newDefault;
2513     int indexToVerify, firstKernelEntryIndex, currentLookupIndex;
2514   if (newIsDefault) {  
2515   config->defaultImage = 0;   /* handle the two cases where the user explictly picks the default
2516   return;   * boot entry index as it would exist post-modification */
2517   } else if ((index >= 0) && config->cfi->defaultIsIndex) {  
2518   if (findEntryByIndex(config, index))   /* Case 1: user chose to make the latest boot entry the default */
2519   config->defaultImage = index;   if (newBootEntryIsDefault) {
2520   else   config->defaultImage = newBootEntryIndex;
  config->defaultImage = -1;  
2521   return;   return;
2522   } else if (defaultKernelPath) {   }
2523   i = 0;  
2524   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   /* Case 2: user picked an arbitrary index as the default boot entry */
2525   config->defaultImage = i;   if (newDefaultBootEntryIndex >= FIRST_ENTRY_INDEX
2526   } else {      && config->cfi->defaultIsIndex) {
2527   config->defaultImage = -1;   indexToVerify = newDefaultBootEntryIndex;
2528    
2529     /* user chose to make latest boot entry the default */
2530     if (newDefaultBootEntryIndex == newBootEntryIndex) {
2531     config->defaultImage = newBootEntryIndex;
2532   return;   return;
2533   }   }
  }  
2534    
2535   /* defaultImage now points to what we'd like to use, but before any   /* the user picks the default index based on the
2536   * order changes */   * order of the bootloader configuration after
2537   if ((config->defaultImage == DEFAULT_SAVED) ||   * modification; ensure we are checking for the
2538      (config->defaultImage == DEFAULT_SAVED_GRUB2))   * existence of the correct entry */
2539   /* default is set to saved, we don't want to change it */   if (newBootEntryIndex < newDefaultBootEntryIndex) {
2540     if (!config->isModified)
2541     indexToVerify--;
2542     }
2543    
2544     /* verify the user selected index will exist */
2545     if (findEntryByIndex(config, indexToVerify)) {
2546     config->defaultImage = newDefaultBootEntryIndex;
2547     } else {
2548     config->defaultImage = NO_DEFAULT_ENTRY;
2549     }
2550    
2551   return;   return;
2552     }
2553    
2554   if (config->defaultImage > -1)   /* handle cases where the index value may shift */
  entry = findEntryByIndex(config, config->defaultImage);  
  else  
  entry = NULL;  
2555    
2556   if (entry && !entry->skip) {   /* check validity of existing default or first-entry-found
2557   /* we can preserve the default */     selection */
2558   if (hasNew)   if (defaultKernelPath) {
2559   config->defaultImage++;   /* user requested first-entry-found */
2560     if (!findEntryByPath(config, defaultKernelPath,
2561         prefix, &firstKernelEntryIndex)) {
2562     /* don't change default if can't find match */
2563     config->defaultImage = NO_DEFAULT_ENTRY;
2564     return;
2565     }
2566    
2567     config->defaultImage = firstKernelEntryIndex;
2568    
2569   /* count the number of entries erased before this one */   /* this is where we start looking for decrement later */
2570   for (j = 0; j < config->defaultImage; j++) {   currentLookupIndex = config->defaultImage;
2571   entry2 = findEntryByIndex(config, j);  
2572   if (entry2->skip)   if (isAddingBootEntry && !config->isModified &&
2573   config->defaultImage--;      (newBootEntryIndex < config->defaultImage)) {
2574     /* increment because new entry added before default */
2575     config->defaultImage++;
2576   }   }
  } else if (hasNew) {  
  config->defaultImage = 0;  
2577   } else {   } else {
2578   /* Either we just erased the default (or the default line was   /* use pre-existing default entry */
2579   * bad to begin with) and didn't put a new one in. We'll use   currentLookupIndex = config->defaultImage;
2580   * the first valid image. */  
2581     if (isAddingBootEntry
2582        && (newBootEntryIndex <= config->defaultImage)) {
2583     config->defaultImage++;
2584    
2585     if (config->isModified) {
2586     currentLookupIndex++;
2587     }
2588     }
2589     }
2590    
2591     /* sanity check - is this entry index valid? */
2592     bootEntry = findEntryByIndex(config, currentLookupIndex);
2593    
2594     if ((bootEntry && bootEntry->skip) || !bootEntry) {
2595     /* entry is to be skipped or is invalid */
2596     if (isAddingBootEntry) {
2597     config->defaultImage = newBootEntryIndex;
2598     return;
2599     }
2600   newDefault =   newDefault =
2601      findTemplate(config, prefix, &config->defaultImage, 1,      findTemplate(config, prefix, &config->defaultImage, 1,
2602   flags);   flags);
2603   if (!newDefault)   if (!newDefault) {
2604   config->defaultImage = -1;   config->defaultImage = NO_DEFAULT_ENTRY;
2605     }
2606    
2607     return;
2608     }
2609    
2610     currentLookupIndex--;
2611    
2612     /* decrement index by the total number of entries deleted */
2613    
2614     for (indexToVerify = currentLookupIndex;
2615         indexToVerify >= FIRST_ENTRY_INDEX; indexToVerify--) {
2616    
2617     bootEntry = findEntryByIndex(config, indexToVerify);
2618    
2619     if (bootEntry && bootEntry->skip) {
2620     config->defaultImage--;
2621     }
2622   }   }
2623  }  }
2624    
# Line 4725  int addNewKernel(struct grubConfig *conf Line 4790  int addNewKernel(struct grubConfig *conf
4790   }   }
4791    
4792   if (updateImage(config, indexs, prefix, newKernelArgs, NULL,   if (updateImage(config, indexs, prefix, newKernelArgs, NULL,
4793   newMBKernelArgs, NULL))   newMBKernelArgs, NULL)) {
4794     config->isModified = 1;
4795   return 1;   return 1;
4796     }
4797    
4798   return 0;   return 0;
4799  }  }
# Line 4789  int main(int argc, const char **argv) Line 4856  int main(int argc, const char **argv)
4856   NULL},   NULL},
4857   {"boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   {"boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4858   _   _
4859   ("filestystem which contains /boot directory (for testing only)"),   ("filesystem which contains /boot directory (for testing only)"),
4860   _("bootfs")},   _("bootfs")},
4861  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4862   {"bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   {"bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
# Line 5178  int main(int argc, const char **argv) Line 5245  int main(int argc, const char **argv)
5245   struct singleEntry *entry;   struct singleEntry *entry;
5246   char *rootspec;   char *rootspec;
5247    
5248   if (config->defaultImage == -1)   if (config->defaultImage == NO_DEFAULT_ENTRY)
5249   return 0;   return 0;
5250   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
5251      cfi->defaultIsSaved)      cfi->defaultIsSaved)
5252   config->defaultImage = 0;   config->defaultImage = FIRST_ENTRY_INDEX;
5253   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
5254   if (!entry)   if (!entry)
5255   return 0;   return 0;
# Line 5205  int main(int argc, const char **argv) Line 5272  int main(int argc, const char **argv)
5272   struct singleLine *line;   struct singleLine *line;
5273   struct singleEntry *entry;   struct singleEntry *entry;
5274    
5275   if (config->defaultImage == -1)   if (config->defaultImage == NO_DEFAULT_ENTRY)
5276   return 0;   return 0;
5277   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
5278      cfi->defaultIsSaved)      cfi->defaultIsSaved)
5279   config->defaultImage = 0;   config->defaultImage = FIRST_ENTRY_INDEX;
5280   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
5281   if (!entry)   if (!entry)
5282   return 0;   return 0;
# Line 5239  int main(int argc, const char **argv) Line 5306  int main(int argc, const char **argv)
5306   return 0;   return 0;
5307    
5308   } else if (displayDefaultIndex) {   } else if (displayDefaultIndex) {
5309   if (config->defaultImage == -1)   if (config->defaultImage == NO_DEFAULT_ENTRY)
5310   return 0;   return 0;
5311   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
5312      cfi->defaultIsSaved)      cfi->defaultIsSaved)
5313   config->defaultImage = 0;   config->defaultImage = FIRST_ENTRY_INDEX;
5314   printf("%i\n", config->defaultImage);   printf("%i\n", config->defaultImage);
5315   return 0;   return 0;
5316    
# Line 5259  int main(int argc, const char **argv) Line 5326  int main(int argc, const char **argv)
5326   markRemovedImage(config, removeKernelPath, bootPrefix);   markRemovedImage(config, removeKernelPath, bootPrefix);
5327   markRemovedImage(config, removeMBKernel, bootPrefix);   markRemovedImage(config, removeMBKernel, bootPrefix);
5328   setDefaultImage(config, newKernelPath != NULL, defaultKernel,   setDefaultImage(config, newKernelPath != NULL, defaultKernel,
5329   makeDefault, bootPrefix, flags, defaultIndex);   makeDefault, bootPrefix, flags, defaultIndex,
5330     newIndex);
5331   setFallbackImage(config, newKernelPath != NULL);   setFallbackImage(config, newKernelPath != NULL);
5332   if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,   if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
5333   removeArgs, newMBKernelArgs, removeMBKernelArgs))   removeArgs, newMBKernelArgs, removeMBKernelArgs))

Legend:
Removed from v.3010  
changed lines
  Added in v.3024