Bug 273814

Summary: sysutils/grub2-bhyve: fails to boot OpenBSD Current kernel
Product: Ports & Packages Reporter: Yuichiro NAITO <naito.yuichiro>
Component: Individual Port(s)Assignee: Hiroki Sato <hrs>
Status: Closed FIXED    
Severity: Affects Some People CC: bsd, freebsd-bugs, grahamperrin, halcyonblue, hrs, osa, sascha.folie
Priority: --- Keywords: bhyve
Version: Latest   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
grub2-bhyve.patch
none
grub2-bhyve.patch.2
none
grub2-bhyve.patch.3 none

Description Yuichiro NAITO 2023-09-15 07:58:35 UTC
Created attachment 244872 [details]
grub2-bhyve.patch

By the following commit, OpenBSD Current kernel has dropped supporting old
console device structure. It causes grub-bhyve not to boot the OpenBSD Current
kernel. The new release 7.4 is coming soon will also unbootable from the
current grub2-bhyve.

https://github.com/Openbsd/src/commit/745c2f60e98fd1f418c104960a567e120624d705

I wrote a patch for this problem that changes to use the new console device
structure by default. And my patch switches to use the old console device
structure if the loading kernel is older than '7.3' release.

The version number is written in the value of 'osrelease' symbol in the OpenBSD
kernel. My patch reads it to see if the kernel is older than '7.3' or not.
All the previous releases contain symbol tables, so no problem. If users strip
the OpenBSD kernel, reading the version number will fail. I added 'kopenbsd -l'
option to force to use the old console device structure. If users boot the
OpenBSD Current or newer kernel, just 'kopenbsd' command works fine although
the kernel is stripped. Because the new console device structure is chosen by
default.

Implementation Note: (record for maintainers)

Reading symbol tables is already implemented in 'grub_openbsd_find_ramdisk'
function. I added reading the value of 'osrelease' symbol in this function to
share the symbol lookup code. And building console device structure is
implemented in 'grub_cmd_openbsd' function, but it is called earlier than
'grub_openbsd_find_ramdisk' function. So 'grub_cmd_openbsd' function never
knows the version number. I changed to build both the new and old console device
structures in 'grub_cmd_openbsd'. They are added to 'tag' list and
'grub_openbsd_boot' function reads the 'tag' list and build stack frame that is
passed to OpenBSD kernel. I changed the building stack frame code to ignore
unnecessary console device structure checked by the 'osrelease' value.

I also added 'openbsd_force_legacy_console' global variable. It's a flag to
force to use the old console device structure. This flag is set by '-l' option.

Testing: 

My poudriere build successfully finished with my patch. And I confirmed my patch
booted OpenBSD kernel from 6.9 to 7.3 and Current successfully. And also the
installers of OpenBSD 6.9 - 7.3 was booted successfully.

Please apply the attached 'grub2-bhyve.patch' to Ports tree.
Comment 1 Hiroki Sato freebsd_committer freebsd_triage 2023-09-15 09:27:27 UTC
Take
Comment 2 Yuichiro NAITO 2023-12-04 07:23:12 UTC
I updated my patch to simplify the logic. The new logic always allocates the new
console boot parameter. If the loading kernel is prior to 7.3, the console boot parameter is replaced by the legacy one. The new one is bigger than the legacy one.
So, it doesn't need to reallocate the memory for the legacy boot parameter.
It just rearranges the memory image.

And I also fixed a "use after free" bug in the previous patch.
Comment 3 Yuichiro NAITO 2023-12-04 07:23:44 UTC
Created attachment 246762 [details]
grub2-bhyve.patch.2
Comment 4 bsd 2024-01-05 17:01:11 UTC
Can this be merged, please?
I can confirm that OpenBSD 7.4 boots in bhyve after this patch is applied.
Comment 5 Yuichiro NAITO 2024-01-06 02:15:22 UTC
Created attachment 247472 [details]
grub2-bhyve.patch.3
Comment 6 Yuichiro NAITO 2024-01-06 02:19:59 UTC
Hi, my previous patch has been committed in the upstream repository. I updated
grub2-bhyve patch to refer to the commit. This change minimizes the patch for 
the Ports tree.

Please apply the `grub2-bhyve.patch.3`. Thank you.
Comment 7 commit-hook freebsd_committer freebsd_triage 2024-01-07 12:34:03 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/ports/commit/?id=c918e3f780ddd4cc6a0b6650854a1a10fe96644f

commit c918e3f780ddd4cc6a0b6650854a1a10fe96644f
Author:     Hiroki Sato <hrs@FreeBSD.org>
AuthorDate: 2024-01-07 12:32:30 +0000
Commit:     Hiroki Sato <hrs@FreeBSD.org>
CommitDate: 2024-01-07 12:32:30 +0000

    sysutils/grub2-bhyve: Add patches to fix loading OpenBSD kernel

    PR:             273814
    Submitted by:   Yuichiro NAITO

 sysutils/grub2-bhyve/Makefile | 5 +++--
 sysutils/grub2-bhyve/distinfo | 4 +++-
 2 files changed, 6 insertions(+), 3 deletions(-)
Comment 8 Hiroki Sato freebsd_committer freebsd_triage 2024-01-07 12:34:32 UTC
Committed.