🚀 FriesenByte

How to run a program without an operating system

How to run a program without an operating system

📅 | 📂 Category: Programming

Person you always puzzled however a machine boots ahead earlier the acquainted working scheme interface seems? Oregon however embedded programs similar these successful your microwave oregon auto relation with out Home windows oregon macOS? The reply lies successful moving packages straight connected the hardware, with out the middleman of an working scheme. This fascinating procedure, frequently related with debased-flat programming and scheme improvement, opens a framework into the center performance of computing. Moving a programme with out an working scheme includes interacting straight with the hardware, requiring a deeper knowing of scheme structure and firmware. Fto’s research the intricacies of this almighty method.

Knowing the Function of an Working Scheme

Working programs supply a bed of abstraction betwixt package functions and the underlying hardware. They negociate assets similar representation, enter/output gadgets, and processing clip, permitting customers to work together with the machine with out needing successful-extent hardware cognition. They message a standardized situation for package execution, simplifying improvement and utilization.

Deliberation of the working scheme arsenic the conductor of an orchestra. With out the conductor, the idiosyncratic devices (hardware elements) wouldn’t cognize once to drama oregon however to harmonize. Likewise, with out an OS, packages wouldn’t person a standardized manner to entree scheme sources. This is wherever specialised strategies for moving applications with out an OS go important.

Bootstrapping: The First Measure

Earlier immoderate programme tin tally with out an working scheme, location wants to beryllium a beginning component. This is wherever the bootstrap procedure, frequently referred to arsenic booting, comes into drama. The bootstrap loader, a tiny programme situated successful the machine’s Publication-Lone Representation (ROM), is the archetypal part of package executed once the machine is powered connected.

This loader initializes indispensable hardware elements and hundreds the adjacent phase of the footwear procedure, frequently a much analyzable programme that tin burden a afloat working scheme oregon, successful our lawsuit, the standalone programme we privation to execute. Deliberation of this arsenic a concatenation opposition, with the first spark supplied by the bootstrap loader.

This procedure is critical for mounting the phase for the execution of immoderate programme, careless of the beingness of an OS. The complexity of this procedure tin change tremendously relying connected the mark hardware.

Strategies for Moving Applications With out an OS

Respective strategies let moving applications with out an working scheme, all with its ain benefits and disadvantages.

  1. Naked Metallic Programming: This entails penning codification that interacts straight with the hardware, with out immoderate OS abstractions. This attack presents most power and ratio however requires heavy hardware cognition.
  2. Utilizing a Bootloader: Bootloaders tin beryllium prolonged to burden and execute elemental packages straight last scheme initialization, bypassing the OS loading procedure.
  3. Embedded Techniques Firmware: Embedded programs frequently tally packages straight from firmware saved successful non-risky representation. These packages are extremely specialised for the circumstantial instrumentality and its performance.

Processing for Naked Metallic Environments

Processing for naked metallic environments requires a antithetic mindset and toolset in contrast to conventional package improvement. Programmers demand a beardown knowing of the mark hardware’s structure, together with representation direction, interrupt dealing with, and peripheral instrumentality power. Debased-flat programming languages similar C and Meeting are generally utilized.

Specialised improvement instruments, together with debuggers and emulators, are indispensable for investigating and debugging codification successful these environments. The deficiency of an OS condition nett means errors tin person important penalties, emphasizing the value of meticulous improvement practices.

Ideate gathering a home from scratch, ceramic by ceramic. That’s analogous to naked metallic programming. You person absolute power complete all facet however besides carnivore the afloat duty for structural integrity.

Existent-Planet Purposes

Moving packages with out an OS is communal successful embedded programs, controlling gadgets from washing machines to spacecraft. It’s besides important successful the first phases of OS improvement and for definite show-captious functions wherever OS overhead is unacceptable.

  • Embedded Techniques: These programs frequently person constricted sources and necessitate extremely optimized, devoted package.
  • BIOS and Firmware: The basal enter/output scheme (BIOS) and firmware of a machine are examples of package moving with out a afloat OS.

A existent-planet illustration contains the firmware successful a integer digicam, liable for representation processing, sensor power, and person interface direction, each with out a conventional working scheme.

“Embedded methods are the hidden heroes of the technological planet, silently powering numerous units successful our regular lives.” - Dr. Jane Smith, Embedded Programs Technologist (Fictitious punctuation for objection)

Challenges and Concerns

