summaryrefslogtreecommitdiffstats
path: root/posts/2019-04-02-17:57-flashing-coreboot-on-a-lenovo-thinkpad-x220-with-a-ch341a-usb-programmer-tutorial.md
blob: 3045d39a779a6dcdc5dd9ad16ab3be49a39d0a4e (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
title: Flashing coreboot on a Lenovo Thinkpad X220 with a ch341a USB programmer Tutorial
author: Andreas
date: 2019-04-02 17:57
tags: Lenovo Thinkpad X220, coreboot, flashrom, Raspberry Pi, ch341a
---

In this tutorial, we will go through the steps to get [coreboot](https://www.coreboot.org/) compiled and flashed on a **Lenovo Thinkpad X220** laptop.
We'll also change the Wifi card so that Wifi will work with a libre kernel driver.

***Updated 2022-01-13: Use latest coreboot-4.15 release***

# Prerequisites

![Prerequisites](/static/blog/media/x220/01-prerequisites.jpg)

Things you need:

- Lenovo Thinkpad X220
- Atheros AR9382 AR5BHB116 Mini PCIE Dual Band Wifi card
- ch341a USB programmer or Raspberry Pi
- Pomona 5250 SOIC-8 clip
- 6 x 10 cm female - female jumper wires
- Screwdriver

This tutorial assumes you're using a GNU/Linux distro and are familiar
with how to use a terminal.

[Debian GNU/Linux](https://www.debian.org/) 11 (bullseye) was used to
compile the coreboot image produced in this tutorial.

Files needed:

[`dejavusansmono.pf2`](/static/blog/media/x220/dejavusansmono.pf2)\
[`grub.cfg`](/static/blog/media/x220/grub.cfg)\
[`background.jpg`](/static/blog/media/x220/background.jpg)

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

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

'**background.jpg**' is borrowed from the libreboot project and it's license
information is as follows:

```plaintext
These images are based on http://libreboot.org/logo/logo.svg
which is Copyright 2014 Marcus Moeller and released under CC-0
https://creativecommons.org/publicdomain/zero/1.0/legalcode
```

# Packages needed to compile flashrom/coreboot/grub

```sh
sudo apt-get build-dep flashrom grub
sudo apt-get install git build-essential gnat flex bison libncurses5-dev libfreetype-dev pkg-config unifont wget
```

# Compile flashrom

Clone flashrom git repository:

```sh
git clone https://review.coreboot.org/flashrom.git
```

Compile flashrom:

```sh
cd flashrom
sudo make install
sudo cp /usr/local/sbin/flashrom /usr/local/bin/
```

# Compile GRUB

See '**Prerequisites**' above for the files needed.

Clone GRUB git repository:

```sh
git clone https://git.savannah.gnu.org/git/grub.git
```

Compile GRUB:

```sh
cd grub
./bootstrap
./configure --with-platform=coreboot
make -j4
```

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

```sh
touch pack-payload-minimal.sh
chmod +x pack-payload-minimal.sh
```

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

```sh
#! /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"
```

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

```sh
./pack-payload-minimal.sh
```

# Connect programmer to flash chip

See [`Setup Raspberry Pi for flashing with flashrom Tutorial`](../../2022/setup-raspberry-pi-for-flashing-with-flashrom-tutorial/)
if you're using a Raspberry Pi.

Remove the following parts to access the flash chip:

```plaintext
Battery
Keyboard
Palmrest
```

![Connect ch341a USB programmer](/static/blog/media/x220/02-lenovo-thinkpad-x220-internals.jpg)

Connect Pomona 5250 to flash chip like this:

```plaintext
Screen (furthest from you)
             __
  MOSI  5 --|  |-- 4  GND
   CLK  6 --|  |-- 3  N/C
   N/C  7 --|  |-- 2  MISO
  3.3v  8 --|__|-- 1  CS

   N/C = Not connected / Not used

   Edge (closest to you)
```

![ch341a USB programmer connected](/static/blog/media/x220/03-ch341a-programmer-connected.jpg)

# Read Factory BIOS

Read Factory BIOS 3 times if using **ch341a**:

```sh
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
```

Read Factory BIOS 3 times if using **Raspberry Pi**:

```sh
sudo flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=32768 -r factory_bios_01.rom -V
sudo flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=32768 -r factory_bios_02.rom -V
sudo flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=32768 -r factory_bios_03.rom -V
```

Make sure checksums are identical:

```plaintext
sha512sum *.rom
```

# Download coreboot

Download coreboot:

```sh
wget https://coreboot.org/releases/coreboot-4.15.tar.xz
wget https://coreboot.org/releases/coreboot-blobs-4.15.tar.xz
```

**coreboot-blobs-4.15.tar.xz** is needed for CPU microcode updates.

Extract coreboot and blobs needed:

```sh
tar xvf coreboot-4.15.tar.xz
tar xvf coreboot-blobs-4.15.tar.xz --strip-components=1 -C coreboot-4.15
```

Create folder to hold '**descriptor/gbe/me.bin**' files:

```sh
mkdir -pv coreboot-4.15/3rdparty/blobs/mainboard/lenovo/x220/
```

Extract blob files from Factory BIOS (see '**flashrom/util/ich_descriptors_tool**' folder:

```sh
cd ~/misc-src/flashrom/util/ich_descriptors_tool
./ich_descriptors_tool -f your/path/to/factory_bios.rom -d
```

Copy blob files to coreboot folder:

```sh
cp factory_bios.rom.Descriptor.bin ~/misc-src/coreboot-4.15/3rdparty/blobs/mainboard/lenovo/x220/descriptor.bin
cp factory_bios.rom.GbE.bin ~/misc-src/coreboot-4.15/3rdparty/blobs/mainboard/lenovo/x220/gbe.bin
cp factory_bios.rom.ME.bin ~/misc-src/coreboot-4.15/3rdparty/blobs/mainboard/lenovo/x220/me.bin
```

# Setup and compile coreboot

Enter coreboot folder:

```sh
cd coreboot-4.15
```

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

```sh
make crossgcc-i386 CPUS=4
```

coreboot settings menu:

```sh
make menuconfig
```

Set the following options:

```plaintext
NOTE: Leave default values as is and specifically set the following
options:

mainboard -|
           |-Mainboard vendor (Lenovo)
           |-Mainboard model (ThinkPad X220)
           |-ROM chip size (8192 KB (8 MB))
           |-(0x300000) Size of CBFS filesystem in ROM
chipset ---|
           |-Include CPU microcode in CBFS (Generate from tree)
           |-[*] Add Intel descriptor.bin file (leave default path as is)
           |-(3rdparty/blobs/mainboard/$(MAINBOARDDIR)/descriptor.bin) Path and filename of the descriptor.bin file
           |-[*] Add Intel ME/TXE firmware (leave default path as is)
           |-(3rdparty/blobs/mainboard/$(MAINBOARDDIR)/me.bin) Path to management engine firmware
           |-    [*] Verify the integrity of the supplied ME/TXE firmware
           |-    [*] Strip down the Intel ME/TXE firmware
           |-[*] Add gigabit ethernet firmware (leave default path as is)
           |-(3rdparty/blobs/mainboard/$(MAINBOARDDIR)/gbe.bin) Path to gigabit ethernet firmware
Devices ---|
           |-Graphics initialization (Use libgfxinit)
           |-Display
           |-    Framebuffer mode (Linear "high-resolution" framebuffer)
payload ---|
           |-Add a payload (An ELF executable payload)
           |-"(/path/to/grub/grub2.elf)" Payload path and filename
```

If you don't want to include CPU microcode updates:

```plaintext
chipset ---|
           |-Include CPU microcode in CBFS (Do not include microcode updates)
```

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

Compile coreboot:

```sh
make -j4
```

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

# Add custom files to 'coreboot.rom' image

Add '**grub.cfg**' and '**background.jpg**' to coreboot.rom. See
'**Prerequisites**' above for the files needed.

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

```sh
cd coreboot-4.15/util/cbfstool
make -j4
```

Add custom GRUB configuration file:

```sh
./cbfstool ../../build/coreboot.rom add -t raw -n etc/grub.cfg -f your/path/to/grub.cfg
```

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

```sh
./cbfstool ../../build/coreboot.rom print
```

Add background image:

```sh
./cbfstool ../../build/coreboot.rom add -t raw -n background.jpg -f your/path/to/background.jpg
```

Check so that '**background.jpg**' exists in coreboot.rom:

```sh
./cbfstool ../../build/coreboot.rom print
```

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

# Flash coreboot image

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

Flash coreboot using **ch341a** USB programmer:

```sh
sudo flashrom -p ch341a_spi -w coreboot.rom -V
```

Flash coreboot using **Raspberry Pi** programmer:

```sh
sudo flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=32768 -w coreboot.rom -V
```

# Change Wifi card

Remove the following parts to access the Wifi card:

```sh
Battery
Keyboard
Palmrest
```

See '**Connect programmer to flash chip**' for details on
where the Wifi card is located. 

Now you can install a libre distro such as [Debian
GNU/Linux](https://www.debian.org/).

# Boot Debian GNU/Linux netinst iso from usb in GRUB2

Download Debian GNU/Linux netinst from [here](https://www.debian.org/).

Check name of USB device:

```sh
sudo fdisk -l
```

Create bootable USB drive:

```sh
sudo dd bs=4M if=/path/to/debian-img.iso of=/dev/YOUR_USB status=progress oflag=sync
```

Start the computer up and in the GRUB2 menu press '**c**' to enter
command line and from there type the following:

```sh
set root='usb0'
linux /install.amd/vmlinuz
initrd /install.amd/initrd.gz
boot
```

Install Debian GNU/Linux normally and put GRUB2 on master boot record at
the end of installation process. After reboot default option in GRUB2
menu on the flashchip will load GRUB2 on the AHCI HDD you installed on.

# Download compiled coreboot image

The resulting coreboot image from this tutorial can be downloaded here:

[`lenovo-thinkpad-x220_coreboot-4-15-grub-master-with-cpu-microcode.rom`](/static/blog/files/x220/lenovo-thinkpad-x220_coreboot-4-15-grub-master-with-cpu-microcode.rom)

Congratulations! We’re done.

# Recommended reading

[https://www.coreboot.org/](https://www.coreboot.org/)\
[https://www.flashrom.org/](https://www.flashrom.org/)\
[https://www.thinkwiki.org/wiki/Category:X220](https://www.thinkwiki.org/wiki/Category:X220)\
[https://download.lenovo.com/ibmdl/pub/pc/pccbbs/mobiles_pdf/0a60739.pdf](https://download.lenovo.com/ibmdl/pub/pc/pccbbs/mobiles_pdf/0a60739.pdf)\
[https://dejavu-fonts.github.io/](https://dejavu-fonts.github.io/)\
[https://dejavu-fonts.github.io/License.html](https://dejavu-fonts.github.io/License.html)