1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
31 * Deal with syslinux_memmap's, which are data structures designed to
32 * hold memory maps. A zonelist is a sorted linked list of memory
33 * ranges, with the guarantee that no two adjacent blocks have the
34 * same range type. Additionally, all unspecified memory have a range
39 #include <syslinux/movebits.h>
42 * Create an empty syslinux_memmap list.
44 struct syslinux_memmap
*syslinux_init_memmap(void)
46 struct syslinux_memmap
*sp
, *ep
;
48 sp
= malloc(sizeof(*sp
));
52 ep
= malloc(sizeof(*ep
));
59 sp
->type
= SMT_UNDEFINED
;
62 ep
->start
= 0; /* Wrap around... */
63 ep
->type
= SMT_END
; /* End of chain */
70 * Add an item to a syslinux_memmap list, potentially overwriting
71 * what is already there.
73 int syslinux_add_memmap(struct syslinux_memmap
**list
,
74 addr_t start
, addr_t len
,
75 enum syslinux_memmap_types type
)
78 struct syslinux_memmap
*mp
, **mpp
;
79 struct syslinux_memmap
*mpi
;
80 struct syslinux_memmap
*range
;
81 enum syslinux_memmap_types oldtype
;
83 /* Remove this to make len == 0 mean all of memory */
87 /* Last byte -- to avoid rollover */
92 while (mp
= *mpp
, start
> mp
->start
&& mp
->type
!= SMT_END
) {
97 if (start
< mp
->start
|| mp
->type
== SMT_END
) {
98 range
= malloc(sizeof(*range
));
102 range
->start
= start
;
109 while (mp
= *mpp
, last
> mp
->start
-1) {
115 if (last
< mp
->start
-1) {
116 range
= malloc(sizeof(*range
));
120 range
->start
= last
+1;
121 range
->type
= oldtype
;
126 /* Now the map is correct, but quite possibly not optimal. Scan the
127 map for ranges which are redundant and remove them. This is
128 technically excessive, since we scan the list to the end even
129 though only part of it could have changed. In particular, the first
130 entry that could change is one earlier than the first one changed
131 above, and once we stop changing things, there shouldn't be any
134 while (mpi
->type
!= SMT_END
) {
136 if (mpi
->type
== mp
->type
) {
137 mpi
->next
= mp
->next
;
140 /* Go on to the next one */
149 * Verify what type a certain memory region is. This function returns
150 * SMT_ERROR if the memory region has multiple types.
152 enum syslinux_memmap_types
syslinux_memmap_type(struct syslinux_memmap
*list
,
153 addr_t start
, addr_t len
)
159 while (list
->type
!= SMT_END
) {
160 llast
= list
->next
->start
-1;
161 if (list
->start
<= start
) {
163 return list
->type
; /* Region has a well-defined type */
164 else if (llast
>= start
)
165 return SMT_ERROR
; /* Crosses region boundary */
170 return SMT_ERROR
; /* Internal error? */
174 * Find the largest zone of a specific type. Returns -1 on failure.
176 int syslinux_memmap_largest(struct syslinux_memmap
*list
,
177 enum syslinux_memmap_types type
,
178 addr_t
*start
, addr_t
*len
)
180 addr_t size
, best_size
= 0;
181 struct syslinux_memmap
*best
= NULL
;
183 while (list
->type
!= SMT_END
) {
184 size
= list
->next
->start
- list
->start
;
186 if (list
->type
== type
&& size
> best_size
) {
197 *start
= best
->start
;
206 void syslinux_free_memmap(struct syslinux_memmap
*list
)
208 struct syslinux_memmap
*ml
;
218 * Duplicate a zonelist. Returns NULL on failure.
220 struct syslinux_memmap
*syslinux_dup_memmap(struct syslinux_memmap
*list
)
222 struct syslinux_memmap
*newlist
= NULL
, **nlp
= &newlist
;
223 struct syslinux_memmap
*ml
;
226 ml
= malloc(sizeof(*ml
));
228 syslinux_free_memmap(newlist
);
231 ml
->start
= list
->start
;
232 ml
->type
= list
->type
;