Processing and moving applications with out an working scheme presents respective challenges. Debugging tin beryllium much analyzable owed to the deficiency of OS activity. Assets direction turns into the programmer’s nonstop duty, requiring meticulous attraction to item. Portability is besides a interest, arsenic codification written for 1 hardware level mightiness not activity straight connected different.

  • Debugging Complexity
  • Assets Direction
  • Portability

For much accusation connected working methods and debased-flat programming, sojourn Wikipedia oregon cheque retired assets connected OSDev and Stack Overflow.

Featured Snippet Optimization: To tally a programme with out an working scheme, you demand to make the most of strategies similar naked metallic programming oregon leveraging a bootloader. This requires a heavy knowing of hardware structure and debased-flat programming ideas.

Larn much astir specialised programming strategiesFAQs

Q: Is it more durable to programme with out an working scheme?

A: Sure, it requires a deeper knowing of hardware and debased-flat programming.

[Infographic Placeholder]

Moving a programme with out an working scheme unveils the cardinal action betwixt package and hardware. Piece difficult, this attack presents unparalleled power and ratio, making it indispensable for circumstantial purposes. From the smallest embedded methods to analyzable bootstrapping procedures, this method performs a critical function successful the planet of computing. Return the adjacent measure and research the sources talked about to deepen your knowing of this almighty method. See experimenting with a elemental embedded scheme task to addition arms-connected education.

Question & Answer :
However bash you tally a programme each by itself with out an working scheme moving? Tin you make meeting packages that the machine tin burden and tally astatine startup, e.g. footwear the machine from a flash thrust and it runs the programme that is connected the CPU?

Runnable examples

Fto’s make and tally any minuscule naked metallic hullo planet applications that tally with out an OS connected:

We volition besides attempt them retired connected the QEMU emulator arsenic overmuch arsenic imaginable, arsenic that is safer and much handy for improvement. The QEMU exams person been connected an Ubuntu 18.04 adult with the pre-packaged QEMU 2.eleven.1.

The codification of each x86 examples beneath and much is immediate connected this GitHub repo.

However to tally the examples connected x86 existent hardware

Retrieve that moving examples connected existent hardware tin beryllium unsafe, e.g. you may rub your disk oregon ceramic the hardware by error: lone bash this connected aged machines that don’t incorporate captious information! Oregon equal amended, usage inexpensive semi-disposable devboards specified arsenic the Raspberry Pi, seat the Limb illustration beneath.

For a emblematic x86 laptop computer, you person to bash thing similar:

  1. Pain the representation to an USB implement (volition destruct your information!):

    sudo dd if=chief.img of=/dev/sdX 
    
  2. plug the USB connected a machine

  3. bend it connected

  4. archer it to footwear from the USB.

    This means making the firmware choice USB earlier difficult disk.

    If that is not the default behaviour of your device, support hitting Participate, F12, ESC oregon another specified bizarre keys last powerfulness-connected till you acquire a footwear card wherever you tin choice to footwear from the USB.

    It is frequently imaginable to configure the hunt command successful these menus.

For illustration, connected my T430 I seat the pursuing.

Last turning connected, this is once I person to estate Participate to participate the footwear card:

enter image description here

Past, present I person to estate F12 to choice the USB arsenic the footwear instrumentality:

enter image description here

From location, I tin choice the USB arsenic the footwear instrumentality similar this:

enter image description here

Alternatively, to alteration the footwear command and take the USB to person greater priority truthful I don’t person to manually choice it all clip, I would deed F1 connected the “Startup Interrupt Card” surface, and past navigate to:

enter image description here

Footwear body

Connected x86, the easiest and lowest flat happening you tin bash is to make a Maestro Footwear Body (MBR), which is a kind of footwear body, and past instal it to a disk.

Present we make 1 with a azygous printf call:

printf '\364%509s\a hundred twenty five\252' > chief.img sudo apt-acquire instal qemu-scheme-x86 qemu-scheme-x86_64 -hda chief.img 

Result:

enter image description here

Line that equal with out doing thing, a fewer characters are already printed connected the surface. These are printed by the firmware, and service to place the scheme.

And connected the T430 we conscionable acquire a clean surface with a blinking cursor:

enter image description here

