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_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
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:256
uint8_t * tmp
Definition: entropy.h:156
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

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 userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
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 NULL
NULL pointer (VOID *)
Definition: Base.h:362

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
#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
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
size_t strnlen(const char *src, size_t max)
Get length of string.
Definition: string.c:240
A device tree.
Definition: fdt.h:76
A lexical descriptor.
Definition: fdt.c:51
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References fdt_descriptor::data, DBGC, fdt_property(), fdt_descriptor::len, name, NULL, offset, rc, and strnlen().

Referenced by fdt_alias(), and register_fdt().

◆ 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 377 of file fdt.c.

377  {
378  struct fdt_descriptor desc;
379  size_t len;
380  int rc;
381 
382  /* Find applicable MAC address property */
383  if ( ( ( rc = fdt_property ( offset, "mac-address", &desc ) ) != 0 ) &&
384  ( ( rc = fdt_property ( offset, "local-mac-address",
385  &desc ) ) != 0 ) ) {
386  return rc;
387  }
388 
389  /* Check length */
391  if ( len != desc.len ) {
392  DBGC ( &fdt, "FDT malformed MAC address \"%s\":\n",
393  desc.name );
394  DBGC_HDA ( &fdt, 0, desc.data, desc.len );
395  return -ERANGE;
396  }
397 
398  /* Fill in MAC address */
399  memcpy ( netdev->hw_addr, desc.data, len );
400 
401  return 0;
402 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
size_t len
Length of tree.
Definition: fdt.h:85
#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 userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
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
uint32_t len
Length.
Definition: ena.h:14
A lexical descriptor.
Definition: fdt.c:51
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:375
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:366

References fdt_descriptor::data, DBGC, DBGC_HDA, ERANGE, fdt_property(), net_device::hw_addr, ll_protocol::hw_addr_len, len, fdt_descriptor::len, net_device::ll_protocol, memcpy(), fdt_descriptor::name, 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 410 of file fdt.c.

410  {
411  const uint8_t *end;
412 
413  /* Record device tree location */
414  fdt.hdr = hdr;
415  fdt.len = be32_to_cpu ( hdr->totalsize );
416  DBGC ( &fdt, "FDT version %d at %p+%#04zx\n",
417  be32_to_cpu ( hdr->version ), fdt.hdr, fdt.len );
418 
419  /* Check signature */
420  if ( hdr->magic != cpu_to_be32 ( FDT_MAGIC ) ) {
421  DBGC ( &fdt, "FDT has invalid magic value %#08x\n",
422  be32_to_cpu ( hdr->magic ) );
423  goto err;
424  }
425 
426  /* Check version */
427  if ( hdr->last_comp_version != cpu_to_be32 ( FDT_VERSION ) ) {
428  DBGC ( &fdt, "FDT unsupported version %d\n",
429  be32_to_cpu ( hdr->last_comp_version ) );
430  goto err;
431  }
432 
433  /* Record structure block location */
434  fdt.structure = be32_to_cpu ( hdr->off_dt_struct );
435  fdt.structure_len = be32_to_cpu ( hdr->size_dt_struct );
436  DBGC ( &fdt, "FDT structure block at +[%#04x,%#04zx)\n",
438  if ( ( fdt.structure > fdt.len ) ||
439  ( fdt.structure_len > ( fdt.len - fdt.structure ) ) ) {
440  DBGC ( &fdt, "FDT structure block exceeds table\n" );
441  goto err;
442  }
443  if ( ( fdt.structure | fdt.structure_len ) &
444  ( FDT_STRUCTURE_ALIGN - 1 ) ) {
445  DBGC ( &fdt, "FDT structure block is misaligned\n" );
446  goto err;
447  }
448 
449  /* Record strings block location */
450  fdt.strings = be32_to_cpu ( hdr->off_dt_strings );
451  fdt.strings_len = be32_to_cpu ( hdr->size_dt_strings );
452  DBGC ( &fdt, "FDT strings block at +[%#04x,%#04zx)\n",
454  if ( ( fdt.strings > fdt.len ) ||
455  ( fdt.strings_len > ( fdt.len - fdt.strings ) ) ) {
456  DBGC ( &fdt, "FDT strings block exceeds table\n" );
457  goto err;
458  }
459 
460  /* Shrink strings block to ensure NUL termination safety */
461  end = ( fdt.raw + fdt.strings + fdt.strings_len );
462  for ( ; fdt.strings_len ; fdt.strings_len-- ) {
463  if ( *(--end) == '\0' )
464  break;
465  }
466  if ( fdt.strings_len != be32_to_cpu ( hdr->size_dt_strings ) ) {
467  DBGC ( &fdt, "FDT strings block shrunk to +[%#04x,%#04zx)\n",
469  }
470 
471  /* Print model name (for debugging) */
472  DBGC ( &fdt, "FDT model is \"%s\"\n", fdt_string ( 0, "model" ) );
473 
474  return 0;
475 
476  err:
477  DBGC_HDA ( &fdt, 0, hdr, sizeof ( *hdr ) );
478  fdt.hdr = NULL;
479  return -EINVAL;
480 }
#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
#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
uint32_t hdr
Message header.
Definition: intelvf.h:12
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:362
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, hdr, fdt::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

◆ 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__