iPXE
Functions | Variables
fdt_test.c File Reference

Flattened device tree self-tests. More...

#include <string.h>
#include <ipxe/fdt.h>
#include <ipxe/image.h>
#include <ipxe/test.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void fdt_test_exec (void)
 Perform FDT self-test. More...
 

Variables

static const uint8_t sifive_u []
 Simplified QEMU sifive_u device tree blob. More...
 
struct self_test fdt_test __self_test
 FDT self-test. More...
 

Detailed Description

Flattened device tree self-tests.

Definition in file fdt_test.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ fdt_test_exec()

static void fdt_test_exec ( void  )
static

Perform FDT self-test.

Definition at line 167 of file fdt_test.c.

167  {
168  struct fdt_descriptor desc;
169  struct fdt_header *hdr;
170  struct fdt fdt;
171  const char *string;
172  struct image *image;
173  uint32_t u32;
174  uint64_t u64;
175  unsigned int count;
176  unsigned int offset;
177 
178  /* Verify parsing */
179  hdr = ( ( struct fdt_header * ) sifive_u );
180  ok ( fdt_parse ( &fdt, hdr, 0 ) != 0 );
181  ok ( fdt_parse ( &fdt, hdr, 1 ) != 0 );
182  ok ( fdt_parse ( &fdt, hdr, ( sizeof ( sifive_u ) - 1 ) ) != 0 );
183  ok ( fdt_parse ( &fdt, hdr, -1UL ) == 0 );
184  ok ( fdt.len == sizeof ( sifive_u ) );
185  ok ( fdt_parse ( &fdt, hdr, sizeof ( sifive_u ) ) == 0 );
186  ok ( fdt.len == sizeof ( sifive_u ) );
187 
188  /* Verify string properties */
189  ok ( ( string = fdt_string ( &fdt, 0, "model" ) ) != NULL );
190  ok ( strcmp ( string, "SiFive HiFive Unleashed A00" ) == 0 );
191  ok ( ( string = fdt_string ( &fdt, 0, "nonexistent" ) ) == NULL );
192 
193  /* Verify string list properties */
194  ok ( ( string = fdt_strings ( &fdt, 0, "model", &count ) ) != NULL );
195  ok ( count == 1 );
196  ok ( memcmp ( string, "SiFive HiFive Unleashed A00", 28 ) == 0 );
197  ok ( ( string = fdt_strings ( &fdt, 0, "compatible",
198  &count ) ) != NULL );
199  ok ( count == 2 );
200  ok ( memcmp ( string, "sifive,hifive-unleashed-a00\0"
201  "sifive,hifive-unleashed", 52 ) == 0 );
202  ok ( ( string = fdt_strings ( &fdt, 0, "nonexistent",
203  &count ) ) == NULL );
204  ok ( count == 0 );
205 
206  /* Verify path lookup */
207  ok ( fdt_path ( &fdt, "", &offset ) == 0 );
208  ok ( offset == 0 );
209  ok ( fdt_path ( &fdt, "/", &offset ) == 0 );
210  ok ( offset == 0 );
211  ok ( fdt_path ( &fdt, "/cpus/cpu@0/interrupt-controller",
212  &offset ) == 0 );
213  ok ( ( string = fdt_string ( &fdt, offset, "compatible" ) ) != NULL );
214  ok ( strcmp ( string, "riscv,cpu-intc" ) == 0 );
215  ok ( fdt_path ( &fdt, "//soc/serial@10010000//", &offset ) == 0 );
216  ok ( ( string = fdt_string ( &fdt, offset, "compatible" ) ) != NULL );
217  ok ( strcmp ( string, "sifive,uart0" ) == 0 );
218  ok ( fdt_path ( &fdt, "/nonexistent", &offset ) != 0 );
219  ok ( fdt_path ( &fdt, "/cpus/nonexistent", &offset ) != 0 );
220  ok ( fdt_path ( &fdt, "/soc/serial@10010000:115200n8",
221  &offset ) == 0 );
222  ok ( ( string = fdt_string ( &fdt, offset, "compatible" ) ) != NULL );
223  ok ( strcmp ( string, "sifive,uart0" ) == 0 );
224 
225  /* Verify 64-bit integer properties */
226  ok ( fdt_u64 ( &fdt, 0, "#address-cells", &u64 ) == 0 );
227  ok ( u64 == 2 );
228  ok ( fdt_path ( &fdt, "/soc/ethernet@10090000", &offset ) == 0 );
229  ok ( fdt_u64 ( &fdt, offset, "max-speed", &u64 ) == 0 );
230  ok ( u64 == 10000000000ULL );
231  ok ( fdt_u64 ( &fdt, offset, "#nonexistent", &u64 ) != 0 );
232 
233  /* Verify 32-bit integer properties */
234  ok ( fdt_u32 ( &fdt, 0, "#address-cells", &u32 ) == 0 );
235  ok ( u32 == 2 );
236  ok ( fdt_u32 ( &fdt, 0, "#nonexistent", &u32 ) != 0 );
237  ok ( fdt_path ( &fdt, "/soc/ethernet@10090000", &offset ) == 0 );
238  ok ( fdt_u32 ( &fdt, offset, "max-speed", &u32 ) != 0 );
239 
240  /* Verify phandle properties */
241  ok ( fdt_path ( &fdt, "/cpus/cpu@0/interrupt-controller",
242  &offset ) == 0 );
243  ok ( fdt_phandle ( &fdt, offset ) == 0x03 );
244  ok ( fdt_path ( &fdt, "/soc/ethernet@10090000/ethernet-phy@0",
245  &offset ) == 0 );
246  ok ( fdt_phandle ( &fdt, offset ) == 0x08 );
247  ok ( fdt_path ( &fdt, "/soc/serial@10010000", &offset ) == 0 );
248  ok ( fdt_phandle ( &fdt, offset ) == 0 );
249 
250  /* Verify cell properties */
251  ok ( fdt_path ( &fdt, "/soc/ethernet@10090000", &offset ) == 0 );
252  ok ( fdt_cells ( &fdt, offset, "reg", 4, 2, &u64 ) == 0 );
253  ok ( u64 == 0x100a0000 );
254  ok ( fdt_cells ( &fdt, offset, "reg", 6, 2, &u64 ) == 0 );
255  ok ( u64 == 0x1000 );
256  ok ( fdt_cells ( &fdt, offset, "reg", 0, 2, &u64 ) == 0 );
257  ok ( u64 == 0x10090000 );
258  ok ( fdt_cells ( &fdt, offset, "reg", 6, 0, &u64 ) == 0 );
259  ok ( u64 == 0x1000 );
260  ok ( fdt_cells ( &fdt, offset, "reg", 8, 0, &u64 ) == 0 );
261  ok ( u64 == 0 );
262  ok ( fdt_cells ( &fdt, offset, "reg", 7, 2, &u64 ) != 0 );
263  ok ( fdt_cells ( &fdt, offset, "notareg", 0, 1, &u64 ) != 0 );
264 
265  /* Verify alias lookup */
266  ok ( fdt_alias ( &fdt, "serial0", &offset ) == 0 );
267  ok ( ( string = fdt_string ( &fdt, offset, "compatible" ) ) != NULL );
268  ok ( strcmp ( string, "sifive,uart0" ) == 0 );
269  ok ( fdt_alias ( &fdt, "nonexistent0", &offset ) != 0 );
270  ok ( fdt_alias ( &fdt, "ethernet0:params", &offset ) == 0 );
271  ok ( ( string = fdt_string ( &fdt, offset, "phy-mode" ) ) != NULL );
272  ok ( strcmp ( string, "gmii" ) == 0 );
273 
274  /* Verify node description */
275  ok ( fdt_path ( &fdt, "/memory@80000000", &offset ) == 0 );
276  ok ( fdt_describe ( &fdt, offset, &desc ) == 0 );
277  ok ( desc.offset == offset );
278  ok ( strcmp ( desc.name, "memory@80000000" ) == 0 );
279  ok ( desc.data == NULL );
280  ok ( desc.len == 0 );
281  ok ( desc.depth == +1 );
282  ok ( fdt_describe ( &fdt, desc.next, &desc ) == 0 );
283  ok ( strcmp ( desc.name, "device_type" ) == 0 );
284  ok ( strcmp ( desc.data, "memory" ) == 0 );
285  ok ( desc.depth == 0 );
286 
287  /* Verify parent lookup */
288  ok ( fdt_path ( &fdt, "/soc/ethernet@10090000/ethernet-phy@0",
289  &offset ) == 0 );
290  ok ( fdt_parent ( &fdt, offset, &offset ) == 0 );
291  ok ( fdt_describe ( &fdt, offset, &desc ) == 0 );
292  ok ( strcmp ( desc.name, "ethernet@10090000" ) == 0 );
293  ok ( fdt_parent ( &fdt, offset, &offset ) == 0 );
294  ok ( fdt_describe ( &fdt, offset, &desc ) == 0 );
295  ok ( strcmp ( desc.name, "soc" ) == 0 );
296  ok ( fdt_parent ( &fdt, offset, &offset ) == 0 );
297  ok ( offset == 0 );
298 
299  /* Verify device tree creation */
300  image = image_memory ( "test.dtb", sifive_u, sizeof ( sifive_u ) );
301  ok ( image != NULL );
302  image_tag ( image, &fdt_image );
303  ok ( fdt_create ( &hdr, "hello world", 0xabcd0000, 0x00001234 ) == 0 );
304  ok ( fdt_parse ( &fdt, hdr, -1UL ) == 0 );
305  ok ( fdt_path ( &fdt, "/chosen", &offset ) == 0 );
306  ok ( ( string = fdt_string ( &fdt, offset, "bootargs" ) ) != NULL );
307  ok ( strcmp ( string, "hello world" ) == 0 );
308  ok ( fdt_u64 ( &fdt, offset, "linux,initrd-start", &u64 ) == 0 );
309  ok ( u64 == 0xabcd0000 );
310  ok ( fdt_u64 ( &fdt, offset, "linux,initrd-end", &u64 ) == 0 );
311  ok ( u64 == 0xabcd1234 );
312  fdt_remove ( hdr );
314 }
int fdt_parent(struct fdt *fdt, unsigned int offset, unsigned int *parent)
Find parent node.
Definition: fdt.c:292
int fdt_alias(struct fdt *fdt, const char *name, unsigned int *offset)
Find node by alias.
Definition: fdt.c:465
size_t len
Length of tree.
Definition: fdt.h:97
int fdt_parse(struct fdt *fdt, struct fdt_header *hdr, size_t max_len)
Parse device tree.
Definition: fdt.c:903
Device tree header.
Definition: fdt.h:18
static const uint8_t sifive_u[]
Simplified QEMU sifive_u device tree blob.
Definition: fdt_test.c:41
struct golan_inbox_hdr hdr
Message header.
Definition: CIB_PRM.h:28
int fdt_describe(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Describe device tree token.
Definition: fdt.c:89
int fdt_path(struct fdt *fdt, const char *path, unsigned int *offset)
Find node by path.
Definition: fdt.c:425
const char * fdt_strings(struct fdt *fdt, unsigned int offset, const char *name, unsigned int *count)
Find strings property.
Definition: fdt.c:539
unsigned long long uint64_t
Definition: stdint.h:13
uint32_t string
Definition: multiboot.h:14
An executable image.
Definition: image.h:23
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
uint32_t fdt_phandle(struct fdt *fdt, unsigned int offset)
Get package handle (phandle) property.
Definition: fdt.c:689
int fdt_u32(struct fdt *fdt, unsigned int offset, const char *name, uint32_t *value)
Get 32-bit integer property.
Definition: fdt.c:663
#define u32
Definition: vga.h:21
void fdt_remove(struct fdt_header *hdr)
Remove device tree.
Definition: fdt.c:1458
uint64_t u64
Definition: stdint.h:25
static unsigned int count
Number of entries.
Definition: dwmac.h:225
const char * fdt_string(struct fdt *fdt, unsigned int offset, const char *name)
Find string property.
Definition: fdt.c:578
unsigned int uint32_t
Definition: stdint.h:12
struct image * image_memory(const char *name, const void *data, size_t len)
Create registered image from block of memory.
Definition: image.c:608
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:357
A device tree.
Definition: fdt.h:88
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
static struct image * image_tag(struct image *image, struct image_tag *tag)
Tag image.
Definition: image.h:296
#define ok(success)
Definition: test.h:46
int fdt_create(struct fdt_header **hdr, const char *cmdline, physaddr_t initrd, size_t initrd_len)
Create device tree.
Definition: fdt.c:1407
int fdt_u64(struct fdt *fdt, unsigned int offset, const char *name, uint64_t *value)
Get 64-bit integer property.
Definition: fdt.c:643
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
int fdt_cells(struct fdt *fdt, unsigned int offset, const char *name, unsigned int index, unsigned int count, uint64_t *value)
Get integer property.
Definition: fdt.c:597
uint32_t u32
Definition: stdint.h:23

References count, desc, fdt_alias(), fdt_cells(), fdt_create(), fdt_describe(), fdt_parent(), fdt_parse(), fdt_path(), fdt_phandle(), fdt_remove(), fdt_string(), fdt_strings(), fdt_u32(), fdt_u64(), hdr, image_memory(), image_tag(), fdt::len, memcmp(), NULL, offset, ok, sifive_u, strcmp(), string, u32, and unregister_image().

Variable Documentation

◆ sifive_u

const uint8_t sifive_u[]
static

Simplified QEMU sifive_u device tree blob.

Definition at line 41 of file fdt_test.c.

Referenced by fdt_test_exec().

◆ __self_test

struct self_test fdt_test __self_test
Initial value:
= {
.name = "fdt",
.exec = fdt_test_exec,
}
static void fdt_test_exec(void)
Perform FDT self-test.
Definition: fdt_test.c:167

FDT self-test.

Definition at line 317 of file fdt_test.c.