iPXE
Macros | Functions | Variables
fdt.c File Reference

Flattened Device Tree. More...

#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <byteswap.h>
#include <ipxe/netdevice.h>
#include <ipxe/image.h>
#include <ipxe/uaccess.h>
#include <ipxe/umalloc.h>
#include <ipxe/fdt.h>

Go to the source code of this file.

Macros

#define FDT_INSERT_PAD   1024
 Amount of free space to add whenever we have to reallocate a tree. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static int fdt_permitted (char ch)
 Check if character is permitted in a name. More...
 
static int fdt_match (const struct fdt_descriptor *desc, const char *name)
 Compare node name. More...
 
int fdt_describe (struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
 Describe device tree token. More...
 
static int fdt_next (struct fdt *fdt, struct fdt_descriptor *desc)
 Describe next device tree token. More...
 
static int fdt_enter (struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
 Enter node. More...
 
static int fdt_depth (struct fdt *fdt, unsigned int offset, unsigned int target)
 Find node relative depth. More...
 
int fdt_parent (struct fdt *fdt, unsigned int offset, unsigned int *parent)
 Find parent node. More...
 
static int fdt_child (struct fdt *fdt, unsigned int offset, const char *name, unsigned int *child)
 Find child node. More...
 
static int fdt_end (struct fdt *fdt, unsigned int offset, unsigned int *end)
 Find end of node. More...
 
int fdt_path (struct fdt *fdt, const char *path, unsigned int *offset)
 Find node by path. More...
 
int fdt_alias (struct fdt *fdt, const char *name, unsigned int *offset)
 Find node by alias. More...
 
static int fdt_property (struct fdt *fdt, unsigned int offset, const char *name, struct fdt_descriptor *desc)
 Find property. More...
 
const char * fdt_strings (struct fdt *fdt, unsigned int offset, const char *name, unsigned int *count)
 Find strings property. More...
 
const char * fdt_string (struct fdt *fdt, unsigned int offset, const char *name)
 Find string property. More...
 
int fdt_cells (struct fdt *fdt, unsigned int offset, const char *name, unsigned int index, unsigned int count, uint64_t *value)
 Get integer property. More...
 
int fdt_u64 (struct fdt *fdt, unsigned int offset, const char *name, uint64_t *value)
 Get 64-bit integer property. More...
 
int fdt_u32 (struct fdt *fdt, unsigned int offset, const char *name, uint32_t *value)
 Get 32-bit integer property. More...
 
uint32_t fdt_phandle (struct fdt *fdt, unsigned int offset)
 Get package handle (phandle) property. More...
 
void fdt_reg_cells (struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs)
 Get region cell size specification. More...
 
int fdt_parent_reg_cells (struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs)
 Get parent region cell size specification. More...
 
int fdt_reg_count (struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs)
 Get number of regions. More...
 
int fdt_reg_address (struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs, unsigned int index, uint64_t *address)
 Get region address. More...
 
int fdt_reg_size (struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs, unsigned int index, uint64_t *size)
 Get region size. More...
 
int fdt_reg (struct fdt *fdt, unsigned int offset, uint64_t *region)
 Get unsized single-entry region address. More...
 
int fdt_mac (struct fdt *fdt, unsigned int offset, struct net_device *netdev)
 Get MAC address from property. More...
 
int fdt_parse (struct fdt *fdt, struct fdt_header *hdr, size_t max_len)
 Parse device tree. More...
 
static int fdt_parse_image (struct fdt *fdt, struct image *image)
 Parse device tree image. More...
 
static int fdt_insert (struct fdt *fdt, unsigned int offset, size_t len)
 Insert empty space. More...
 
static void fdt_nop (struct fdt *fdt, unsigned int offset, size_t len)
 Fill space in structure block with FDT_NOP. More...
 
static int fdt_insert_nop (struct fdt *fdt, unsigned int offset, size_t len)
 Insert FDT_NOP padded space in structure block. More...
 
static int fdt_insert_string (struct fdt *fdt, const char *string, unsigned int *offset)
 Insert string in strings block. More...
 
static int fdt_ensure_child (struct fdt *fdt, unsigned int offset, const char *name, unsigned int *child)
 Ensure child node exists. More...
 
static int fdt_set (struct fdt *fdt, unsigned int offset, const char *name, const void *data, size_t len)
 Set property value. More...
 
static int fdt_urealloc (struct fdt *fdt, size_t len)
 Reallocate device tree via urealloc() More...
 
static int fdt_bootargs (struct fdt *fdt, const char *cmdline, physaddr_t initrd, size_t initrd_len)
 Populate device tree with boot arguments. More...
 
int fdt_create (struct fdt_header **hdr, const char *cmdline, physaddr_t initrd, size_t initrd_len)
 Create device tree. More...
 
void fdt_remove (struct fdt_header *hdr)
 Remove device tree. More...
 
 REQUIRING_SYMBOL (fdt_describe)
 
 REQUIRE_OBJECT (config_fdt)
 

Variables

struct fdt sysfdt
 The system flattened device tree (if present) More...
 
struct image_tag fdt_image __image_tag
 The downloaded flattened device tree tag. More...
 

Detailed Description

Flattened Device Tree.

Definition in file fdt.c.

Macro Definition Documentation

◆ FDT_INSERT_PAD

#define FDT_INSERT_PAD   1024

Amount of free space to add whenever we have to reallocate a tree.

Definition at line 52 of file fdt.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ fdt_permitted()

static int fdt_permitted ( char  ch)
static

Check if character is permitted in a name.

Parameters
chCharacter
Return values
is_permittedCharacter is permitted in a name

Definition at line 60 of file fdt.c.

60  {
61  static const char permitted[] = ",._+?#-";
62 
63  return ( isalnum ( ch ) || strchr ( permitted, ch ) );
64 }
uint8_t ch
Definition: registers.h:83
static int isalnum(int character)
Check if character is alphanumeric.
Definition: ctype.h:86
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:271

References ch, isalnum(), and strchr().

Referenced by fdt_match().

◆ fdt_match()

static int fdt_match ( const struct fdt_descriptor desc,
const char *  name 
)
static

Compare node name.

Parameters
descToken descriptor
nameName (terminated by NUL or any non-permitted character)
Return values
is_matchName matches token descriptor

Definition at line 73 of file fdt.c.

73  {
74  size_t len = strlen ( desc->name );
75 
76  /* Check name and terminator */
77  return ( ( memcmp ( desc->name, name, len ) == 0 ) &&
78  ( ! ( name[len] && fdt_permitted ( name[len] ) ) ) );
79 }
const char * name
Definition: ath9k_hw.c:1984
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
ring len
Length.
Definition: dwmac.h:231
static int fdt_permitted(char ch)
Check if character is permitted in a name.
Definition: fdt.c:60
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114

References desc, fdt_permitted(), len, memcmp(), name, and strlen().

Referenced by fdt_child(), and fdt_property().

◆ fdt_describe()

int fdt_describe ( struct fdt fdt,
unsigned int  offset,
struct fdt_descriptor desc 
)

Describe device tree token.

Parameters
fdtDevice tree
offsetOffset within structure block
descToken descriptor to fill in
Return values
rcReturn status code

Definition at line 89 of file fdt.c.

90  {
91  const fdt_token_t *token;
92  const void *data;
93  const struct fdt_prop *prop;
94  unsigned int name_off;
95  size_t remaining;
96  size_t len;
97 
98  /* Sanity checks */
99  assert ( offset <= fdt->len );
100  assert ( ( offset & ( FDT_STRUCTURE_ALIGN - 1 ) ) == 0 );
101 
102  /* Initialise descriptor */
103  memset ( desc, 0, sizeof ( *desc ) );
104  desc->offset = offset;
105 
106  /* Locate token and calculate remaining space */
107  token = ( fdt->raw + fdt->structure + offset );
108  remaining = ( fdt->len - offset );
109  if ( remaining < sizeof ( *token ) ) {
110  DBGC ( fdt, "FDT truncated tree at +%#04x\n", offset );
111  return -EINVAL;
112  }
113  remaining -= sizeof ( *token );
114  data = ( ( ( const void * ) token ) + sizeof ( *token ) );
115  len = 0;
116 
117  /* Handle token */
118  switch ( *token ) {
119 
120  case cpu_to_be32 ( FDT_BEGIN_NODE ):
121 
122  /* Start of node */
123  desc->name = data;
124  len = ( strnlen ( desc->name, remaining ) + 1 /* NUL */ );
125  if ( remaining < len ) {
126  DBGC ( fdt, "FDT unterminated node name at +%#04x\n",
127  offset );
128  return -EINVAL;
129  }
130  desc->depth = +1;
131  break;
132 
133  case cpu_to_be32 ( FDT_END_NODE ):
134 
135  /* End of node */
136  desc->depth = -1;
137  break;
138 
139  case cpu_to_be32 ( FDT_PROP ):
140 
141  /* Property */
142  prop = data;
143  if ( remaining < sizeof ( *prop ) ) {
144  DBGC ( fdt, "FDT truncated property at +%#04x\n",
145  offset );
146  return -EINVAL;
147  }
148  desc->data = ( ( ( const void * ) prop ) + sizeof ( *prop ) );
149  desc->len = be32_to_cpu ( prop->len );
150  len = ( sizeof ( *prop ) + desc->len );
151  if ( remaining < len ) {
152  DBGC ( fdt, "FDT overlength property at +%#04x\n",
153  offset );
154  return -EINVAL;
155  }
156  name_off = be32_to_cpu ( prop->name_off );
157  if ( name_off > fdt->strings_len ) {
158  DBGC ( fdt, "FDT property name outside strings "
159  "block at +%#04x\n", offset );
160  return -EINVAL;
161  }
162  desc->name = ( fdt->raw + fdt->strings + name_off );
163  break;
164 
165  case cpu_to_be32 ( FDT_NOP ):
166 
167  /* Do nothing */
168  break;
169 
170  default:
171 
172  /* Unrecognised or unexpected token */
173  DBGC ( fdt, "FDT unexpected token %#08x at +%#04x\n",
174  be32_to_cpu ( *token ), offset );
175  return -EINVAL;
176  }
177 
178  /* Calculate offset to next token */
179  len = ( ( len + FDT_STRUCTURE_ALIGN - 1 ) &
180  ~( FDT_STRUCTURE_ALIGN - 1 ) );
181  offset += ( sizeof ( *token ) + len );
182  desc->next = offset;
183 
184  /* Sanity checks */
185  assert ( offset <= fdt->len );
186 
187  return 0;
188 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
const char * name
Definition: ath9k_hw.c:1984
#define FDT_STRUCTURE_ALIGN
Alignment of structure block.
Definition: fdt.h:74
#define FDT_NOP
NOP token.
Definition: fdt.h:68
size_t len
Length of tree.
Definition: fdt.h:97
void * raw
Raw data.
Definition: fdt.h:94
#define DBGC(...)
Definition: compiler.h:505
size_t strings_len
Length of strings block.
Definition: fdt.h:107
uint32_t fdt_token_t
Device tree token.
Definition: fdt.h:48
uint32_t len
Data length.
Definition: fdt.h:62
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
#define be32_to_cpu(value)
Definition: byteswap.h:116
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
unsigned int structure
Offset to structure block.
Definition: fdt.h:101
Property fragment.
Definition: fdt.h:60
size_t strnlen(const char *src, size_t max)
Get length of string.
Definition: string.c:255
u8 token
Definition: CIB_PRM.h:42
#define FDT_PROP
Property token.
Definition: fdt.h:57
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:88
uint32_t name_off
Name offset.
Definition: fdt.h:64
#define FDT_END_NODE
End node token.
Definition: fdt.h:54
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define FDT_BEGIN_NODE
Begin node token.
Definition: fdt.h:51
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
unsigned int strings
Offset to strings block.
Definition: fdt.h:105
void * memset(void *dest, int character, size_t len) __nonnull

References assert(), be32_to_cpu, cpu_to_be32, data, DBGC, desc, EINVAL, FDT_BEGIN_NODE, FDT_END_NODE, FDT_NOP, FDT_PROP, FDT_STRUCTURE_ALIGN, fdt_prop::len, fdt::len, len, memset(), fdt_prop::name_off, offset, fdt::raw, fdt::strings, fdt::strings_len, strnlen(), fdt::structure, and token.

Referenced by dt_probe_children(), dt_probe_node(), fdt_enter(), fdt_next(), fdt_test_exec(), and fdtmem_update_node().

◆ fdt_next()

static int fdt_next ( struct fdt fdt,
struct fdt_descriptor desc 
)
static

Describe next device tree token.

Parameters
fdtDevice tree
descToken descriptor to update
Return values
rcReturn status code

Definition at line 197 of file fdt.c.

197  {
198 
199  /* Describe next token */
200  return fdt_describe ( fdt, desc->next, desc );
201 }
int fdt_describe(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Describe device tree token.
Definition: fdt.c:89
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
A device tree.
Definition: fdt.h:88

References desc, and fdt_describe().

Referenced by fdt_child(), fdt_depth(), fdt_end(), fdt_parent(), and fdt_property().

◆ fdt_enter()

static int fdt_enter ( struct fdt fdt,
unsigned int  offset,
struct fdt_descriptor desc 
)
static

Enter node.

Parameters
fdtDevice tree
offsetStarting node offset
descBegin node descriptor to fill in
Return values
rcReturn status code

Definition at line 211 of file fdt.c.

212  {
213  int rc;
214 
215  /* Find begin node token */
216  for ( ; ; offset = desc->next ) {
217 
218  /* Describe token */
219  if ( ( rc = fdt_describe ( fdt, offset, desc ) ) != 0 ) {
220  DBGC ( fdt, "FDT +%#04x has malformed node: %s\n",
221  offset, strerror ( rc ) );
222  return rc;
223  }
224 
225  /* Check for begin node token */
226  if ( desc->depth > 0 )
227  return 0;
228 
229  /* Check for non-NOPs */
230  if ( desc->depth ) {
231  DBGC ( fdt, "FDT +%#04x has spurious node end at "
232  "+%#04x\n", offset, desc->offset );
233  return -EINVAL;
234  }
235  if ( desc->name ) {
236  DBGC ( fdt, "FDT +%#04x has spurious property at "
237  "+%#04x\n", offset, desc->offset );
238  return -EINVAL;
239  }
240  }
241 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int fdt_describe(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Describe device tree token.
Definition: fdt.c:89
#define DBGC(...)
Definition: compiler.h:505
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
A device tree.
Definition: fdt.h:88
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References DBGC, desc, EINVAL, fdt_describe(), offset, rc, and strerror().

Referenced by fdt_child(), fdt_depth(), fdt_end(), fdt_parent(), fdt_property(), and fdt_set().

◆ fdt_depth()

static int fdt_depth ( struct fdt fdt,
unsigned int  offset,
unsigned int  target 
)
static

Find node relative depth.

Parameters
fdtDevice tree
offsetStarting node offset
targetTarget node offset
Return values
depthDepth, or negative error

Definition at line 251 of file fdt.c.

252  {
253  struct fdt_descriptor desc;
254  int depth;
255  int rc;
256 
257  /* Enter node */
258  if ( ( rc = fdt_enter ( fdt, offset, &desc ) ) != 0 )
259  return rc;
260 
261  /* Find target node */
262  for ( depth = 0 ; depth >= 0 ; depth += desc.depth ) {
263 
264  /* Describe token */
265  if ( ( rc = fdt_next ( fdt, &desc ) ) != 0 ) {
266  DBGC ( fdt, "FDT +%#04x has malformed node: %s\n",
267  offset, strerror ( rc ) );
268  return rc;
269  }
270 
271  /* Check for target node */
272  if ( desc.offset == target ) {
273  DBGC2 ( fdt, "FDT +%#04x has descendant node +%#04x "
274  "at depth +%d\n", offset, target, depth );
275  return depth;
276  }
277  }
278 
279  DBGC ( fdt, "FDT +#%04x has no descendant node +%#04x\n",
280  offset, target );
281  return -ENOENT;
282 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int depth
Depth change.
Definition: fdt.h:132
static int fdt_enter(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Enter node.
Definition: fdt.c:211
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static int fdt_next(struct fdt *fdt, struct fdt_descriptor *desc)
Describe next device tree token.
Definition: fdt.c:197
A device tree.
Definition: fdt.h:88
#define DBGC2(...)
Definition: compiler.h:522
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References DBGC, DBGC2, fdt_descriptor::depth, desc, ENOENT, fdt_enter(), fdt_next(), offset, rc, and strerror().

Referenced by fdt_parent().

◆ fdt_parent()

int fdt_parent ( struct fdt fdt,
unsigned int  offset,
unsigned int *  parent 
)

Find parent node.

Parameters
fdtDevice tree
offsetStarting node offset
parentParent node offset to fill in
Return values
rcReturn status code

Definition at line 292 of file fdt.c.

292  {
293  struct fdt_descriptor desc;
294  int pdepth;
295  int depth;
296  int rc;
297 
298  /* Find depth from root of tree */
299  depth = fdt_depth ( fdt, 0, offset );
300  if ( depth < 0 ) {
301  rc = depth;
302  return rc;
303  }
304  pdepth = ( depth - 1 );
305 
306  /* Enter root node */
307  if ( ( rc = fdt_enter ( fdt, 0, &desc ) ) != 0 )
308  return rc;
309  *parent = desc.offset;
310 
311  /* Find parent node */
312  for ( depth = 0 ; depth >= 0 ; depth += desc.depth ) {
313 
314  /* Describe token */
315  if ( ( rc = fdt_next ( fdt, &desc ) ) != 0 ) {
316  DBGC ( fdt, "FDT +%#04x has malformed node: %s\n",
317  offset, strerror ( rc ) );
318  return rc;
319  }
320 
321  /* Record possible parent node */
322  if ( ( depth == pdepth ) && desc.name && ( ! desc.data ) )
323  *parent = desc.offset;
324 
325  /* Check for target node */
326  if ( desc.offset == offset ) {
327  DBGC2 ( fdt, "FDT +%#04x has parent node at +%#04x\n",
328  offset, *parent );
329  return 0;
330  }
331  }
332 
333  DBGC ( fdt, "FDT +#%04x has no parent node\n", offset );
334  return -ENOENT;
335 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int depth
Depth change.
Definition: fdt.h:132
static int fdt_enter(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Enter node.
Definition: fdt.c:211
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static int fdt_next(struct fdt *fdt, struct fdt_descriptor *desc)
Describe next device tree token.
Definition: fdt.c:197
static int fdt_depth(struct fdt *fdt, unsigned int offset, unsigned int target)
Find node relative depth.
Definition: fdt.c:251
A device tree.
Definition: fdt.h:88
#define DBGC2(...)
Definition: compiler.h:522
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References DBGC, DBGC2, fdt_descriptor::depth, desc, ENOENT, fdt_depth(), fdt_enter(), fdt_next(), offset, rc, and strerror().

Referenced by fdt_parent_reg_cells(), and fdt_test_exec().

◆ fdt_child()

static int fdt_child ( struct fdt fdt,
unsigned int  offset,
const char *  name,
unsigned int *  child 
)
static

Find child node.

Parameters
fdtDevice tree
offsetStarting node offset
nameNode name
childChild node offset to fill in
Return values
rcReturn status code

Definition at line 346 of file fdt.c.

347  {
348  struct fdt_descriptor desc;
349  int depth;
350  int rc;
351 
352  /* Enter node */
353  if ( ( rc = fdt_enter ( fdt, offset, &desc ) ) != 0 )
354  return rc;
355 
356  /* Find child node */
357  for ( depth = 0 ; depth >= 0 ; depth += desc.depth ) {
358 
359  /* Describe token */
360  if ( ( rc = fdt_next ( fdt, &desc ) ) != 0 ) {
361  DBGC ( fdt, "FDT +%#04x has malformed node: %s\n",
362  offset, strerror ( rc ) );
363  return rc;
364  }
365 
366  /* Check for matching immediate child node */
367  if ( ( depth == 0 ) && desc.name && ( ! desc.data ) ) {
368  DBGC2 ( fdt, "FDT +%#04x has child node \"%s\" at "
369  "+%#04x\n", offset, desc.name, desc.offset );
370  assert ( desc.depth > 0 );
371  if ( fdt_match ( &desc, name ) ) {
372  *child = desc.offset;
373  return 0;
374  }
375  }
376  }
377 
378  DBGC2 ( fdt, "FDT +%#04x has no child node \"%s\"\n", offset, name );
379  return -ENOENT;
380 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
static int fdt_match(const struct fdt_descriptor *desc, const char *name)
Compare node name.
Definition: fdt.c:73
int depth
Depth change.
Definition: fdt.h:132
static int fdt_enter(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Enter node.
Definition: fdt.c:211
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static int fdt_next(struct fdt *fdt, struct fdt_descriptor *desc)
Describe next device tree token.
Definition: fdt.c:197
A device tree.
Definition: fdt.h:88
#define DBGC2(...)
Definition: compiler.h:522
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References assert(), DBGC, DBGC2, fdt_descriptor::depth, desc, ENOENT, fdt_enter(), fdt_match(), fdt_next(), name, offset, rc, and strerror().

Referenced by fdt_alias(), fdt_ensure_child(), fdt_parse(), and fdt_path().

◆ fdt_end()

static int fdt_end ( struct fdt fdt,
unsigned int  offset,
unsigned int *  end 
)
static

Find end of node.

Parameters
fdtDevice tree
offsetStarting node offset
endEnd of node offset to fill in
Return values
rcReturn status code

Definition at line 390 of file fdt.c.

391  {
392  struct fdt_descriptor desc;
393  int depth;
394  int rc;
395 
396  /* Enter node */
397  if ( ( rc = fdt_enter ( fdt, offset, &desc ) ) != 0 )
398  return rc;
399 
400  /* Find end of this node */
401  for ( depth = 0 ; depth >= 0 ; depth += desc.depth ) {
402 
403  /* Describe token */
404  if ( ( rc = fdt_next ( fdt, &desc ) ) != 0 ) {
405  DBGC ( fdt, "FDT +%#04x has malformed node: %s\n",
406  offset, strerror ( rc ) );
407  return rc;
408  }
409  }
410 
411  /* Record end offset */
412  *end = desc.offset;
413  DBGC2 ( fdt, "FDT +%#04x has end at +%#04x\n", offset, *end );
414  return 0;
415 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int depth
Depth change.
Definition: fdt.h:132
static int fdt_enter(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Enter node.
Definition: fdt.c:211
#define DBGC(...)
Definition: compiler.h:505
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static int fdt_next(struct fdt *fdt, struct fdt_descriptor *desc)
Describe next device tree token.
Definition: fdt.c:197
A device tree.
Definition: fdt.h:88
#define DBGC2(...)
Definition: compiler.h:522
uint32_t end
Ending offset.
Definition: netvsc.h:18
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References DBGC, DBGC2, fdt_descriptor::depth, desc, end, fdt_enter(), fdt_next(), offset, rc, and strerror().

Referenced by fdt_ensure_child().

◆ fdt_path()

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

Find node by path.

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

Definition at line 425 of file fdt.c.

425  {
426  const char *tmp = path;
427  int rc;
428 
429  /* Initialise offset */
430  *offset = 0;
431 
432  /* Traverse tree one path segment at a time */
433  while ( 1 ) {
434 
435  /* Skip any leading '/' */
436  while ( *tmp == '/' )
437  tmp++;
438 
439  /* Terminate if there are no more path components */
440  if ( ! *tmp )
441  break;
442 
443  /* Find child */
444  if ( ( rc = fdt_child ( fdt, *offset, tmp, offset ) ) != 0 )
445  return rc;
446 
447  /* Move to next path component, if any */
448  tmp = strchr ( tmp, '/' );
449  if ( ! tmp )
450  break;
451  }
452 
453  DBGC2 ( fdt, "FDT found path \"%s\" at +%#04x\n", path, *offset );
454  return 0;
455 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned long tmp
Definition: linux_pci.h:64
static int fdt_child(struct fdt *fdt, unsigned int offset, const char *name, unsigned int *child)
Find child node.
Definition: fdt.c:346
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:271
A device tree.
Definition: fdt.h:88
#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_test_exec(), fdtcon_default(), and fdtmem_update_tree().

◆ fdt_alias()

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

Find node by alias.

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

Definition at line 465 of file fdt.c.

465  {
466  const char *alias;
467  int rc;
468 
469  /* Locate "/aliases" node */
470  if ( ( rc = fdt_child ( fdt, 0, "aliases", offset ) ) != 0 )
471  return rc;
472 
473  /* Locate alias property */
474  if ( ( alias = fdt_string ( fdt, *offset, name ) ) == NULL )
475  return -ENOENT;
476  DBGC ( fdt, "FDT alias \"%s\" is \"%s\"\n", name, alias );
477 
478  /* Locate aliased node */
479  if ( ( rc = fdt_path ( fdt, alias, offset ) ) != 0 )
480  return rc;
481 
482  return 0;
483 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
int fdt_path(struct fdt *fdt, const char *path, unsigned int *offset)
Find node by path.
Definition: fdt.c:425
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
const char * fdt_string(struct fdt *fdt, unsigned int offset, const char *name)
Find string property.
Definition: fdt.c:578
static int fdt_child(struct fdt *fdt, unsigned int offset, const char *name, unsigned int *child)
Find child node.
Definition: fdt.c:346
A device tree.
Definition: fdt.h:88
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 fdt_test_exec(), fdtcon_default(), and smscusb_fdt_fetch_mac().

◆ fdt_property()

static int fdt_property ( struct fdt fdt,
unsigned int  offset,
const char *  name,
struct fdt_descriptor desc 
)
static

Find property.

Parameters
fdtDevice tree
offsetStarting node offset
nameProperty name
descToken descriptor to fill in
Return values
rcReturn status code

Definition at line 494 of file fdt.c.

495  {
496  int depth;
497  int rc;
498 
499  /* Enter node */
500  if ( ( rc = fdt_enter ( fdt, offset, desc ) ) != 0 )
501  return rc;
502 
503  /* Find property */
504  for ( depth = 0 ; depth == 0 ; depth += desc->depth ) {
505 
506  /* Describe token */
507  if ( ( rc = fdt_next ( fdt, desc ) ) != 0 ) {
508  DBGC ( fdt, "FDT +%#04x has malformed node: %s\n",
509  offset, strerror ( rc ) );
510  return rc;
511  }
512 
513  /* Check for matching immediate child property */
514  if ( desc->data ) {
515  DBGC2 ( fdt, "FDT +%#04x has property \"%s\" at "
516  "+%#04x len %#zx\n", offset, desc->name,
517  desc->offset, desc->len );
518  assert ( desc->depth == 0 );
519  if ( fdt_match ( desc, name ) ) {
520  DBGC2_HDA ( fdt, 0, desc->data, desc->len );
521  return 0;
522  }
523  }
524  }
525 
526  DBGC2 ( fdt, "FDT +%#04x has no property \"%s\"\n", offset, name );
527  return -ENOENT;
528 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
static int fdt_match(const struct fdt_descriptor *desc, const char *name)
Compare node name.
Definition: fdt.c:73
int depth
Depth change.
Definition: fdt.h:132
static int fdt_enter(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Enter node.
Definition: fdt.c:211
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define DBGC2_HDA(...)
Definition: compiler.h:523
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static int fdt_next(struct fdt *fdt, struct fdt_descriptor *desc)
Describe next device tree token.
Definition: fdt.c:197
A device tree.
Definition: fdt.h:88
#define DBGC2(...)
Definition: compiler.h:522
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References assert(), DBGC, DBGC2, DBGC2_HDA, fdt_descriptor::depth, desc, ENOENT, fdt_enter(), fdt_match(), fdt_next(), name, offset, rc, and strerror().

Referenced by fdt_cells(), fdt_mac(), fdt_reg_count(), fdt_set(), and fdt_strings().

◆ fdt_strings()

const char* fdt_strings ( struct fdt fdt,
unsigned int  offset,
const char *  name,
unsigned int *  count 
)

Find strings property.

Parameters
fdtDevice tree
offsetStarting node offset
nameProperty name
countString count to fill in
Return values
stringString property, or NULL on error

Definition at line 539 of file fdt.c.

540  {
541  struct fdt_descriptor desc;
542  const char *data;
543  size_t len;
544  int rc;
545 
546  /* Return a zero count on error */
547  *count = 0;
548 
549  /* Find property */
550  if ( ( rc = fdt_property ( fdt, offset, name, &desc ) ) != 0 )
551  return NULL;
552 
553  /* Check NUL termination */
554  data = desc.data;
555  if ( desc.len && ( data[ desc.len - 1 ] != '\0' ) ) {
556  DBGC ( fdt, "FDT unterminated string property \"%s\"\n",
557  name );
558  return NULL;
559  }
560 
561  /* Count number of strings */
562  for ( len = desc.len ; len-- ; ) {
563  if ( data[len] == '\0' )
564  (*count)++;
565  }
566 
567  return data;
568 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
#define DBGC(...)
Definition: compiler.h:505
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
static int fdt_property(struct fdt *fdt, unsigned int offset, const char *name, struct fdt_descriptor *desc)
Find property.
Definition: fdt.c:494
ring len
Length.
Definition: dwmac.h:231
static unsigned int count
Number of entries.
Definition: dwmac.h:225
A device tree.
Definition: fdt.h:88
uint8_t data[48]
Additional event data.
Definition: ena.h:22
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References count, data, DBGC, desc, fdt_property(), len, name, NULL, offset, and rc.

Referenced by dt_find_driver(), fdt_string(), and fdt_test_exec().

◆ fdt_string()

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

Find string property.

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

Definition at line 578 of file fdt.c.

579  {
580  unsigned int count;
581 
582  /* Find strings property */
583  return fdt_strings ( fdt, offset, name, &count );
584 }
const char * name
Definition: ath9k_hw.c:1984
const char * fdt_strings(struct fdt *fdt, unsigned int offset, const char *name, unsigned int *count)
Find strings property.
Definition: fdt.c:539
static unsigned int count
Number of entries.
Definition: dwmac.h:225
A device tree.
Definition: fdt.h:88
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References count, fdt_strings(), name, and offset.

Referenced by fdt_alias(), fdt_parse(), fdt_test_exec(), fdtcon_default(), and fdtmem_update_node().

◆ fdt_cells()

int fdt_cells ( struct fdt fdt,
unsigned int  offset,
const char *  name,
unsigned int  index,
unsigned int  count,
uint64_t value 
)

Get integer property.

Parameters
fdtDevice tree
offsetStarting node offset
nameProperty name
indexStarting cell index
countNumber of cells (or 0 to read all remaining cells)
valueInteger value to fill in
Return values
rcReturn status code

Definition at line 597 of file fdt.c.

598  {
599  struct fdt_descriptor desc;
600  const uint32_t *cell;
601  unsigned int total;
602  int rc;
603 
604  /* Clear value */
605  *value = 0;
606 
607  /* Find property */
608  if ( ( rc = fdt_property ( fdt, offset, name, &desc ) ) != 0 )
609  return rc;
610  cell = desc.data;
611 
612  /* Determine number of cells */
613  total = ( desc.len / sizeof ( *cell ) );
614  if ( ( index > total ) || ( count > ( total - index ) ) ) {
615  DBGC ( fdt, "FDT truncated integer \"%s\"\n", name );
616  return -ERANGE;
617  }
618  if ( ! count )
619  count = ( total - index );
620  if ( count > ( sizeof ( *value ) / sizeof ( *cell ) ) ) {
621  DBGC ( fdt, "FDT overlength integer \"%s\"\n", name );
622  return -ERANGE;
623  }
624 
625  /* Read value */
626  for ( cell += index ; count ; cell++, count-- ) {
627  *value <<= 32;
628  *value |= be32_to_cpu ( *cell );
629  }
630 
631  return 0;
632 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
#define DBGC(...)
Definition: compiler.h:505
long index
Definition: bigint.h:62
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
#define be32_to_cpu(value)
Definition: byteswap.h:116
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
static int fdt_property(struct fdt *fdt, unsigned int offset, const char *name, struct fdt_descriptor *desc)
Find property.
Definition: fdt.c:494
static unsigned int count
Number of entries.
Definition: dwmac.h:225
#define ERANGE
Result too large.
Definition: errno.h:639
unsigned int uint32_t
Definition: stdint.h:12
A device tree.
Definition: fdt.h:88
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References be32_to_cpu, count, DBGC, desc, ERANGE, fdt_property(), index, name, offset, rc, and value.

Referenced by fdt_reg_address(), fdt_reg_size(), fdt_test_exec(), and fdt_u64().

◆ fdt_u64()

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

Get 64-bit integer property.

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

Definition at line 643 of file fdt.c.

644  {
645  int rc;
646 
647  /* Read value */
648  if ( ( rc = fdt_cells ( fdt, offset, name, 0, 0, value ) ) != 0 )
649  return rc;
650 
651  return 0;
652 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
A device tree.
Definition: fdt.h:88
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
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

References fdt_cells(), name, offset, rc, and value.

Referenced by fdt_test_exec(), and fdt_u32().

◆ fdt_u32()

int fdt_u32 ( struct fdt fdt,
unsigned int  offset,
const char *  name,
uint32_t value 
)

Get 32-bit integer property.

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

Definition at line 663 of file fdt.c.

664  {
665  uint64_t value64;
666  int rc;
667 
668  /* Read value */
669  if ( ( rc = fdt_u64 ( fdt, offset, name, &value64 ) ) != 0 )
670  return rc;
671 
672  /* Check range */
673  *value = value64;
674  if ( *value != value64 ) {
675  DBGC ( fdt, "FDT overlength 32-bit integer \"%s\"\n", name );
676  return -ERANGE;
677  }
678 
679  return 0;
680 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
#define DBGC(...)
Definition: compiler.h:505
unsigned long long uint64_t
Definition: stdint.h:13
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define ERANGE
Result too large.
Definition: errno.h:639
A device tree.
Definition: fdt.h:88
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
int fdt_u64(struct fdt *fdt, unsigned int offset, const char *name, uint64_t *value)
Get 64-bit integer property.
Definition: fdt.c:643

References DBGC, ERANGE, fdt_u64(), name, offset, rc, and value.

Referenced by dwgpio_probe(), dwuart_probe(), fdt_phandle(), fdt_reg_cells(), and fdt_test_exec().

◆ fdt_phandle()

uint32_t fdt_phandle ( struct fdt fdt,
unsigned int  offset 
)

Get package handle (phandle) property.

Parameters
fdtDevice tree
offsetStarting node offset
Return values
phandlePackage handle, or 0 on error

Definition at line 689 of file fdt.c.

689  {
690  uint32_t phandle;
691  int rc;
692 
693  /* Get "phandle" or "linux,phandle" property */
694  if ( ( ( rc = fdt_u32 ( fdt, offset, "phandle", &phandle ) ) == 0 ) ||
695  ( ( rc = fdt_u32 ( fdt, offset, "linux,phandle",
696  &phandle ) ) == 0 ) ) {
697  assert ( phandle != 0 );
698  return phandle;
699  }
700 
701  return 0;
702 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
int fdt_u32(struct fdt *fdt, unsigned int offset, const char *name, uint32_t *value)
Get 32-bit integer property.
Definition: fdt.c:663
unsigned int uint32_t
Definition: stdint.h:12
A device tree.
Definition: fdt.h:88
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References assert(), fdt_u32(), offset, and rc.

Referenced by dt_probe_node(), and fdt_test_exec().

◆ fdt_reg_cells()

void fdt_reg_cells ( struct fdt fdt,
unsigned int  offset,
struct fdt_reg_cells regs 
)

Get region cell size specification.

Parameters
fdtDevice tree
offsetStarting (parent) node offset
regsRegion cell size specification to fill in

Note that address-cells and size-cells are defined on the immediate parent node, rather than on the node with the "reg" property itself.

Definition at line 715 of file fdt.c.

716  {
717  int rc;
718 
719  /* Read #address-cells, if present */
720  if ( ( rc = fdt_u32 ( fdt, offset, "#address-cells",
721  &regs->address_cells ) ) != 0 ) {
722  regs->address_cells = FDT_DEFAULT_ADDRESS_CELLS;
723  }
724 
725  /* Read #size-cells, if present */
726  if ( ( rc = fdt_u32 ( fdt, offset, "#size-cells",
727  &regs->size_cells ) ) != 0 ) {
728  regs->size_cells = FDT_DEFAULT_SIZE_CELLS;
729  }
730 
731  /* Calculate stride */
732  regs->stride = ( regs->address_cells + regs->size_cells );
733 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int fdt_u32(struct fdt *fdt, unsigned int offset, const char *name, uint32_t *value)
Get 32-bit integer property.
Definition: fdt.c:663
struct i386_regs regs
Definition: registers.h:15
A device tree.
Definition: fdt.h:88
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define FDT_DEFAULT_SIZE_CELLS
Default number of size cells, if not specified.
Definition: fdt.h:149
#define FDT_DEFAULT_ADDRESS_CELLS
Default number of address cells, if not specified.
Definition: fdt.h:146

References FDT_DEFAULT_ADDRESS_CELLS, FDT_DEFAULT_SIZE_CELLS, fdt_u32(), offset, rc, and regs.

Referenced by fdt_parent_reg_cells(), and fdtmem_update_node().

◆ fdt_parent_reg_cells()

int fdt_parent_reg_cells ( struct fdt fdt,
unsigned int  offset,
struct fdt_reg_cells regs 
)

Get parent region cell size specification.

Parameters
fdtDevice tree
offsetStarting node offset
regsRegion cell size specification to fill in
Return values
rcReturn status code

Definition at line 743 of file fdt.c.

744  {
745  unsigned int parent;
746  int rc;
747 
748  /* Get parent node */
749  if ( ( rc = fdt_parent ( fdt, offset, &parent ) ) != 0 )
750  return rc;
751 
752  /* Read #address-cells and #size-cells, if present */
753  fdt_reg_cells ( fdt, parent, regs );
754 
755  return 0;
756 }
int fdt_parent(struct fdt *fdt, unsigned int offset, unsigned int *parent)
Find parent node.
Definition: fdt.c:292
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void fdt_reg_cells(struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs)
Get region cell size specification.
Definition: fdt.c:715
struct i386_regs regs
Definition: registers.h:15
A device tree.
Definition: fdt.h:88
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References fdt_parent(), fdt_reg_cells(), offset, rc, and regs.

Referenced by dt_ioremap(), and fdt_reg().

◆ fdt_reg_count()

int fdt_reg_count ( struct fdt fdt,
unsigned int  offset,
struct fdt_reg_cells regs 
)

Get number of regions.

Parameters
fdtDevice tree
offsetStarting node offset
regsRegion cell size specification
Return values
countNumber of regions, or negative error

Definition at line 766 of file fdt.c.

767  {
768  struct fdt_descriptor desc;
769  const uint32_t *cell;
770  unsigned int count;
771  int rc;
772 
773  /* Find property */
774  if ( ( rc = fdt_property ( fdt, offset, "reg", &desc ) ) != 0 )
775  return rc;
776 
777  /* Determine number of regions */
778  count = ( desc.len / ( regs->stride * sizeof ( *cell ) ) );
779  return count;
780 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
static int fdt_property(struct fdt *fdt, unsigned int offset, const char *name, struct fdt_descriptor *desc)
Find property.
Definition: fdt.c:494
static unsigned int count
Number of entries.
Definition: dwmac.h:225
unsigned int uint32_t
Definition: stdint.h:12
struct i386_regs regs
Definition: registers.h:15
A device tree.
Definition: fdt.h:88
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References count, desc, fdt_property(), offset, rc, and regs.

Referenced by fdtmem_update_node().

◆ fdt_reg_address()

int fdt_reg_address ( struct fdt fdt,
unsigned int  offset,
struct fdt_reg_cells regs,
unsigned int  index,
uint64_t address 
)

Get region address.

Parameters
fdtDevice tree
offsetStarting node offset
regsRegion cell size specification
indexRegion index
addressRegion starting address to fill in
Return values
rcReturn status code

Definition at line 792 of file fdt.c.

794  {
795  unsigned int cell = ( index * regs->stride );
796  int rc;
797 
798  /* Read relevant portion of region array */
799  if ( ( rc = fdt_cells ( fdt, offset, "reg", cell, regs->address_cells,
800  address ) ) != 0 ) {
801  return rc;
802  }
803 
804  return 0;
805 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint64_t address
Base address.
Definition: ena.h:24
long index
Definition: bigint.h:62
struct i386_regs regs
Definition: registers.h:15
A device tree.
Definition: fdt.h:88
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
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

References address, fdt_cells(), index, offset, rc, and regs.

Referenced by dt_ioremap(), fdt_reg(), and fdtmem_update_node().

◆ fdt_reg_size()

int fdt_reg_size ( struct fdt fdt,
unsigned int  offset,
struct fdt_reg_cells regs,
unsigned int  index,
uint64_t size 
)

Get region size.

Parameters
fdtDevice tree
offsetStarting node offset
regsRegion cell size specification
indexRegion index
sizeRegion size to fill in
Return values
rcReturn status code

Definition at line 817 of file fdt.c.

819  {
820  unsigned int cell = ( ( index * regs->stride ) + regs->address_cells );
821  int rc;
822 
823  /* Read relevant portion of region array */
824  if ( ( rc = fdt_cells ( fdt, offset, "reg", cell, regs->size_cells,
825  size ) ) != 0 ) {
826  return rc;
827  }
828 
829  return 0;
830 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint16_t size
Buffer size.
Definition: dwmac.h:14
long index
Definition: bigint.h:62
struct i386_regs regs
Definition: registers.h:15
A device tree.
Definition: fdt.h:88
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
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

References fdt_cells(), index, offset, rc, regs, and size.

Referenced by dt_ioremap(), and fdtmem_update_node().

◆ fdt_reg()

int fdt_reg ( struct fdt fdt,
unsigned int  offset,
uint64_t region 
)

Get unsized single-entry region address.

Parameters
fdtDevice tree
offsetStarting node offset
addressRegion address to fill in
Return values
rcReturn status code

Many region types (e.g. I2C bus addresses) can only ever contain a single region with no size cells specified.

Definition at line 843 of file fdt.c.

843  {
844  struct fdt_reg_cells regs;
845  int rc;
846 
847  /* Get parent region cell size specification */
848  if ( ( rc = fdt_parent_reg_cells ( fdt, offset, &regs ) ) != 0 )
849  return rc;
850 
851  /* Get first region address */
852  if ( ( rc = fdt_reg_address ( fdt, offset, &regs, 0, region ) ) != 0 )
853  return rc;
854 
855  return 0;
856 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A device tree region cell size specification.
Definition: fdt.h:136
int fdt_reg_address(struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs, unsigned int index, uint64_t *address)
Get region address.
Definition: fdt.c:792
struct i386_regs regs
Definition: registers.h:15
A device tree.
Definition: fdt.h:88
int fdt_parent_reg_cells(struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs)
Get parent region cell size specification.
Definition: fdt.c:743
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References fdt_parent_reg_cells(), fdt_reg_address(), offset, rc, and regs.

Referenced by dwgpio_probe().

◆ fdt_mac()

int fdt_mac ( struct fdt fdt,
unsigned int  offset,
struct net_device netdev 
)

Get MAC address from property.

Parameters
fdtDevice tree
offsetStarting node offset
netdevNetwork device
Return values
rcReturn status code

Definition at line 866 of file fdt.c.

867  {
868  struct fdt_descriptor desc;
869  size_t len;
870  int rc;
871 
872  /* Find applicable MAC address property */
873  if ( ( ( rc = fdt_property ( fdt, offset, "mac-address",
874  &desc ) ) != 0 ) &&
875  ( ( rc = fdt_property ( fdt, offset, "local-mac-address",
876  &desc ) ) != 0 ) ) {
877  return rc;
878  }
879 
880  /* Check length */
882  if ( len != desc.len ) {
883  DBGC ( fdt, "FDT malformed MAC address \"%s\":\n",
884  desc.name );
885  DBGC_HDA ( fdt, 0, desc.data, desc.len );
886  return -ERANGE;
887  }
888 
889  /* Fill in MAC address */
890  memcpy ( netdev->hw_addr, desc.data, len );
891 
892  return 0;
893 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
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 int fdt_property(struct fdt *fdt, unsigned int offset, const char *name, struct fdt_descriptor *desc)
Find property.
Definition: fdt.c:494
ring len
Length.
Definition: dwmac.h:231
static struct net_device * netdev
Definition: gdbudp.c:52
#define ERANGE
Result too large.
Definition: errno.h:639
A device tree.
Definition: fdt.h:88
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
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 cgem_probe(), dwmac_probe(), and smscusb_fdt_fetch_mac().

◆ fdt_parse()

int fdt_parse ( struct fdt fdt,
struct fdt_header hdr,
size_t  max_len 
)

Parse device tree.

Parameters
fdtDevice tree
hdrDevice tree header
max_lenMaximum device tree length
Return values
rcReturn status code

Definition at line 903 of file fdt.c.

903  {
904  const uint8_t *nul;
905  unsigned int chosen;
906  size_t end;
907 
908  /* Sanity check */
909  if ( sizeof ( *hdr ) > max_len ) {
910  DBGC ( fdt, "FDT length %#zx too short for header\n",
911  max_len );
912  goto err;
913  }
914 
915  /* Record device tree location */
916  fdt->hdr = hdr;
917  fdt->len = be32_to_cpu ( hdr->totalsize );
918  fdt->used = sizeof ( *hdr );
919  if ( fdt->len > max_len ) {
920  DBGC ( fdt, "FDT has invalid length %#zx / %#zx\n",
921  fdt->len, max_len );
922  goto err;
923  }
924  DBGC ( fdt, "FDT version %d at %p+%#04zx (phys %#08lx)\n",
925  be32_to_cpu ( hdr->version ), fdt->hdr, fdt->len,
926  virt_to_phys ( hdr ) );
927 
928  /* Check signature */
929  if ( hdr->magic != cpu_to_be32 ( FDT_MAGIC ) ) {
930  DBGC ( fdt, "FDT has invalid magic value %#08x\n",
931  be32_to_cpu ( hdr->magic ) );
932  goto err;
933  }
934 
935  /* Check version */
936  if ( hdr->last_comp_version != cpu_to_be32 ( FDT_VERSION ) ) {
937  DBGC ( fdt, "FDT unsupported version %d\n",
938  be32_to_cpu ( hdr->last_comp_version ) );
939  goto err;
940  }
941 
942  /* Record structure block location */
943  fdt->structure = be32_to_cpu ( hdr->off_dt_struct );
944  fdt->structure_len = be32_to_cpu ( hdr->size_dt_struct );
945  DBGC ( fdt, "FDT structure block at +[%#04x,%#04zx)\n",
947  if ( ( fdt->structure > fdt->len ) ||
948  ( fdt->structure_len > ( fdt->len - fdt->structure ) ) ) {
949  DBGC ( fdt, "FDT structure block exceeds table\n" );
950  goto err;
951  }
952  if ( ( fdt->structure | fdt->structure_len ) &
953  ( FDT_STRUCTURE_ALIGN - 1 ) ) {
954  DBGC ( fdt, "FDT structure block is misaligned\n" );
955  goto err;
956  }
957  end = ( fdt->structure + fdt->structure_len );
958  if ( fdt->used < end )
959  fdt->used = end;
960 
961  /* Record strings block location */
962  fdt->strings = be32_to_cpu ( hdr->off_dt_strings );
963  fdt->strings_len = be32_to_cpu ( hdr->size_dt_strings );
964  DBGC ( fdt, "FDT strings block at +[%#04x,%#04zx)\n",
965  fdt->strings, ( fdt->strings + fdt->strings_len ) );
966  if ( ( fdt->strings > fdt->len ) ||
967  ( fdt->strings_len > ( fdt->len - fdt->strings ) ) ) {
968  DBGC ( fdt, "FDT strings block exceeds table\n" );
969  goto err;
970  }
971  end = ( fdt->strings + fdt->strings_len );
972  if ( fdt->used < end )
973  fdt->used = end;
974 
975  /* Shrink strings block to ensure NUL termination safety */
976  nul = ( fdt->raw + fdt->strings + fdt->strings_len );
977  for ( ; fdt->strings_len ; fdt->strings_len-- ) {
978  if ( *(--nul) == '\0' )
979  break;
980  }
981  if ( fdt->strings_len != be32_to_cpu ( hdr->size_dt_strings ) ) {
982  DBGC ( fdt, "FDT strings block shrunk to +[%#04x,%#04zx)\n",
983  fdt->strings, ( fdt->strings + fdt->strings_len ) );
984  }
985 
986  /* Record memory reservation block location */
987  fdt->reservations = be32_to_cpu ( hdr->off_mem_rsvmap );
988  DBGC ( fdt, "FDT memory reservations at +[%#04x,...)\n",
989  fdt->reservations );
990  if ( fdt->used <= fdt->reservations ) {
991  /* No size field exists: assume whole table is used */
992  fdt->used = fdt->len;
993  }
994 
995  /* Identify free space (if any) */
996  if ( fdt->used < fdt->len ) {
997  DBGC ( fdt, "FDT free space at +[%#04zx,%#04zx)\n",
998  fdt->used, fdt->len );
999  }
1000 
1001  /* Print model name and boot arguments (for debugging) */
1002  if ( DBG_LOG ) {
1003  DBGC ( fdt, "FDT model is \"%s\"\n",
1004  fdt_string ( fdt, 0, "model" ) );
1005  if ( fdt_child ( fdt, 0, "chosen", &chosen ) == 0 ) {
1006  DBGC ( fdt, "FDT boot arguments \"%s\"\n",
1007  fdt_string ( fdt, chosen, "bootargs" ) );
1008  }
1009  }
1010 
1011  return 0;
1012 
1013  err:
1014  DBGC_HDA ( fdt, 0, hdr, sizeof ( *hdr ) );
1015  memset ( fdt, 0, sizeof ( *fdt ) );
1016  return -EINVAL;
1017 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
#define FDT_STRUCTURE_ALIGN
Alignment of structure block.
Definition: fdt.h:74
unsigned int reservations
Offset to memory reservation block.
Definition: fdt.h:109
size_t len
Length of tree.
Definition: fdt.h:97
struct golan_inbox_hdr hdr
Message header.
Definition: CIB_PRM.h:28
void * raw
Raw data.
Definition: fdt.h:94
#define DBGC(...)
Definition: compiler.h:505
size_t strings_len
Length of strings block.
Definition: fdt.h:107
struct fdt_header * hdr
Tree header.
Definition: fdt.h:92
#define be32_to_cpu(value)
Definition: byteswap.h:116
#define FDT_MAGIC
Magic signature.
Definition: fdt.h:42
#define DBGC_HDA(...)
Definition: compiler.h:506
const char * fdt_string(struct fdt *fdt, unsigned int offset, const char *name)
Find string property.
Definition: fdt.c:578
unsigned int structure
Offset to structure block.
Definition: fdt.h:101
#define FDT_VERSION
Expected device tree version.
Definition: fdt.h:45
static int fdt_child(struct fdt *fdt, unsigned int offset, const char *name, unsigned int *child)
Find child node.
Definition: fdt.c:346
unsigned char uint8_t
Definition: stdint.h:10
size_t used
Used length of tree.
Definition: fdt.h:99
size_t structure_len
Length of structure block.
Definition: fdt.h:103
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:88
uint32_t end
Ending offset.
Definition: netvsc.h:18
#define DBG_LOG
Definition: compiler.h:317
unsigned int strings
Offset to strings block.
Definition: fdt.h:105
void * memset(void *dest, int character, size_t len) __nonnull

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

Referenced by efi_fdt_init(), fdt_parse_image(), fdt_test_exec(), fdtmem_register(), and fdtmem_relocate().

◆ fdt_parse_image()

static int fdt_parse_image ( struct fdt fdt,
struct image image 
)
static

Parse device tree image.

Parameters
fdtDevice tree
imageImage
Return values
rcReturn status code

Definition at line 1026 of file fdt.c.

1026  {
1027  int rc;
1028 
1029  /* Parse image */
1030  if ( ( rc = fdt_parse ( fdt, image->rwdata, image->len ) ) != 0 ) {
1031  DBGC ( fdt, "FDT image \"%s\" is invalid: %s\n",
1032  image->name, strerror ( rc ) );
1033  return rc;
1034  }
1035 
1036  DBGC ( fdt, "FDT image is \"%s\"\n", image->name );
1037  return 0;
1038 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int fdt_parse(struct fdt *fdt, struct fdt_header *hdr, size_t max_len)
Parse device tree.
Definition: fdt.c:903
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
size_t len
Length of raw file image.
Definition: image.h:55
A device tree.
Definition: fdt.h:88
void * rwdata
Writable data.
Definition: image.h:52
char * name
Name.
Definition: image.h:37

References DBGC, fdt_parse(), image::len, image::name, rc, image::rwdata, and strerror().

Referenced by fdt_create().

◆ fdt_insert()

static int fdt_insert ( struct fdt fdt,
unsigned int  offset,
size_t  len 
)
static

Insert empty space.

Parameters
fdtDevice tree
offsetOffset at which to insert space
lenLength to insert (must be a multiple of FDT_MAX_ALIGN)
Return values
rcReturn status code

Definition at line 1048 of file fdt.c.

1048  {
1049  size_t free;
1050  size_t new;
1051  int rc;
1052 
1053  /* Sanity checks */
1054  assert ( offset <= fdt->used );
1055  assert ( fdt->used <= fdt->len );
1056  assert ( ( len % FDT_MAX_ALIGN ) == 0 );
1057 
1058  /* Reallocate tree if necessary */
1059  free = ( fdt->len - fdt->used );
1060  if ( free < len ) {
1061  if ( ! fdt->realloc ) {
1062  DBGC ( fdt, "FDT is not reallocatable\n" );
1063  return -ENOTSUP;
1064  }
1065  new = ( fdt->len + ( len - free ) + FDT_INSERT_PAD );
1066  if ( ( rc = fdt->realloc ( fdt, new ) ) != 0 )
1067  return rc;
1068  }
1069  assert ( ( fdt->used + len ) <= fdt->len );
1070 
1071  /* Insert empty space */
1072  memmove ( ( fdt->raw + offset + len ), ( fdt->raw + offset ),
1073  ( fdt->used - offset ) );
1074  memset ( ( fdt->raw + offset ), 0, len );
1075  fdt->used += len;
1076 
1077  /* Update offsets
1078  *
1079  * We assume that we never need to legitimately insert data at
1080  * the start of a block, and therefore can unambiguously
1081  * determine which block offsets need to be updated.
1082  *
1083  * It is the caller's responsibility to update the length (and
1084  * contents) of the block into which it has inserted space.
1085  */
1086  if ( fdt->structure >= offset ) {
1087  fdt->structure += len;
1089  DBGC ( fdt, "FDT structure block now at +[%#04x,%#04zx)\n",
1090  fdt->structure,
1091  ( fdt->structure + fdt->structure_len ) );
1092  }
1093  if ( fdt->strings >= offset ) {
1094  fdt->strings += len;
1096  DBGC ( fdt, "FDT strings block now at +[%#04x,%#04zx)\n",
1097  fdt->strings, ( fdt->strings + fdt->strings_len ) );
1098  }
1099  if ( fdt->reservations >= offset ) {
1100  fdt->reservations += len;
1102  DBGC ( fdt, "FDT memory reservations now at +[%#04x,...)\n",
1103  fdt->reservations );
1104  }
1105 
1106  return 0;
1107 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int reservations
Offset to memory reservation block.
Definition: fdt.h:109
int(* realloc)(struct fdt *fdt, size_t len)
Reallocate device tree.
Definition: fdt.h:116
size_t len
Length of tree.
Definition: fdt.h:97
void * raw
Raw data.
Definition: fdt.h:94
#define DBGC(...)
Definition: compiler.h:505
size_t strings_len
Length of strings block.
Definition: fdt.h:107
struct fdt_header * hdr
Tree header.
Definition: fdt.h:92
uint32_t off_dt_struct
Offset to structure block.
Definition: fdt.h:24
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
uint32_t off_dt_strings
Offset to strings block.
Definition: fdt.h:26
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
unsigned int structure
Offset to structure block.
Definition: fdt.h:101
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
size_t used
Used length of tree.
Definition: fdt.h:99
uint32_t off_mem_rsvmap
Offset to memory reservation block.
Definition: fdt.h:28
void * memmove(void *dest, const void *src, size_t len) __nonnull
size_t structure_len
Length of structure block.
Definition: fdt.h:103
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:88
#define FDT_INSERT_PAD
Amount of free space to add whenever we have to reallocate a tree.
Definition: fdt.c:52
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define FDT_MAX_ALIGN
Maximum alignment of any block.
Definition: fdt.h:77
unsigned int strings
Offset to strings block.
Definition: fdt.h:105
void * memset(void *dest, int character, size_t len) __nonnull

References assert(), cpu_to_be32, DBGC, ENOTSUP, FDT_INSERT_PAD, FDT_MAX_ALIGN, free, fdt::hdr, fdt::len, len, memmove(), memset(), fdt_header::off_dt_strings, fdt_header::off_dt_struct, fdt_header::off_mem_rsvmap, offset, fdt::raw, rc, fdt::realloc, fdt::reservations, fdt::strings, fdt::strings_len, fdt::structure, fdt::structure_len, and fdt::used.

Referenced by fdt_insert_nop(), and fdt_insert_string().

◆ fdt_nop()

static void fdt_nop ( struct fdt fdt,
unsigned int  offset,
size_t  len 
)
static

Fill space in structure block with FDT_NOP.

Parameters
fdtDevice tree
offsetStarting offset
lenLength (must be a multiple of FDT_STRUCTURE_ALIGN)

Definition at line 1116 of file fdt.c.

1116  {
1117  fdt_token_t *token;
1118  unsigned int count;
1119 
1120  /* Sanity check */
1121  assert ( ( len % FDT_STRUCTURE_ALIGN ) == 0 );
1122 
1123  /* Fill with FDT_NOP */
1124  token = ( fdt->raw + fdt->structure + offset );
1125  count = ( len / sizeof ( *token ) );
1126  while ( count-- )
1127  *(token++) = cpu_to_be32 ( FDT_NOP );
1128 }
#define FDT_STRUCTURE_ALIGN
Alignment of structure block.
Definition: fdt.h:74
#define FDT_NOP
NOP token.
Definition: fdt.h:68
void * raw
Raw data.
Definition: fdt.h:94
uint32_t fdt_token_t
Device tree token.
Definition: fdt.h:48
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
static unsigned int count
Number of entries.
Definition: dwmac.h:225
unsigned int structure
Offset to structure block.
Definition: fdt.h:101
u8 token
Definition: CIB_PRM.h:42
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:88
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References assert(), count, cpu_to_be32, FDT_NOP, FDT_STRUCTURE_ALIGN, len, offset, fdt::raw, fdt::structure, and token.

Referenced by fdt_insert_nop(), and fdt_set().

◆ fdt_insert_nop()

static int fdt_insert_nop ( struct fdt fdt,
unsigned int  offset,
size_t  len 
)
static

Insert FDT_NOP padded space in structure block.

Parameters
fdtDevice tree
offsetOffset at which to insert space
lenMinimal length to insert
Return values
rcReturn status code

Definition at line 1138 of file fdt.c.

1139  {
1140  int rc;
1141 
1142  /* Sanity check */
1143  assert ( ( offset % FDT_STRUCTURE_ALIGN ) == 0 );
1144 
1145  /* Round up inserted length to maximal alignment */
1146  len = ( ( len + FDT_MAX_ALIGN - 1 ) & ~( FDT_MAX_ALIGN - 1 ) );
1147 
1148  /* Insert empty space in structure block */
1149  if ( ( rc = fdt_insert ( fdt, ( fdt->structure + offset ),
1150  len ) ) != 0 )
1151  return rc;
1152 
1153  /* Fill with NOPs */
1154  fdt_nop ( fdt, offset, len );
1155 
1156  /* Update structure block size */
1157  fdt->structure_len += len;
1159  DBGC ( fdt, "FDT structure block now at +[%#04x,%#04zx)\n",
1161 
1162  return 0;
1163 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define FDT_STRUCTURE_ALIGN
Alignment of structure block.
Definition: fdt.h:74
#define DBGC(...)
Definition: compiler.h:505
struct fdt_header * hdr
Tree header.
Definition: fdt.h:92
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
unsigned int structure
Offset to structure block.
Definition: fdt.h:101
size_t structure_len
Length of structure block.
Definition: fdt.h:103
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:88
uint32_t size_dt_struct
Length of structure block.
Definition: fdt.h:38
static void fdt_nop(struct fdt *fdt, unsigned int offset, size_t len)
Fill space in structure block with FDT_NOP.
Definition: fdt.c:1116
static int fdt_insert(struct fdt *fdt, unsigned int offset, size_t len)
Insert empty space.
Definition: fdt.c:1048
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define FDT_MAX_ALIGN
Maximum alignment of any block.
Definition: fdt.h:77

References assert(), cpu_to_be32, DBGC, fdt_insert(), FDT_MAX_ALIGN, fdt_nop(), FDT_STRUCTURE_ALIGN, fdt::hdr, len, offset, rc, fdt_header::size_dt_struct, fdt::structure, and fdt::structure_len.

Referenced by fdt_ensure_child(), and fdt_set().

◆ fdt_insert_string()

static int fdt_insert_string ( struct fdt fdt,
const char *  string,
unsigned int *  offset 
)
static

Insert string in strings block.

Parameters
fdtDevice tree
stringString
offsetString offset to fill in
Return values
rcReturn status code

Definition at line 1173 of file fdt.c.

1174  {
1175  size_t len = ( strlen ( string ) + 1 /* NUL */ );
1176  int rc;
1177 
1178  /* Round up inserted length to maximal alignment */
1179  len = ( ( len + FDT_MAX_ALIGN - 1 ) & ~( FDT_MAX_ALIGN - 1 ) );
1180 
1181  /* Insert space at end of strings block */
1182  if ( ( rc = fdt_insert ( fdt, ( fdt->strings + fdt->strings_len ),
1183  len ) ) != 0 )
1184  return rc;
1185 
1186  /* Append string to strings block */
1187  *offset = fdt->strings_len;
1188  strcpy ( ( fdt->raw + fdt->strings + *offset ), string );
1189 
1190  /* Update strings block size */
1191  fdt->strings_len += len;
1193  DBGC ( fdt, "FDT strings block now at +[%#04x,%#04zx)\n",
1194  fdt->strings, ( fdt->strings + fdt->strings_len ) );
1195 
1196  return 0;
1197 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void * raw
Raw data.
Definition: fdt.h:94
#define DBGC(...)
Definition: compiler.h:505
size_t strings_len
Length of strings block.
Definition: fdt.h:107
struct fdt_header * hdr
Tree header.
Definition: fdt.h:92
ring len
Length.
Definition: dwmac.h:231
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:346
uint32_t size_dt_strings
Length of string block.
Definition: fdt.h:36
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:88
static int fdt_insert(struct fdt *fdt, unsigned int offset, size_t len)
Insert empty space.
Definition: fdt.c:1048
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define FDT_MAX_ALIGN
Maximum alignment of any block.
Definition: fdt.h:77
unsigned int strings
Offset to strings block.
Definition: fdt.h:105

References cpu_to_be32, DBGC, fdt_insert(), FDT_MAX_ALIGN, fdt::hdr, len, offset, fdt::raw, rc, fdt_header::size_dt_strings, strcpy(), fdt::strings, fdt::strings_len, and strlen().

Referenced by fdt_set().

◆ fdt_ensure_child()

static int fdt_ensure_child ( struct fdt fdt,
unsigned int  offset,
const char *  name,
unsigned int *  child 
)
static

Ensure child node exists.

Parameters
fdtDevice tree
offsetStarting node offset
nameNew node name
childChild node offset to fill in
Return values
rcReturn status code

Definition at line 1208 of file fdt.c.

1209  {
1210  size_t name_len = ( strlen ( name ) + 1 /* NUL */ );
1211  fdt_token_t *token;
1212  size_t len;
1213  int rc;
1214 
1215  /* Find existing child node, if any */
1216  if ( ( rc = fdt_child ( fdt, offset, name, child ) ) == 0 )
1217  return 0;
1218 
1219  /* Find end of parent node */
1220  if ( ( rc = fdt_end ( fdt, offset, child ) ) != 0 )
1221  return rc;
1222 
1223  /* Insert space for child node (with maximal alignment) */
1224  len = ( sizeof ( fdt_token_t ) /* BEGIN_NODE */ + name_len +
1225  sizeof ( fdt_token_t ) /* END_NODE */ );
1226  if ( ( rc = fdt_insert_nop ( fdt, *child, len ) ) != 0 )
1227  return rc;
1228 
1229  /* Construct node */
1230  token = ( fdt->raw + fdt->structure + *child );
1231  *(token++) = cpu_to_be32 ( FDT_BEGIN_NODE );
1232  memcpy ( token, name, name_len );
1233  name_len = ( ( name_len + FDT_STRUCTURE_ALIGN - 1 ) &
1234  ~( FDT_STRUCTURE_ALIGN - 1 ) );
1235  token = ( ( ( void * ) token ) + name_len );
1236  *(token++) = cpu_to_be32 ( FDT_END_NODE );
1237  DBGC2 ( fdt, "FDT +%#04x created child \"%s\" at +%#04x\n",
1238  offset, name, *child );
1239 
1240  return 0;
1241 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
#define FDT_STRUCTURE_ALIGN
Alignment of structure block.
Definition: fdt.h:74
void * raw
Raw data.
Definition: fdt.h:94
uint32_t fdt_token_t
Device tree token.
Definition: fdt.h:48
void * memcpy(void *dest, const void *src, size_t len) __nonnull
ring len
Length.
Definition: dwmac.h:231
unsigned int structure
Offset to structure block.
Definition: fdt.h:101
static int fdt_child(struct fdt *fdt, unsigned int offset, const char *name, unsigned int *child)
Find child node.
Definition: fdt.c:346
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
static int fdt_insert_nop(struct fdt *fdt, unsigned int offset, size_t len)
Insert FDT_NOP padded space in structure block.
Definition: fdt.c:1138
u8 token
Definition: CIB_PRM.h:42
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:88
#define DBGC2(...)
Definition: compiler.h:522
#define FDT_END_NODE
End node token.
Definition: fdt.h:54
#define FDT_BEGIN_NODE
Begin node token.
Definition: fdt.h:51
static int fdt_end(struct fdt *fdt, unsigned int offset, unsigned int *end)
Find end of node.
Definition: fdt.c:390
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References cpu_to_be32, DBGC2, FDT_BEGIN_NODE, fdt_child(), fdt_end(), FDT_END_NODE, fdt_insert_nop(), FDT_STRUCTURE_ALIGN, len, memcpy(), name, offset, fdt::raw, rc, strlen(), fdt::structure, and token.

Referenced by fdt_bootargs().

◆ fdt_set()

static int fdt_set ( struct fdt fdt,
unsigned int  offset,
const char *  name,
const void *  data,
size_t  len 
)
static

Set property value.

Parameters
fdtDevice tree
offsetStarting node offset
nameProperty name
dataProperty data, or NULL to delete property
lenLength of property data
Return values
rcReturn status code

Definition at line 1253 of file fdt.c.

1254  {
1255  struct fdt_descriptor desc;
1256  struct {
1258  struct fdt_prop prop;
1259  uint8_t data[0];
1260  } __attribute__ (( packed )) *hdr;
1261  unsigned int string;
1262  size_t erase;
1263  size_t insert;
1264  int rc;
1265 
1266  /* Find and reuse existing property, if any */
1267  if ( ( rc = fdt_property ( fdt, offset, name, &desc ) ) == 0 ) {
1268 
1269  /* Reuse existing name */
1270  hdr = ( fdt->raw + fdt->structure + desc.offset );
1271  string = be32_to_cpu ( hdr->prop.name_off );
1272 
1273  /* Erase existing property */
1274  erase = ( sizeof ( *hdr ) + desc.len );
1275  erase = ( ( erase + FDT_STRUCTURE_ALIGN - 1 ) &
1276  ~( FDT_STRUCTURE_ALIGN - 1 ) );
1277  fdt_nop ( fdt, desc.offset, erase );
1278  DBGC2 ( fdt, "FDT +%#04x erased property \"%s\"\n",
1279  offset, name );
1280 
1281  /* Calculate insertion position and length */
1282  insert = ( ( desc.len < len ) ? ( len - desc.len ) : 0 );
1283 
1284  } else {
1285 
1286  /* Create name */
1287  if ( ( rc = fdt_insert_string ( fdt, name, &string ) ) != 0 )
1288  return rc;
1289 
1290  /* Enter node */
1291  if ( ( rc = fdt_enter ( fdt, offset, &desc ) ) != 0 )
1292  return rc;
1293  assert ( desc.depth > 0 );
1294  desc.offset = desc.next;
1295 
1296  /* Calculate insertion length */
1297  insert = ( sizeof ( *hdr ) + len );
1298  }
1299 
1300  /* Leave property erased if applicable */
1301  if ( ! data )
1302  return 0;
1303 
1304  /* Insert space */
1305  if ( ( rc = fdt_insert_nop ( fdt, desc.offset, insert ) ) != 0 )
1306  return rc;
1307 
1308  /* Construct property */
1309  hdr = ( fdt->raw + fdt->structure + desc.offset );
1310  hdr->token = cpu_to_be32 ( FDT_PROP );
1311  hdr->prop.len = cpu_to_be32 ( len );
1312  hdr->prop.name_off = cpu_to_be32 ( string );
1313  memset ( hdr->data, 0, ( ( len + FDT_STRUCTURE_ALIGN - 1 ) &
1314  ~( FDT_STRUCTURE_ALIGN - 1 ) ) );
1315  memcpy ( hdr->data, data, len );
1316  DBGC2 ( fdt, "FDT +%#04x created property \"%s\"\n", offset, name );
1317  DBGC2_HDA ( fdt, 0, hdr->data, len );
1318 
1319  return 0;
1320 }
#define __attribute__(x)
Definition: compiler.h:10
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
#define FDT_STRUCTURE_ALIGN
Alignment of structure block.
Definition: fdt.h:74
int erase(void)
Completely clear the screen.
Definition: clear.c:97
struct golan_inbox_hdr hdr
Message header.
Definition: CIB_PRM.h:28
static int fdt_enter(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Enter node.
Definition: fdt.c:211
void * raw
Raw data.
Definition: fdt.h:94
uint32_t string
Definition: multiboot.h:14
uint32_t fdt_token_t
Device tree token.
Definition: fdt.h:48
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static int fdt_insert_string(struct fdt *fdt, const char *string, unsigned int *offset)
Insert string in strings block.
Definition: fdt.c:1173
#define be32_to_cpu(value)
Definition: byteswap.h:116
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static int fdt_property(struct fdt *fdt, unsigned int offset, const char *name, struct fdt_descriptor *desc)
Find property.
Definition: fdt.c:494
ring len
Length.
Definition: dwmac.h:231
unsigned int structure
Offset to structure block.
Definition: fdt.h:101
#define DBGC2_HDA(...)
Definition: compiler.h:523
Property fragment.
Definition: fdt.h:60
static int fdt_insert_nop(struct fdt *fdt, unsigned int offset, size_t len)
Insert FDT_NOP padded space in structure block.
Definition: fdt.c:1138
unsigned char uint8_t
Definition: stdint.h:10
u8 token
Definition: CIB_PRM.h:42
#define FDT_PROP
Property token.
Definition: fdt.h:57
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:88
#define DBGC2(...)
Definition: compiler.h:522
uint8_t data[48]
Additional event data.
Definition: ena.h:22
static void fdt_nop(struct fdt *fdt, unsigned int offset, size_t len)
Fill space in structure block with FDT_NOP.
Definition: fdt.c:1116
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
const void * data
Property data (if applicable)
Definition: fdt.h:128
void * memset(void *dest, int character, size_t len) __nonnull

References __attribute__, assert(), be32_to_cpu, cpu_to_be32, data, fdt_descriptor::data, DBGC2, DBGC2_HDA, desc, erase(), fdt_enter(), fdt_insert_nop(), fdt_insert_string(), fdt_nop(), FDT_PROP, fdt_property(), FDT_STRUCTURE_ALIGN, hdr, len, memcpy(), memset(), name, offset, fdt::raw, rc, string, fdt::structure, and token.

Referenced by fdt_bootargs().

◆ fdt_urealloc()

static int fdt_urealloc ( struct fdt fdt,
size_t  len 
)
static

Reallocate device tree via urealloc()

Parameters
fdtDevice tree
lenNew total length
Return values
rcReturn status code

Definition at line 1329 of file fdt.c.

1329  {
1330  void *new;
1331 
1332  /* Sanity check */
1333  assert ( len >= fdt->used );
1334 
1335  /* Attempt reallocation */
1336  new = urealloc ( fdt->raw, len );
1337  if ( ! new ) {
1338  DBGC ( fdt, "FDT could not reallocate from +%#04zx to "
1339  "+%#04zx\n", fdt->len, len );
1340  return -ENOMEM;
1341  }
1342  DBGC ( fdt, "FDT reallocated from +%#04zx to +%#04zx\n",
1343  fdt->len, len );
1344 
1345  /* Update device tree */
1346  fdt->raw = new;
1347  fdt->len = len;
1348  fdt->hdr->totalsize = cpu_to_be32 ( len );
1349 
1350  return 0;
1351 }
size_t len
Length of tree.
Definition: fdt.h:97
void * raw
Raw data.
Definition: fdt.h:94
#define DBGC(...)
Definition: compiler.h:505
struct fdt_header * hdr
Tree header.
Definition: fdt.h:92
#define ENOMEM
Not enough space.
Definition: errno.h:534
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
size_t used
Used length of tree.
Definition: fdt.h:99
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:88
uint32_t totalsize
Total size of device tree.
Definition: fdt.h:22
void * urealloc(void *ptr, size_t new_size)
Reallocate external memory.

References assert(), cpu_to_be32, DBGC, ENOMEM, fdt::hdr, fdt::len, len, fdt::raw, fdt_header::totalsize, urealloc(), and fdt::used.

Referenced by fdt_create().

◆ fdt_bootargs()

static int fdt_bootargs ( struct fdt fdt,
const char *  cmdline,
physaddr_t  initrd,
size_t  initrd_len 
)
static

Populate device tree with boot arguments.

Parameters
fdtDevice tree
cmdlineCommand line, or NULL
initrdInitial ramdisk address (or 0 for no initrd)
initrd_lenInitial ramdisk length (or 0 for no initrd)
Return values
rcReturn status code

Definition at line 1362 of file fdt.c.

1363  {
1364  unsigned int chosen;
1365  physaddr_t addr;
1366  const void *data;
1367  size_t len;
1368  int rc;
1369 
1370  /* Ensure "chosen" node exists */
1371  if ( ( rc = fdt_ensure_child ( fdt, 0, "chosen", &chosen ) ) != 0 )
1372  return rc;
1373 
1374  /* Set or clear "bootargs" property */
1375  len = ( cmdline ? ( strlen ( cmdline ) + 1 /* NUL */ ) : 0 );
1376  if ( ( rc = fdt_set ( fdt, chosen, "bootargs", cmdline, len ) ) != 0 )
1377  return rc;
1378 
1379  /* Set or clear initrd properties */
1380  data = ( initrd_len ? &addr : NULL );
1381  len = ( initrd_len ? sizeof ( addr ) : 0 );
1382  addr = initrd;
1383  addr = ( ( sizeof ( addr ) == sizeof ( uint64_t ) ) ?
1384  cpu_to_be64 ( addr ) : cpu_to_be32 ( addr ) );
1385  if ( ( rc = fdt_set ( fdt, chosen, "linux,initrd-start", data,
1386  len ) ) != 0 )
1387  return rc;
1388  addr = ( initrd + initrd_len );
1389  addr = ( ( sizeof ( addr ) == sizeof ( uint64_t ) ) ?
1390  cpu_to_be64 ( addr ) : cpu_to_be32 ( addr ) );
1391  if ( ( rc = fdt_set ( fdt, chosen, "linux,initrd-end", data,
1392  len ) ) != 0 )
1393  return rc;
1394 
1395  return 0;
1396 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int fdt_set(struct fdt *fdt, unsigned int offset, const char *name, const void *data, size_t len)
Set property value.
Definition: fdt.c:1253
unsigned long long uint64_t
Definition: stdint.h:13
ring len
Length.
Definition: dwmac.h:231
uint32_t addr
Buffer address.
Definition: dwmac.h:20
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
unsigned long physaddr_t
Definition: stdint.h:20
#define cpu_to_be32(value)
Definition: byteswap.h:110
A device tree.
Definition: fdt.h:88
#define cpu_to_be64(value)
Definition: byteswap.h:111
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint32_t cmdline
Definition: multiboot.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static int fdt_ensure_child(struct fdt *fdt, unsigned int offset, const char *name, unsigned int *child)
Ensure child node exists.
Definition: fdt.c:1208
#define initrd_len
Definition: runtime.c:63

References addr, cmdline, cpu_to_be32, cpu_to_be64, data, fdt_ensure_child(), fdt_set(), initrd_len, len, NULL, rc, and strlen().

Referenced by fdt_create().

◆ fdt_create()

int fdt_create ( struct fdt_header **  hdr,
const char *  cmdline,
physaddr_t  initrd,
size_t  initrd_len 
)

Create device tree.

Parameters
hdrDevice tree header to fill in (may be set to NULL)
cmdlineCommand line, or NULL
initrdInitial ramdisk address (or 0 for no initrd)
initrd_lenInitial ramdisk length (or 0 for no initrd)
Return values
rcReturn status code

Definition at line 1407 of file fdt.c.

1408  {
1409  struct image *image;
1410  struct fdt fdt;
1411  void *copy;
1412  int rc;
1413 
1414  /* Use system FDT as the base by default */
1415  memcpy ( &fdt, &sysfdt, sizeof ( fdt ) );
1416 
1417  /* If an FDT image exists, use this instead */
1418  image = find_image_tag ( &fdt_image );
1419  if ( image && ( ( rc = fdt_parse_image ( &fdt, image ) ) != 0 ) )
1420  goto err_image;
1421 
1422  /* Exit successfully if we have no base FDT */
1423  if ( ! fdt.len ) {
1424  DBGC ( &fdt, "FDT has no base tree\n" );
1425  goto no_fdt;
1426  }
1427 
1428  /* Create modifiable copy */
1429  copy = umalloc ( fdt.len );
1430  if ( ! copy ) {
1431  rc = -ENOMEM;
1432  goto err_alloc;
1433  }
1434  memcpy ( copy, fdt.raw, fdt.len );
1435  fdt.raw = copy;
1437 
1438  /* Populate boot arguments */
1439  if ( ( rc = fdt_bootargs ( &fdt, cmdline, initrd, initrd_len ) ) != 0 )
1440  goto err_bootargs;
1441 
1442  no_fdt:
1443  *hdr = fdt.raw;
1444  return 0;
1445 
1446  err_bootargs:
1447  ufree ( fdt.raw );
1448  err_alloc:
1449  err_image:
1450  return rc;
1451 }
static __always_inline void ufree(void *ptr)
Free external memory.
Definition: umalloc.h:67
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int(* realloc)(struct fdt *fdt, size_t len)
Reallocate device tree.
Definition: fdt.h:116
static int fdt_urealloc(struct fdt *fdt, size_t len)
Reallocate device tree via urealloc()
Definition: fdt.c:1329
size_t len
Length of tree.
Definition: fdt.h:97
struct golan_inbox_hdr hdr
Message header.
Definition: CIB_PRM.h:28
void * raw
Raw data.
Definition: fdt.h:94
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
struct image * find_image_tag(struct image_tag *tag)
Find image by tag.
Definition: image.c:392
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static int fdt_bootargs(struct fdt *fdt, const char *cmdline, physaddr_t initrd, size_t initrd_len)
Populate device tree with boot arguments.
Definition: fdt.c:1362
A device tree.
Definition: fdt.h:88
static __always_inline void * umalloc(size_t size)
Allocate external memory.
Definition: umalloc.h:56
uint32_t cmdline
Definition: multiboot.h:16
static int fdt_parse_image(struct fdt *fdt, struct image *image)
Parse device tree image.
Definition: fdt.c:1026
#define initrd_len
Definition: runtime.c:63
struct fdt sysfdt
The system flattened device tree (if present)
Definition: fdt.c:44

References cmdline, DBGC, ENOMEM, fdt_bootargs(), fdt_parse_image(), fdt_urealloc(), find_image_tag(), hdr, initrd_len, fdt::len, memcpy(), fdt::raw, rc, fdt::realloc, sysfdt, ufree(), and umalloc().

Referenced by efi_fdt_install(), fdt_test_exec(), and lkrn_exec().

◆ fdt_remove()

void fdt_remove ( struct fdt_header hdr)

Remove device tree.

Parameters
hdrDevice tree header, or NULL

Definition at line 1458 of file fdt.c.

1458  {
1459 
1460  /* Free modifiable copy */
1461  ufree ( hdr );
1462 }
static __always_inline void ufree(void *ptr)
Free external memory.
Definition: umalloc.h:67
struct golan_inbox_hdr hdr
Message header.
Definition: CIB_PRM.h:28

References hdr, and ufree().

Referenced by efi_fdt_install(), efi_fdt_uninstall(), fdt_test_exec(), and lkrn_exec().

◆ REQUIRING_SYMBOL()

REQUIRING_SYMBOL ( fdt_describe  )

◆ REQUIRE_OBJECT()

REQUIRE_OBJECT ( config_fdt  )

Variable Documentation

◆ sysfdt

struct fdt sysfdt

◆ __image_tag

struct image_tag fdt_image __image_tag
Initial value:
= {
.name = "FDT",
}

The downloaded flattened device tree tag.

Definition at line 47 of file fdt.c.