Skip to content

Commit 9708d01

Browse files
committed
lib: libfdt: Import libfdt v1.7.0
From https://git.kernel.org/pub/scm/utils/dtc/dtc.git tag v1.7.0 subdirectory libfdt
1 parent 541d9bf commit 9708d01

File tree

9 files changed

+60
-34
lines changed

9 files changed

+60
-34
lines changed

lib/libfdt/fdt.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ int fdt_check_header(const void *fdt)
106106
}
107107
hdrsize = fdt_header_size(fdt);
108108
if (!can_assume(VALID_DTB)) {
109-
110109
if ((fdt_totalsize(fdt) < hdrsize)
111110
|| (fdt_totalsize(fdt) > INT_MAX))
112111
return -FDT_ERR_TRUNCATED;
@@ -115,9 +114,7 @@ int fdt_check_header(const void *fdt)
115114
if (!check_off_(hdrsize, fdt_totalsize(fdt),
116115
fdt_off_mem_rsvmap(fdt)))
117116
return -FDT_ERR_TRUNCATED;
118-
}
119117

120-
if (!can_assume(VALID_DTB)) {
121118
/* Bounds check structure block */
122119
if (!can_assume(LATEST) && fdt_version(fdt) < 17) {
123120
if (!check_off_(hdrsize, fdt_totalsize(fdt),
@@ -165,7 +162,7 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
165162
uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
166163
{
167164
const fdt32_t *tagp, *lenp;
168-
uint32_t tag;
165+
uint32_t tag, len, sum;
169166
int offset = startoffset;
170167
const char *p;
171168

@@ -191,12 +188,19 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
191188
lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
192189
if (!can_assume(VALID_DTB) && !lenp)
193190
return FDT_END; /* premature end */
191+
192+
len = fdt32_to_cpu(*lenp);
193+
sum = len + offset;
194+
if (!can_assume(VALID_DTB) &&
195+
(INT_MAX <= sum || sum < (uint32_t) offset))
196+
return FDT_END; /* premature end */
197+
194198
/* skip-name offset, length and value */
195-
offset += sizeof(struct fdt_property) - FDT_TAGSIZE
196-
+ fdt32_to_cpu(*lenp);
199+
offset += sizeof(struct fdt_property) - FDT_TAGSIZE + len;
200+
197201
if (!can_assume(LATEST) &&
198-
fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 &&
199-
((offset - fdt32_to_cpu(*lenp)) % 8) != 0)
202+
fdt_version(fdt) < 0x10 && len >= 8 &&
203+
((offset - len) % 8) != 0)
200204
offset += 4;
201205
break;
202206

lib/libfdt/fdt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ struct fdt_reserve_entry {
3535

3636
struct fdt_node_header {
3737
fdt32_t tag;
38-
char name[0];
38+
char name[];
3939
};
4040

4141
struct fdt_property {
4242
fdt32_t tag;
4343
fdt32_t len;
4444
fdt32_t nameoff;
45-
char data[0];
45+
char data[];
4646
};
4747

4848
#endif /* !__ASSEMBLY */

lib/libfdt/fdt_addresses.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
7373
/* check validity of address */
7474
prop = data;
7575
if (addr_cells == 1) {
76-
if ((addr > UINT32_MAX) || ((UINT32_MAX + 1 - addr) < size))
76+
if ((addr > UINT32_MAX) || (((uint64_t) UINT32_MAX + 1 - addr) < size))
7777
return -FDT_ERR_BADVALUE;
7878

7979
fdt32_st(prop, (uint32_t)addr);

lib/libfdt/fdt_check.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ int fdt_check_full(const void *fdt, size_t bufsize)
6666
int len;
6767

6868
name = fdt_get_name(fdt, offset, &len);
69+
if (!name)
70+
return len;
71+
6972
if (*name || len)
7073
return -FDT_ERR_BADSTRUCTURE;
7174
}

lib/libfdt/fdt_overlay.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,22 @@ static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
4040
return fdt32_to_cpu(*val);
4141
}
4242

43-
/**
44-
* overlay_get_target - retrieves the offset of a fragment's target
45-
* @fdt: Base device tree blob
46-
* @fdto: Device tree overlay blob
47-
* @fragment: node offset of the fragment in the overlay
48-
* @pathp: pointer which receives the path of the target (or NULL)
49-
*
50-
* overlay_get_target() retrieves the target offset in the base
51-
* device tree of a fragment, no matter how the actual targeting is
52-
* done (through a phandle or a path)
53-
*
54-
* returns:
55-
* the targeted node offset in the base device tree
56-
* Negative error code on error
57-
*/
58-
static int overlay_get_target(const void *fdt, const void *fdto,
59-
int fragment, char const **pathp)
43+
int fdt_overlay_target_offset(const void *fdt, const void *fdto,
44+
int fragment_offset, char const **pathp)
6045
{
6146
uint32_t phandle;
6247
const char *path = NULL;
6348
int path_len = 0, ret;
6449

6550
/* Try first to do a phandle based lookup */
66-
phandle = overlay_get_target_phandle(fdto, fragment);
51+
phandle = overlay_get_target_phandle(fdto, fragment_offset);
6752
if (phandle == (uint32_t)-1)
6853
return -FDT_ERR_BADPHANDLE;
6954

7055
/* no phandle, try path */
7156
if (!phandle) {
7257
/* And then a path based lookup */
73-
path = fdt_getprop(fdto, fragment, "target-path", &path_len);
58+
path = fdt_getprop(fdto, fragment_offset, "target-path", &path_len);
7459
if (path)
7560
ret = fdt_path_offset(fdt, path);
7661
else
@@ -636,7 +621,7 @@ static int overlay_merge(void *fdt, void *fdto)
636621
if (overlay < 0)
637622
return overlay;
638623

639-
target = overlay_get_target(fdt, fdto, fragment, NULL);
624+
target = fdt_overlay_target_offset(fdt, fdto, fragment, NULL);
640625
if (target < 0)
641626
return target;
642627

@@ -779,7 +764,7 @@ static int overlay_symbol_update(void *fdt, void *fdto)
779764
return -FDT_ERR_BADOVERLAY;
780765

781766
/* get the target of the fragment */
782-
ret = overlay_get_target(fdt, fdto, fragment, &target_path);
767+
ret = fdt_overlay_target_offset(fdt, fdto, fragment, &target_path);
783768
if (ret < 0)
784769
return ret;
785770
target = ret;
@@ -801,7 +786,7 @@ static int overlay_symbol_update(void *fdt, void *fdto)
801786

802787
if (!target_path) {
803788
/* again in case setprop_placeholder changed it */
804-
ret = overlay_get_target(fdt, fdto, fragment, &target_path);
789+
ret = fdt_overlay_target_offset(fdt, fdto, fragment, &target_path);
805790
if (ret < 0)
806791
return ret;
807792
target = ret;

lib/libfdt/fdt_ro.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,12 @@ const void *fdt_getprop_by_offset(const void *fdt, int offset,
481481
if (!can_assume(VALID_INPUT)) {
482482
name = fdt_get_string(fdt, fdt32_ld_(&prop->nameoff),
483483
&namelen);
484+
*namep = name;
484485
if (!name) {
485486
if (lenp)
486487
*lenp = namelen;
487488
return NULL;
488489
}
489-
*namep = name;
490490
} else {
491491
*namep = fdt_string(fdt, fdt32_ld_(&prop->nameoff));
492492
}

lib/libfdt/fdt_strerror.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static struct fdt_errtabent fdt_errtable[] = {
3939
FDT_ERRTABENT(FDT_ERR_BADOVERLAY),
4040
FDT_ERRTABENT(FDT_ERR_NOPHANDLES),
4141
FDT_ERRTABENT(FDT_ERR_BADFLAGS),
42+
FDT_ERRTABENT(FDT_ERR_ALIGNMENT),
4243
};
4344
#define FDT_ERRTABSIZE ((int)(sizeof(fdt_errtable) / sizeof(fdt_errtable[0])))
4445

lib/libfdt/libfdt.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
131131
* to work even with unaligned pointers on platforms (such as ARMv5) that don't
132132
* like unaligned loads and stores.
133133
*/
134+
static inline uint16_t fdt16_ld(const fdt16_t *p)
135+
{
136+
const uint8_t *bp = (const uint8_t *)p;
137+
138+
return ((uint16_t)bp[0] << 8) | bp[1];
139+
}
140+
134141
static inline uint32_t fdt32_ld(const fdt32_t *p)
135142
{
136143
const uint8_t *bp = (const uint8_t *)p;
@@ -653,6 +660,13 @@ int fdt_next_property_offset(const void *fdt, int offset);
653660
const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
654661
int offset,
655662
int *lenp);
663+
static inline struct fdt_property *fdt_get_property_by_offset_w(void *fdt,
664+
int offset,
665+
int *lenp)
666+
{
667+
return (struct fdt_property *)(uintptr_t)
668+
fdt_get_property_by_offset(fdt, offset, lenp);
669+
}
656670

657671
/**
658672
* fdt_get_property_namelen - find a property based on substring
@@ -2109,6 +2123,24 @@ int fdt_del_node(void *fdt, int nodeoffset);
21092123
*/
21102124
int fdt_overlay_apply(void *fdt, void *fdto);
21112125

2126+
/**
2127+
* fdt_overlay_target_offset - retrieves the offset of a fragment's target
2128+
* @fdt: Base device tree blob
2129+
* @fdto: Device tree overlay blob
2130+
* @fragment_offset: node offset of the fragment in the overlay
2131+
* @pathp: pointer which receives the path of the target (or NULL)
2132+
*
2133+
* fdt_overlay_target_offset() retrieves the target offset in the base
2134+
* device tree of a fragment, no matter how the actual targeting is
2135+
* done (through a phandle or a path)
2136+
*
2137+
* returns:
2138+
* the targeted node offset in the base device tree
2139+
* Negative error code on error
2140+
*/
2141+
int fdt_overlay_target_offset(const void *fdt, const void *fdto,
2142+
int fragment_offset, char const **pathp);
2143+
21122144
/**********************************************************************/
21132145
/* Debugging / informational functions */
21142146
/**********************************************************************/

lib/libfdt/version.lds

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ LIBFDT_1.2 {
7777
fdt_appendprop_addrrange;
7878
fdt_setprop_inplace_namelen_partial;
7979
fdt_create_with_flags;
80+
fdt_overlay_target_offset;
8081
local:
8182
*;
8283
};

0 commit comments

Comments
 (0)