123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- #include <libfdt_env.h>
- #ifndef USE_HOSTCC
- #include <fdt.h>
- #include <libfdt.h>
- #else
- #include "fdt_host.h"
- #endif
- #include "libfdt_internal.h"
- static int fdt_add_region(struct fdt_region_state *info, int offset, int size)
- {
- struct fdt_region *reg;
- reg = info->region ? &info->region[info->count - 1] : NULL;
- if (info->can_merge && info->count &&
- info->count <= info->max_regions &&
- reg && offset <= reg->offset + reg->size) {
- reg->size = offset + size - reg->offset;
- } else if (info->count++ < info->max_regions) {
- if (reg) {
- reg++;
- reg->offset = offset;
- reg->size = size;
- }
- } else {
- return -1;
- }
- return 0;
- }
- static int region_list_contains_offset(struct fdt_region_state *info,
- const void *fdt, int target)
- {
- struct fdt_region *reg;
- int num;
- target += fdt_off_dt_struct(fdt);
- for (reg = info->region, num = 0; num < info->count; reg++, num++) {
- if (target >= reg->offset && target < reg->offset + reg->size)
- return 1;
- }
- return 0;
- }
- int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
- int max_regions, struct fdt_region_state *info)
- {
- int base = fdt_off_dt_struct(fdt);
- int node, node_end, offset;
- int did_alias_header;
- node = fdt_subnode_offset(fdt, 0, "aliases");
- if (node < 0)
- return -FDT_ERR_NOTFOUND;
-
- node_end = fdt_next_subnode(fdt, node);
- if (node_end <= 0)
- return -FDT_ERR_BADLAYOUT;
- node_end -= sizeof(fdt32_t);
- did_alias_header = 0;
- info->region = region;
- info->count = count;
- info->can_merge = 0;
- info->max_regions = max_regions;
- for (offset = fdt_first_property_offset(fdt, node);
- offset >= 0;
- offset = fdt_next_property_offset(fdt, offset)) {
- const struct fdt_property *prop;
- const char *name;
- int target, next;
- prop = fdt_get_property_by_offset(fdt, offset, NULL);
- name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
- target = fdt_path_offset(fdt, name);
- if (!region_list_contains_offset(info, fdt, target))
- continue;
- next = fdt_next_property_offset(fdt, offset);
- if (next < 0)
- next = node_end;
- if (!did_alias_header) {
- fdt_add_region(info, base + node, 12);
- did_alias_header = 1;
- }
- fdt_add_region(info, base + offset, next - offset);
- }
-
- if (did_alias_header)
- fdt_add_region(info, base + node_end, sizeof(fdt32_t));
- return info->count < max_regions ? info->count : -FDT_ERR_NOSPACE;
- }
- static int fdt_include_supernodes(struct fdt_region_state *info, int depth)
- {
- int base = fdt_off_dt_struct(info->fdt);
- int start, stop_at;
- int i;
-
- for (i = 0; i <= depth; i++) {
- if (!info->stack[i].included) {
- start = info->stack[i].offset;
-
- fdt_next_tag(info->fdt, start, &stop_at);
- if (fdt_add_region(info, base + start, stop_at - start))
- return -1;
-
- info->stack[i].included = 1;
- info->can_merge = 1;
- }
-
- if (!info->stack[i].want)
- info->stack[i].want = WANT_NODES_ONLY;
- }
- return 0;
- }
- enum {
- FDT_DONE_NOTHING,
- FDT_DONE_MEM_RSVMAP,
- FDT_DONE_STRUCT,
- FDT_DONE_END,
- FDT_DONE_STRINGS,
- FDT_DONE_ALL,
- };
- int fdt_first_region(const void *fdt,
- int (*h_include)(void *priv, const void *fdt, int offset,
- int type, const char *data, int size),
- void *priv, struct fdt_region *region,
- char *path, int path_len, int flags,
- struct fdt_region_state *info)
- {
- struct fdt_region_ptrs *p = &info->ptrs;
-
- info->fdt = fdt;
- info->can_merge = 1;
- info->max_regions = 1;
- info->start = -1;
- p->want = WANT_NOTHING;
- p->end = path;
- *p->end = '\0';
- p->nextoffset = 0;
- p->depth = -1;
- p->done = FDT_DONE_NOTHING;
- return fdt_next_region(fdt, h_include, priv, region,
- path, path_len, flags, info);
- }
- int fdt_next_region(const void *fdt,
- int (*h_include)(void *priv, const void *fdt, int offset,
- int type, const char *data, int size),
- void *priv, struct fdt_region *region,
- char *path, int path_len, int flags,
- struct fdt_region_state *info)
- {
- int base = fdt_off_dt_struct(fdt);
- int last_node = 0;
- const char *str;
- info->region = region;
- info->count = 0;
- if (info->ptrs.done < FDT_DONE_MEM_RSVMAP &&
- (flags & FDT_REG_ADD_MEM_RSVMAP)) {
-
- if (fdt_add_region(info, fdt_off_mem_rsvmap(fdt),
- fdt_off_dt_struct(fdt) -
- fdt_off_mem_rsvmap(fdt)))
- return 0;
- info->can_merge = 0;
- info->ptrs.done = FDT_DONE_MEM_RSVMAP;
- }
-
- while (info->ptrs.done < FDT_DONE_STRUCT) {
- const struct fdt_property *prop;
- struct fdt_region_ptrs p;
- const char *name;
- int include = 0;
- int stop_at = 0;
- uint32_t tag;
- int offset;
- int val;
- int len;
-
- p = info->ptrs;
-
- offset = p.nextoffset;
- tag = fdt_next_tag(fdt, offset, &p.nextoffset);
- stop_at = p.nextoffset;
- switch (tag) {
- case FDT_PROP:
- stop_at = offset;
- prop = fdt_get_property_by_offset(fdt, offset, NULL);
- str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
- val = h_include(priv, fdt, last_node, FDT_IS_PROP, str,
- strlen(str) + 1);
- if (val == -1) {
- include = p.want >= WANT_NODES_AND_PROPS;
- } else {
- include = val;
-
- if ((flags & FDT_REG_SUPERNODES) && val &&
- !p.want)
- p.want = WANT_NODES_ONLY;
- }
-
- break;
- case FDT_NOP:
- include = p.want >= WANT_NODES_AND_PROPS;
- stop_at = offset;
- break;
- case FDT_BEGIN_NODE:
- last_node = offset;
- p.depth++;
- if (p.depth == FDT_MAX_DEPTH)
- return -FDT_ERR_TOODEEP;
- name = fdt_get_name(fdt, offset, &len);
- if (p.end - path + 2 + len >= path_len)
- return -FDT_ERR_NOSPACE;
-
- if (p.end != path + 1)
- *p.end++ = '/';
- strcpy(p.end, name);
- p.end += len;
- info->stack[p.depth].want = p.want;
- info->stack[p.depth].offset = offset;
-
- if (p.want == WANT_NODES_ONLY ||
- !(flags & (FDT_REG_DIRECT_SUBNODES |
- FDT_REG_ALL_SUBNODES))) {
- stop_at = offset;
- p.want = WANT_NOTHING;
- }
- val = h_include(priv, fdt, offset, FDT_IS_NODE, path,
- p.end - path + 1);
-
- if (val) {
- p.want = (flags & FDT_REG_ALL_SUBNODES) ?
- WANT_ALL_NODES_AND_PROPS :
- WANT_NODES_AND_PROPS;
- }
-
- else if (p.want) {
- if (p.want != WANT_ALL_NODES_AND_PROPS)
- p.want--;
-
- } else {
- stop_at = offset;
- }
-
- include = p.want;
- info->stack[p.depth].included = include;
- break;
- case FDT_END_NODE:
- include = p.want;
- if (p.depth < 0)
- return -FDT_ERR_BADSTRUCTURE;
-
- if (!p.want && !(flags & FDT_REG_DIRECT_SUBNODES))
- stop_at = offset;
- p.want = info->stack[p.depth].want;
- p.depth--;
- while (p.end > path && *--p.end != '/')
- ;
- *p.end = '\0';
- break;
- case FDT_END:
-
- include = 1;
- p.done = FDT_DONE_STRUCT;
- break;
- }
-
- if (include && info->start == -1) {
-
- if (flags & FDT_REG_SUPERNODES) {
- if (fdt_include_supernodes(info, p.depth))
- return 0;
- }
- info->start = offset;
- }
-
- if (!include && info->start != -1) {
- if (fdt_add_region(info, base + info->start,
- stop_at - info->start))
- return 0;
- info->start = -1;
- info->can_merge = 1;
- }
-
- info->ptrs = p;
- }
-
- if (info->ptrs.done < FDT_DONE_END) {
- if (info->ptrs.nextoffset != fdt_size_dt_struct(fdt))
- return -FDT_ERR_BADSTRUCTURE;
- if (fdt_add_region(info, base + info->start,
- info->ptrs.nextoffset - info->start))
- return 0;
- info->ptrs.done++;
- }
- if (info->ptrs.done < FDT_DONE_STRINGS) {
- if (flags & FDT_REG_ADD_STRING_TAB) {
- info->can_merge = 0;
- if (fdt_off_dt_strings(fdt) <
- base + info->ptrs.nextoffset)
- return -FDT_ERR_BADLAYOUT;
- if (fdt_add_region(info, fdt_off_dt_strings(fdt),
- fdt_size_dt_strings(fdt)))
- return 0;
- }
- info->ptrs.done++;
- }
- return info->count > 0 ? 0 : -FDT_ERR_NOTFOUND;
- }
|