Skip to content

Commit 0968d15

Browse files
committed
Refactor the x86 GPT code to be generically available. Upgrade update_disk.c support for other targets.
1 parent c0b60f4 commit 0968d15

File tree

10 files changed

+691
-217
lines changed

10 files changed

+691
-217
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ line-count-nrf52:
528528
cloc --force-lang-def cloc_lang_def.txt src/boot_arm.c src/image.c src/libwolfboot.c src/loader.c src/update_flash.c hal/nrf52.c
529529

530530
line-count-x86:
531-
cloc --force-lang-def cloc_lang_def.txt src/boot_x86_fsp.c src/boot_x86_fsp_payload.c src/boot_x86_fsp_start.S src/image.c src/keystore.c src/libwolfboot.c src/loader.c src/string.c src/update_disk.c src/x86/ahci.c src/x86/ata.c src/x86/common.c src/x86/gpt.c src/x86/hob.c src/pci.c src/x86/tgl_fsp.c hal/x86_fsp_tgl.c hal/x86_uart.c
531+
cloc --force-lang-def cloc_lang_def.txt src/boot_x86_fsp.c src/boot_x86_fsp_payload.c src/boot_x86_fsp_start.S src/image.c src/keystore.c src/libwolfboot.c src/loader.c src/string.c src/update_disk.c src/gpt.c src/x86/ahci.c src/x86/ata.c src/x86/common.c src/x86/disk.c src/x86/hob.c src/pci.c src/x86/tgl_fsp.c hal/x86_fsp_tgl.c hal/x86_uart.c
532532

533533
stack-usage: wolfboot.bin
534534
$(Q)echo $(STACK_USAGE) > .stack_usage

arch.mk

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,10 @@ endif
561561
ifeq ($(ARCH),RISCV64)
562562
CROSS_COMPILE?=riscv64-unknown-elf-
563563
CFLAGS+=-DMMU -DWOLFBOOT_DUALBOOT
564-
UPDATE_OBJS:=src/update_ram.o
564+
#UPDATE_OBJS:=src/update_ram.o
565+
UPDATE_OBJS:=src/update_disk.o
566+
CFLAGS+=-DWOLFBOOT_UPDATE_DISK
567+
OBJS += src/gpt.o
565568
ARCH_FLAGS=-march=rv64imafdc -mabi=lp64d -mcmodel=medany
566569
CFLAGS+=-fno-builtin-printf -DUSE_M_TIME -g -nostartfiles -DARCH_RISCV64
567570
CFLAGS+=$(ARCH_FLAGS)
@@ -1030,6 +1033,9 @@ ifeq ($(filter $(TARGET),x86_fsp_qemu kontron_vx3060_s2),$(TARGET))
10301033
FSP_TGL=1
10311034
CFLAGS+=-DWOLFBOOT_TGL=1
10321035
endif
1036+
1037+
# load to address in RAM after wolfBoot (aligned to 16 bytes)
1038+
CFLAGS+=-DWOLFBOOT_NO_LOAD_ADDRESS
10331039
endif
10341040

10351041
ifeq ($(TARGET),x86_fsp_qemu)
@@ -1110,7 +1116,8 @@ ifeq ("${FSP}", "1")
11101116
CFLAGS+=-DWOLFBOOT_USE_PCI
11111117
OBJS += src/x86/ahci.o
11121118
OBJS += src/x86/ata.o
1113-
OBJS += src/x86/gpt.o
1119+
OBJS += src/gpt.o
1120+
OBJS += src/x86/disk.o
11141121
OBJS += src/x86/mptable.o
11151122
OBJS += src/stage2_params.o
11161123
OBJS += src/x86/exceptions.o

hal/mpfs250.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#include "printf.h"
4343
#include "loader.h"
44+
#include "disk.h"
4445

4546
/* Placeholder functions - to be implemented */
4647
void hal_init(void)
@@ -124,6 +125,38 @@ void* hal_get_dts_address(void)
124125
}
125126
#endif
126127

