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