X Tutup
Skip to content

Commit 24f0c62

Browse files
authored
Merge pull request systemd#22791 from keszybz/bootctl-invert-order
Invert order of entries w/o sort-key in sd-boot menu
2 parents 5b39139 + 62a4b58 commit 24f0c62

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

src/boot/efi/boot.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,10 +1675,9 @@ static INTN config_entry_compare(const ConfigEntry *a, const ConfigEntry *b) {
16751675
assert(b);
16761676

16771677
/* Order entries that have no tries left to the end of the list */
1678-
if (a->tries_left == 0 && b->tries_left != 0)
1679-
return 1;
1680-
if (a->tries_left != 0 && b->tries_left == 0)
1681-
return -1;
1678+
r = CMP(a->tries_left == 0, b->tries_left == 0);
1679+
if (r != 0)
1680+
return r;
16821681

16831682
/* If there's a sort key defined for *both* entries, then we do new-style ordering, i.e. by
16841683
* sort-key/machine-id/version, with a final fallback to id. If there's no sort key for either, we do
@@ -1687,8 +1686,8 @@ static INTN config_entry_compare(const ConfigEntry *a, const ConfigEntry *b) {
16871686
r = CMP(!a->sort_key, !b->sort_key);
16881687
if (r != 0) /* one is old-style, one new-style */
16891688
return r;
1690-
if (a->sort_key && b->sort_key) {
16911689

1690+
if (a->sort_key && b->sort_key) {
16921691
r = strcmp(a->sort_key, b->sort_key);
16931692
if (r != 0)
16941693
return r;
@@ -1704,30 +1703,23 @@ static INTN config_entry_compare(const ConfigEntry *a, const ConfigEntry *b) {
17041703
return r;
17051704
}
17061705

1707-
/* Now order by ID (the version is likely part of the ID, thus note that this might put the oldest
1708-
* version last, not first, i.e. specifying a sort key explicitly is thus generally preferable, to
1709-
* take benefit of the explicit sorting above.) */
1710-
r = strverscmp_improved(a->id, b->id);
1706+
/* Now order by ID. The version is likely part of the ID, thus note that this will generatelly put
1707+
* the newer versions earlier. Specifying a sort key explicitly is preferable, because it gives an
1708+
* explicit sort order. */
1709+
r = -strverscmp_improved(a->id, b->id);
17111710
if (r != 0)
17121711
return r;
17131712

1714-
if (a->tries_left == UINTN_MAX ||
1715-
b->tries_left == UINTN_MAX)
1713+
if (a->tries_left == UINTN_MAX || b->tries_left == UINTN_MAX)
17161714
return 0;
17171715

17181716
/* If both items have boot counting, and otherwise are identical, put the entry with more tries left first */
1719-
if (a->tries_left < b->tries_left)
1720-
return 1;
1721-
if (a->tries_left > b->tries_left)
1722-
return -1;
1717+
r = -CMP(a->tries_left, b->tries_left);
1718+
if (r != 0)
1719+
return r;
17231720

17241721
/* If they have the same number of tries left, then let the one win which was tried fewer times so far */
1725-
if (a->tries_done > b->tries_done)
1726-
return 1;
1727-
if (a->tries_done < b->tries_done)
1728-
return -1;
1729-
1730-
return 0;
1722+
return CMP(a->tries_done, b->tries_done);
17311723
}
17321724

17331725
static UINTN config_entry_find(Config *config, const CHAR16 *needle) {

src/shared/bootspec.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ static int boot_entry_load(
154154
}
155155

156156
void boot_config_free(BootConfig *config) {
157-
size_t i;
158-
159157
assert(config);
160158

161159
free(config->default_pattern);
@@ -171,7 +169,7 @@ void boot_config_free(BootConfig *config) {
171169
free(config->entry_default);
172170
free(config->entry_selected);
173171

174-
for (i = 0; i < config->n_entries; i++)
172+
for (size_t i = 0; i < config->n_entries; i++)
175173
boot_entry_free(config->entries + i);
176174
free(config->entries);
177175
}
@@ -256,6 +254,7 @@ static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
256254
r = CMP(!a->sort_key, !b->sort_key);
257255
if (r != 0)
258256
return r;
257+
259258
if (a->sort_key && b->sort_key) {
260259
r = strcmp(a->sort_key, b->sort_key);
261260
if (r != 0)
@@ -270,7 +269,7 @@ static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
270269
return r;
271270
}
272271

273-
return strverscmp_improved(a->id, b->id);
272+
return -strverscmp_improved(a->id, b->id);
274273
}
275274

276275
static int boot_entries_find(
@@ -418,12 +417,9 @@ static int find_sections(
418417

419418
_cleanup_free_ struct PeSectionHeader *sections = NULL;
420419
_cleanup_free_ char *osrelease = NULL, *cmdline = NULL;
421-
size_t i, n_sections;
422-
struct DosFileHeader dos;
423-
struct PeHeader pe;
424-
uint64_t start;
425420
ssize_t n;
426421

422+
struct DosFileHeader dos;
427423
n = pread(fd, &dos, sizeof(dos), 0);
428424
if (n < 0)
429425
return log_error_errno(errno, "Failed read DOS header: %m");
@@ -433,7 +429,9 @@ static int find_sections(
433429
if (dos.Magic[0] != 'M' || dos.Magic[1] != 'Z')
434430
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "DOS executable magic missing, refusing.");
435431

436-
start = unaligned_read_le32(&dos.ExeHeader);
432+
uint64_t start = unaligned_read_le32(&dos.ExeHeader);
433+
434+
struct PeHeader pe;
437435
n = pread(fd, &pe, sizeof(pe), start);
438436
if (n < 0)
439437
return log_error_errno(errno, "Failed to read PE header: %m");
@@ -443,7 +441,7 @@ static int find_sections(
443441
if (pe.Magic[0] != 'P' || pe.Magic[1] != 'E' || pe.Magic[2] != 0 || pe.Magic[3] != 0)
444442
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PE executable magic missing, refusing.");
445443

446-
n_sections = unaligned_read_le16(&pe.FileHeader.NumberOfSections);
444+
size_t n_sections = unaligned_read_le16(&pe.FileHeader.NumberOfSections);
447445
if (n_sections > 96)
448446
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PE header has too many sections, refusing.");
449447

@@ -459,7 +457,7 @@ static int find_sections(
459457
if ((size_t) n != n_sections * sizeof(struct PeSectionHeader))
460458
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading sections, refusing.");
461459

462-
for (i = 0; i < n_sections; i++) {
460+
for (size_t i = 0; i < n_sections; i++) {
463461
_cleanup_free_ char *k = NULL;
464462
uint32_t offset, size;
465463
char **b;

src/test/test-bootspec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ TEST_RET(bootspec_sort) {
8686
assert_se(streq(config.entries[2].id, "c.conf"));
8787

8888
/* The following ones have no sort key, hence order by version compared ids, lowest first */
89-
assert_se(streq(config.entries[3].id, "a-5.conf"));
89+
assert_se(streq(config.entries[3].id, "b.conf"));
9090
assert_se(streq(config.entries[4].id, "a-10.conf"));
91-
assert_se(streq(config.entries[5].id, "b.conf"));
91+
assert_se(streq(config.entries[5].id, "a-5.conf"));
9292

9393
return 0;
9494
}

0 commit comments

Comments
 (0)
X Tutup