128+
/* TODO: Add support for reading uSD card with GPT (Global Partition Table) */
129+
/* The partition ID's are determined using BOOT_PART_A and BOOT_PART_B. */
130+
int disk_open(int drv)
131+
{
132+
(void)drv;
133+
return 0;
134+
}
135+
int disk_read(int drv, int part, uint64_t off, uint64_t sz, uint8_t *buf)
136+
{
137+
(void)drv;
138+
(void)part;
139+
(void)off;
140+
(void)sz;
141+
(void)buf;
142+
return 0;
143+
}
144+
int disk_write(int drv, int part, uint64_t off, uint64_t sz, const uint8_t *buf)
145+
{
146+
(void)drv;
147+
(void)part;
148+
(void)off;
149+
(void)sz;
150+
(void)buf;
151+
return 0;
152+
}
153+
int disk_find_partition_by_label(int drv, const char *label)
154+
{
155+
(void)drv;
156+
(void)label;
157+
return 0;
158+
}
159+
127160
#ifdef DEBUG_UART
128161

129162
#ifndef DEBUG_UART_BASE

include/x86/gpt.h renamed to include/disk.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* gpt.h
1+
/* disk.h
22
*
33
* Copyright (C) 2025 wolfSSL Inc.
44
*
@@ -19,10 +19,15 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22-
#ifndef GPT_H
23-
#define GPT_H
22+
#ifndef _WOLFBOOT_DISK_H
23+
#define _WOLFBOOT_DISK_H
24+
25+
#include <stdint.h>
26+
2427
int disk_open(int drv);
2528
int disk_read(int drv, int part, uint64_t off, uint64_t sz, uint8_t *buf);
2629
int disk_write(int drv, int part, uint64_t off, uint64_t sz, const uint8_t *buf);
2730
int disk_find_partition_by_label(int drv, const char *label);
28-
#endif
31+
32+
#endif /* _WOLFBOOT_DISK_H */
33+

