xref: /aosp_15_r20/external/coreboot/Documentation/tutorial/part1.md (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1Tutorial, part 1: Starting from scratch
2===========================================
3
4This tutorial will guide you through the process of setting up a working
5coreboot toolchain. In same cases you will find specific instructions
6for Debian (apt-get), Fedora (dnf) and Arch Linux (pacman) based package
7management systems. Use the instructions according to your system.
8
9To test the toolchain and make sure it works, we will build coreboot for
10an emulated system provided by QEMU. This allows you to get familiar
11with the general process of configuring and building coreboot without
12needing to flash any hardware.
13
14**IMPORTANT:**
15**Do not attempt to flash the coreboot ROM built here to a real board**
16
17coreboot is board specific, so a ROM built for one board model (such as
18the QEMU emulation boards) cannot be expected to work on a different
19board. You must reconfigure coreboot for your board and rebuild the ROM
20before flashing it to a physical system.
21
22**Note: Summaries of each of the steps are at the end of the document.**
23
24
25Download, configure, and build coreboot
26---------------------------------------
27
28
29### Step 1 - Install tools and libraries needed for coreboot
30
31Debian based distros:
32`sudo apt-get install -y bison build-essential curl flex git gnat
33libncurses-dev libssl-dev zlib1g-dev pkgconf`
34
35Arch based distros:
36`sudo pacman -S base-devel curl git gcc-ada ncurses zlib`
37
38Redhat based distros:
39`sudo dnf install git make gcc-gnat flex bison xz bzip2 gcc g++
40ncurses-devel wget zlib-devel patch`
41
42
43### Step 2 - Download coreboot source tree
44
45```Bash
46git clone https://review.coreboot.org/coreboot
47cd coreboot
48```
49
50
51
52### Step 3 - Build the coreboot toolchain
53
54Please note that this can take a significant amount of time. Use `CPUS=`
55to specify number of `make` jobs to run in parallel.
56
57This will list toolchain options and supported architectures:
58
59```Bash
60make help_toolchain
61```
62
63Here are some examples:
64
65```Bash
66make crossgcc-i386 CPUS=$(nproc)       # build i386 toolchain
67make crossgcc-aarch64 CPUS=$(nproc)    # build Aarch64 toolchain
68make crossgcc-riscv CPUS=$(nproc)      # build RISC-V toolchain
69```
70
71Note that the i386 toolchain is currently used for all x86 platforms,
72including x86_64. For this tutorial we only need the i386 toolchain.
73
74Also note that you can possibly use your system toolchain, but the
75results are not reproducible, and may have issues, so this is not
76recommended.  See step 5 to use your system toolchain.
77
78
79### Step 4 - Build the payload - coreinfo
80
81```Bash
82make -C payloads/coreinfo olddefconfig
83make -C payloads/coreinfo
84```
85
86
87### Step 5 - Configure the build
88
89#### Configure your mainboard
90
91```Bash
92make menuconfig
93```
94
95Do the next steps in the menu:
96
97```Text
98select 'Mainboard' menu
99Beside 'Mainboard vendor' should be '(Emulation)'
100Beside 'Mainboard model' should be 'QEMU x86 i440fx/piix4'
101select < Exit >
102```
103
104These should be the default selections, so if anything else was set, run
105`make distclean` to remove your old config file and start over.
106
107#### Optionally use your system toolchain (Again, not recommended)
108
109```Text
110select 'General Setup' menu
111select 'Allow building with any toolchain'
112select < Exit >
113```
114
115#### Select the payload
116
117```Text
118select 'Payload' menu
119select 'Payload to add (SeaBIOS) --->'
120choose 'An ELF executable payload'
121select 'Payload path and filename'
122enter 'payloads/coreinfo/build/coreinfo.elf'
123select < Exit >
124select < Exit >
125select < Yes >
126```
127
128#### Check your configuration (optional step):
129
130```Bash
131make savedefconfig
132cat defconfig
133```
134
135There should only be 9 lines (or 10 if you're using the system
136toolchain):
137
138```Text
139CONFIG_CBFS_SIZE=0x00400000
140CONFIG_CONSOLE_CBMEM_BUFFER_SIZE=0x20000
141CONFIG_SUBSYSTEM_VENDOR_ID=0x0000
142CONFIG_SUBSYSTEM_DEVICE_ID=0x0000
143CONFIG_I2C_TRANSFER_TIMEOUT_US=500000
144CONFIG_CONSOLE_QEMU_DEBUGCON_PORT=0x402
145CONFIG_POST_IO_PORT=0x80
146CONFIG_PAYLOAD_ELF=y
147CONFIG_PAYLOAD_FILE="payloads/coreinfo/build/coreinfo.elf"
148```
149
150Note that this may differ depending on the revision of the coreboot
151source you are building from and should not be taken as the required
152contents of defconfig.
153
154### Step 6 - Build coreboot
155
156```Bash
157make
158```
159
160At the end of the build, you should see:
161
162`Built emulation/qemu-i440fx (QEMU x86 i440fx/piix4)`
163
164This means your build was successful. The output from the build is in
165the `build` directory. `build/coreboot.rom` is the full rom file.
166
167
168Test the image using QEMU
169-------------------------
170
171
172### Step 7 - Install QEMU
173
174* Debian: `sudo apt-get install -y qemu-system`
175* Arch: `sudo pacman -S qemu`
176* Redhat: `sudo dnf install qemu`
177
178
179### Step 8 - Run QEMU
180
181Start QEMU, and point it to the ROM you just built:
182
183```Bash
184qemu-system-x86_64 -bios build/coreboot.rom -serial stdio
185```
186
187You should see the serial output of coreboot in the original console
188window, and a new window will appear running the coreinfo payload.
189
190
191Summary
192-------
193
194
195### Step 1 summary - Install tools and libraries needed for coreboot
196
197Depending on your distribution you have installed the minimum additional
198software requirements to continue with downloading and building
199coreboot.  Not every distribution has the tools, that would be required,
200installed by default. In the following we shortly introduce the purpose
201of the installed packages:
202
203* `build-essential` or `base-devel` are the basic tools for building software.
204* `git` is needed to download coreboot from the coreboot git repository.
205* `libncurses-dev` or `ncurses` is needed to build the menu for 'make menuconfig'
206* `m4, bison, curl, flex, zlib1g-dev, gcc, gnat` and `g++` or `clang`
207are needed to build the coreboot toolchain. `gcc` and `gnat` have to be
208of the same version.
209* `libssl-dev, pkgconf` are needed to build coreboot image (Step 6).
210In particular, `libcrypto` provided by `libssl-dev` package.
211
212If you started with a different distribution or package management
213system you might need to install other packages. Most likely they are
214named slightly different. If that is the case for you, we'd like to
215encourage you to contribute to the project and submit a pull request
216with an update for this documentation for your system.
217
218
219### Step 2 summary - Download coreboot source tree
220
221This will download a 'read-only' copy of the coreboot tree. This just
222means that if you made changes to the coreboot tree, you couldn't
223immediately contribute them back to the community. To pull a copy of
224coreboot that would allow you to contribute back, you would first need
225to sign up for an account on gerrit.
226
227
228### Step 3 summary - Build the coreboot toolchain.
229
230This builds one of the coreboot cross-compiler toolchains for X86
231platforms.  Because of the variability of compilers and the other
232required tools between the various operating systems that coreboot can
233be built on, coreboot supplies and uses its own cross-compiler toolchain
234to build the binaries that end up as part of the coreboot ROM. The
235toolchain provided by the operating system (the 'host toolchain') is
236used to build various tools that will run on the local system during the
237build process.
238
239
240### Step 4 summary - Build the payload
241
242To actually do anything useful with coreboot, you need to build a
243payload to include into the rom. The idea behind coreboot is that it
244does the minimum amount possible before passing control of the machine
245to a payload. There are various payloads such as grub or SeaBIOS that
246are typically used to boot the operating system. Instead, we used
247coreinfo, a small demonstration payload that allows the user to look at
248various things such as memory and the contents of the coreboot file
249system (CBFS) - the pieces that make up the coreboot rom.
250
251Usually, the coreboot build system automatically builds the payload
252selected in the "Payload to add" menu and sets it as the default payload
253(also known as the "primary payload"). Such payloads are able to boot an
254operating system and may be able to load another payload. Although
255coreinfo can be found in the "Secondary Payloads" menu, in which case it
256would be handled automatically, it is not available as a primary payload
257since it cannot load an OS or another payload. Secondary payloads must
258be loaded from other primary or secondary payloads and will not be run
259when coreboot hands off execution after initializing hardware. Thus, to
260get coreinfo to run as if it were a primary payload, it must be manually
261built and explicitly set as the primary payload using the "ELF
262executable payload" option.
263
264
265### Step 5 summary - Configure the build
266
267This step configures coreboot's build options using the menuconfig
268interface to Kconfig. Kconfig is the same configuration program used by
269the linux kernel. It allows you to enable, disable, and change various
270values to control the coreboot build process, including which
271mainboard(motherboard) to use, which toolchain to use, and how the
272runtime debug console should be presented and saved.  Anytime you change
273mainboards in Kconfig, you should always run `make distclean` before
274running `make menuconfig`. Due to the way that Kconfig works, values
275will be kept from the previous mainboard if you skip the clean step.
276This leads to a hybrid configuration which may or may not work as
277expected.
278
279
280### Step 6 summary - Build coreboot
281
282You may notice that a number of other pieces are downloaded at the
283beginning of the build process. These are the git submodules used in
284various coreboot builds.  By default, the _blobs_ submodule is not
285downloaded. This git submodule may be required for other builds for
286microcode or other binaries. To enable downloading this submodule,
287select the option "Allow use of binary-only repository" in the "General
288Setup" menu of Kconfig This attempts to build the coreboot rom. The rom
289file itself ends up in the build directory as 'coreboot.rom'. At the end
290of the build process, the build displayed the contents of the rom file.
291
292
293### Step 7 summary - Install QEMU
294
295QEMU is a processor emulator which we can use to show the coreboot boot
296process in a virtualised environment.
297
298
299### Step 8 summary - Run QEMU
300
301Here's the command line instruction broken down:
302* `qemu-system-x86_64`
303This starts the QEMU emulator with the i440FX host PCI bridge and PIIX3
304PCI to ISA bridge.
305* `-bios build/coreboot.rom`
306Use the coreboot rom image that we just built. If this flag is left out,
307the standard SeaBIOS image that comes with QEMU is used.
308* `-serial stdio`
309Send the serial output to the console. This allows you to view the
310coreboot boot log.
311