iPXE
Data Structures | Macros | Typedefs | Functions | Variables
fdt.h File Reference

Flattened Device Tree. More...

#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  fdt_header
 Device tree header. More...
 
struct  fdt_prop
 Property fragment. More...
 
struct  fdt
 A device tree. More...
 

Macros

#define FDT_MAGIC   0xd00dfeed
 Magic signature. More...
 
#define FDT_VERSION   16
 Expected device tree version. More...
 
#define FDT_BEGIN_NODE   0x00000001
 Begin node token. More...
 
#define FDT_END_NODE   0x00000002
 End node token. More...
 
#define FDT_PROP   0x00000003
 Property token. More...
 
#define FDT_NOP   0x00000004
 NOP token. More...
 
#define FDT_END   0x00000009
 End of structure block. More...
 
#define FDT_STRUCTURE_ALIGN   ( sizeof ( fdt_token_t ) )
 Alignment of structure block. More...
 

Typedefs

typedef uint32_t fdt_token_t
 Device tree token. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
struct fdt_header __attribute__ ((packed))
 
int fdt_path (const char *path, unsigned int *offset)
 Find node by path. More...
 
int fdt_alias (const char *name, unsigned int *offset)
 Find node by alias. More...
 
const char * fdt_string (unsigned int offset, const char *name)
 Find string property. More...
 
int fdt_u64 (unsigned int offset, const char *name, uint64_t *value)
 Find integer property. More...
 
int fdt_mac (unsigned int offset, struct net_device *netdev)
 Get MAC address from property. More...
 
int register_fdt (const struct fdt_header *hdr)
 Register device tree. More...
 

Variables

uint32_t magic
 Magic signature. More...
 
uint32_t totalsize
 Total size of device tree. More...
 
uint32_t off_dt_struct
 Offset to structure block. More...
 
uint32_t off_dt_strings
 Offset to strings block. More...
 
uint32_t off_mem_rsvmap
 Offset to memory reservation block. More...
 
uint32_t version
 Version of this data structure. More...
 
uint32_t last_comp_version
 Lowest version to which this structure is compatible. More...
 
uint32_t boot_cpuid_phys
 Physical ID of the boot CPU. More...
 
uint32_t size_dt_strings
 Length of string block. More...
 
uint32_t size_dt_struct
 Length of structure block. More...
 
uint32_t len
 Data length. More...
 
uint32_t name_off
 Name offset. More...
 
struct fdt __attribute__
 

Detailed Description

Flattened Device Tree.

Definition in file fdt.h.

Macro Definition Documentation

◆ FDT_MAGIC

#define FDT_MAGIC   0xd00dfeed

Magic signature.

Definition at line 41 of file fdt.h.

◆ FDT_VERSION

#define FDT_VERSION   16

Expected device tree version.

Definition at line 44 of file fdt.h.

◆ FDT_BEGIN_NODE

#define FDT_BEGIN_NODE   0x00000001

Begin node token.

Definition at line 50 of file fdt.h.

◆ FDT_END_NODE

#define FDT_END_NODE   0x00000002

End node token.

Definition at line 53 of file fdt.h.

◆ FDT_PROP

#define FDT_PROP   0x00000003

Property token.

Definition at line 56 of file fdt.h.

◆ FDT_NOP

#define FDT_NOP   0x00000004

NOP token.

Definition at line 67 of file fdt.h.

◆ FDT_END

#define FDT_END   0x00000009

End of structure block.

Definition at line 70 of file fdt.h.

◆ FDT_STRUCTURE_ALIGN

#define FDT_STRUCTURE_ALIGN   ( sizeof ( fdt_token_t ) )

Alignment of structure block.

Definition at line 73 of file fdt.h.

Typedef Documentation

◆ fdt_token_t

Device tree token.

Definition at line 47 of file fdt.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __attribute__()

struct fdt_header __attribute__ ( (packed)  )

◆ fdt_path()

int fdt_path ( const char *  path,
unsigned int *  offset 
)

Find node by path.

Parameters
pathNode path
offsetOffset to fill in
Return values
rcReturn status code

Definition at line 241 of file fdt.c.