include/gpt.h

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/* gpt.h
2+
*
3+
* Generic GPT (GUID Partition Table) parsing support.
4+
*
5+
* Copyright (C) 2025 wolfSSL Inc.
6+
*
7+
* This file is part of wolfBoot.
8+
*
9+
* wolfBoot is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* wolfBoot is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, write to the Free Software
21+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
22+
*/
23+
24+
#ifndef GPT_H
25+
#define GPT_H
26+
27+
#include <stdint.h>
28+
29+
/* GPT Constants */
30+
#define GPT_SECTOR_SIZE 0x200
31+
#define GPT_SIGNATURE 0x5452415020494645ULL /* "EFI PART" */
32+
#define GPT_PTYPE_PROTECTIVE 0xEE
33+
#define GPT_PART_NAME_SIZE 36
34+
#define GPT_MBR_ENTRY_START 0x01BE
35+
#define GPT_MBR_BOOTSIG_OFFSET 0x01FE
36+
#define GPT_MBR_BOOTSIG_VALUE 0xAA55
37+
38+
/**
39+
* @brief MBR partition table entry structure.
40+
*
41+
* This packed structure defines the layout of an MBR partition table entry
42+
* used to identify GPT partitions (protective MBR).
43+
*/
44+
struct __attribute__((packed)) gpt_mbr_part_entry {
45+
uint8_t stat;
46+
uint8_t chs_first[3];
47+
uint8_t ptype;
48+
uint8_t chs_last[3];
49+
uint32_t lba_first;
50+
uint32_t lba_size;
51+
};
52+
53+
/**
54+
* @brief GPT (GUID Partition Table) header structure.
55+
*/
56+
struct __attribute__((packed)) guid_ptable {
57+
uint64_t signature;
58+
uint32_t revision;
59+
uint32_t hdr_size;
60+
uint32_t hdr_crc32;
61+
uint32_t res0;
62+
uint64_t main_lba;
63+
uint64_t backup_lba;
64+
uint64_t first_usable;
65+
uint64_t last_usable;
66+
uint64_t disk_guid[2];
67+
uint64_t start_array;
68+
uint32_t n_part;
69+
uint32_t array_sz;
70+
uint32_t part_crc;
71+
uint8_t res1[GPT_SECTOR_SIZE - 0x5C];
72+
};
73+
74+
/**
75+
* @brief GPT partition entry structure.
76+
*
77+
* This packed structure defines the layout of a GPT partition entry
78+
* used to describe individual partitions on the disk.
79+
*/
80+
struct __attribute__((packed)) gpt_part_entry {
81+
uint64_t type[2];
82+
uint64_t uuid[2];
83+
uint64_t first;
84+
uint64_t last;
85+
uint64_t flags;
86+
uint16_t name[GPT_PART_NAME_SIZE];
87+
};
88+
89+
/**
90+
* @brief Parsed partition information.
91+
*
92+
* This structure holds parsed information about a partition extracted
93+
* from a GPT partition entry.
94+
*/
95+
struct gpt_part_info {
96+
uint64_t start; /* Start offset in bytes */
97+
uint64_t end; /* End offset in bytes */
98+
uint16_t name[GPT_PART_NAME_SIZE];
99+
};
100+
101+
/**
102+
* @brief Check MBR for protective GPT partition entry.
103+
*
104+
* Scans the MBR sector for a protective GPT partition entry (type 0xEE)
105+
* and validates the boot signature.
106+
*
107+
* @param[in] mbr_sector Pointer to 512-byte MBR sector data.
108+
* @param[out] gpt_lba If not NULL, receives the LBA of the GPT header.
109+
*
110+
* @return 0 on success (valid protective MBR found), -1 on error.
111+
*/
112+
int gpt_check_mbr_protective(const uint8_t *mbr_sector, uint32_t *gpt_lba);
113+
114+
/**
115+
* @brief Parse and validate a GPT header.
116+
*
117+
* Validates the GPT signature and copies header data to the output structure.
118+
*
119+
* @param[in] sector Pointer to 512-byte GPT header sector data.
120+
* @param[out] hdr Pointer to structure to receive parsed header.
121+
*
122+
* @return 0 on success (valid GPT header), -1 on error.
123+
*/
124+
int gpt_parse_header(const uint8_t *sector, struct guid_ptable *hdr);
125+
126+
/**
127+
* @brief Parse a GPT partition entry.
128+
*
129+
* Parses a single partition entry and extracts partition information.
130+
* Returns success only if the partition entry is valid (non-zero type GUID).
131+
*
132+
* @param[in] entry_data Pointer to partition entry data.
133+
* @param[in] entry_size Size of the partition entry in bytes.
134+
* @param[out] part Pointer to structure to receive parsed partition info.
135+
*
136+
* @return 0 on success (valid partition entry), -1 if entry is empty/invalid.
137+
*/
138+
int gpt_parse_partition(const uint8_t *entry_data, uint32_t entry_size,
139+
struct gpt_part_info *part);
140+
141+
/**
142+
* @brief Compare UTF-16 partition name with ASCII string.
143+
*
144+
* Compares a GPT partition name (UTF-16LE) with an ASCII string label.
145+
*
146+
* @param[in] utf16_name UTF-16LE partition name from GPT entry.
147+
* @param[in] ascii_label ASCII string to compare against.
148+
*
149+
* @return 1 if names match, 0 if they don't match.
150+
*/
151+
int gpt_part_name_eq(const uint16_t *utf16_name, const char *ascii_label);
152+
153+
#endif /* GPT_H */
154+

include/x86/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ uint32_t io_read32(uint16_t port);
6262
void reset(uint8_t warm);
6363
void delay(int msec);
6464
void panic();
65+
#define wolfBoot_panic() panic()
6566
void cpuid(uint32_t eax_param,
6667
uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
6768
int cpuid_is_1gb_page_supported();

0 commit comments

Comments
 (0)