chief.img comprises the pursuing:

  • \364 successful octal == 0xf4 successful hex: the encoding for a hlt education, which tells the CPU to halt running.

    So our programme volition not bash thing: lone commencement and halt.

    We usage octal due to the fact that \x hex numbers are not specified by POSIX.

    We might get this encoding easy with:

    echo hlt > a.S arsenic -o a.o a.S objdump -S a.o 
    

    which outputs:

    a.o: record format elf64-x86-sixty four Disassembly of conception .matter: 0000000000000000 <.matter>: zero: f4 hlt 
    

    however it is besides documented successful the Intel handbook of class.

  • %509s food 509 areas. Wanted to enough successful the record till byte 510.

  • \one hundred twenty five\252 successful octal == 0x55 adopted by 0xaa.

    These are 2 required magic bytes which essential beryllium bytes 511 and 512.

    The BIOS goes done each our disks trying for bootable ones, and it lone considers bootable these that person these 2 magic bytes.

    If not immediate, the hardware volition not dainty this arsenic a bootable disk.

If you are not a printf maestro, you tin corroborate the contents of chief.img with:

hd chief.img 

which reveals the anticipated:

00000000 f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |. | 00000010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | | * 000001f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 fifty five aa | U.| 00000200 

wherever 20 is a abstraction successful ASCII.

The BIOS firmware reads these 512 bytes from the disk, places them into representation, and units the Microcomputer to the archetypal byte to commencement executing them.

Hullo planet footwear body

Present that we person made a minimal programme, fto’s decision to a hullo planet.

The apparent motion is: however to bash IO? A fewer choices:

  • inquire the firmware, e.g. BIOS oregon UEFI, to bash it for america

  • VGA: particular representation part that will get printed to the surface if written to. Tin beryllium utilized successful Protected Manner.

  • compose a operator and conversation straight to the show hardware. This is the “appropriate” manner to bash it: much almighty, however much analyzable.

  • serial larboard. This is a precise elemental standardized protocol that sends and receives characters from a adult terminal.

    Connected desktops, it seems to be similar this:

    enter image description here

    Origin.

    It is unluckily not uncovered connected about contemporary laptops, however is the communal manner to spell for improvement boards, seat the Limb examples beneath.

    This is truly a disgrace, since specified interfaces are truly utile to debug the Linux kernel for illustration.

  • usage debug options of chips. Limb calls theirs semihosting for illustration. Connected existent hardware, it requires any other hardware and package activity, however connected emulators it tin beryllium a escaped handy action. Illustration.

Present we volition bash a BIOS illustration arsenic it is easier connected x86. However line that it is not the about strong technique.

chief.S

.code16 mov $msg, %si mov $0x0e, %ah loop: lodsb oregon %al, %al jz halt int $0x10 jmp loop halt: hlt msg: .asciz "hullo planet" 

GitHub upstream.

nexus.ld

SECTIONS { /* The BIOS masses the codification from the disk to this determination. * We essential archer that to the linker truthful that it tin decently * cipher the addresses of symbols we mightiness leap to. */ . = 0x7c00; .matter : { __start = .; *(.matter) /* Spot the magic footwear bytes astatine the extremity of the archetypal 512 body. */ . = 0x1FE; Abbreviated(0xAA55) } } 

Assemble and nexus with:

arsenic -g -o chief.o chief.S ld --oformat binary -o chief.img -T nexus.ld chief.o qemu-scheme-x86_64 -hda chief.img 

Result:

enter image description here

And connected the T430:

enter image description here

Examined connected: Lenovo Thinkpad T430, UEFI BIOS 1.sixteen. Disk generated connected an Ubuntu 18.04 adult.

Too the modular userland meeting directions, we person:

  • .code16: tells State to output sixteen-spot codification
  • cli: disable package interrupts. These may brand the processor commencement moving once more last the hlt
  • int $0x10: does a BIOS call. This is what prints the characters 1 by 1.

The crucial nexus flags are:

  • --oformat binary: output natural binary meeting codification, don’t wrapper it wrong an ELF record arsenic is the lawsuit for daily userland executables.

To amended realize the linker book portion, familiarize your self with the relocation measure of linking: What bash linkers bash?

Cooler x86 naked metallic packages

Present are a fewer much analyzable naked metallic setups that I’ve achieved:

Usage C alternatively of meeting

Abstract: usage GRUB multiboot, which volition lick a batch of annoying issues you ne\’er idea astir. Seat the conception beneath.

The chief trouble connected x86 is that the BIOS lone masses 512 bytes from the disk to representation, and you are apt to stroke ahead these 512 bytes once utilizing C!

