1
/* Copyright 2013-2014 IBM Corp.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
19
const void *HDIF_get_idata(const struct HDIF_common_hdr *hdif, unsigned int di,
22
const struct HDIF_common_hdr *hdr = hdif;
23
const struct HDIF_idata_ptr *iptr;
25
if (be16_to_cpu(hdr->d1f0) != 0xd1f0) {
26
prerror("HDIF: Bad header format !\n");
30
if (di >= be16_to_cpu(hdr->idptr_count)) {
31
prerror("HDIF: idata index out of range !\n");
35
iptr = (void *)hdif + be32_to_cpu(hdr->idptr_off)
36
+ di * sizeof(struct HDIF_idata_ptr);
39
*size = be32_to_cpu(iptr->size);
41
return (void *)hdif + be32_to_cpu(iptr->offset);
44
const void *HDIF_get_iarray_item(const struct HDIF_common_hdr *hdif,
45
unsigned int di, unsigned int ai,
48
const struct HDIF_array_hdr *ahdr;
52
arr = HDIF_get_idata(hdif, di, &asize);
56
if (asize < sizeof(struct HDIF_array_hdr)) {
57
prerror("HDIF: idata block too small for array !\n");
63
if (ai >= be32_to_cpu(ahdr->ecnt)) {
64
prerror("HDIF: idata array index out of range !\n");
69
*size = be32_to_cpu(ahdr->eactsz);
71
return arr + be32_to_cpu(ahdr->offset) + ai * be32_to_cpu(ahdr->esize);
74
int HDIF_get_iarray_size(const struct HDIF_common_hdr *hdif, unsigned int di)
76
const struct HDIF_array_hdr *ahdr;
80
arr = HDIF_get_idata(hdif, di, &asize);
84
if (asize < sizeof(struct HDIF_array_hdr)) {
85
prerror("HDIF: idata block too small for array !\n");
90
return be32_to_cpu(ahdr->ecnt);
93
struct HDIF_child_ptr *
94
HDIF_child_arr(const struct HDIF_common_hdr *hdif, unsigned int idx)
96
struct HDIF_child_ptr *children;
98
children = (void *)hdif + be32_to_cpu(hdif->child_off);
100
if (idx >= be16_to_cpu(hdif->child_count)) {
101
prerror("HDIF: child array idx out of range!\n");
105
return &children[idx];
108
struct HDIF_common_hdr *HDIF_child(const struct HDIF_common_hdr *hdif,
109
const struct HDIF_child_ptr *child,
111
const char *eyecatcher)
113
void *base = (void *)hdif;
114
struct HDIF_common_hdr *ret;
117
/* child must be in hdif's child array */
118
child_off = (void *)child - (base + be32_to_cpu(hdif->child_off));
119
assert(child_off % sizeof(struct HDIF_child_ptr) == 0);
120
assert(child_off / sizeof(struct HDIF_child_ptr)
121
< be16_to_cpu(hdif->child_count));
123
assert(idx < be32_to_cpu(child->count));
125
if (be32_to_cpu(child->size) < sizeof(struct HDIF_common_hdr)) {
126
prerror("HDIF: %s child #%i too small: %u\n",
127
eyecatcher, idx, be32_to_cpu(child->size));
131
ret = base + be32_to_cpu(child->offset)
132
+ be32_to_cpu(child->size) * idx;
133
if (!HDIF_check(ret, eyecatcher)) {
134
prerror("HDIF: %s child #%i bad type\n",