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.
- 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.
- Utilizing a Bootloader: Bootloaders tin beryllium prolonged to burden and execute elemental packages straight last scheme initialization, bypassing the OS loading procedure.
- 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:
- an x86 Lenovo Thinkpad T430 laptop computer with UEFI BIOS 1.sixteen firmware
- an Limb-based mostly Raspberry Pi three
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:
-
Pain the representation to an USB implement (volition destruct your information!):
sudo dd if=chief.img of=/dev/sdX
-
plug the USB connected a machine
-
bend it connected
-
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:
Past, present I person to estate F12 to choice the USB arsenic the footwear instrumentality:
From location, I tin choice the USB arsenic the footwear instrumentality similar this:
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:
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:
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:
chief.img
comprises the pursuing:
-
\364
successful octal ==0xf4
successful hex: the encoding for ahlt
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 by0xaa
.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:
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"
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:
And connected the T430:
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 codificationcli
: disable package interrupts. These may brand the processor commencement moving once more last thehlt
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:
- multicore: What does multicore meeting communication expression similar?
- paging: However does x86 paging activity?
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??
-
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 instrumentalityexit()
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:
T430:
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 thatchief.img
is a difficult disk linked to the hardware, andhda
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:
-
a fewer elemental QEMU C + Newlib and natural meeting examples present connected GitHub.
The punctual.c illustration for illustration takes enter from your adult terminal and offers backmost output each done the simulated UART:
participate a quality obtained: a fresh alloc of 1 bytes astatine code 0x0x4000a1c0 participate a quality bought: b fresh alloc of 2 bytes astatine code 0x0x4000a1c0 participate a quality
Seat besides: However to brand naked metallic Limb applications and tally them connected QEMU?
-
a full automated Raspberry Pi blinker setup astatine: https://github.com/cirosantilli/raspberry-pi-naked-metallic-blinker
Seat besides: However to tally a C programme with nary OS connected the Raspberry Pi?
To “seat” the LEDs connected QEMU you person to compile QEMU from origin with a debug emblem: https://raspberrypi.stackexchange.com/questions/56373/is-it-imaginable-to-acquire-the-government-of-the-leds-and-gpios-successful-a-qemu-emulation-similar-t
Adjacent, you ought to attempt a UART hullo planet. You tin commencement from the blinker illustration, and regenerate the kernel with this 1: https://github.com/dwelch67/raspberrypi/actor/bce377230c2cdd8ff1e40919fdedbc2533ef5a00/uart01
Archetypal acquire the UART running with Raspbian arsenic I’ve defined astatine: https://raspberrypi.stackexchange.com/questions/38/fix-for-ssh-with out-a-surface/54394#54394 It volition expression thing similar this:
Brand certain to usage the correct pins, oregon other you tin pain your UART to USB converter, I’ve completed it doubly already by abbreviated circuiting crushed and 5V…
Eventually link to the serial from the adult with:
surface /dev/ttyUSB0 115200
For the Raspberry Pi, we usage a Micro SD paper alternatively of an USB implement to incorporate our executable, for which you usually demand an adapter to link to your machine:
Don’t bury to unlock the SD adapter arsenic proven astatine: https://askubuntu.com/questions/213889/microsd-paper-is-fit-to-publication-lone-government-however-tin-i-compose-information-connected-it/814585#814585
https://github.com/dwelch67/raspberrypi seems similar the about fashionable naked metallic Raspberry Pi tutorial disposable present.
Any variations from x86 see:
-
IO is completed by penning to magic addresses straight, location is nary
successful
andretired
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
- http://wiki.osdev.org is a large origin for these issues.
- https://github.com/scanlime/metalkit is a much automated / broad naked metallic compilation scheme, that offers a small customized API