Skip to content

Commit 5515d6d

Browse files
authored
update edk2-uefi tutorial (#234)
1 parent a1fe5f0 commit 5515d6d

File tree

8 files changed

+90
-2938
lines changed

8 files changed

+90
-2938
lines changed

docs/src/tutorials/edk2-uefi/building-the-application.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ This Dockerfile will obtain the EDK2 source and compile the BaseTools, then copy
3838

3939
We will want to get our built UEFI application from the container, which we can
4040
do using the `docker cp` command. There are a few files we want to copy, so we'll
41-
use this script `build.sh` to automate the process:
41+
use this script `build.sh` to automate the process.
42+
43+
It will also copy the `tsffs.h` header into the harness sources, copy the minimal boot disk
44+
and create a initial fuzzing corpus to prepare the project.
4245

4346
```sh
4447
#!/bin/bash
@@ -49,26 +52,36 @@ CONTAINER_UID=$(echo "${RANDOM}" | sha256sum | head -c 8)
4952
CONTAINER_NAME="${IMAGE_NAME}-tmp-${CONTAINER_UID}"
5053

5154
mkdir -p "${SCRIPT_DIR}/project/"
55+
# copy minimal boot disk
56+
cp "${SCRIPT_DIR}/../../rsrc/minimal_boot_disk.craff" "${SCRIPT_DIR}/project/"
57+
58+
# copy tsffs.h header into src
59+
cp "${SCRIPT_DIR}/../../../harness/tsffs.h" "${SCRIPT_DIR}/src/"
5260
docker build -t "${IMAGE_NAME}" -f "Dockerfile" "${SCRIPT_DIR}"
5361
docker create --name "${CONTAINER_NAME}" "${IMAGE_NAME}"
54-
docker cp \
55-
"${CONTAINER_NAME}:/edk2/Tutorial/Build/CryptoPkg/All/DEBUG_GCC/X64/Tutorial/Tutorial/DEBUG/Tutorial.efi" \
56-
"${SCRIPT_DIR}/project/Tutorial.efi"
57-
docker cp \
58-
"${CONTAINER_NAME}:/edk2/Tutorial/Build/CryptoPkg/All/DEBUG_GCC/X64/Tutorial/Tutorial/DEBUG/Tutorial.map" \
59-
"${SCRIPT_DIR}/project/Tutorial.map"
60-
docker cp \
61-
"${CONTAINER_NAME}:/edk2/Tutorial/Build/CryptoPkg/All/DEBUG_GCC/X64/Tutorial/Tutorial/DEBUG/Tutorial.debug" \
62-
"${SCRIPT_DIR}/project/Tutorial.debug"
62+
63+
for file_ext in efi map debug; do
64+
docker cp \
65+
"${CONTAINER_NAME}:/edk2/Tutorial/Build/CryptoPkg/All/DEBUG_GCC/X64/Tutorial/Tutorial/DEBUG/Tutorial.efi" \
66+
"${SCRIPT_DIR}/project/Tutorial.${file_ext}"
67+
done
68+
6369
docker rm -f "${CONTAINER_NAME}"
70+
71+
# ensure corpus
72+
if [ ! -d "${SCRIPT_DIR}/corpus" ]; then
73+
mkdir "${SCRIPT_DIR}/corpus"
74+
curl -L -o "${SCRIPT_DIR}/corpus/0" https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/0
75+
curl -L -o "${SCRIPT_DIR}/corpus/1" https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/1
76+
curl -L -o "${SCRIPT_DIR}/corpus/2" https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/2
77+
curl -L -o "${SCRIPT_DIR}/corpus/3" https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/3
78+
fi
6479
```
6580

6681
The script will build the image, create a container using it, copy the relevant files
6782
to our host machine (in a `project` directory), then delete the container.
6883

69-
Mark the script executable and then we'll go ahead and run it with:
70-
84+
Let's go ahead and run it:
7185
```sh
72-
chmod +x build.sh
7386
./build.sh
74-
```
87+
```

docs/src/tutorials/edk2-uefi/configuring-the-fuzzer.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,15 @@ Re-compile the application by running the build script.
7575
7676
## Obtain a Corpus
7777
78-
The fuzzer will take input from the `corpus` directory in the project directory, so
79-
we'll create that directory and add some sample certificate files in DER format as
80-
our input corpus.
81-
82-
```sh
83-
mkdir corpus
84-
curl -L -o corpus/0 https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/0
85-
curl -L -o corpus/1 https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/1
86-
curl -L -o corpus/2 https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/2
87-
curl -L -o corpus/3 https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/3
78+
The fuzzer will take input from the `corpus` directory located under `edk2-uefi`:
79+
80+
```python
81+
@tsffs.corpus_directory = SIM_lookup_file("%simics%/../corpus")
8882
```
8983

84+
In `build.sh` we have already created that directory and added some sample
85+
certificate files in DER format as our input corpus.
86+
9087
## Configuring the Fuzzer
9188

9289
Even though we loaded the fuzzer module, it didn't run previously because we did not
@@ -95,14 +92,14 @@ script, we'll add each of the following lines.
9592

9693
First, we need to create an actual `tsffs` object to instantiate the fuzzer.
9794

98-
```simics
95+
```python
9996
load-module tsffs # You should already have this
10097
init-tsffs
10198
```
10299

103100
Next, we'll set the log level to maximum for demonstration purposes:
104101

105-
```simics
102+
```python
106103
tsffs.log-level 4
107104
```
108105

@@ -111,7 +108,7 @@ into our UEFI application. This is the default, so these calls can be skipped in
111108
usage unless you want to change the defaults, they are just provided here for
112109
completeness.
113110

114-
```simics
111+
```python
115112
@tsffs.start_on_harness = True
116113
@tsffs.stop_on_harness = True
117114
```
@@ -121,24 +118,28 @@ fuzz for. In our case, these are timeouts (we'll set the timeout to 3 seconds) t
121118
hangs, and CPU exceptions. we'll enable exceptions 13 for general protection fault and
122119
14 for page faults to detect out of bounds reads and writes.
123120

124-
```simics
121+
```python
125122
@tsffs.timeout = 3.0
126123
@tsffs.exceptions = [13, 14]
127124
```
128125

129-
We'll tell the fuzzer where to take its corpus and save its solutions. The fuzzer will
130-
take its corpus from the `corpus` directory and save solutions to the `solutions`
131-
directory in the project by default, so this call can be skipped in real usage unless
132-
you want to change the defaults.
126+
By default, TSFFS expects the `corpus` and `solutions` directories to be located within
127+
the Simics project directory.
128+
129+
However, Since our fuzzer is configured to read its corpus from the `../corpus`
130+
directory (relative to the `project` directory), we must explicitly specify the
131+
correct path using the following configuration:
133132

134-
```simics
135-
@tsffs.corpus_directory = SIM_lookup_file("%simics%/corpus")
133+
```python
134+
# project/../corpus
135+
@tsffs.corpus_directory = SIM_lookup_file("%simics%/../corpus")
136+
# set solutions directory (default location, explicitly defined for clarity)
136137
@tsffs.solutions_directory = SIM_lookup_file("%simics%/solutions")
137138
```
138139

139140
We'll also *delete* the following code from the `run.simics` script:
140141

141-
```simics
142+
```python
142143
script-branch {
143144
bp.time.wait-for seconds = 30
144145
quit 0

docs/src/tutorials/edk2-uefi/testing-the-application.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ You should see (at least, but likely more packages):
1818
```txt
1919
Installed Base Packages
2020
Package Number Name Version Installed Paths
21-
1000 Simics-Base 6.0.169 /home/rhart/simics/simics-6.0.169
21+
1000 Simics-Base 6.0.185 /home/rhart/simics/simics-6.0.185
2222
2323
Installed Addon Packages
2424
Package Number Name Version Installed Paths
25-
2096 QSP-x86 6.0.70 /home/rhart/simics/simics-qsp-x86-6.0.70
26-
8112 QSP-CPU 6.0.17 /home/rhart/simics/simics-qsp-cpu-6.0.17
27-
31337 TSFFS 6.0.1 /home/rhart/simics/simics-tsffs-6.0.1
25+
2096 QSP-x86 6.0.73 /home/rhart/simics/simics-qsp-x86-6.0.73
26+
8112 QSP-CPU 6.0.21 /home/rhart/simics/simics-qsp-cpu-6.0.21
27+
31337 TSFFS 6.1.6 /home/rhart/simics/simics-tsffs-6.1.6
2828
```
2929

3030
in the list!
@@ -35,25 +35,27 @@ The build script for our application created a `project` directory for us if it
3535
exist, so we'll instantiate that directory as our project with `ispm`:
3636

3737
```sh
38-
ispm projects project --create 1000-6.0.185 2096-6.0.70 8112-6.0.17 31337-latest \
38+
ispm projects project --create 1000-6.0.185 2096-6.0.73 8112-6.0.21 31337-latest \
3939
--ignore-existing-files
4040
cd project
4141
```
4242

43-
## Get the Minimal Boot Disk
43+
## Minimal Boot Disk
4444

4545
The TSFFS repository provides a boot disk called `minimal_boot_disk.craff` which
4646
provides a filesystem and the *Simics Agent* to allow us to easily download our UEFI
47-
application to the filesystem so we can run it. Copy the file
48-
`examples/rsrc/minimal_boot_disk.craff` into your `project` directory.
47+
application to the filesystem so we can run it.
48+
49+
Note: this boot disk has already been copied by `build.sh` into the `project` directory
50+
in the previous step.
4951

5052
## Create a Script
5153

5254
Our initial script will load (but not use *yet*) the TSFFS module, then configure and
5355
start our simple x86-64 platform and run our UEFI application. In the `project`
5456
directory, create `run.simics`:
5557

56-
```simics
58+
```python
5759
# Load the TSFFS module (to make sure we can load it)
5860
load-module tsffs
5961

docs/src/tutorials/edk2-uefi/writing-the-application.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ The exact meaning of all the entries in the `Tutorial.inf` file is out of scope
8282
tutorial, but in general this file declares the packages and libraries our application
8383
needs.
8484

85-
```txt
85+
```ini
8686
[Defines]
8787
INF_VERSION = 0x00010005
8888
BASE_NAME = Tutorial
@@ -112,7 +112,7 @@ needs.
112112
The descriptor file also declares classes and libraries that are needed to build the
113113
whole platform including our application and requisite additional libraries.
114114

115-
```txt
115+
```ini
116116
[Defines]
117117
PLATFORM_NAME = Tutorial
118118
PLATFORM_GUID = 0458dade-8b6e-4e45-b773-1b27cbda3e06
@@ -204,15 +204,14 @@ UefiMain(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable) {
204204
Print(L"CA Certificate:\n");
205205
hexdump(CACert, CACertSize);
206206

207-
BOOLEAN Status = X509VerifyCert(Cert, CertSize, CACert, CACertSize);
207+
X509VerifyCert(Cert, CertSize, CACert, CACertSize);
208208

209209
if (Input) {
210210
FreePages(Input, EFI_SIZE_TO_PAGES(MaxInputSize));
211211
}
212212

213213
return EFI_SUCCESS;
214214
}
215-
216215
```
217216
218217
Now that we have some code, we'll move on to building.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
project/*
2-
!project/run.simics
2+
src/tsffs.h
3+
!project/run.simics

examples/tutorials/edk2-uefi/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ ENV DEBIAN_FRONTEND=noninteractive
44

55
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
66

7-
ENV EDK2_REPO_URL "https://github.com/tianocore/edk2.git"
8-
ENV EDK2_REPO_HASH "edk2-stable202505"
9-
ENV EDK2_PATH "/edk2"
7+
ENV EDK2_REPO_URL="https://github.com/tianocore/edk2.git"
8+
ENV EDK2_REPO_HASH="edk2-stable202505"
9+
ENV EDK2_PATH="/edk2"
1010

1111

1212
RUN git clone "${EDK2_REPO_URL}" "${EDK2_PATH}" && \

examples/tutorials/edk2-uefi/build.sh

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,27 @@ CONTAINER_UID=$(echo "${RANDOM}" | sha256sum | head -c 8)
1111
CONTAINER_NAME="${IMAGE_NAME}-tmp-${CONTAINER_UID}"
1212

1313
mkdir -p "${SCRIPT_DIR}/project/"
14+
# copy minimal boot disk
15+
cp "${SCRIPT_DIR}/../../rsrc/minimal_boot_disk.craff" "${SCRIPT_DIR}/project/"
16+
17+
# copy tsffs.h header into src
18+
cp "${SCRIPT_DIR}/../../../harness/tsffs.h" "${SCRIPT_DIR}/src/"
1419
docker build -t "${IMAGE_NAME}" -f "Dockerfile" "${SCRIPT_DIR}"
1520
docker create --name "${CONTAINER_NAME}" "${IMAGE_NAME}"
16-
docker cp \
17-
"${CONTAINER_NAME}:/edk2/Tutorial/Build/CryptoPkg/All/DEBUG_GCC/X64/Tutorial/Tutorial/DEBUG/Tutorial.efi" \
18-
"${SCRIPT_DIR}/project/Tutorial.efi"
19-
docker cp \
20-
"${CONTAINER_NAME}:/edk2/Tutorial/Build/CryptoPkg/All/DEBUG_GCC/X64/Tutorial/Tutorial/DEBUG/Tutorial.map" \
21-
"${SCRIPT_DIR}/project/Tutorial.map"
22-
docker cp \
23-
"${CONTAINER_NAME}:/edk2/Tutorial/Build/CryptoPkg/All/DEBUG_GCC/X64/Tutorial/Tutorial/DEBUG/Tutorial.debug" \
24-
"${SCRIPT_DIR}/project/Tutorial.debug"
25-
docker rm -f "${CONTAINER_NAME}"
21+
22+
for file_ext in efi map debug; do
23+
docker cp \
24+
"${CONTAINER_NAME}:/edk2/Tutorial/Build/CryptoPkg/All/DEBUG_GCC/X64/Tutorial/Tutorial/DEBUG/Tutorial.${file_ext}" \
25+
"${SCRIPT_DIR}/project/Tutorial.${file_ext}"
26+
done
27+
28+
docker rm -f "${CONTAINER_NAME}"
29+
30+
# ensure corpus
31+
if [ ! -d "${SCRIPT_DIR}/corpus" ]; then
32+
mkdir "${SCRIPT_DIR}/corpus"
33+
curl -L -o "${SCRIPT_DIR}/corpus/0" https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/0
34+
curl -L -o "${SCRIPT_DIR}/corpus/1" https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/1
35+
curl -L -o "${SCRIPT_DIR}/corpus/2" https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/2
36+
curl -L -o "${SCRIPT_DIR}/corpus/3" https://github.com/dvyukov/go-fuzz-corpus/raw/master/x509/certificate/corpus/3
37+
fi

0 commit comments

Comments
 (0)