Magellan Linux

Diff of /trunk/grubby/grubby.c

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

revision 3018 by niro, Tue Jun 27 14:37:30 2017 UTC revision 3031 by niro, Tue Jun 27 14:48:09 2017 UTC
# Line 681  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 1300  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 1709  static void writeDefault(FILE * out, cha Line 1712  static void writeDefault(FILE * out, cha
1712   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1713   if (cfg->defaultImage >= FIRST_ENTRY_INDEX && cfg->cfi->setEnv) {   if (cfg->defaultImage >= FIRST_ENTRY_INDEX && cfg->cfi->setEnv) {
1714   char *title;   char *title;
1715   entry = findEntryByIndex(cfg, cfg->defaultImage);   int trueIndex, currentIndex;
1716    
1717     trueIndex = 0;
1718     currentIndex = 0;
1719    
1720     while ((entry = findEntryByIndex(cfg, currentIndex))) {
1721     if (!entry->skip) {
1722     if (trueIndex == cfg->defaultImage) {
1723     break;
1724     }
1725     trueIndex++;
1726     }
1727     currentIndex++;
1728     }
1729   line = getLineByType(LT_MENUENTRY, entry->lines);   line = getLineByType(LT_MENUENTRY, entry->lines);
1730   if (!line)   if (!line)
1731   line = getLineByType(LT_TITLE, entry->lines);   line = getLineByType(LT_TITLE, entry->lines);
# Line 2436  struct singleEntry *findTemplate(struct Line 2452  struct singleEntry *findTemplate(struct
2452   index = 0;   index = 0;
2453   while ((entry = findEntryByIndex(cfg, index))) {   while ((entry = findEntryByIndex(cfg, index))) {
2454   if (suitableImage(entry, prefix, skipRemoved, flags)) {   if (suitableImage(entry, prefix, skipRemoved, flags)) {
2455   int j;   int j, unmodifiedIndex;
2456   for (j = 0; j < index; j++) {  
2457     unmodifiedIndex = index;
2458    
2459     for (j = 0; j < unmodifiedIndex; j++) {
2460   entry2 = findEntryByIndex(cfg, j);   entry2 = findEntryByIndex(cfg, j);
2461   if (entry2->skip)   if (entry2->skip)
2462   index--;   index--;
# Line 2498  void markRemovedImage(struct grubConfig Line 2517  void markRemovedImage(struct grubConfig
2517   entry->skip = 1;   entry->skip = 1;
2518  }  }
2519    
2520  void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath,  void setDefaultImage(struct grubConfig *config, int isAddingBootEntry,
2521       const char *defaultKernelPath, int newBootEntryIsDefault,       const char *defaultKernelPath, int newBootEntryIsDefault,
2522       const char *prefix, int flags, int newDefaultBootEntryIndex)       const char *prefix, int flags,
2523         int newDefaultBootEntryIndex, int newBootEntryIndex)
2524  {  {
2525   struct singleEntry *entry, *entry2, *newDefault;   struct singleEntry *bootEntry, *newDefault;
2526   int i, j;   int indexToVerify, firstKernelEntryIndex, currentLookupIndex;
2527    
2528            /* initialize */
2529            currentLookupIndex = FIRST_ENTRY_INDEX;
2530    
2531     /* handle the two cases where the user explictly picks the default
2532     * boot entry index as it would exist post-modification */
2533    
2534     /* Case 1: user chose to make the latest boot entry the default */
2535   if (newBootEntryIsDefault) {   if (newBootEntryIsDefault) {
2536   config->defaultImage = FIRST_ENTRY_INDEX;   config->defaultImage = newBootEntryIndex;
2537   return;   return;
2538   } else if ((newDefaultBootEntryIndex >= 0) && config->cfi->defaultIsIndex) {   }
2539   if (findEntryByIndex(config, newDefaultBootEntryIndex))  
2540     /* Case 2: user picked an arbitrary index as the default boot entry */
2541     if (newDefaultBootEntryIndex >= FIRST_ENTRY_INDEX) {
2542     indexToVerify = newDefaultBootEntryIndex;
2543    
2544     /* user chose to make latest boot entry the default */
2545     if (newDefaultBootEntryIndex == newBootEntryIndex) {
2546     config->defaultImage = newBootEntryIndex;
2547     return;
2548     }
2549    
2550     /* the user picks the default index based on the
2551     * order of the bootloader configuration after
2552     * modification; ensure we are checking for the
2553     * existence of the correct entry */
2554     if (newBootEntryIndex < newDefaultBootEntryIndex) {
2555     if (!config->isModified)
2556     indexToVerify--;
2557     }
2558    
2559     /* verify the user selected index will exist */
2560     if (findEntryByIndex(config, indexToVerify)) {
2561   config->defaultImage = newDefaultBootEntryIndex;   config->defaultImage = newDefaultBootEntryIndex;
2562   else   } else {
2563   config->defaultImage = NO_DEFAULT_ENTRY;   config->defaultImage = NO_DEFAULT_ENTRY;
2564     }
2565    
2566   return;   return;
2567   } else if (defaultKernelPath) {   }
2568   i = 0;  
2569   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   /* handle cases where the index value may shift */
2570   config->defaultImage = i;  
2571   } else {   /* check validity of existing default or first-entry-found
2572       selection */
2573     if (defaultKernelPath) {
2574                    /* we must initialize this */
2575                    firstKernelEntryIndex = 0;
2576     /* user requested first-entry-found */
2577     if (!findEntryByPath(config, defaultKernelPath,
2578         prefix, &firstKernelEntryIndex)) {
2579     /* don't change default if can't find match */
2580   config->defaultImage = NO_DEFAULT_ENTRY;   config->defaultImage = NO_DEFAULT_ENTRY;
2581   return;   return;
2582   }   }
  }  
2583    
2584   /* defaultImage now points to what we'd like to use, but before any   config->defaultImage = firstKernelEntryIndex;
  * order changes */  
  if ((config->defaultImage == DEFAULT_SAVED) ||  
     (config->defaultImage == DEFAULT_SAVED_GRUB2))  
  /* default is set to saved, we don't want to change it */  
  return;  
2585    
2586   if (config->defaultImage >= FIRST_ENTRY_INDEX)   /* this is where we start looking for decrement later */
2587   entry = findEntryByIndex(config, config->defaultImage);   currentLookupIndex = config->defaultImage;
  else  
  entry = NULL;  
2588    
2589   if (entry && !entry->skip) {   if (isAddingBootEntry && !config->isModified &&
2590   /* we can preserve the default */      (newBootEntryIndex < config->defaultImage)) {
2591   if (isUserSpecifiedKernelPath)   /* increment because new entry added before default */
2592   config->defaultImage++;   config->defaultImage++;
   
  /* count the number of entries erased before this one */  
  for (j = 0; j < config->defaultImage; j++) {  
  entry2 = findEntryByIndex(config, j);  
  if (entry2->skip)  
  config->defaultImage--;  
2593   }   }
  } else if (isUserSpecifiedKernelPath) {  
  config->defaultImage = FIRST_ENTRY_INDEX;  
2594   } else {   } else {
2595   /* Either we just erased the default (or the default line was                  /* check to see if the default is stored in the environment */
2596   * bad to begin with) and didn't put a new one in. We'll use                  if (config->defaultImage < FIRST_ENTRY_INDEX) {
2597   * the first valid image. */                      if (config->defaultImage == DEFAULT_SAVED || config->defaultImage == DEFAULT_SAVED_GRUB2)
2598                        {
2599                            if (config->cfi->defaultIsSaved) {
2600                                if (config->cfi->getEnv) {
2601                                    char *defaultTitle = config->cfi->getEnv(config->cfi, "saved_entry");
2602    
2603                                    if (defaultTitle) {
2604                                        if (isnumber(defaultTitle)) {
2605                                            currentLookupIndex = atoi(defaultTitle);
2606                                        } else {
2607                                            findEntryByTitle(config, defaultTitle, &currentLookupIndex);
2608                                        }
2609                                        /* set the default Image to an actual index */
2610                                        config->defaultImage = currentLookupIndex;
2611                                    }
2612                                }
2613                             }
2614                        }
2615                    } else {
2616                            /* use pre-existing default entry from the file*/
2617                            currentLookupIndex = config->defaultImage;
2618                    }
2619    
2620     if (isAddingBootEntry
2621        && (newBootEntryIndex <= config->defaultImage)) {
2622     config->defaultImage++;
2623    
2624     if (config->isModified) {
2625     currentLookupIndex++;
2626     }
2627     }
2628     }
2629    
2630     /* sanity check - is this entry index valid? */
2631     bootEntry = findEntryByIndex(config, currentLookupIndex);
2632    
2633     if ((bootEntry && bootEntry->skip) || !bootEntry) {
2634     /* entry is to be skipped or is invalid */
2635     if (isAddingBootEntry) {
2636     config->defaultImage = newBootEntryIndex;
2637     return;
2638     }
2639   newDefault =   newDefault =
2640      findTemplate(config, prefix, &config->defaultImage, 1,      findTemplate(config, prefix, &config->defaultImage, 1,
2641   flags);   flags);
2642   if (!newDefault)   if (!newDefault) {
2643   config->defaultImage = NO_DEFAULT_ENTRY;   config->defaultImage = NO_DEFAULT_ENTRY;
2644     }
2645    
2646     return;
2647     }
2648    
2649     currentLookupIndex--;
2650    
2651     /* decrement index by the total number of entries deleted */
2652    
2653     for (indexToVerify = currentLookupIndex;
2654         indexToVerify >= FIRST_ENTRY_INDEX; indexToVerify--) {
2655    
2656     bootEntry = findEntryByIndex(config, indexToVerify);
2657    
2658     if (bootEntry && bootEntry->skip) {
2659     config->defaultImage--;
2660     }
2661   }   }
2662  }  }
2663    
# Line 2586  void setFallbackImage(struct grubConfig Line 2686  void setFallbackImage(struct grubConfig
2686   }   }
2687  }  }
2688    
2689  void displayEntry(struct singleEntry *entry, const char *prefix, int index)  void displayEntry(struct grubConfig *config, struct singleEntry *entry, const char *prefix, int index)
2690  {  {
2691   struct singleLine *line;   struct singleLine *line;
2692   char *root = NULL;   char *root = NULL;
# Line 2682  void displayEntry(struct singleEntry *en Line 2782  void displayEntry(struct singleEntry *en
2782    
2783   line = getLineByType(LT_TITLE, entry->lines);   line = getLineByType(LT_TITLE, entry->lines);
2784   if (line) {   if (line) {
2785   printf("title=%s\n", line->elements[1].item);                  char *entryTitle;
2786                    /* if we can extractTitle, then it's a zipl config and
2787                     * if not then we go ahead with what's existed prior */
2788                    entryTitle = extractTitle(config, line);
2789                    if (!entryTitle) {
2790                        entryTitle=line->elements[1].item;
2791                    }
2792     printf("title=%s\n", entryTitle);
2793   } else {   } else {
2794   char *title;   char *title;
2795   line = getLineByType(LT_MENUENTRY, entry->lines);   line = getLineByType(LT_MENUENTRY, entry->lines);
# Line 3098  int displayInfo(struct grubConfig *confi Line 3205  int displayInfo(struct grubConfig *confi
3205   printf("lba\n");   printf("lba\n");
3206   }   }
3207    
3208   displayEntry(entry, prefix, i);   displayEntry(config, entry, prefix, i);
3209    
3210   i++;   i++;
3211   while ((entry = findEntryByPath(config, kernel, prefix, &i))) {   while ((entry = findEntryByPath(config, kernel, prefix, &i))) {
3212   displayEntry(entry, prefix, i);   displayEntry(config, entry, prefix, i);
3213   i++;   i++;
3214   }   }
3215    
# Line 4729  int addNewKernel(struct grubConfig *conf Line 4836  int addNewKernel(struct grubConfig *conf
4836   }   }
4837    
4838   if (updateImage(config, indexs, prefix, newKernelArgs, NULL,   if (updateImage(config, indexs, prefix, newKernelArgs, NULL,
4839   newMBKernelArgs, NULL))   newMBKernelArgs, NULL)) {
4840     config->isModified = 1;
4841   return 1;   return 1;
4842     }
4843    
4844   return 0;   return 0;
4845  }  }
# Line 5263  int main(int argc, const char **argv) Line 5372  int main(int argc, const char **argv)
5372   markRemovedImage(config, removeKernelPath, bootPrefix);   markRemovedImage(config, removeKernelPath, bootPrefix);
5373   markRemovedImage(config, removeMBKernel, bootPrefix);   markRemovedImage(config, removeMBKernel, bootPrefix);
5374   setDefaultImage(config, newKernelPath != NULL, defaultKernel,   setDefaultImage(config, newKernelPath != NULL, defaultKernel,
5375   makeDefault, bootPrefix, flags, defaultIndex);   makeDefault, bootPrefix, flags, defaultIndex,
5376     newIndex);
5377   setFallbackImage(config, newKernelPath != NULL);   setFallbackImage(config, newKernelPath != NULL);
5378   if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,   if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
5379   removeArgs, newMBKernelArgs, removeMBKernelArgs))   removeArgs, newMBKernelArgs, removeMBKernelArgs))

Legend:
Removed from v.3018  
changed lines
  Added in v.3031