241  {
242  char *tmp = ( ( char * ) path );
243  char *del;
244  int rc;
245 
246  /* Initialise offset */
247  *offset = 0;
248 
249  /* Traverse tree one path segment at a time */
250  while ( *tmp ) {
251 
252  /* Skip any leading '/' */
253  while ( *tmp == '/' )
254  tmp++;
255 
256  /* Find next '/' delimiter and convert to NUL */
257  del = strchr ( tmp, '/' );
258  if ( del )
259  *del = '\0';
260 
261  /* Find child and restore delimiter */
262  rc = fdt_child ( *offset, tmp, offset );
263  if ( del )
264  *del = '/';
265  if ( rc != 0 )
266  return rc;
267 
268  /* Move to next path component, if any */
269  while ( *tmp && ( *tmp != '/' ) )
270  tmp++;
271  }
272 
273  DBGC2 ( &fdt, "FDT found path \"%s\" at +%#04x\n", path, *offset );
274  return 0;
275 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned long tmp
Definition: linux_pci.h:63
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:271
static int fdt_child(unsigned int offset, const char *name, unsigned int *child)
Find child node.
Definition: fdt.c:193
A device tree.
Definition: fdt.h:76
#define DBGC2(...)
Definition: compiler.h:522
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References DBGC2, fdt_child(), offset, rc, strchr(), and tmp.

Referenced by fdt_alias().

◆ fdt_alias()

int fdt_alias ( const char *  name,
unsigned int *  offset 
)

Find node by alias.

Parameters
nameAlias name
offsetOffset to fill in
Return values
rcReturn status code

Definition at line 284 of file fdt.c.

284  {
285  const char *alias;
286  int rc;
287 
288  /* Locate "/aliases" node */
289  if ( ( rc = fdt_child ( 0, "aliases", offset ) ) != 0 )
290  return rc;
291 
292  /* Locate alias property */
293  if ( ( alias = fdt_string ( *offset, name ) ) == NULL )
294  return -ENOENT;
295  DBGC ( &fdt, "FDT alias \"%s\" is \"%s\"\n", name, alias );
296 
297  /* Locate aliased node */
298  if ( ( rc = fdt_path ( alias, offset ) ) != 0 )
299  return rc;
300 
301  return 0;
302 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
int fdt_path(const char *path, unsigned int *offset)
Find node by path.
Definition: fdt.c:241
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
const char * fdt_string(unsigned int offset, const char *name)
Find string property.
Definition: fdt.c:352
static int fdt_child(unsigned int offset, const char *name, unsigned int *child)
Find child node.
Definition: fdt.c:193
A device tree.
Definition: fdt.h:76
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References DBGC, ENOENT, fdt_child(), fdt_path(), fdt_string(), name, NULL, offset, and rc.

Referenced by smscusb_fdt_fetch_mac().

◆ fdt_string()

const char* fdt_string ( unsigned int  offset,
const char *  name 
)

Find string property.

Parameters
offsetStarting node offset
nameProperty name
Return values
stringString property, or NULL on error

Definition at line 352 of file fdt.c.

352  {
353  struct fdt_descriptor desc;
354  int rc;
355 
356  /* Find property */
357  if ( ( rc = fdt_property ( offset, name, &desc ) ) != 0 )
358  return NULL;
359 
360  /* Check NUL termination */
361  if ( strnlen ( desc.data, desc.len ) == desc.len ) {
362  DBGC ( &fdt, "FDT unterminated string property \"%s\"\n",
363  name );
364  return NULL;
365  }
366 
367  return desc.data;
368 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
uint64_t desc
Microcode descriptor list physical address.
Definition: ucode.h:12
#define DBGC(...)
Definition: compiler.h:505
static int fdt_property(unsigned int offset, const char *name, struct fdt_descriptor *desc)
Find property.
Definition: fdt.c:312
size_t strnlen(const char *src, size_t max)
Get length of string.
Definition: string.c:255
A device tree.
Definition: fdt.h:76
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
A lexical descriptor.
Definition: fdt.c:51
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References DBGC, desc, fdt_property(), name, NULL, offset, rc, and strnlen().

Referenced by fdt_alias(), and register_fdt().

◆ fdt_u64()

int fdt_u64 ( unsigned int  offset,
const char *  name,
uint64_t value 
)

Find integer property.

Parameters
offsetStarting node offset
nameProperty name
valueInteger value to fill in
Return values
rcReturn status code

Definition at line 378 of file fdt.c.

378  {
379  struct fdt_descriptor desc;
380  const uint8_t *data;
381  size_t remaining;
382  int rc;
383 
384  /* Clear value */
385  *value = 0;
386 
387  /* Find property */
388  if ( ( rc = fdt_property ( offset, name, &desc ) ) != 0 )
389  return rc;
390 
391  /* Check range */
392  if ( desc.len > sizeof ( *value ) ) {
393  DBGC ( &fdt, "FDT oversized integer property \"%s\"\n", name );
394  return -ERANGE;
395  }
396 
397  /* Parse value */
398  data = desc.data;
399  remaining = desc.len;
400  while ( remaining-- ) {
401  *value <<= 8;
402  *value |= *(data++);
403  }
404 
405  return 0;
406 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
uint64_t desc
Microcode descriptor list physical address.
Definition: ucode.h:12
#define DBGC(...)
Definition: compiler.h:505
static int fdt_property(unsigned int offset, const char *name, struct fdt_descriptor *desc)
Find property.
Definition: fdt.c:312
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define ERANGE
Result too large.
Definition: errno.h:639
unsigned char uint8_t
Definition: stdint.h:10
A device tree.
Definition: fdt.h:76
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
A lexical descriptor.
Definition: fdt.c:51

References data, DBGC, desc, ERANGE, fdt_property(), name, offset, rc, and value.

◆ fdt_mac()

int fdt_mac ( unsigned int  offset,
struct net_device netdev 
)

Get MAC address from property.

Parameters
offsetStarting node offset
netdevNetwork device
Return values
rcReturn status code

Definition at line 415 of file fdt.c.

415  {
416  struct fdt_descriptor desc;
417  size_t len;
418  int rc;
419 
420  /* Find applicable MAC address property */
421  if ( ( ( rc = fdt_property ( offset, "mac-address", &desc ) ) != 0 ) &&
422  ( ( rc = fdt_property ( offset, "local-mac-address",
423  &desc ) ) != 0 ) ) {
424  return rc;
425  }
426 
427  /* Check length */
429  if ( len != desc.len ) {
430  DBGC ( &fdt, "FDT malformed MAC address \"%s\":\n",
431  desc.name );
432  DBGC_HDA ( &fdt, 0, desc.data, desc.len );
433  return -ERANGE;
434  }
435 
436  /* Fill in MAC address */
437  memcpy ( netdev->hw_addr, desc.data, len );
438 
439  return 0;
440 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint64_t desc
Microcode descriptor list physical address.
Definition: ucode.h:12
#define DBGC(...)
Definition: compiler.h:505
static int fdt_property(unsigned int offset, const char *name, struct fdt_descriptor *desc)
Find property.
Definition: fdt.c:312
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint8_t hw_addr_len
Hardware address length.
Definition: netdevice.h:196
#define DBGC_HDA(...)
Definition: compiler.h:506
static struct net_device * netdev
Definition: gdbudp.c:52
#define ERANGE
Result too large.
Definition: errno.h:639
A device tree.
Definition: fdt.h:76
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
A lexical descriptor.
Definition: fdt.c:51
uint32_t len
Length.
Definition: ena.h:14
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:381
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:372

References DBGC, DBGC_HDA, desc, ERANGE, fdt_property(), net_device::hw_addr, ll_protocol::hw_addr_len, len, net_device::ll_protocol, memcpy(), netdev, offset, and rc.

Referenced by smscusb_fdt_fetch_mac().

◆ register_fdt()

int register_fdt ( const struct fdt_header hdr)

Register device tree.

Parameters
fdtDevice tree header
Return values
rcReturn status code

Definition at line 448 of file fdt.c.

448  {
449  const uint8_t *end;
450 
451  /* Record device tree location */
452  fdt.hdr = hdr;
453  fdt.len = be32_to_cpu ( hdr->totalsize );
454  DBGC ( &fdt, "FDT version %d at %p+%#04zx\n",
455  be32_to_cpu ( hdr->version ), fdt.hdr, fdt.len );
456 
457  /* Check signature */
458  if ( hdr->magic != cpu_to_be32 ( FDT_MAGIC ) ) {
459  DBGC ( &fdt, "FDT has invalid magic value %#08x\n",
460  be32_to_cpu ( hdr->magic ) );
461  goto err;
462  }
463 
464  /* Check version */
465  if ( hdr->last_comp_version != cpu_to_be32 ( FDT_VERSION ) ) {
466  DBGC ( &fdt, "FDT unsupported version %d\n",
467  be32_to_cpu ( hdr->last_comp_version ) );
468  goto err;
469  }
470 
471  /* Record structure block location */
472  fdt.structure = be32_to_cpu ( hdr->off_dt_struct );
473  fdt.structure_len = be32_to_cpu ( hdr->size_dt_struct );
474  DBGC ( &fdt, "FDT structure block at +[%#04x,%#04zx)\n",
476  if ( ( fdt.structure > fdt.len ) ||
477  ( fdt.structure_len > ( fdt.len - fdt.structure ) ) ) {
478  DBGC ( &fdt, "FDT structure block exceeds table\n" );
479  goto err;
480  }
481  if ( ( fdt.structure | fdt.structure_len ) &
482  ( FDT_STRUCTURE_ALIGN - 1 ) ) {
483  DBGC ( &fdt, "FDT structure block is misaligned\n" );
484  goto err;
485  }
486 
487  /* Record strings block location */
488  fdt.strings = be32_to_cpu ( hdr->off_dt_strings );
489  fdt.strings_len = be32_to_cpu ( hdr->size_dt_strings );
490  DBGC ( &fdt, "FDT strings block at +[%#04x,%#04zx)\n",
492  if ( ( fdt.strings > fdt.len ) ||
493  ( fdt.strings_len > ( fdt.len - fdt.strings ) ) ) {
494  DBGC ( &fdt, "FDT strings block exceeds table\n" );
495  goto err;
496  }
497 
498  /* Shrink strings block to ensure NUL termination safety */
499  end = ( fdt.raw + fdt.strings + fdt.strings_len );
500  for ( ; fdt.strings_len ; fdt.strings_len-- ) {
501  if ( *(--end) == '\0' )
502  break;
503  }
504  if ( fdt.strings_len != be32_to_cpu ( hdr->size_dt_strings ) ) {
505  DBGC ( &fdt, "FDT strings block shrunk to +[%#04x,%#04zx)\n",
507  }
508 
509  /* Print model name (for debugging) */
510  DBGC ( &fdt, "FDT model is \"%s\"\n", fdt_string ( 0, "model" ) );
511 
512  return 0;
513 
514  err:
515  DBGC_HDA ( &fdt, 0, hdr, sizeof ( *hdr ) );
516  fdt.hdr = NULL;
517  return -EINVAL;
518 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
#define FDT_STRUCTURE_ALIGN
Alignment of structure block.
Definition: fdt.h:73
size_t len
Length of tree.
Definition: fdt.h:85
struct golan_inbox_hdr hdr
Message header.
Definition: CIB_PRM.h:28
#define DBGC(...)
Definition: compiler.h:505
const struct fdt_header * hdr
Tree header.
Definition: fdt.h:80
size_t strings_len
Length of strings block.
Definition: fdt.h:93
const char * fdt_string(unsigned int offset, const char *name)
Find string property.
Definition: fdt.c:352
#define be32_to_cpu(value)
Definition: byteswap.h:116
#define FDT_MAGIC
Magic signature.
Definition: fdt.h:41
#define DBGC_HDA(...)
Definition: compiler.h:506
unsigned int structure
Offset to structure block.
Definition: fdt.h:87
#define FDT_VERSION
Expected device tree version.
Definition: fdt.h:44
unsigned char uint8_t
Definition: stdint.h:10
size_t structure_len
Length of structure block.
Definition: fdt.h:89
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:76
const void * raw
Raw data.
Definition: fdt.h:82
uint32_t end
Ending offset.
Definition: netvsc.h:18
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
unsigned int strings
Offset to strings block.
Definition: fdt.h:91

References be32_to_cpu, cpu_to_be32, DBGC, DBGC_HDA, EINVAL, end, FDT_MAGIC, fdt_string(), FDT_STRUCTURE_ALIGN, FDT_VERSION, fdt::hdr, hdr, fdt::len, NULL, fdt::raw, fdt::strings, fdt::strings_len, fdt::structure, and fdt::structure_len.

Referenced by efi_fdt_init().

Variable Documentation

◆ magic

uint32_t magic

Magic signature.

Definition at line 12 of file fdt.h.

◆ totalsize

uint32_t totalsize

Total size of device tree.

Definition at line 14 of file fdt.h.

◆ off_dt_struct

uint32_t off_dt_struct

Offset to structure block.

Definition at line 16 of file fdt.h.

◆ off_dt_strings

uint32_t off_dt_strings

Offset to strings block.

Definition at line 18 of file fdt.h.

◆ off_mem_rsvmap

uint32_t off_mem_rsvmap

Offset to memory reservation block.

Definition at line 20 of file fdt.h.

◆ version

uint32_t version

Version of this data structure.

Definition at line 22 of file fdt.h.

◆ last_comp_version

uint32_t last_comp_version

Lowest version to which this structure is compatible.

Definition at line 24 of file fdt.h.

◆ boot_cpuid_phys

uint32_t boot_cpuid_phys

Physical ID of the boot CPU.

Definition at line 26 of file fdt.h.

◆ size_dt_strings

uint32_t size_dt_strings

Length of string block.

Definition at line 28 of file fdt.h.

◆ size_dt_struct

uint32_t size_dt_struct

Length of structure block.

Definition at line 30 of file fdt.h.

◆ len

uint32_t len

Data length.

Definition at line 49 of file fdt.h.

◆ name_off

uint32_t name_off

Name offset.

Definition at line 51 of file fdt.h.

Referenced by fdt_traverse().

◆ __attribute__