/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright Pantelis Antoniou 2006
* Copyright (C) IBM Corporation 2006
*
* Authors: Pantelis Antoniou <pantelis@embeddedalley.com>
* Hollis Blanchard <hollisb@us.ibm.com>
* Mark A. Greer <mgreer@mvista.com>
* Paul Mackerras <paulus@samba.org>
*/
#include <string.h>
#include <stddef.h>
#include "flatdevtree.h"
#include "flatdevtree_env.h"
#define _ALIGN(x, al) (((x) + (al) - 1) & ~((al) - 1))
static char *ft_root_node(struct ft_cxt *cxt)
{
return cxt->rgn[FT_STRUCT].start;
}
/* Routines for keeping node ptrs returned by ft_find_device current */
/* First entry not used b/c it would return 0 and be taken as NULL/error */
static void *ft_get_phandle(struct ft_cxt *cxt, char *node)
{
unsigned int i;
if (!node)
return NULL;
for (i = 1; i < cxt->nodes_used; i++) /* already there? */
if (cxt->node_tbl[i] == node)
return (void *)i;
if (cxt->nodes_used < cxt->node_max) {
cxt->node_tbl[cxt->nodes_used] = node;
return (void *)cxt->nodes_used++;
}
return NULL;
}
static char *ft_node_ph2node(struct ft_cxt *cxt, const void *phandle)
{
unsigned int i = (unsigned int)phandle;
if (i < cxt->nodes_used)
return cxt->node_tbl[i];
return NULL;
}
static void ft_node_update_before(struct ft_cxt *cxt, char *addr, int shift)
{
unsigned int i;
if (shift == 0)
return;
for (i = 1; i < cxt->nodes_used; i++)
if (cxt->node_tbl[i] < addr)
cxt->node_tbl[i] += shift;
}
static void ft_node_update_after(struct ft_cxt *cxt, char *addr, int shift)
{
unsigned int i;
if (shift == 0)
return;
for (i = 1; i < cxt->nodes_used; i++)
if (cxt->node_tbl[i] >= addr)
cxt->node_tbl[i] += shift;
}
/* Struct used to return info from ft_next() */
struct ft_atom {
u32 tag;
const char *name;
void *data;
u32 size;
};
/* Set ptrs to current one's info; return addr of next one */
static char *ft_next(struct ft_cxt *cxt, char *p, struct ft_atom *ret)
{
u32 sz;
if (p >= cxt->rgn[FT_STRUCT].start + cxt->rgn[FT_STRUCT].size)
return NULL;
ret->tag = be32_to_cpu(*(u32 *) p);
p += 4;
switch (ret->tag) { /* Tag */
case OF_DT_BEGIN_NODE:
ret->name = p;
ret->data = (void *)(p - 4); /* start of node */
p += _ALIGN(strlen(p) + 1, 4);
break;
case OF_DT_PROP:
ret->size = sz = be32_to_cpu(*(u32 *) p);
ret->name = cxt->str_anchor + be32_to_cpu(*(u32 *) (p + 4));
ret->data = (void *)(p + 8);
p += 8 + _ALIGN(sz, 4);
break;
case OF_DT_END_NODE:
case OF_DT_NOP:
break;
case OF_DT_END:
default:
p = NULL;
break;
}
return p;
}
#define HDR_SIZE _ALIGN(sizeof(struct boot_param_header), 8)
#define EXPAND_INCR 1024 /* alloc this much extra when expanding */
/* See if the regions are in the standard order and non-overlapping */
static int ft_ordered(struct ft_cxt *cxt)
{
char *p = (char *)cxt->bph + HDR_SIZE;
enum ft_rgn_id r;
for (r <