Page 1 of 1

Getting meaty skeleton to run on bare metal

Posted: Mon Jul 22, 2024 3:26 pm
by Ban1p7
I made my OS based on the "meaty skeleton" example, and it was all working great on qemu, but I tried burning the .iso to a USB the same way I did to install linux (cat my_file.iso > /dev/sdx). I was then met with the error message:

Code: Select all

error: invalid arch-independent ELF magic.
I tried the same with the default meaty skeleton, just cloning the repository, running iso.sh and burning it to a USB, and got the exact same error message, so I don't think I've messed anything up adding to the code.
I've looked online for this sort of error message, and it seems like there's something wrong with the boot.s file that defines the magic number and other information for grub to boot from, but this doesn't explain why it runs in a VM fine.
Thus, I'm guessing it's a hardware issue, especially since I'm using a modern x86_64 laptop, not an old x86 32 bit computer, though most things in osdev seem to be backwards compatible, so I don't think that should be an issue... (?)
This is my first OS so I've never had to mess about getting grub to work before, so I'd appreciate help with finding what's causing the error, especially since it's happening for me even on the meaty skeleton example.
I'm using i686-elf-gcc version 14.1.0, i686-elf-as version 2.42 and grub version 2.06, if that's significant. I don't believe any of those are outdated.

Re: Getting meaty skeleton to run on bare metal

Posted: Mon Jul 22, 2024 4:32 pm
by Octocontrabass
Ban1p7 wrote: Mon Jul 22, 2024 3:26 pmit was all working great on qemu,
When you run it in QEMU, what type of disk are you telling QEMU to emulate? The tutorial includes a script to tell QEMU to emulate a CD (with the "-cdrom" option), but you might see something different if you tell QEMU to emulate a hard disk (with the "-hda" option) instead.

If you see the same problem in QEMU, there's probably something wrong with your copy of GRUB. It's pretty common to be missing pieces if you installed it through a package manager (and you can install those missing pieces using the same package manager).
Ban1p7 wrote: Mon Jul 22, 2024 3:26 pm(cat my_file.iso > /dev/sdx)
Not "/dev/sdx1" or anything like that, right?
Ban1p7 wrote: Mon Jul 22, 2024 3:26 pmThus, I'm guessing it's a hardware issue, especially since I'm using a modern x86_64 laptop, not an old x86 32 bit computer, though most things in osdev seem to be backwards compatible, so I don't think that should be an issue... (?)
A modern x86-64 laptop probably has UEFI and might have completely dropped support for legacy BIOS. QEMU still uses legacy BIOS by default. (UEFI also requires different pieces of GRUB that you might be missing.)

Re: Getting meaty skeleton to run on bare metal

Posted: Tue Jul 23, 2024 4:00 am
by Ban1p7
Octocontrabass wrote: Mon Jul 22, 2024 4:32 pm but you might see something different if you tell QEMU to emulate a hard disk (with the "-hda" option) instead.
I was doing -cdrom but -hda didn't seem to make a difference on qemu, though it did throw this warning (it works anyway though, I'm guessing it's only bad if the .iso is not raw, which mine apparently is).

Code: Select all

WARNING: Image format was not specified for 'klerbos.iso' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
Octocontrabass wrote: Mon Jul 22, 2024 4:32 pm It's pretty common to be missing pieces if you installed it through a package manager
What pieces would be missing? I'd have thought that it wouldn't be able to properly build the .iso if it had missing pieces?
Octocontrabass wrote: Mon Jul 22, 2024 4:32 pm Not "/dev/sdx1" or anything like that, right?
Yeah, I didn't specify a single partition, just the whole /dev/sdx
Octocontrabass wrote: Mon Jul 22, 2024 4:32 pm A modern x86-64 laptop probably has UEFI and might have completely dropped support for legacy BIOS. QEMU still uses legacy BIOS by default.
That sounds like it's likely the problem. Is there a way to disable the legacy BIOS in qemu? I've looked online, and it listed a bunch of -smbios options, and

Code: Select all

-smbios type=0 uefi=on
looks like it would make it UEFI only, but it gives this message and doesn't seem to start a VM.

Code: Select all

qemu-system-i386: uefi=on: drive with bus=0, unit=0 (index=0) exists
It looks like it has turned on UEFI; does this mean the legacy BIOS is disabled, or is there something else I need to disable that? And if legacy BIOS is disabled, is the VM not starting because I don't yet support UEFI boot or is it because I've misconfigured something in QEMU?

Re: Getting meaty skeleton to run on bare metal

Posted: Tue Jul 23, 2024 11:41 am
by Octocontrabass
Ban1p7 wrote: Tue Jul 23, 2024 4:00 amWhat pieces would be missing? I'd have thought that it wouldn't be able to properly build the .iso if it had missing pieces?
GRUB is secretly a whole bunch of separate bootloaders in a trenchcoat. The piece for legacy BIOS boot from a hard disk is separate from the piece for legacy BIOS boot from a CD, and both are separate from the pieces for x64 UEFI boot. When you build the ISO, grub-mkrescue assumes you know which pieces you need for the PC you want to boot, so it doesn't complain when some are missing.

