2501 |
entry->skip = 1; |
entry->skip = 1; |
2502 |
} |
} |
2503 |
|
|
2504 |
void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath, |
void setDefaultImage(struct grubConfig *config, int isAddingBootEntry, |
2505 |
const char *defaultKernelPath, int newBootEntryIsDefault, |
const char *defaultKernelPath, int newBootEntryIsDefault, |
2506 |
const char *prefix, int flags, int newDefaultBootEntryIndex) |
const char *prefix, int flags, |
2507 |
|
int newDefaultBootEntryIndex, int newBootEntryIndex) |
2508 |
{ |
{ |
2509 |
struct singleEntry *entry, *entry2, *newDefault; |
struct singleEntry *bootEntry, *newDefault; |
2510 |
int i, j; |
int indexToVerify, firstKernelEntryIndex, currentLookupIndex; |
2511 |
|
|
2512 |
|
/* handle the two cases where the user explictly picks the default |
2513 |
|
* boot entry index as it would exist post-modification */ |
2514 |
|
|
2515 |
|
/* Case 1: user chose to make the latest boot entry the default */ |
2516 |
if (newBootEntryIsDefault) { |
if (newBootEntryIsDefault) { |
2517 |
config->defaultImage = FIRST_ENTRY_INDEX; |
config->defaultImage = newBootEntryIndex; |
2518 |
return; |
return; |
2519 |
} else if ((newDefaultBootEntryIndex >= 0) && config->cfi->defaultIsIndex) { |
} |
2520 |
if (findEntryByIndex(config, newDefaultBootEntryIndex)) |
|
2521 |
|
/* Case 2: user picked an arbitrary index as the default boot entry */ |
2522 |
|
if (newDefaultBootEntryIndex >= FIRST_ENTRY_INDEX |
2523 |
|
&& config->cfi->defaultIsIndex) { |
2524 |
|
indexToVerify = newDefaultBootEntryIndex; |
2525 |
|
|
2526 |
|
/* user chose to make latest boot entry the default */ |
2527 |
|
if (newDefaultBootEntryIndex == newBootEntryIndex) { |
2528 |
|
config->defaultImage = newBootEntryIndex; |
2529 |
|
return; |
2530 |
|
} |
2531 |
|
|
2532 |
|
/* the user picks the default index based on the |
2533 |
|
* order of the bootloader configuration after |
2534 |
|
* modification; ensure we are checking for the |
2535 |
|
* existence of the correct entry */ |
2536 |
|
if (newBootEntryIndex < newDefaultBootEntryIndex) { |
2537 |
|
if (!config->isModified) |
2538 |
|
indexToVerify--; |
2539 |
|
} |
2540 |
|
|
2541 |
|
/* verify the user selected index will exist */ |
2542 |
|
if (findEntryByIndex(config, indexToVerify)) { |
2543 |
config->defaultImage = newDefaultBootEntryIndex; |
config->defaultImage = newDefaultBootEntryIndex; |
2544 |
else |
} else { |
2545 |
config->defaultImage = NO_DEFAULT_ENTRY; |
config->defaultImage = NO_DEFAULT_ENTRY; |
2546 |
|
} |
2547 |
|
|
2548 |
return; |
return; |
2549 |
} else if (defaultKernelPath) { |
} |
2550 |
i = 0; |
|
2551 |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
/* handle cases where the index value may shift */ |
2552 |
config->defaultImage = i; |
|
2553 |
} else { |
/* check validity of existing default or first-entry-found |
2554 |
|
selection */ |
2555 |
|
if (defaultKernelPath) { |
2556 |
|
/* user requested first-entry-found */ |
2557 |
|
if (!findEntryByPath(config, defaultKernelPath, |
2558 |
|
prefix, &firstKernelEntryIndex)) { |
2559 |
|
/* don't change default if can't find match */ |
2560 |
config->defaultImage = NO_DEFAULT_ENTRY; |
config->defaultImage = NO_DEFAULT_ENTRY; |
2561 |
return; |
return; |
2562 |
} |
} |
|
} |
|
2563 |
|
|
2564 |
/* 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; |
|
2565 |
|
|
2566 |
if (config->defaultImage >= FIRST_ENTRY_INDEX) |
/* this is where we start looking for decrement later */ |
2567 |
entry = findEntryByIndex(config, config->defaultImage); |
currentLookupIndex = config->defaultImage; |
|
else |
|
|
entry = NULL; |
|
2568 |
|
|
2569 |
if (entry && !entry->skip) { |
if (isAddingBootEntry && !config->isModified && |
2570 |
/* we can preserve the default */ |
(newBootEntryIndex < config->defaultImage)) { |
2571 |
if (isUserSpecifiedKernelPath) |
/* increment because new entry added before default */ |
2572 |
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--; |
|
2573 |
} |
} |
|
} else if (isUserSpecifiedKernelPath) { |
|
|
config->defaultImage = FIRST_ENTRY_INDEX; |
|
2574 |
} else { |
} else { |
2575 |
/* Either we just erased the default (or the default line was |
/* use pre-existing default entry */ |
2576 |
* bad to begin with) and didn't put a new one in. We'll use |
currentLookupIndex = config->defaultImage; |
2577 |
* the first valid image. */ |
|
2578 |
|
if (isAddingBootEntry |
2579 |
|
&& (newBootEntryIndex <= config->defaultImage)) { |
2580 |
|
config->defaultImage++; |
2581 |
|
|
2582 |
|
if (config->isModified) { |
2583 |
|
currentLookupIndex++; |
2584 |
|
} |
2585 |
|
} |
2586 |
|
} |
2587 |
|
|
2588 |
|
/* sanity check - is this entry index valid? */ |
2589 |
|
bootEntry = findEntryByIndex(config, currentLookupIndex); |
2590 |
|
|
2591 |
|
if ((bootEntry && bootEntry->skip) || !bootEntry) { |
2592 |
|
/* entry is to be skipped or is invalid */ |
2593 |
|
if (isAddingBootEntry) { |
2594 |
|
config->defaultImage = newBootEntryIndex; |
2595 |
|
return; |
2596 |
|
} |
2597 |
newDefault = |
newDefault = |
2598 |
findTemplate(config, prefix, &config->defaultImage, 1, |
findTemplate(config, prefix, &config->defaultImage, 1, |
2599 |
flags); |
flags); |
2600 |
if (!newDefault) |
if (!newDefault) { |
2601 |
config->defaultImage = NO_DEFAULT_ENTRY; |
config->defaultImage = NO_DEFAULT_ENTRY; |
2602 |
|
} |
2603 |
|
|
2604 |
|
return; |
2605 |
|
} |
2606 |
|
|
2607 |
|
currentLookupIndex--; |
2608 |
|
|
2609 |
|
/* decrement index by the total number of entries deleted */ |
2610 |
|
|
2611 |
|
for (indexToVerify = currentLookupIndex; |
2612 |
|
indexToVerify >= FIRST_ENTRY_INDEX; indexToVerify--) { |
2613 |
|
|
2614 |
|
bootEntry = findEntryByIndex(config, indexToVerify); |
2615 |
|
|
2616 |
|
if (bootEntry && bootEntry->skip) { |
2617 |
|
config->defaultImage--; |
2618 |
|
} |
2619 |
} |
} |
2620 |
} |
} |
2621 |
|
|
5323 |
markRemovedImage(config, removeKernelPath, bootPrefix); |
markRemovedImage(config, removeKernelPath, bootPrefix); |
5324 |
markRemovedImage(config, removeMBKernel, bootPrefix); |
markRemovedImage(config, removeMBKernel, bootPrefix); |
5325 |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, |
5326 |
makeDefault, bootPrefix, flags, defaultIndex); |
makeDefault, bootPrefix, flags, defaultIndex, |
5327 |
|
newIndex); |
5328 |
setFallbackImage(config, newKernelPath != NULL); |
setFallbackImage(config, newKernelPath != NULL); |
5329 |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
5330 |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) |