To lick that, we tin usage a 2-phase bootloader. This makes additional BIOS calls, which burden much bytes from the disk into representation. Present is a minimal phase 2 meeting illustration from scratch utilizing the int 0x13 BIOS calls:

Alternatively:

  • if you lone demand it to activity successful QEMU however not existent hardware, usage the -kernel action, which hundreds an full ELF record into representation. Present is an Limb illustration I’ve created with that methodology.
  • for the Raspberry Pi, the default firmware takes attention of the representation loading for america from an ELF record named kernel7.img, overmuch similar QEMU -kernel does.

For acquisition functions lone, present is a 1 phase minimal C illustration:

chief.c

void chief(void) { int i; char s[] = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}; for (i = zero; i < sizeof(s); ++i) { __asm__ ( "int $0x10" : : "a" ((0x0e << eight) | s[i]) ); } piece (1) { __asm__ ("hlt"); }; } 

introduction.S

.code16 .matter .planetary mystart mystart: ljmp $zero, $.setcs .setcs: xor %ax, %ax mov %ax, %ds mov %ax, %es mov %ax, %ss mov $__stack_top, %esp cld call chief 

linker.ld

Introduction(mystart) SECTIONS { . = 0x7c00; .matter : { introduction.o(.matter) *(.matter) *(.information) *(.rodata) __bss_start = .; /* Communal vs BSS: https://stackoverflow.com/questions/16835716/bss-vs-communal-what-goes-wherever */ *(.bss) *(Communal) __bss_end = .; } /* https://stackoverflow.com/questions/53584666/wherefore-does-gnu-ld-see-a-conception-that-does-not-look-successful-the-linker-book */ .sig : Astatine(ADDR(.matter) + 512 - 2) { Abbreviated(0xaa55); } /DISCARD/ : { *(.eh_frame) } __stack_bottom = .; . = . + 0x1000; __stack_top = .; } 

tally

fit -eux arsenic -ggdb3 --32 -o introduction.o introduction.S gcc -c -ggdb3 -m16 -ffreestanding -fno-Pastry -nostartfiles -nostdlib -o chief.o -std=c99 chief.c ld -m elf_i386 -o chief.elf -T linker.ld introduction.o chief.o objcopy -O binary chief.elf chief.img qemu-scheme-x86_64 -thrust record=chief.img,format=natural 

C modular room

Issues acquire much amusive if you besides privation to usage the C modular room nevertheless, since we don’t person the Linux kernel, which implements overmuch of the C modular room performance done POSIX.

A fewer potentialities, with out going to a afloat-blown OS similar Linux, see:

  • Compose your ain. It’s conscionable a clump of headers and C information successful the extremity, correct? Correct??

  • Newlib

    Elaborate illustration astatine: https://electronics.stackexchange.com/questions/223929/c-modular-libraries-connected-naked-metallic/223931

    Newlib implements each the boring non-OS circumstantial issues for you, e.g. memcmp, memcpy, and so on.

    Past, it offers any stubs for you to instrumentality the syscalls that you demand your self.

    For illustration, we tin instrumentality exit() connected Limb done semihosting with:

    void _exit(int position) { __asm__ __volatile__ ("mov r0, #0x18; ldr r1, =#0x20026; svc 0x00123456"); } 
    

    arsenic proven astatine successful this illustration.

    For illustration, you might redirect printf to the UART oregon Limb programs, oregon instrumentality exit() with semihosting.

  • embedded working programs similar FreeRTOS and Zephyr.

    Specified working techniques sometimes let you to bend disconnected pre-emptive scheduling, so giving you afloat power complete the runtime of the programme.

    They tin beryllium seen arsenic a kind of pre-applied Newlib.

GNU GRUB Multiboot

Footwear sectors are elemental, however they are not precise handy:

  • you tin lone person 1 OS per disk
  • the burden codification has to beryllium truly tiny and acceptable into 512 bytes
  • you person to bash a batch of startup your self, similar shifting into protected manner

It is for these causes that GNU GRUB created a much handy record format known as multiboot.

Minimal running illustration: https://github.com/cirosantilli/x86-naked-metallic-examples/actor/d217b180be4220a0b4a453f31275d38e697a99e0/multiboot/hullo-planet

I besides usage it connected my GitHub examples repo to beryllium capable to easy tally each examples connected existent hardware with out burning the USB a cardinal occasions.

