diff --git a/build/service-scripts/boot-success.sh b/build/service-scripts/boot-success.sh index 9b0a2040..13183a95 100755 --- a/build/service-scripts/boot-success.sh +++ b/build/service-scripts/boot-success.sh @@ -93,16 +93,18 @@ commit_slot() { # commit_slot : make BOOTX64.EFI this slot's kernel } # However a trial ends: a stale slot entry would outrank BOOTX64.EFI at every cold boot, -# and the committed slot's own entry is a second way to it should that file be lost. +# and the committed slot's own entry, in the place Kryptik's had in the boot order, is a +# second way to it should that file be lost. # It runs before the trial record is removed or set aside: while that stands # kryptik-update arms nothing, so no BootNext an apply has just set is forgotten here. forget_entries() { # forget_entries COMMITTED-SLOT - if ! kryptik-efiboot forget >/dev/null 2>&1; then - say "the firmware's Kryptik entries could not be removed; its own boot order may not name the committed slot" - return 1 - fi - kryptik-efiboot ensure "$1" >/dev/null 2>&1 \ - || say "slot $1 has no firmware entry of its own; BOOTX64.EFI is the only way to it" + kryptik-efiboot keep "$1" >/dev/null 2>&1 + case $? in + 0) ;; + 3) say "slot $1 has no firmware entry of its own; BOOTX64.EFI is the only way to it" ;; + *) say "the firmware's Kryptik entries could not be removed; its own boot order may not name the committed slot" + return 1 ;; + esac return 0 } diff --git a/docs/design/boot-and-updates.md b/docs/design/boot-and-updates.md index 8ce6cda0..95f679b5 100644 --- a/docs/design/boot-and-updates.md +++ b/docs/design/boot-and-updates.md @@ -119,17 +119,23 @@ anything names it. A kernel's signature is the firmware's to check, at boot. The installed disk boots `BOOTX64.EFI` through the removable-media path with no firmware variables. Some firmware boots an internal disk only from a -Boot#### entry, so the installer also gives slot a its own, last in -`BootOrder`, after `forget` has removed an earlier install's; a firmware that -refuses it leaves the disk to `BOOTX64.EFI`, and the installer says so. +Boot#### entry, so the installer also gives slot a its own, after `forget` +has removed an earlier install's; a firmware that refuses it leaves the disk +to `BOOTX64.EFI`, and the installer says so. A new entry goes in `BootOrder` +after the last entry that does not boot from the network: a firmware lists +its network boot after the disks, and an entry behind it would wait on the +network at every boot, while one ahead of another system's entry would +change what that system's users boot. Entries are kept by slot, not by disk, so a machine holds one Kryptik's: a second Kryptik disk in it boots by its own `BOOTX64.EFI`. `kryptik-efiboot` adds a `Kryptik slot ` Boot#### -entry for a trial. When the trial ends, committed or not, `forget` removes +entry for a trial. When the trial ends, committed or not, `keep` removes Kryptik's entries and `BootNext`, and the committed slot gets its own entry -back. Firmware re-adds its own disk entry at the end of `BootOrder`, so an -entry left in front of it must boot what `BOOTX64.EFI` boots; this one does, -and is a second way to the committed slot should `BOOTX64.EFI` be lost. An +back where the first of them stood, so a place the user gave it in the +firmware's setup outlasts every update. Firmware re-adds its own disk entry +at the end of `BootOrder`, so an entry left in front of it must boot what +`BOOTX64.EFI` boots; this one does, and is a second way to the committed +slot should `BOOTX64.EFI` be lost. An entry is Kryptik's when its description and its file say so, never by its number: slot a's is `Boot00A0` and slot b's `Boot00B0` unless another system already uses that number, and then the next free one, the other system's diff --git a/tools/efi/kryptik-efiboot.c b/tools/efi/kryptik-efiboot.c index 1ca78453..dcd0c212 100644 --- a/tools/efi/kryptik-efiboot.c +++ b/tools/efi/kryptik-efiboot.c @@ -8,9 +8,12 @@ * the partition when it is not this installation's * kryptik-efiboot forget delete Kryptik's entries, and BootNext if it names one, * drop them from BootOrder: the firmware boots BOOTX64.EFI + * kryptik-efiboot keep SLOT [ESP] forget, then ensure SLOT where Kryptik's first entry + * stood in BootOrder; exits 3 if only the entry failed * * An entry is Kryptik's by its description and file, never its number: slot a's is Boot00A0 - * and slot b's Boot00B0 unless another system holds that number, then the next free one. */ + * and slot b's Boot00B0 unless another system holds that number, then the next free one. A new + * entry goes in BootOrder after the last entry that does not boot from the network. */ #define _GNU_SOURCE #include #include @@ -285,6 +288,35 @@ static int holds(const unsigned char *list, size_t len, uint16_t num) { return 0; } +/* Does entry NUM boot from the network: a MAC, IPv4, IPv6, iSCSI, URI or Wi-Fi node in its + * device path? An entry this misses counts as another system's, which nothing goes ahead of. */ +static int is_network(uint16_t num) { + char var[9]; snprintf(var, sizeof var, "Boot%04X", (unsigned)num); + unsigned char buf[2048]; size_t n = 0; + if (read_var(var, buf, sizeof buf, &n) || n < 6) return 0; + size_t i = 6; + while (i + 1 < n) { uint16_t c = buf[i] | (buf[i+1] << 8); i += 2; if (!c) break; } + size_t end = i + (size_t)(buf[4] | (buf[5] << 8)); + while (i + 4 <= end && i + 4 <= n) { + unsigned t = buf[i], st = buf[i+1]; size_t l = (size_t)(buf[i+2] | (buf[i+3] << 8)); + if (l < 4 || t == 0x7f) break; + if (t == 3 && (st == 11 || st == 12 || st == 13 || st == 19 || st == 24 || st == 28)) return 1; + i += l; + } + return 0; +} + +/* Where in ORDER a new entry goes, counted in entries: AT if that is a place in it, else after + * the last entry that does not boot from the network. A firmware lists its network boot after + * the disks and adds entries after that, where each boot would wait on the network first; no + * other system's entry is passed. */ +static size_t place(const unsigned char *order, size_t ol, size_t at) { + size_t n = ol / 2; + if (at <= n) return at; + while (n > 0 && is_network((uint16_t)(order[2*n-2] | (order[2*n-1] << 8)))) n--; + return n; +} + /* Where SLOT's entry goes: its existing entry (own number first), else its own number if free, * else the next number that no variable and no place in ORDER uses. */ static int slot_var(const char *slot, char var[9], const unsigned char *order, size_t ol) { @@ -301,9 +333,10 @@ static int slot_var(const char *slot, char var[9], const unsigned char *order, s return -1; } -static int ensure_with(const char *slot, const struct part *esp, char var[9], const unsigned char *order, size_t ol); +static int ensure_with(const char *slot, const struct part *esp, char var[9], const unsigned char *order, size_t ol, size_t at); -static int ensure_entry(const char *slot, const char *named, char var[9]) { +/* AT is the entry's place in BootOrder should it not be there (place), SIZE_MAX for none. */ +static int ensure_entry(const char *slot, const char *named, char var[9], size_t at) { if (strcmp(slot, "a") && strcmp(slot, "b")) { errno = 0; return die("slot must be a or b"); } struct part esp; errno = 0; if (find_esp(&esp, named)) { @@ -313,13 +346,13 @@ static int ensure_entry(const char *slot, const char *named, char var[9]) { } unsigned char *order; size_t ol; if (read_order(&order, &ol)) return die("BootOrder cannot be read whole, so it is not rewritten"); - int r = ensure_with(slot, &esp, var, order, ol); + int r = ensure_with(slot, &esp, var, order, ol, at); free(order); return r; } /* ensure_entry, with BootOrder as read. */ -static int ensure_with(const char *slot, const struct part *esp, char var[9], const unsigned char *order, size_t ol) { +static int ensure_with(const char *slot, const struct part *esp, char var[9], const unsigned char *order, size_t ol, size_t at) { if (slot_var(slot, var, order, ol)) { errno = 0; return die("no Boot#### number is free"); } char own[9]; snprintf(own, sizeof own, "Boot%04X", !strcmp(slot, "a") ? 0x00A0u : 0x00B0u); if (strcmp(own, var) && var_exists(own)) printf("%s is another system's entry; slot %s's is %s\n", own, slot, var); @@ -335,13 +368,15 @@ static int ensure_with(const char *slot, const struct part *esp, char var[9], co if (write_var(var, buf, n)) return die("writing the Boot#### entry failed"); printf("%s -> %s on %s (PARTUUID %s)\n", var, file, esp->dev, esp->uuid); } - /* Last in BootOrder, for a firmware that ignores BootNext; forget removes it. */ + /* In BootOrder too, for a firmware that ignores BootNext; forget removes it. */ uint16_t num = (uint16_t)strtol(var + 4, NULL, 16); if (holds(order, ol, num)) return 0; unsigned char *grown = malloc(ol + 2); if (!grown) return die("updating BootOrder failed"); - if (ol) memcpy(grown, order, ol); - put_u16(grown + ol, num); + size_t k = 2 * place(order, ol, at); + if (k) memcpy(grown, order, k); + put_u16(grown + k, num); + if (ol > k) memcpy(grown + k + 2, order + k, ol - k); int bad = write_var("BootOrder", grown, ol + 2); free(grown); return bad ? die("updating BootOrder failed") : 0; @@ -349,7 +384,7 @@ static int ensure_with(const char *slot, const struct part *esp, char var[9], co static int cmd_set_next(const char *slot) { char var[9]; - if (ensure_entry(slot, NULL, var)) return 1; + if (ensure_entry(slot, NULL, var, SIZE_MAX)) return 1; unsigned char v[2]; put_u16(v, (uint16_t)strtol(var + 4, NULL, 16)); if (write_var("BootNext", v, 2)) return die("writing BootNext failed"); printf("BootNext = %s (one boot)\n", var); @@ -357,8 +392,10 @@ static int cmd_set_next(const char *slot) { } /* Drop Kryptik's entries, and a BootNext naming one or no entry, so BOOTX64.EFI (the committed - * slot) boots: a firmware re-adds its disk entry last, behind any leftover Kryptik entry. */ -static int cmd_forget(void) { + * slot) boots: a firmware re-adds its disk entry last, behind any leftover Kryptik entry. *AT + * gets the place in what is left of BootOrder where the first of them stood, SIZE_MAX if none. */ +static int cmd_forget(size_t *at) { + *at = SIZE_MAX; char names[256][9], mine[256][9]; int cnt = boot_vars(names, 256), no = 0; unsigned char ours[512]; size_t on = 0; for (int i = 0; i < cnt; i++) @@ -369,6 +406,7 @@ static int cmd_forget(void) { for (size_t i = 0; i + 1 < ol; i += 2) { uint16_t e = (uint16_t)(order[i] | (order[i+1] << 8)); if (!holds(ours, on, e)) kl += put_u16(kept + kl, e); + else if (*at == SIZE_MAX) *at = kl / 2; } int failed = kl != ol && (kl == 0 ? delete_var("BootOrder") : write_var("BootOrder", kept, kl)); free(order); free(kept); @@ -387,6 +425,17 @@ static int cmd_forget(void) { return 0; } +/* The end of a trial: forget, then SLOT's entry where Kryptik's first one stood, so a place the + * user gave it in the firmware's setup outlasts every update. The entry is made even if forget + * failed, as after forget then ensure: 1 when forget failed, 3 when only the entry did. */ +static int cmd_keep(const char *slot, const char *named) { + if (strcmp(slot, "a") && strcmp(slot, "b")) { errno = 0; die("slot must be a or b"); return 2; } + size_t at; char var[9]; + int forgot = cmd_forget(&at) == 0; + int made = ensure_entry(slot, named, var, at) == 0; + return !forgot ? 1 : made ? 0 : 3; +} + static void print_entry(const char *var) { char desc[128], file[256]; int active = 0; if (option_text(var, desc, sizeof desc, file, sizeof file, &active)) return; @@ -407,12 +456,13 @@ static int cmd_list(void) { } int main(int argc, char **argv) { - if (argc < 2) { fprintf(stderr, "usage: kryptik-efiboot list | set-next a|b | clear-next | ensure a|b [ESP] | forget\n"); return 2; } + if (argc < 2) { fprintf(stderr, "usage: kryptik-efiboot list | set-next a|b | clear-next | ensure a|b [ESP] | forget | keep a|b [ESP]\n"); return 2; } if (access(EFIVARS, R_OK)) { errno = 0; return die("no efivarfs at " EFIVARS " (not a UEFI boot, or sysinit did not mount it)"); } if (!strcmp(argv[1], "list")) return cmd_list(); if (!strcmp(argv[1], "set-next") && argc == 3) return cmd_set_next(argv[2]); - if (!strcmp(argv[1], "ensure") && (argc == 3 || argc == 4)) { char var[9]; return ensure_entry(argv[2], argc == 4 ? argv[3] : NULL, var); } - if (!strcmp(argv[1], "forget")) return cmd_forget(); + if (!strcmp(argv[1], "ensure") && (argc == 3 || argc == 4)) { char var[9]; return ensure_entry(argv[2], argc == 4 ? argv[3] : NULL, var, SIZE_MAX); } + if (!strcmp(argv[1], "forget")) { size_t at; return cmd_forget(&at); } + if (!strcmp(argv[1], "keep") && (argc == 3 || argc == 4)) return cmd_keep(argv[2], argc == 4 ? argv[3] : NULL); if (!strcmp(argv[1], "clear-next")) { if (delete_var("BootNext")) return die("deleting BootNext failed"); printf("BootNext cleared\n"); return 0; } fprintf(stderr, "kryptik-efiboot: unknown command\n"); return 2; } diff --git a/tools/install/kryptik-install.sh b/tools/install/kryptik-install.sh index b2389905..7d5b2898 100755 --- a/tools/install/kryptik-install.sh +++ b/tools/install/kryptik-install.sh @@ -447,7 +447,7 @@ rm -rf "$MNT_BASE/tesp/kryptik-report" rm -f "$MNT_BASE/tesp/EFI/kryptik/kryptik-b.efi" "$MNT_BASE/tesp/kryptik/version-b" sync umount "$MNT_BASE/tesp" || die "could not unmount the new ESP" -# A firmware entry for slot a as well, last in BootOrder, as kryptik-recover and boot-success +# A firmware entry for slot a as well, ahead of any network boot, as kryptik-recover and boot-success # leave one: some firmware boots an internal disk only from an entry, not by BOOTX64.EFI. # Entries of an earlier Kryptik go first, as they name a partition this install replaced. kryptik-efiboot forget > /dev/null 2>&1 || true diff --git a/tools/tests/boot-success.sh b/tools/tests/boot-success.sh index ea3488ad..f98ca97d 100755 --- a/tools/tests/boot-success.sh +++ b/tools/tests/boot-success.sh @@ -39,10 +39,13 @@ cat > "$T/bin/kryptik-efiboot" <<'EOF' #!/bin/sh echo "efiboot $*" >> "$KTEST/calls" # Whether the trial record still stood when the entries were forgotten. -[ "$1" = forget ] && { [ -e "$KTEST/boot/trial" ] && echo kept || echo gone; } >> "$KTEST/forget-saw" +[ "$1" = keep ] && { [ -e "$KTEST/boot/trial" ] && echo kept || echo gone; } >> "$KTEST/forget-saw" # What the firmware's variables say, as the case wrote them. [ "$1" = list ] && cat "$KTEST/efilist" 2>/dev/null -[ ! -e "$KTEST/efiboot_fails" ] && [ ! -e "$KTEST/efiboot_fails_$1" ] +# keep exits 1 when the forgetting failed, 3 when only the committed slot's entry did. +[ -e "$KTEST/efiboot_fails" ] && exit 1 +[ "$1" = keep ] && [ -e "$KTEST/efiboot_fails_ensure" ] && exit 3 +[ ! -e "$KTEST/efiboot_fails_$1" ] EOF cat > "$T/bin/reboot" <<'EOF' #!/bin/sh @@ -122,7 +125,7 @@ echo "-- a slot nothing asked for" # slot b runs: a firmware entry or BootNext set from outside booted it. run_case stray b "" persistent "" $ALL; go check "the earlier slot booted from outside is reported as uncommitted, not as ok" "$RESULT" "uncommitted b" -check "its entries are forgotten, the committed slot gets its own, and the machine reboots" "$CALLS" "mount -t vfat -o ro,nosuid,nodev,noexec /dev/vda1 $KTEST/run/esp umount $KTEST/run/esp efiboot forget efiboot ensure a reboot " +check "its entries are forgotten, the committed slot gets its own, and the machine reboots" "$CALLS" "mount -t vfat -o ro,nosuid,nodev,noexec /dev/vda1 $KTEST/run/esp umount $KTEST/run/esp efiboot keep a reboot " check "BOOTX64.EFI and the committed slot are left as they were" "$(cat "$KTEST/esp/EFI/BOOT/BOOTX64.EFI")|$(cat "$KTEST/esp/kryptik/committed-slot")" "kernel-a|a" check "the reboot is on record" "$([[ -e "$KTEST/boot/uncommitted" ]] && echo marked || echo unmarked)" "marked" run_case stray2 b "" persistent "" $ALL; : > "$KTEST/boot/uncommitted"; go @@ -144,10 +147,10 @@ check "committed-slot records b" "$(cat "$KTEST/esp/kryptik/committed-slot")" "b check "the new committed-slot record is fsynced under its temporary name" "$(grep -c -x "sync -f $KTEST/run/esp/kryptik/committed-slot.new" "$KTEST/syncs" 2>/dev/null)" "1" check "the trial record is gone" "$([[ -e "$KTEST/boot/trial" ]] && echo present || echo gone)" "gone" check "the entries are forgotten while the trial record still stands, so no apply arms in between" "$(cat "$KTEST/forget-saw" 2>/dev/null)" "kept" -check "the trial's firmware entries and BootNext are forgotten after the commit, the committed slot gets its own, and its release goes to the clock's floor" "$CALLS" "mount -t vfat -o rw,nosuid,nodev,noexec /dev/vda1 $KTEST/run/esp umount $KTEST/run/esp efiboot forget efiboot ensure b kryptikd time committed $KTEST/boot/release-b " +check "the trial's firmware entries and BootNext are forgotten after the commit, the committed slot gets its own, and its release goes to the clock's floor" "$CALLS" "mount -t vfat -o rw,nosuid,nodev,noexec /dev/vda1 $KTEST/run/esp umount $KTEST/run/esp efiboot keep b kryptikd time committed $KTEST/boot/release-b " run_case recfail b "" persistent 'b\narmed=1\n' $ALL; : > "$KTEST/sync_fails"; go check "a committed-slot record that cannot be written fails the commit: the trial stays, for the next boot to commit again" \ - "$RESULT|$(cat "$KTEST/esp/kryptik/committed-slot")|$([[ -e "$KTEST/boot/trial" ]] && echo kept || echo gone)|$(grep -c 'efiboot forget' "$KTEST/calls" 2>/dev/null)" "commit-failed b|a|kept|0" + "$RESULT|$(cat "$KTEST/esp/kryptik/committed-slot")|$([[ -e "$KTEST/boot/trial" ]] && echo kept || echo gone)|$(grep -c 'efiboot keep' "$KTEST/calls" 2>/dev/null)" "commit-failed b|a|kept|0" run_case bootfail b "" persistent 'b\narmed=1\n' $ALL; : > "$KTEST/sync_fails_boot"; go check "a boot file that cannot be replaced fails the commit, and the record goes on naming the slot BOOTX64.EFI boots" \ "$RESULT|$(cat "$KTEST/esp/EFI/BOOT/BOOTX64.EFI")|$(cat "$KTEST/esp/kryptik/committed-slot")|$([[ -e "$KTEST/boot/trial" ]] && echo kept || echo gone)" "commit-failed b|kernel-a|a|kept" @@ -171,7 +174,7 @@ check "... and the set-aside record says why, which the next boot's result does run_case unzones b "" persistent 'b\narmed=1\n' $ALL; rm -f "$KTEST/zones_ok"; go check "trial whose zones do not load: not committed" "${RESULT%%:*}" "trial-unhealthy b" run_case unforget b "" persistent 'b\narmed=1\n' eudev; go -check "an unhealthy trial forgets its entries, gives the committed slot its own, then reboots" "$CALLS" "efiboot forget efiboot ensure a reboot " +check "an unhealthy trial forgets its entries, gives the committed slot its own, then reboots" "$CALLS" "efiboot keep a reboot " check "an unhealthy trial's entries go before its record is set aside" "$(cat "$KTEST/forget-saw" 2>/dev/null)" "kept" check "... and several failures are kept in the order they were found" "$(sed -n 's/^why=//p' "$KTEST/boot/trial.failed")" \ "came up unhealthy: service seatd is not up; service kryptikd-serve is not up; service net-zone is not up; service getty-tty1 is not up" @@ -179,7 +182,7 @@ run_case unforget2 b "" persistent 'b\narmed=1\n' eudev; : > "$KTEST/efiboot_fai check "... and reboots if they stay: its record, now trial.failed, keeps it from coming back" "$(reboots)" "1" # On a degraded state /var is a tmpfs, so the trial record is out of reach. run_case degraded b "" degraded "" $ALL; go -check "trial on a degraded state: known from the ESP, not committed, forgotten, rebooted" "${RESULT%%:*}|$(cat "$KTEST/esp/EFI/BOOT/BOOTX64.EFI")|$CALLS" "trial-unhealthy b|kernel-a|mount -t vfat -o ro,nosuid,nodev,noexec /dev/vda1 $KTEST/run/esp umount $KTEST/run/esp efiboot forget efiboot ensure a reboot " +check "trial on a degraded state: known from the ESP, not committed, forgotten, rebooted" "${RESULT%%:*}|$(cat "$KTEST/esp/EFI/BOOT/BOOTX64.EFI")|$CALLS" "trial-unhealthy b|kernel-a|mount -t vfat -o ro,nosuid,nodev,noexec /dev/vda1 $KTEST/run/esp umount $KTEST/run/esp efiboot keep a reboot " run_case degraded2 b "" degraded "" $ALL; : > "$KTEST/efiboot_fails"; go check "... not rebooted while its entries stay: nothing else keeps the next boot from being it" "$(reboots)" "0" run_case degraded3 a "" degraded "" $ALL; go @@ -202,20 +205,20 @@ check "back on the old slot with BootNext consumed: trial-failed" "$RESULT" "tri check "the record moved to trial.failed" "$([[ -e "$KTEST/boot/trial.failed" ]] && cat "$KTEST/boot/trial.failed" | head -1)" "b" check "... saying it never came up to be judged" "$(sed -n 's/^why=//p' "$KTEST/boot/trial.failed")" "did not come up to be judged" check "BOOTX64.EFI untouched" "$(cat "$KTEST/esp/EFI/BOOT/BOOTX64.EFI")" "kernel-a" -check "the failed trial's entries are forgotten, and the committed slot gets its own" "$CALLS" "efiboot list efiboot forget efiboot ensure a " +check "the failed trial's entries are forgotten, and the committed slot gets its own" "$CALLS" "efiboot list efiboot keep a " check "... before its record is set aside" "$(cat "$KTEST/forget-saw" 2>/dev/null)" "kept" run_case untried a "" persistent 'b\narmed=1\n' $ALL printf 'BootNext: BootB0B0\nBootOrder: Boot0001 BootB0B0\n BootB0B0: Kryptik slot b file=\\EFI\\kryptik\\kryptik-b.efi [active]\n' > "$KTEST/efilist"; go check "BootNext still naming the trial's entry: the firmware never tried it, and the record says so" \ "$RESULT|$(sed -n 's/^why=//p' "$KTEST/boot/trial.failed")" \ 'trial-failed b|was never started: the firmware left BootNext set, so it may ignore a one-time boot request (after --retry, pick "Kryptik slot b" in its boot menu)' -check "... and its entries and BootNext are forgotten as for any failed trial" "$CALLS" "efiboot list efiboot forget efiboot ensure a " +check "... and its entries and BootNext are forgotten as for any failed trial" "$CALLS" "efiboot list efiboot keep a " run_case untried2 a "" persistent 'b\narmed=1\n' $ALL printf 'BootNext: Boot0003\n Boot0003: Windows Boot Manager file=\\EFI\\Microsoft\\Boot\\bootmgfw.efi [active]\n' > "$KTEST/efilist"; go check "a BootNext naming another system's entry says nothing about the trial" "$(sed -n 's/^why=//p' "$KTEST/boot/trial.failed")" "did not come up to be judged" run_case interrupted a "" persistent 'b\narmed=0\n' $ALL; go check "old slot with an armed=0 record: arming was interrupted, nothing failed" "$RESULT" "arming-interrupted b" -check "the interrupted arming's entry is forgotten, and the committed slot gets its own" "$CALLS" "efiboot forget efiboot ensure a " +check "the interrupted arming's entry is forgotten, and the committed slot gets its own" "$CALLS" "efiboot keep a " check "... before its record goes" "$(cat "$KTEST/forget-saw" 2>/dev/null)" "kept" check "no trial.failed for an interruption" "$([[ -e "$KTEST/boot/trial.failed" ]] && echo present || echo none)" "none" run_case legacy a "" persistent 'b\n' $ALL; go diff --git a/tools/tests/efiboot.sh b/tools/tests/efiboot.sh index 0709787f..174c9952 100755 --- a/tools/tests/efiboot.sh +++ b/tools/tests/efiboot.sh @@ -43,6 +43,8 @@ opt() { printf '\\x01\\x00\\x00\\x00\\x%02x\\x%02x%s\\x04\\x04\\x%02x\\x%02x%s\\x7f\\xff\\x04\\x00' \ $(( (fl + 4) & 255 )) $(( (fl + 4) >> 8 )) "$(ucs2 "$1")" $(( fl & 255 )) $(( fl >> 8 )) "$(ucs2 "$2")" } +# netopt DESC: an active load option that boots from the network (a MAC node), as a printf format. +netopt() { printf '\\x01\\x00\\x00\\x00\\x29\\x00%s\\x03\\x0b\\x25\\x00%s\\x01\\x7f\\xff\\x04\\x00' "$(ucs2 "$1")" "$(printf '\\x00%.0s' $(seq 32))"; } KA="$(opt 'Kryptik slot a' '\EFI\kryptik\kryptik-a.efi')" KB="$(opt 'Kryptik slot b' '\EFI\kryptik\kryptik-b.efi')" WIN="$(opt 'Windows Boot Manager' '\EFI\Microsoft\Boot\bootmgfw.efi')" @@ -123,6 +125,42 @@ rm -f "$V"/* efiboot ensure b /dev/sdz9 > "$T/out" 2>&1; rc=$? check "a partition without the kryptik-esp label is refused, and nothing is written" "$rc|$(grep -c 'is not a kryptik-esp partition' "$T/out")|$(find "$V" -type f | wc -l)" "1|1|0" +echo "-- a new entry goes ahead of network boot, never ahead of another system's" +NET="$(netopt 'IPv4 Network')" +rm -f "$V"/*; var Boot0000 "$WIN"; var Boot0001 "$MACHINE"; var Boot0002 "$NET"; var Boot0003 "$NET" +var BootOrder '\x00\x00\x01\x00\x02\x00\x03\x00' +efiboot ensure a > "$T/out" 2>&1; rc=$? +check "ensure succeeds" "$rc" "0" +check "slot a's entry goes after the disks and ahead of both network entries" "$(data BootOrder)" "00000100a00002000300" +rm -f "$V"/*; var Boot0000 "$WIN"; var Boot0002 "$NET"; var BootOrder '\x02\x00\x00\x00' +efiboot ensure a > "$T/out" 2>&1 +check "but behind another system's entry listed after the network" "$(data BootOrder)" "02000000a000" +rm -f "$V"/*; var Boot0002 "$NET"; var BootOrder '\x02\x00' +efiboot ensure a > "$T/out" 2>&1 +check "and first in an order of network boot alone" "$(data BootOrder)" "a0000200" + +echo "-- keep: the committed slot's entry where Kryptik's stood" +# The trial's entry went in after slot a's; committed, slot b takes the place slot a had. +rm -f "$V"/*; var Boot0001 "$MACHINE"; var Boot0002 "$NET"; var Boot00A0 "$KA"; var BootOrder '\x01\x00\xa0\x00\x02\x00' +efiboot set-next b > "$T/out" 2>&1 +check "a trial's entry goes behind the committed one, ahead of the network" "$(data BootOrder)" "0100a000b0000200" +efiboot keep b > "$T/out" 2>&1; rc=$? +check "keep succeeds" "$rc" "0" +check "slot b's entry stands where slot a's did, and slot a's is gone" "$(data BootOrder)|$(data Boot00A0)|$(data BootNext)" "0100b0000200|absent|absent" +check "slot b's entry is Boot00B0" "$(efiboot list | grep -cF 'Boot00B0: Kryptik slot b file=\EFI\kryptik\kryptik-b.efi')" "1" +# Moved first in the firmware's setup, it stays first. +rm -f "$V"/*; var Boot0000 "$WIN"; var Boot0001 "$MACHINE"; var Boot00A0 "$KA"; var BootOrder '\xa0\x00\x00\x00\x01\x00' +efiboot keep a > "$T/out" 2>&1 +check "an entry the user moved first stays first" "$(data BootOrder)" "a00000000100" +rm -f "$V"/*; var Boot0001 "$MACHINE"; var Boot0002 "$NET"; var BootOrder '\x01\x00\x02\x00' +efiboot keep a > "$T/out" 2>&1 +check "with no Kryptik entry in the order it goes where ensure puts one" "$(data BootOrder)" "0100a0000200" +rm -f "$V"/*; var Boot0001 "$MACHINE"; var Boot00A0 "$KA"; var BootOrder '\x01\x00\xa0\x00' +efiboot keep b /dev/sdz9 > "$T/out" 2>&1; rc=$? +check "an entry that cannot be made exits 3, with Kryptik's others forgotten" "$rc|$(data BootOrder)|$(data Boot00A0)" "3|0100|absent" +efiboot keep c > "$T/out" 2>&1; rc=$? +check "a slot other than a or b is refused before anything is forgotten" "$rc|$(data BootOrder)" "2|0100" + echo "-- an order longer than 254 entries" # 300 of the machine's own numbers, 0x1000 up: past the 508 bytes a 512-byte read held. rm -f "$V"/*; long=""; want="" diff --git a/tools/update/kryptik-recover b/tools/update/kryptik-recover index e7492de9..51d1bc03 100755 --- a/tools/update/kryptik-recover +++ b/tools/update/kryptik-recover @@ -150,10 +150,15 @@ commit_slot() { # commit_slot SLOT (ESP mounted rw at /run/kryptik-recover) esp_put /run/kryptik-recover/kryptik/committed-slot "$s" || die "cannot record slot $s as committed" sync say "committed: BOOTX64.EFI is now slot $s ($(esp_version "/run/kryptik-recover/kryptik/version-$s"))" - # As boot-success does: forget Kryptik's firmware entries, then give the committed slot its own. + # As boot-success does: forget Kryptik's firmware entries, then give the committed slot its + # own where theirs stood. if [ -d /sys/firmware/efi/efivars ]; then - kryptik-efiboot forget >/dev/null 2>&1 || say "the firmware's Kryptik entries could not be removed; its own order may still name the other slot" - kryptik-efiboot ensure "$s" "$ESP" >/dev/null 2>&1 || say "slot $s has no firmware entry of its own; BOOTX64.EFI is the only way to it" + rc=0; kryptik-efiboot keep "$s" "$ESP" >/dev/null 2>&1 || rc=$? + case $rc in + 0) ;; + 3) say "slot $s has no firmware entry of its own; BOOTX64.EFI is the only way to it" ;; + *) say "the firmware's Kryptik entries could not be removed; its own order may still name the other slot" ;; + esac fi }