系统环境:
OS:CentOS Stream release 9 (cmd: cat /etc/redhat-release)
Linux Kernel:Linux 5.14.0-142.el9.x86_64 (cmd: uname -a)
1. Bochs virtual machine
Bochs
It is an open source virtual machine software, and it provides related debugging functions that are beneficial to system development. If you are also a reader who studies the operating system or is interested in Linux
the kernel , then this software will help you better debug and improve the development content.
Download link:
Bochs official website: https://bochs.sourceforge.io/
Sourceforge download: https://sourceforge.net/projects/bochs/files/bochs/
Github download: https://github.com/stlintel/Bochs
2. Compile and install Bochs
The author chooses to download sourceforge
on
2.2. Decompress the source package
After downloading, you need to decompress the source package.
[imaginemiracle@imos-ws Downloads]$ tar -zxvf bochs-2.7.tar.gz
2.3. Install dependencies and configure Bochs
Install related dependencies, otherwise configuration or compilation will lack dependencies first.
[imaginemiracle@imos-ws bochs-2.7]$ sudo yum install gcc-c++
[imaginemiracle@imos-ws bochs-2.7]$ sudo yum install gtk2-devel
[imaginemiracle@imos-ws bochs-2.7]$ sudo yum install readline-devel
[imaginemiracle@imos-ws bochs-2.7]$ sudo yum install xorg-x11-server-Xdmx.x86_64
2.3.1. Use configure to generate Makefile
Enter the source code directory after decompression, and use configure
to configure the source code. Here, the author uses --prefix
to configure the installation directory to a specified location. Readers who do not need to specify do not need to add this parameter.
[imaginemiracle@imos-ws bochs-2.7]$ ./configure --prefix=/pgm/bochs --with-x11 --with-wx --enable-debugger --enable-all-optimizations --enable-readline --enable-long-phy-address --enable-ltdl-install --enable-idle-hack --enable-a20-pin --enable-x86-64 --enable-smp --enable-cpu-level=6 --enable-large-ramfile --enable-repeat-speedups --enable-fast-function-calls --enable-handlers-chaining --enable-trace-linking --enable-configurable-msrs --enable-show-ips --enable-cpp --enable-debugger-gui --enable-iodebug --enable-logging --enable-assert-checks --enable-fpu --enable-vmx=2 --enable-svm --enable-3dnow --enable-alignment-check --enable-monitor-mwait --enable-avx --enable-evex --enable-x86-debugger --enable-pci --enable-usb --enable-voodoo
2.3.2. make compile
After the configuration does not report an error, it can be compiled directly.
[imaginemiracle@imos-ws bochs-2.7]$ make
2.3.3. 报错:No rule to make target ‘parser.cc’, needed by ‘parser.o’.
You may encounter errors like this or similar when compiling:
make[1]: *** No rule to make target 'parser.cc', needed by 'parser.o'. Stop.
2.3.4. Solve the problem of no parser.cc
Just copy the corresponding .cpp
file as .cc
a file:
[imaginemiracle@imos-ws bochs-2.7]$ cp bx_debug/parser.cpp bx_debug/parser.cc
[注]:不要疑问要怎么找到复制文件的目录所在。出错行紧后会提示从出错目录退出,这个目录便是缺少文件的目录,需要复制文件的目录也就在这里了。
2.3.5. Continue to make, continue to report an error: fatal error: config.h: No such file or directory
Continue to execute make
the command , but there will be such an error again: the prompt is not foundconfig.h
cd bx_debug && \
make libdebug.a
make[1]: Entering directory '/home/imaginemiracle/Downloads/bochs-2.7/bx_debug'
g++ -g -O2 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -c -o parser.o parser.cc
In file included from parser.y:8:
debug.h:25:10: fatal error: config.h: No such file or directory
25 | #include "config.h"
| ^~~~~~~~~~
compilation terminated.
make[1]: *** [<builtin>: parser.o] Error 1
make[1]: Leaving directory '/home/imaginemiracle/Downloads/bochs-2.7/bx_debug'
make: *** [Makefile:327: bx_debug/libdebug.a] Error 2
2.3.6. Solve the problem of not finding config.h
About this header file, we can see it in the main directory of the source code
[imaginemiracle@imos-ws bochs-2.7]$ ls
aclocal.m4 bxversion.rc configure gui ltdl.c osdep.cpp README
bios bxversion.rc.in configure.in host ltdlconf.h osdep.h README-wxWidgets
bochs.h CHANGES COPYING install-sh ltdlconf.h.in param_names.h TESTFORM.txt
build config.cpp cpu instrument ltmain.sh PARAM_TREE.txt TODO
bx_debug config.guess cpudb.h iodev main.cpp patches win32_enh_dbg.rc
bxdisasm.cpp config.h crc.cpp libtool Makefile pc_system.cpp win32res.rc
bxthread.cpp config.h.in doc LICENSE Makefile.in pc_system.h wxbochs.rc
bxthread.h config.log docs-html logio.cpp memory plugin.cpp
bxversion.h config.status extplugin.h logio.h misc plugin.h
bxversion.h.in config.sub gdbstub.cpp ltdl-bochs.h msrs.def qemu-queue.h
Open error file
[imaginemiracle@imos-ws bochs-2.7]$ vim bx_debug/debug.h
Just modify the in the 25
line to be#include "config.h"
.#include "../config.h"
2.3.7. Continue to make, continue to report errors: fatal error: osdep.h: No such file or directory and fatal error: cpu/decoder/decoder.h: No such file or directory
There are two other errors similar to the previous one, which can also be found in the main directory osdep.h
, so open bx_debug/debug.h
and #include “osdep.h”
change to , and modify #include "../osdep.h"
the 36
line of .#include "cpu/decoder/decoder.h"
2.3.8. Continue to make, if it is correct, it will report an error: No rule to make target 'misc/bximage.cc', needed by 'misc/bximage.o' and a series of such problems
This type of problem is similar to the first error, and there will be a corresponding .cpp
file , .cc
just copy it as a file.
[imaginemiracle@imos-ws bochs-2.7]$ cp misc/bximage.cpp misc/bximage.cc
[imaginemiracle@imos-ws bochs-2.7]$ cp iodev/hdimage/hdimage.cpp iodev/hdimage/hdimage.cc
[imaginemiracle@imos-ws bochs-2.7]$ cp iodev/hdimage/vmware3.cpp iodev/hdimage/vmware3.cc
[imaginemiracle@imos-ws bochs-2.7]$ cp iodev/hdimage/vmware4.cpp iodev/hdimage/vmware4.cc
[imaginemiracle@imos-ws bochs-2.7]$ cp iodev/hdimage/vpc.cpp iodev/hdimage/vpc.cc
[imaginemiracle@imos-ws bochs-2.7]$ cp iodev/hdimage/vbox.cpp iodev/hdimage/vbox.cc
2.3.9. Continue to make and install after make install
make
There should be nothing wrong with executing at this time , execute make install
the command to install.
[imaginemiracle@imos-ws bochs-2.7]$ make install
Check the installation directory. If you are using the default installation directory, you can type it directly boch
and use tab
the key to complete it.
[imaginemiracle@imos-ws ~]$ ls /pgm/bochs/bin/
bochs bximage
OK, if you see an executable file generated here, it means bochs
that it is installed.
3. Make a floppy disk image file
The one we just Bochs
installed comes with a bximage
tool called , which is used to create virtual machine images.
Let’s look at the specific usage method:
(1) Execute directly bximage
(if it is installed to a specified directory like the author, you first need to add it to ~/.bashrc
the file and update the environment variable source ~/.bashrc
, for example: export PATH=$PATH:/pgm/bochs/bin
)
[imaginemiracle@imos-ws img]$ bximage
========================================================================
bximage
Disk Image Creation / Conversion / Resize and Commit Tool for Bochs
$Id: bximage.cc 14091 2021-01-30 17:37:42Z sshwarts $
========================================================================
1. Create new floppy or hard disk image
2. Convert hard disk image to other format (mode)
3. Resize hard disk image
4. Commit 'undoable' redolog to base image
5. Disk image info
0. Quit
Please choose one [0] 1
(2) Input here 1
means to choose to create a floppy disk or hard disk, press Enter and the following content will appear;
Create image
Do you want to create a floppy disk image or a hard disk image?
Please type hd or fd. [hd] fd
(3) Here let us choose whether the type of creation is a floppy disk ( fd
) or a hard disk ( hd
), enter here fd
and Enter;
Choose the size of floppy disk image to create.
Please type 160k, 180k, 320k, 360k, 720k, 1.2M, 1.44M, 1.68M, 1.72M, or 2.88M.
[1.44M]
(4) Next, you will be asked to choose the size of the created floppy disk, the default value is 1.44MB
, just press Enter here to use the default value;
What should be the name of the image?
[a.img] imboot.img
Creating floppy image 'imboot.img' with 2880 sectors
The following line should appear in your bochsrc:
floppya: image="imboot.img", status=inserted
(5) In the next step, it will ask what is the name of the created floppy disk. The default name is a.img
the name entered by the author here imboot.img
, because this floppy disk will be used as a boot
program .
ok, here the floppy disk image file is created.
4. Bochs operating environment configuration
Bochs
A configuration file for our reference will be provided in the installation directory /pgm/bochs/share/doc/bochs/bochsrc-sample.txt
of , copy a copy to the local for reference or directly copy the author's configuration.
# Configuration file generated by Bochs
#=======================================================================
plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, parallel=1, serial=1, iodebug=1
config_interface: textconfig
#display_library: wx
display_library: x
romimage: file=$BXSHARE/BIOS-bochs-latest, options=fastboot
romimage: file=/pgm/bochs/share/bochs/BIOS-bochs-latest
vgaromimage: file=/pgm/bochs/share/bochs/VGABIOS-lgpl-latest
# choose the boot disk
boot: floppy
floppy_bootsig_check: disabled=0
floppya: type=1_44, 1_44=/home/imaginemiracle/Miracle/Projects/imOS/img/imboot.img, status=inserted, write_protected=0
cpu: model=corei7_haswell_4770, count=1:1:1, ips=50000000, reset_on_triple_fault=1, ignore_bad_msrs=1, msrs="msrs.def"
cpu: cpuid_limit_winnt=0
cpuid: x86_64=1, mmx=1, level=6, sep=1, simd=sse4_2, apic=xapic, aes=1, movbe=1, xsave=1, apic=x2apic, sha=1, adx=1, xsaveopt=1, avx_f16c=1, avx_fma=1, bmi=bmi2, 1g_pages=1, pcid=1, fsgsbase=1, smep=1, smap=1, mwait=1, vmx=1
cpuid: family=6, model=0x1a, stepping=5, vendor_string="GenuineIntel", brand_string="Intel(R) Core(TM) i7-4770 CPU (Haswell)"
#memory: guest=512, host=256
pci: enabled=1, chipset=i440fx
vga: extension=vbe, update_freq=5
#voodoo: enabled=1, model=voodoo1
keyboard: type=mf, serial_delay=250, paste_delay=100000, user_shortcut=none
mouse: enabled=0, type=ps2, toggle=ctrl+mbutton
clock: sync=none, time0=local, rtc_sync=0
#cmosimage: file=cmos.img, rtc_init=time0
private_colormap: enabled=0
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
ata0-master: type=none
ata0-slave: type=none
ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15
ata1-master: type=none
ata1-slave: type=none
ata2: enabled=0, ioaddr1=0x1e8, ioaddr2=0x3e0, irq=11
ata3: enabled=0, ioaddr1=0x168, ioaddr2=0x360, irq=9
boot: floppy
#boot: disk
floppy_bootsig_check: disabled=0
#log: /dev/null
log: bochsout.txt
logprefix: %t%e%d
panic: action=ask
error: action=report
info: action=report
debug: action=ignore, pci=report # report BX_DEBUG from module 'pci'
debugger_log: -
#com1: enabled=1, mode=term, dev=/dev/ttyp9
parport1: enabled=1, file="parport.out"
#sound: driver=default, waveout=/dev/dsp. wavein=, midiout=
speaker: enabled=1, mode=system
parport1: enabled=1, file=none
parport2: enabled=0
com1: enabled=1, mode=null
com2: enabled=0
com3: enabled=0
com4: enabled=0
#e1000: enabled=1, mac=52:54:00:12:34:56, ethmod=slirp, script=slirp.conf
magic_break: enabled=0
#debug_symbols: file="kernel.sym"
print_timestamps: enabled=1
port_e9_hack: enabled=0
private_colormap: enabled=0
megs: 2048
It should be noted that there is floppya
a configuration of , the image size and file path here are bximage
created by the tool before imboot.img
, and the path and size here must be written correctly, otherwise it will not start.
floppya: type=1_44, 1_44=/home/imaginemiracle/Miracle/Project/imOS/img/imboot.img, status=inserted, write_protected=0
5. Write a simple Boot program
Create and open a new file , and write code basedimboot.S
on :BIOS
boot
org 0x7c00 ; BIOS 会加载引导程序到内存地址 0x7c00 处
BaseOfStack equ 0x7c00 ; 定义一个标识符 BaseOfStack,代表值 0x7c00
Start:
mov ax, cs ; 将代码寄存器 cs 的段基地址设置到 ds、es、ss寄存器
mov ds, ax
mov es, ax
mov ss, ax
mov sp, BaseOfStack
; 清屏
mov ax, 0600h
mov bx, 0700h
mov cx, 0h
mov dx, 0184fh
int 10h
; 设置光标位置
mov ax, 0200h
mov bx, 0000h
mov dx, 0000h
int 10h
; 在屏幕上打印字符串 "The imboot is working!"
mov ax, 1301h
mov bx, 000fh
mov dx, 0000h
mov cx, 22
push ax
mov ax, ds
mov es, ax
pop ax
mov bp, IMBootMessage
int 10h
; 软盘驱动器复位
xor ah, ah
xor dl, dl
int 13h
jmp $
IMBootMessage: dd "The imboot is working!"
; 填充 0 到结尾前 2 个字节
times 510 - ($ - $$) db 0
dw 0xaa55 ; BIOS会检测软盘的第 0 磁头第 1 扇区最后两个字节是否为 0x55aa(代码中写成 0xaa55 是由于x86 使用小端存储),以确定这个扇区是否是引导扇区
This assembly code mainly uses the BIOS
interrupt service routine int 10h
and the function of int 13h
completing screen operation and driver reset.
The same is true for using different functions by changing the register that int 10h
holds the main function number.ah
int 13h
interrupt service | Main function number AH Value |
Functional description | AL Function |
BH Function |
BL Function |
CH Function |
CL Function |
DH Function |
DL Function |
ES:BP |
---|---|---|---|---|---|---|---|---|---|---|
INT 10h |
02h |
set cursor position | page number | - | - | - | - | the column number of the cursor | the line number of the cursor | - |
INT 10h |
06h |
Scroll the window by the specified range | The number of scrolling columns; means 0 to clear the screen, and other attributes are invalid at this time |
Set the attribute color attribute of the empty position after scrolling : * bit 0~2: font color (0: black 1: blue 2: green 3: green 4: red 5: purple 6: brown 7: white) * bit 3: font Brightness (0: default 1: highlight) * bit 4~6: background color (the value of the color is the same as above) * bit 7: font flashing (0: off 1: enable) |
- | The coordinate column number of the upper left corner of the scrolling range | Coordinate line number of the upper left corner of the scrolling range | The coordinate column number of the lower right corner of the scroll range | The coordinate line number of the lower right corner of the scroll range | - |
INT 10h |
13h |
display a line of string | Write mode: 00h : the string property is BL controlled by and the length CX is controlled by (unit: Byte), the cursor position is not changed; 01h : same as above, but the display ends and the cursor is updated to the end of the string; 02h : the string is controlled by its last byte, CX Change the control unit Word , do not change the cursor position; 03h : same 02h , but update the cursor position to the end of the string |
page number | character attributes, same as06h |
string length | string length | The line number where the cursor is located | The column number where the cursor is located | The memory address of the string to be displayed |
INT 13h |
00h |
reset disk drive | - | - | - | - | - | - | 00h : first floppy drive ( drive A: ); 01h : second floppy drive ( drive B: ); … 7fh : 128th floppy drive; 80h : first hard drive; … ffh : 128th hard drive |
- |
6. Run the Boot program on Bochs
6.1. Download and install NASM compiler
We have written the code above, but it has not been compiled yet. The compiler will be used for NASM
compilation , so let’s install it first.
Install other dependencies first: if it is Ubuntu
a system , you can use it sudo apt-get install build-essential
directly
# Ubuntu 系统
imaginemiracle:~$ sudo apt-get install build-essential
imaginemiracle:~$ sudo apt-get install curl
# CentOS 系统
[imaginemiracle@imos-ws imboot]$ sudo yum install make automake gcc gcc-c++ kernel-devel
[imaginemiracle@imos-ws imboot]$ sudo yum install -y curl
Download and install nasm
the compiler .
[imaginemiracle@imos-ws Downloads]$ curl -O -L https://www.nasm.us/pub/nasm/releasebuilds/2.15/nasm-2.15.tar.gz
[imaginemiracle@imos-ws Downloads]$ tar -zxvf nasm-2.15.tar.gz
[imaginemiracle@imos-ws Downloads]$ cd nasm-2.15/
[imaginemiracle@imos-ws nasm-2.15]$ ./autogen.sh
[imaginemiracle@imos-ws nasm-2.15]$ ./configure --prefix=/pgm/nasm
[imaginemiracle@imos-ws nasm-2.15]$ make install
Add nasm
to the environment variable vim ~/.bashrc
, add the following content
export PATH=$PATH:/pgm/bochs/bin:/pgm/nasm/bin
update environment variables
source ~/.bashrc
6.2. Compile the program and burn it into the floppy disk
Compile the code with nasm
the tools boot
:
[imaginemiracle@imos-ws imboot]$ nasm imboot.S -o imboot.bin
[imaginemiracle@imos-ws imboot]$ ls
imboot.bin imboot.S
Compilation will generate a binary file, burn this file into the previously made floppy imboot.img
disk :
[imaginemiracle@imos-ws img]$ dd if=../imboot/imboot.bin of=./imboot.img bs=512 count=1 conv=notrunc
1+0 records in
1+0 records out
512 bytes copied, 0.000104768 s, 4.9 MB/s
Open the image file imboot.img
to check whether the programming or code writing is correct.
[imaginemiracle@imos-ws img]$ vim imboot.img
Because the file is a binary file, you will see "garbled characters" after opening it. It doesn't matter, you can use %!xxd
the tool convert it to 16
binary for viewing.
After conversion, we can see the following content.
What we need to check is mainly whether the last two bytes of the first sector of the floppy disk are 0x55aa
, which marks whether it is the end of the boot sector. A sector is bytes in 512
size , and here there are 16
bytes in a row, so bytes 511
and 512
should appear at the end of 32
row . ( 512 / 16 = 32
)
ok, it seems that our code and programming are ok, we can continue to the next step.
7. Start our Boot code with Bochs
Use bochs
the command and specify the configuration file as the previously written configuration file.
[imaginemiracle@imos-ws bochs-run]$ bochs -f bochsrc
After running, there will be the following output:
========================================================================
Bochs x86 Emulator 2.7
Built from SVN snapshot on August 1, 2021
Timestamp: Sun Aug 1 10:07:00 CEST 2021
========================================================================
00000000000i[ ] BXSHARE not set. using compile time default '/pgm/bochs/share/bochs'
00000000000i[ ] reading configuration from bochsrc
00000000000i[ ] Ignoring magic break points
------------------------------
Bochs Configuration: Main Menu
------------------------------
This is the Bochs Configuration Interface, where you can describe the
machine that you want to simulate. Bochs has already searched for a
configuration file (typically called bochsrc.txt) and loaded it if it
could be found. When you are satisfied with the configuration, go
ahead and start the simulation.
You can also start bochs with the -q option to skip these menus.
1. Restore factory default configuration
2. Read options from...
3. Edit options
4. Save options to...
5. Restore the Bochs state from...
6. Begin simulation
7. Quit now
Please choose one: [6]
The default choice here is 6
to start running the image file we gave, so just enter and press Enter here.
At this time, you should see a black screen interface. At this time, you need to continue to enter c
or continue
to tell bochs
Run boot
the program.
At this time, you can see that the string in the code The imboot is working!
has been printed out, which means that Boot
the program has been executed.
#End of this article
At this point you have BIOS
mastered boot
the workflow of the and program, as well as the actual simulation on the virtual machine.
If you think this article is helpful to you, please leave a like^v^*
Please respect the author, and please indicate the source when reprinting! Thank you for your cooperation~
[Author]: Imagine Miracle
[Copyright]: This work is licensed under the "Attribution-Non-Commercial Use-Share Alike 4.0 International" license agreement.
[Link to this article]: https://blog.csdn.net/qq_36393978/article/details/126260487