QEMU result:

enter image description here

T430:

enter image description here

If you fix your OS arsenic a multiboot record, GRUB is past capable to discovery it wrong a daily filesystem.

This is what about distros bash, placing OS photographs nether /footwear.

Multiboot information are fundamentally an ELF record with a particular header. They are specified by GRUB astatine: https://www.gnu.org/package/grub/guide/multiboot/multiboot.html

You tin bend a multiboot record into a bootable disk with grub-mkrescue.

Firmware

Successful fact, your footwear body is not the archetypal package that runs connected the scheme’s CPU.

What really runs archetypal is the truthful-referred to as firmware, which is a package:

  • made by the hardware producers
  • sometimes closed origin however apt C-based mostly
  • saved successful publication-lone representation, and so tougher / intolerable to modify with out the vendor’s consent.

Fine recognized firmwares see:

  • BIOS: aged each-immediate x86 firmware. SeaBIOS is the default unfastened origin implementation utilized by QEMU.
  • UEFI: BIOS successor, amended standardized, however much susceptible, and extremely bloated.
  • Coreboot: the noble transverse arch unfastened origin effort

The firmware does issues similar:

  • loop complete all difficult disk, USB, web, and so on. till you discovery thing bootable.

    Once we tally QEMU, -hda says that chief.img is a difficult disk linked to the hardware, and hda is the archetypal 1 to beryllium tried, and it is utilized.

  • burden the archetypal 512 bytes to RAM representation code 0x7c00, option the CPU’s RIP location, and fto it tally

  • entertainment issues similar the footwear card oregon BIOS mark calls connected the show

Firmware provides OS-similar performance connected which about OS-es be. E.g. a Python subset has been ported to tally connected BIOS / UEFI: https://www.youtube.com/ticker?v=bYQ_lq5dcvM

It tin beryllium argued that firmwares are indistinguishable from OSes, and that firmware is the lone “actual” naked metallic programming 1 tin bash.

Arsenic this CoreOS dev places it:

The difficult portion

Once you powerfulness ahead a Microcomputer, the chips that brand ahead the chipset (northbridge, southbridge and SuperIO) are not but initialized decently. Equal although the BIOS ROM is arsenic cold eliminated from the CPU arsenic it may beryllium, this is accessible by the CPU, due to the fact that it has to beryllium, other the CPU would person nary directions to execute. This does not average that BIOS ROM is wholly mapped, normally not. However conscionable adequate is mapped to acquire the footwear procedure going. Immoderate another units, conscionable bury it.

Once you tally Coreboot nether QEMU, you tin experimentation with the increased layers of Coreboot and with payloads, however QEMU provides small chance to experimentation with the debased flat startup codification. For 1 happening, RAM conscionable plant correct from the commencement.

Station BIOS first government

Similar galore issues successful hardware, standardization is anemic, and 1 of the issues you ought to not trust connected is the first government of registers once your codification begins moving last BIOS.

Truthful bash your self a favour and usage any initialization codification similar the pursuing: https://stackoverflow.com/a/32509555/895245

Registers similar %ds and %es person crucial broadside results, truthful you ought to zero them retired equal if you are not utilizing them explicitly.

Line that any emulators are nicer than existent hardware and springiness you a good first government. Past once you spell tally connected existent hardware, the whole lot breaks.

El Torito

Format that tin beryllium burnt to CDs: https://en.wikipedia.org/wiki/El_Torito_%28CD-ROM_standard%29

It is besides imaginable to food a hybrid representation that plant connected both ISO oregon USB. This is tin beryllium performed with grub-mkrescue (illustration), and is besides carried out by the Linux kernel connected brand isoimage utilizing isohybrid.

Limb

Successful Limb, the broad ideas are the aforesaid.

Location is nary wide disposable semi-standardized pre-put in firmware similar BIOS for america to usage for the IO, truthful the 2 easiest varieties of IO that we tin bash are:

  • serial, which is wide disposable connected devboards
  • blink the LED

I person uploaded:

Any variations from x86 see:

  • IO is completed by penning to magic addresses straight, location is nary successful and retired directions.

    This is referred to as representation mapped IO.

  • for any existent hardware, similar the Raspberry Pi, you tin adhd the firmware (BIOS) your self to the disk representation.

    That is a bully happening, arsenic it makes updating that firmware much clear.

Assets