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--; |
2504 |
entry->skip = 1; |
entry->skip = 1; |
2505 |
} |
} |
2506 |
|
|
2507 |
void setDefaultImage(struct grubConfig *config, int isUserSpecifiedKernelPath, |
void setDefaultImage(struct grubConfig *config, int isAddingBootEntry, |
2508 |
const char *defaultKernelPath, int newBootEntryIsDefault, |
const char *defaultKernelPath, int newBootEntryIsDefault, |
2509 |
const char *prefix, int flags, int newDefaultBootEntryIndex) |
const char *prefix, int flags, |
2510 |
|
int newDefaultBootEntryIndex, int newBootEntryIndex) |
2511 |
{ |
{ |
2512 |
struct singleEntry *entry, *entry2, *newDefault; |
struct singleEntry *bootEntry, *newDefault; |
2513 |
int i, j; |
int indexToVerify, firstKernelEntryIndex, currentLookupIndex; |
2514 |
|
|
2515 |
|
/* handle the two cases where the user explictly picks the default |
2516 |
|
* boot entry index as it would exist post-modification */ |
2517 |
|
|
2518 |
|
/* Case 1: user chose to make the latest boot entry the default */ |
2519 |
if (newBootEntryIsDefault) { |
if (newBootEntryIsDefault) { |
2520 |
config->defaultImage = FIRST_ENTRY_INDEX; |
config->defaultImage = newBootEntryIndex; |
2521 |
return; |
return; |
2522 |
} else if ((newDefaultBootEntryIndex >= 0) && config->cfi->defaultIsIndex) { |
} |
2523 |
if (findEntryByIndex(config, newDefaultBootEntryIndex)) |
|
2524 |
|
/* Case 2: user picked an arbitrary index as the default boot entry */ |
2525 |
|
if (newDefaultBootEntryIndex >= FIRST_ENTRY_INDEX |
2526 |
|
&& config->cfi->defaultIsIndex) { |
2527 |
|
indexToVerify = newDefaultBootEntryIndex; |
2528 |
|
|
2529 |
|
/* user chose to make latest boot entry the default */ |
2530 |
|
if (newDefaultBootEntryIndex == newBootEntryIndex) { |
2531 |
|
config->defaultImage = newBootEntryIndex; |
2532 |
|
return; |
2533 |
|
} |
2534 |
|
|
2535 |
|
/* the user picks the default index based on the |
2536 |
|
* order of the bootloader configuration after |
2537 |
|
* modification; ensure we are checking for the |
2538 |
|
* existence of the correct entry */ |
2539 |
|
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; |
config->defaultImage = newDefaultBootEntryIndex; |
2547 |
else |
} else { |
2548 |
config->defaultImage = NO_DEFAULT_ENTRY; |
config->defaultImage = NO_DEFAULT_ENTRY; |
2549 |
|
} |
2550 |
|
|
2551 |
return; |
return; |
2552 |
} else if (defaultKernelPath) { |
} |
2553 |
i = 0; |
|
2554 |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
/* handle cases where the index value may shift */ |
2555 |
config->defaultImage = i; |
|
2556 |
} else { |
/* check validity of existing default or first-entry-found |
2557 |
|
selection */ |
2558 |
|
if (defaultKernelPath) { |
2559 |
|
/* 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; |
config->defaultImage = NO_DEFAULT_ENTRY; |
2564 |
return; |
return; |
2565 |
} |
} |
|
} |
|
2566 |
|
|
2567 |
/* 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; |
|
2568 |
|
|
2569 |
if (config->defaultImage >= FIRST_ENTRY_INDEX) |
/* this is where we start looking for decrement later */ |
2570 |
entry = findEntryByIndex(config, config->defaultImage); |
currentLookupIndex = config->defaultImage; |
|
else |
|
|
entry = NULL; |
|
2571 |
|
|
2572 |
if (entry && !entry->skip) { |
if (isAddingBootEntry && !config->isModified && |
2573 |
/* we can preserve the default */ |
(newBootEntryIndex < config->defaultImage)) { |
2574 |
if (isUserSpecifiedKernelPath) |
/* increment because new entry added before default */ |
2575 |
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--; |
|
2576 |
} |
} |
|
} else if (isUserSpecifiedKernelPath) { |
|
|
config->defaultImage = FIRST_ENTRY_INDEX; |
|
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 = NO_DEFAULT_ENTRY; |
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 |
|
|
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)) |