summaryrefslogtreecommitdiffstats
path: root/blog/data/2019-04-03-16:59:50-flashing-coreboot-on-gigabyte-ga-g41m-es2l-with-a-ch341a-usb-programmer-tutorial.txt
blob: b70d62ba04aa68beb843f6f1b128af90fbd13226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
Title: Flashing coreboot on Gigabyte GA-G41M-ES2L with a ch341a USB programmer Tutorial
Tags: gigabyte-ga-g41m-es2l, coreboot, libreboot, flashrom, ch341a 
Date: 2019-04-03 16:59:50
Format: markdown

In this tutorial, we will go through the steps to get
[coreboot](https://www.coreboot.org/) compiled and flashed on Gigabyte
GA-G41M-ES2L. 
__CUT__

## Prerequisites

![Prerequisites](../media/gigabyte-ga-g41m-es2l/01-prerequisites.jpg)

Things you need:

> * Gigabyte GA-G41M-ES2L
> * ch341a USB programmer
> * Pomona 5250 SOIC-8 clip
> * 6 x 10 cm female - female jumper wires

Files needed:

[dejavusansmono.pf2](../media/gigabyte-ga-g41m-es2l/dejavusansmono.pf2)<br>
[grub.cfg](../media/gigabyte-ga-g41m-es2l/grub.cfg)<br>

For more information about DejaVu Fonts, see:<br>
[https://dejavu-fonts.github.io/](https://dejavu-fonts.github.io/)

For DejaVu Font license, see:<br>
[https://dejavu-fonts.github.io/License.html](https://dejavu-fonts.github.io/License.html)

## Packages needed to compile flashrom/coreboot/grub

<pre>
sudo apt-get install git build-essential gnat flex bison libncurses5-dev wget libpci-dev libusb-dev libftdi1 libftdi-dev unifont zlib1g-dev
</pre>

## Compile flashrom

Clone flashrom git repository:

<pre>
git clone https://review.coreboot.org/flashrom.git
</pre>

Compile flashrom:

<pre>
cd flashrom
make -j4
sudo make install
sudo cp /usr/local/sbin/flashrom /usr/local/bin/
</pre>

## Compile GRUB

See '**Prerequisites**' above for the font file needed.

Clone GRUB git repository:

<pre>
git clone https://git.savannah.gnu.org/git/grub.git
</pre>

Compile GRUB:

<pre>
cd grub
git checkout grub-2.02
./autogen.sh
./configure --with-platform=coreboot
make -j4
</pre>

Create '**pack-payload-minimal.sh**' script file:

<pre>
touch pack-payload-minimal.sh
chmod +x pack-payload-minimal.sh
</pre>

Paste the following in '**pack-payload-minimal.sh**':

<pre>
#! /bin/bash

# ./pack-payload-minimal.sh – To be invoked within GRUB project folder, packs
# an executable elf payload file for the coreboot project, using just one
# keyboard layout file. Adapt “settings” to your needs. Specify a lighter
# pf2-fontfile if available on your system.

# settings
elf_output="grub2.elf"
#pf2_fontfile="unicode.pf2"
pf2_fontfile="dejavusansmono.pf2"
keyboard_layout="se"
grub_modules="cbfs"

# generate keyboard layout
ckbcomp "${keyboard_layout}" | ./grub-mklayout -o "${keyboard_layout}.gkb"

# pack the GRUB payload file
./grub-mkstandalone \
        --grub-mkimage=./grub-mkimage \
        -O i386-coreboot \
        -o "${elf_output}" \
        -d grub-core \
        --fonts= \
        --themes= \
        --locales= \
        --modules="${grub_modules}" \
        /boot/grub/grub.cfg=coreboot.cfg \
        /boot/grub/fonts/${pf2_fontfile}="${pf2_fontfile}" \
        /boot/grub/layouts/${keyboard_layout}.gkb="${keyboard_layout}.gkb"

# message
echo "Payload ${elf_output} has been packed with:"
echo " * fontfile: ${pf2_fontfile}"
echo " * keyboard layout: ${keyboard_layout}"
echo " * GRUB modules, to be preloaded: ${grub_modules}"

# code snippet suggestion
echo "Don't forget to add 'keymap ${keyboard_layout}' to your GRUB Configuration File."

# clean up
rm "${keyboard_layout}.gkb"
</pre>

Execute the script to create '**grub2.elf**' file:

<pre>
./pack-payload-minimal.sh
</pre>

## Connect ch341a USB programmer to flash chip

![Connect ch341a USB programmer](../media/gigabyte-ga-g41m-es2l/02-gigabyte-ga-g41m-es2l-internals.jpg)

Connect Pomona 5250 to flash chip named '**M_BIOS**' like this:

<pre>
             __
    CS  5 --|  |-- 4  VCC
  MISO  6 --|  |-- 3  N/C
   N/C  7 --|  |-- 2  CLK
   GND  8 --|__|-- 1  MOSI

</pre>

![ch341a USB programmer connected](../media/gigabyte-ga-g41m-es2l/03-ch341a-programmer-connected.jpg)

## Read Factory BIOS

Read Factory BIOS 3 times:

<pre>
sudo flashrom -p ch341a_spi -r factory_bios_01.rom -V
sudo flashrom -p ch341a_spi -r factory_bios_02.rom -V
sudo flashrom -p ch341a_spi -r factory_bios_03.rom -V
</pre>

Make sure checksums are identical:

<pre>
sha512sum *.rom
</pre>

## Download coreboot

Download coreboot:

<pre>
wget https://coreboot.org/releases/coreboot-4.9.tar.xz
</pre>

Extract coreboot:

<pre>
tar xvf coreboot-4.9.tar.xz
</pre>

## Setup and compile coreboot

Enter coreboot folder:

<pre>
cd coreboot-4.9
</pre>

Build toolchain needed first (this will take a long time, be patient):

<pre>
make crossgcc CPUS=4
</pre>

coreboot settings menu:

<pre>
make menuconfig
</pre>

Set the following options:

<pre>
NOTE: Leave default values as is and specifically set the following
options:

mainboard ---------|
                   |-Mainboard vendor (GIGABYTE)
                   |-Mainboard model (GA-G41M-ES2L)
                   |-ROM chip size (1024 KB (1 MB))
                   |-(0x100000) Size of CBFS filesystem in ROM
chipset -----------|
                   |-Include CPU microcode in CBFS (Do not include microcode updates)
Devices -----------|
                   |-Graphics initialization (Use libgfxinit)
                   |-Display
                   |-    Legacy VGA text mode
Generic Drivers ---|
                   |-(XX:XX:XX:XX:XX:XX) Realtek rt8168 mac address
payload -----------|
                   |-Add a payload (An ELF executable payload)
                   |-"(grub2.elf)" Payload path and filename
</pre>

This will create a '**.config**' file containing all settings.

Compile coreboot:

<pre>
make -j4
</pre>

This will create '**build/coreboot.rom**' image with the size of 1mb.

## Add custom files to 'coreboot.rom' image

Add '**grub.cfg**' to coreboot.rom. See '**Prerequisites**' above for
the file needed.

Make sure '**cbfstool**' is built:

<pre>
cd coreboot-4.9/util/cbfstool 
make -j4
</pre>

Add custom GRUB configuration file:

<pre>
./cbfstool ../../build/coreboot.rom add -t raw -n etc/grub.cfg -f your/path/to/grub.cfg
</pre>

Check so that '**etc/grub.cfg**' exists in coreboot.rom:

<pre>
./cbfstool ../../build/coreboot.rom print
</pre>

Done! Now it's time to flash the new '**coreboot.rom**' image!

## Flash coreboot image

See '**Connect ch341a USB programmer to flash chip**' for details on how
to connect ch341a USB programmer.

Flash coreboot using ch341a USB programmer:

<pre>
sudo flashrom -p ch341a_spi -w coreboot.rom -V
</pre>

## Additional parts needed

To build a desktop/server the following additional parts are needed:

<pre>
Desktop case
Power supply
CPU
CPU Fan
Memory
Network card
</pre>

Here's the parts I used to build a server:

<pre>
Desktop case: Fractal Design Define R6 Gunmetal
Power supply: Corsair RM750X 750W v2
CPU: Intel Core 2 Quad Q9550 2,83 GHz 12M
CPU Fan: Arctic Cooling Freezer 7 Pro rev.2
Memory: Corsair 2 x 2 GB DDR2 800 (PC2-6400) Memory
Network card: D-Link DGE-528T PCI (RTL8169 kernel driver)
PCI Express to USB 3.0 4-Port PCI Express Expansion Card Adapter Hub VIA 5Gbps
PCI Express SATA 3 Controller Card, 2 Port PCIe SATA III 6GB/s Internal Ada L80
Samsung 850 EVO 250GB SSD harddrive
</pre>

The parts were carefully choosen so that the server would be as close to
completely silent as possible and be able to run 100% free/libre
software.

For a list of compatible CPU's (socket LGA 775), see:<br>
[https://en.wikipedia.org/wiki/List_of_Intel_Core_2_microprocessors](https://en.wikipedia.org/wiki/List_of_Intel_Core_2_microprocessors)

The onboard network card doesn't work by default and I recommend getting
a PCI or PCI Express network card. I'm using a PCI network card (D-Link
DGE-528T) which works with default Debian GNU/Linux kernel without
proprietary drivers. It's using the RTL8169 driver for it.

Congratulations! We're done. Now you can install a libre distro such as
[Debian GNU/Linux](https://www.debian.org/). I recommend setting up
[full disc encryption (including
/boot)](https://libreboot.org/docs/gnulinux/encrypted_debian.html) ! The
'**grub.cfg**' supports and defaults to this option.

## Recommended reading

[https://www.coreboot.org/](https://www.coreboot.org/)<br>
[https://libreboot.org/](https://libreboot.org/)<br>
[https://flashrom.org/Flashrom](https://flashrom.org/Flashrom)<br>
[https://github.com/boseji/CH341-Store](https://github.com/boseji/CH341-Store)<br>
[https://libreboot.org/docs/gnulinux/encrypted_debian.html](https://libreboot.org/docs/gnulinux/encrypted_debian.html)<br>
[https://en.wikipedia.org/wiki/List_of_Intel_Core_2_microprocessors](https://en.wikipedia.org/wiki/List_of_Intel_Core_2_microprocessors)<br>
[https://dejavu-fonts.github.io/](https://dejavu-fonts.github.io/)<br>
[https://dejavu-fonts.github.io/License.html](https://dejavu-fonts.github.io/License.html)<br>