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 |
|
|
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; |
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) { |
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 == '"' || |
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", |
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))) { |
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) { |
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; |
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); |
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); |
1736 |
"saved_entry", title); |
"saved_entry", title); |
1737 |
} |
} |
1738 |
} |
} |
1739 |
} else if (cfg->defaultImage > -1) { |
} else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) { |
1740 |
if (cfg->cfi->defaultIsIndex) { |
if (cfg->cfi->defaultIsIndex) { |
1741 |
if (cfg->cfi->defaultIsVariable) { |
if (cfg->cfi->defaultIsVariable) { |
1742 |
fprintf(out, "%sset default=\"%d\"\n", indent, |
fprintf(out, "%sset default=\"%d\"\n", indent, |
2440 |
} |
} |
2441 |
} |
} |
2442 |
} |
} |
2443 |
} else if (cfg->defaultImage > -1) { |
} else if (cfg->defaultImage >= FIRST_ENTRY_INDEX) { |
2444 |
entry = findEntryByIndex(cfg, cfg->defaultImage); |
entry = findEntryByIndex(cfg, cfg->defaultImage); |
2445 |
if (entry && suitableImage(entry, prefix, skipRemoved, flags)) { |
if (entry && suitableImage(entry, prefix, skipRemoved, flags)) { |
2446 |
if (indexPtr) |
if (indexPtr) |
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--; |
2517 |
entry->skip = 1; |
entry->skip = 1; |
2518 |
} |
} |
2519 |
|
|
2520 |
void setDefaultImage(struct grubConfig *config, int hasNew, |
void setDefaultImage(struct grubConfig *config, int isAddingBootEntry, |
2521 |
const char *defaultKernelPath, int newIsDefault, |
const char *defaultKernelPath, int newBootEntryIsDefault, |
2522 |
const char *prefix, int flags, int index) |
const char *prefix, int flags, |
2523 |
{ |
int newDefaultBootEntryIndex, int newBootEntryIndex) |
2524 |
struct singleEntry *entry, *entry2, *newDefault; |
{ |
2525 |
int i, j; |
struct singleEntry *bootEntry, *newDefault; |
2526 |
|
int indexToVerify, firstKernelEntryIndex, currentLookupIndex; |
2527 |
if (newIsDefault) { |
|
2528 |
config->defaultImage = 0; |
/* handle the two cases where the user explictly picks the default |
2529 |
|
* boot entry index as it would exist post-modification */ |
2530 |
|
|
2531 |
|
/* Case 1: user chose to make the latest boot entry the default */ |
2532 |
|
if (newBootEntryIsDefault) { |
2533 |
|
config->defaultImage = newBootEntryIndex; |
2534 |
return; |
return; |
2535 |
} else if ((index >= 0) && config->cfi->defaultIsIndex) { |
} |
2536 |
if (findEntryByIndex(config, index)) |
|
2537 |
config->defaultImage = index; |
/* Case 2: user picked an arbitrary index as the default boot entry */ |
2538 |
else |
if (newDefaultBootEntryIndex >= FIRST_ENTRY_INDEX |
2539 |
config->defaultImage = -1; |
&& config->cfi->defaultIsIndex) { |
2540 |
return; |
indexToVerify = newDefaultBootEntryIndex; |
2541 |
} else if (defaultKernelPath) { |
|
2542 |
i = 0; |
/* user chose to make latest boot entry the default */ |
2543 |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
if (newDefaultBootEntryIndex == newBootEntryIndex) { |
2544 |
config->defaultImage = i; |
config->defaultImage = newBootEntryIndex; |
|
} else { |
|
|
config->defaultImage = -1; |
|
2545 |
return; |
return; |
2546 |
} |
} |
|
} |
|
2547 |
|
|
2548 |
/* defaultImage now points to what we'd like to use, but before any |
/* the user picks the default index based on the |
2549 |
* order changes */ |
* order of the bootloader configuration after |
2550 |
if ((config->defaultImage == DEFAULT_SAVED) || |
* modification; ensure we are checking for the |
2551 |
(config->defaultImage == DEFAULT_SAVED_GRUB2)) |
* existence of the correct entry */ |
2552 |
/* default is set to saved, we don't want to change it */ |
if (newBootEntryIndex < newDefaultBootEntryIndex) { |
2553 |
|
if (!config->isModified) |
2554 |
|
indexToVerify--; |
2555 |
|
} |
2556 |
|
|
2557 |
|
/* verify the user selected index will exist */ |
2558 |
|
if (findEntryByIndex(config, indexToVerify)) { |
2559 |
|
config->defaultImage = newDefaultBootEntryIndex; |
2560 |
|
} else { |
2561 |
|
config->defaultImage = NO_DEFAULT_ENTRY; |
2562 |
|
} |
2563 |
|
|
2564 |
return; |
return; |
2565 |
|
} |
2566 |
|
|
2567 |
if (config->defaultImage > -1) |
/* handle cases where the index value may shift */ |
|
entry = findEntryByIndex(config, config->defaultImage); |
|
|
else |
|
|
entry = NULL; |
|
2568 |
|
|
2569 |
if (entry && !entry->skip) { |
/* check validity of existing default or first-entry-found |
2570 |
/* we can preserve the default */ |
selection */ |
2571 |
if (hasNew) |
if (defaultKernelPath) { |
2572 |
config->defaultImage++; |
/* user requested first-entry-found */ |
2573 |
|
if (!findEntryByPath(config, defaultKernelPath, |
2574 |
|
prefix, &firstKernelEntryIndex)) { |
2575 |
|
/* don't change default if can't find match */ |
2576 |
|
config->defaultImage = NO_DEFAULT_ENTRY; |
2577 |
|
return; |
2578 |
|
} |
2579 |
|
|
2580 |
|
config->defaultImage = firstKernelEntryIndex; |
2581 |
|
|
2582 |
|
/* this is where we start looking for decrement later */ |
2583 |
|
currentLookupIndex = config->defaultImage; |
2584 |
|
|
2585 |
/* count the number of entries erased before this one */ |
if (isAddingBootEntry && !config->isModified && |
2586 |
for (j = 0; j < config->defaultImage; j++) { |
(newBootEntryIndex < config->defaultImage)) { |
2587 |
entry2 = findEntryByIndex(config, j); |
/* increment because new entry added before default */ |
2588 |
if (entry2->skip) |
config->defaultImage++; |
|
config->defaultImage--; |
|
2589 |
} |
} |
|
} else if (hasNew) { |
|
|
config->defaultImage = 0; |
|
2590 |
} else { |
} else { |
2591 |
/* Either we just erased the default (or the default line was |
/* use pre-existing default entry */ |
2592 |
* bad to begin with) and didn't put a new one in. We'll use |
currentLookupIndex = config->defaultImage; |
2593 |
* the first valid image. */ |
|
2594 |
|
if (isAddingBootEntry |
2595 |
|
&& (newBootEntryIndex <= config->defaultImage)) { |
2596 |
|
config->defaultImage++; |
2597 |
|
|
2598 |
|
if (config->isModified) { |
2599 |
|
currentLookupIndex++; |
2600 |
|
} |
2601 |
|
} |
2602 |
|
} |
2603 |
|
|
2604 |
|
/* sanity check - is this entry index valid? */ |
2605 |
|
bootEntry = findEntryByIndex(config, currentLookupIndex); |
2606 |
|
|
2607 |
|
if ((bootEntry && bootEntry->skip) || !bootEntry) { |
2608 |
|
/* entry is to be skipped or is invalid */ |
2609 |
|
if (isAddingBootEntry) { |
2610 |
|
config->defaultImage = newBootEntryIndex; |
2611 |
|
return; |
2612 |
|
} |
2613 |
newDefault = |
newDefault = |
2614 |
findTemplate(config, prefix, &config->defaultImage, 1, |
findTemplate(config, prefix, &config->defaultImage, 1, |
2615 |
flags); |
flags); |
2616 |
if (!newDefault) |
if (!newDefault) { |
2617 |
config->defaultImage = -1; |
config->defaultImage = NO_DEFAULT_ENTRY; |
2618 |
|
} |
2619 |
|
|
2620 |
|
return; |
2621 |
|
} |
2622 |
|
|
2623 |
|
currentLookupIndex--; |
2624 |
|
|
2625 |
|
/* decrement index by the total number of entries deleted */ |
2626 |
|
|
2627 |
|
for (indexToVerify = currentLookupIndex; |
2628 |
|
indexToVerify >= FIRST_ENTRY_INDEX; indexToVerify--) { |
2629 |
|
|
2630 |
|
bootEntry = findEntryByIndex(config, indexToVerify); |
2631 |
|
|
2632 |
|
if (bootEntry && bootEntry->skip) { |
2633 |
|
config->defaultImage--; |
2634 |
|
} |
2635 |
} |
} |
2636 |
} |
} |
2637 |
|
|
4803 |
} |
} |
4804 |
|
|
4805 |
if (updateImage(config, indexs, prefix, newKernelArgs, NULL, |
if (updateImage(config, indexs, prefix, newKernelArgs, NULL, |
4806 |
newMBKernelArgs, NULL)) |
newMBKernelArgs, NULL)) { |
4807 |
|
config->isModified = 1; |
4808 |
return 1; |
return 1; |
4809 |
|
} |
4810 |
|
|
4811 |
return 0; |
return 0; |
4812 |
} |
} |
5258 |
struct singleEntry *entry; |
struct singleEntry *entry; |
5259 |
char *rootspec; |
char *rootspec; |
5260 |
|
|
5261 |
if (config->defaultImage == -1) |
if (config->defaultImage == NO_DEFAULT_ENTRY) |
5262 |
return 0; |
return 0; |
5263 |
if (config->defaultImage == DEFAULT_SAVED_GRUB2 && |
if (config->defaultImage == DEFAULT_SAVED_GRUB2 && |
5264 |
cfi->defaultIsSaved) |
cfi->defaultIsSaved) |
5265 |
config->defaultImage = 0; |
config->defaultImage = FIRST_ENTRY_INDEX; |
5266 |
entry = findEntryByIndex(config, config->defaultImage); |
entry = findEntryByIndex(config, config->defaultImage); |
5267 |
if (!entry) |
if (!entry) |
5268 |
return 0; |
return 0; |
5285 |
struct singleLine *line; |
struct singleLine *line; |
5286 |
struct singleEntry *entry; |
struct singleEntry *entry; |
5287 |
|
|
5288 |
if (config->defaultImage == -1) |
if (config->defaultImage == NO_DEFAULT_ENTRY) |
5289 |
return 0; |
return 0; |
5290 |
if (config->defaultImage == DEFAULT_SAVED_GRUB2 && |
if (config->defaultImage == DEFAULT_SAVED_GRUB2 && |
5291 |
cfi->defaultIsSaved) |
cfi->defaultIsSaved) |
5292 |
config->defaultImage = 0; |
config->defaultImage = FIRST_ENTRY_INDEX; |
5293 |
entry = findEntryByIndex(config, config->defaultImage); |
entry = findEntryByIndex(config, config->defaultImage); |
5294 |
if (!entry) |
if (!entry) |
5295 |
return 0; |
return 0; |
5319 |
return 0; |
return 0; |
5320 |
|
|
5321 |
} else if (displayDefaultIndex) { |
} else if (displayDefaultIndex) { |
5322 |
if (config->defaultImage == -1) |
if (config->defaultImage == NO_DEFAULT_ENTRY) |
5323 |
return 0; |
return 0; |
5324 |
if (config->defaultImage == DEFAULT_SAVED_GRUB2 && |
if (config->defaultImage == DEFAULT_SAVED_GRUB2 && |
5325 |
cfi->defaultIsSaved) |
cfi->defaultIsSaved) |
5326 |
config->defaultImage = 0; |
config->defaultImage = FIRST_ENTRY_INDEX; |
5327 |
printf("%i\n", config->defaultImage); |
printf("%i\n", config->defaultImage); |
5328 |
return 0; |
return 0; |
5329 |
|
|
5339 |
markRemovedImage(config, removeKernelPath, bootPrefix); |
markRemovedImage(config, removeKernelPath, bootPrefix); |
5340 |
markRemovedImage(config, removeMBKernel, bootPrefix); |
markRemovedImage(config, removeMBKernel, bootPrefix); |
5341 |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, |
5342 |
makeDefault, bootPrefix, flags, defaultIndex); |
makeDefault, bootPrefix, flags, defaultIndex, |
5343 |
|
newIndex); |
5344 |
setFallbackImage(config, newKernelPath != NULL); |
setFallbackImage(config, newKernelPath != NULL); |
5345 |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
5346 |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) |