I'd need to know how you got your copy of GRUB to tell you how to make sure you have all the pieces you might need.
Ban1p7 wrote: Tue Jul 23, 2024 4:00 amIs there a way to disable the legacy BIOS in qemu?
The only way to disable it is to replace it with OVMF. (That might also be a separate package you need to install.) If you have a combined OVMF.fd file, you can pass "-bios path/to/OVMF.fd" to boot in UEFI mode. If you have separate OVMF_CODE.fd and OVMF_VARS.fd files, it's a bit more complicated. Make a copy of OVMF_VARS.fd, then use something like this:

Code: Select all

-drive if=pflash,format=raw,unit=0,file=path/to/OVMF_CODE.fd,readonly=on
-drive if=pflash,format=raw,unit=1,file=OVMF_VARS_copy.fd
Keep in mind the tutorial uses VGA text mode, which basically doesn't exist under UEFI. You might be better off poking around in the laptop's firmware setup to see if it has a CSM or legacy boot option you might have missed.

It's also entirely possible that the laptop is already using legacy boot and you've just hit a firmware bug. You could try GRUB 2.12 and see if it has a workaround.

Re: Getting meaty skeleton to run on bare metal

Posted: Tue Jul 23, 2024 12:58 pm
by Ban1p7
Octocontrabass wrote: Tue Jul 23, 2024 11:41 am I'd need to know how you got your copy of GRUB to tell you how to make sure you have all the pieces you might need.
I just installed linux mint and it was there. I probably updated it with apt get upgrade at some point, because

Code: Select all

sudo apt list | grep grub
lists a whole bunch of grub stuff that apt could update.
Octocontrabass wrote: Tue Jul 23, 2024 11:41 am You might be better off poking around in the laptop's firmware setup to see if it has a CSM or legacy boot option you might have missed.
I've checked and it doesn't seem like there is any sort of option like that. Turns out the boot menu to select which drive to boot from says UEFI at the top, so UEFI booting a legacy BIOS OS is almost certainly the issue. I can't find any option to change it from UEFI anywhere, and it doesn't mention CSM or legacy boot.
Octocontrabass wrote: Tue Jul 23, 2024 11:41 am If you have a combined OVMF.fd file
I'm probably being silly, but the wiki says I should be able to install OVMF by using apt, and apt says I already have it, but where do I get OVMF.fd or OVMF_anythingelse.fd? It looks like using it is documented in its wiki, but it doesn't say where to find OVMF.fd. I tried

Code: Select all

find . "OVMF*.fd"
which said I didn't have it, so I don't think I already have it. (?)
Octocontrabass wrote: Tue Jul 23, 2024 11:41 am Keep in mind the tutorial uses VGA text mode, which basically doesn't exist under UEFI.
I'm probably gonna restart based on the UEFI example once I get UEFI emulation configured on QEMU. I think my PS/2 keyboard driver is going to be the only thing I can salvage. (My firmware setup had a USB emulation setting, which I'm pretty sure means my laptop's keyboard and USB mouse should be able to work as PS/2 equivalents, which look a lot simpler to drive than USB.)

Re: Getting meaty skeleton to run on bare metal

Posted: Tue Jul 23, 2024 3:30 pm
by Octocontrabass
Ban1p7 wrote: Tue Jul 23, 2024 12:58 pmI just installed linux mint and it was there.
The only one you might be missing is grub-efi-amd64-bin, since that's the only one you haven't tested in QEMU yet.
Ban1p7 wrote: Tue Jul 23, 2024 12:58 pmI'm probably being silly, but the wiki says I should be able to install OVMF by using apt, and apt says I already have it, but where do I get OVMF.fd or OVMF_anythingelse.fd?
The combined single-file version should be available as "/usr/share/ovmf/OVMF.fd" or "/usr/share/qemu/OVMF.fd". If you'd rather use the split files, there should be a few different versions in /usr/share/OVMF/.
Ban1p7 wrote: Tue Jul 23, 2024 12:58 pmI'm probably gonna restart based on the UEFI example once I get UEFI emulation configured on QEMU.
That example is for writing a UEFI application, like a bootloader. Your kernel should not be a UEFI application. If you want to write a 64-bit kernel without writing a bootloader or using GRUB, Limine is a good choice.

Re: Getting meaty skeleton to run on bare metal

Posted: Wed Jul 24, 2024 5:33 am
by Ban1p7
Octocontrabass wrote: Tue Jul 23, 2024 3:30 pm grub-efi-amd64-bin
I ran

Code: Select all

sudo apt list | grep grub
again and it listed grub-efi-amd64-bin/jammy-updates, so I think I have it
Octocontrabass wrote: Tue Jul 23, 2024 3:30 pm should be available as "/usr/share/ovmf/OVMF.fd"
Thanks a tonne!
Octocontrabass wrote: Tue Jul 23, 2024 3:30 pm That example is for writing a UEFI application, like a bootloader. Your kernel should not be a UEFI application
Yeah lol, I just saw "You must have fully understood the Bare Bones tutorial first" and C code and assumed it was the same thing but for UEFI.
Octocontrabass wrote: Tue Jul 23, 2024 3:30 pm If you want to write a 64-bit kernel without writing a bootloader or using GRUB, Limine is a good choice.

I probably would have used GRUB, but Limine just instantly for exactly what I want it to do. Definitely gonna switch to Limine.
I think that's all, thanks so much for your help, and sorry for being a bit silly :D