~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/skiboot/hdata/hdif.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2013-2014 IBM Corp.
 
2
 *
 
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
 
6
 *
 
7
 *      http://www.apache.org/licenses/LICENSE-2.0
 
8
 *
 
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
 
12
 * implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#include "hdif.h"
 
18
 
 
19
const void *HDIF_get_idata(const struct HDIF_common_hdr *hdif, unsigned int di,
 
20
                           unsigned int *size)
 
21
{
 
22
        const struct HDIF_common_hdr *hdr = hdif;
 
23
        const struct HDIF_idata_ptr *iptr;
 
24
 
 
25
        if (be16_to_cpu(hdr->d1f0) != 0xd1f0) {
 
26
                prerror("HDIF: Bad header format !\n");
 
27
                return NULL;
 
28
        }
 
29
 
 
30
        if (di >= be16_to_cpu(hdr->idptr_count)) {
 
31
                prerror("HDIF: idata index out of range !\n");
 
32
                return NULL;
 
33
        }
 
34
 
 
35
        iptr = (void *)hdif + be32_to_cpu(hdr->idptr_off)
 
36
                + di * sizeof(struct HDIF_idata_ptr);
 
37
 
 
38
        if (size)
 
39
                *size = be32_to_cpu(iptr->size);
 
40
 
 
41
        return (void *)hdif + be32_to_cpu(iptr->offset);
 
42
}
 
43
 
 
44
const void *HDIF_get_iarray_item(const struct HDIF_common_hdr *hdif,
 
45
                                 unsigned int di, unsigned int ai,
 
46
                                 unsigned int *size)
 
47
{
 
48
        const struct HDIF_array_hdr *ahdr;
 
49
        unsigned int asize;
 
50
        const void *arr;
 
51
 
 
52
        arr = HDIF_get_idata(hdif, di, &asize);
 
53
        if (!arr)
 
54
                return NULL;
 
55
 
 
56
        if (asize < sizeof(struct HDIF_array_hdr)) {
 
57
                prerror("HDIF: idata block too small for array !\n");
 
58
                return NULL;
 
59
        }
 
60
 
 
61
        ahdr = arr;
 
62
 
 
63
        if (ai >= be32_to_cpu(ahdr->ecnt)) {
 
64
                prerror("HDIF: idata array index out of range !\n");
 
65
                return NULL;
 
66
        }
 
67
 
 
68
        if (size)
 
69
                *size = be32_to_cpu(ahdr->eactsz);
 
70
 
 
71
        return arr + be32_to_cpu(ahdr->offset) + ai * be32_to_cpu(ahdr->esize);
 
72
}
 
73
 
 
74
int HDIF_get_iarray_size(const struct HDIF_common_hdr *hdif, unsigned int di)
 
75
{
 
76
        const struct HDIF_array_hdr *ahdr;
 
77
        unsigned int asize;
 
78
        const void *arr;
 
79
 
 
80
        arr = HDIF_get_idata(hdif, di, &asize);
 
81
        if (!arr)
 
82
                return -1;
 
83
 
 
84
        if (asize < sizeof(struct HDIF_array_hdr)) {
 
85
                prerror("HDIF: idata block too small for array !\n");
 
86
                return -1;
 
87
        }
 
88
 
 
89
        ahdr = arr;
 
90
        return be32_to_cpu(ahdr->ecnt);
 
91
}
 
92
 
 
93
struct HDIF_child_ptr *
 
94
HDIF_child_arr(const struct HDIF_common_hdr *hdif, unsigned int idx)
 
95
{
 
96
        struct HDIF_child_ptr *children;
 
97
 
 
98
        children = (void *)hdif + be32_to_cpu(hdif->child_off);
 
99
 
 
100
        if (idx >= be16_to_cpu(hdif->child_count)) {
 
101
                prerror("HDIF: child array idx out of range!\n");
 
102
                return NULL;
 
103
        }
 
104
 
 
105
        return &children[idx];
 
106
}
 
107
 
 
108
struct HDIF_common_hdr *HDIF_child(const struct HDIF_common_hdr *hdif,
 
109
                                   const struct HDIF_child_ptr *child,
 
110
                                   unsigned int idx,
 
111
                                   const char *eyecatcher)
 
112
{
 
113
        void *base = (void *)hdif;
 
114
        struct HDIF_common_hdr *ret;
 
115
        long child_off;
 
116
 
 
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));
 
122
 
 
123
        assert(idx < be32_to_cpu(child->count));
 
124
 
 
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));
 
128
                return NULL;
 
129
        }
 
130
 
 
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",
 
135
                        eyecatcher, idx);
 
136
                return NULL;
 
137
        }
 
138
 
 
139
        return ret;
 
140
}