1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2009 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall
16 * be included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
27 * ----------------------------------------------------------------------- */
32 * Deal with syslinux_memmap's, which are data structures designed to
33 * hold memory maps. A zonelist is a sorted linked list of memory
34 * ranges, with the guarantee that no two adjacent blocks have the
35 * same range type. Additionally, all unspecified memory have a range
40 #include <syslinux/align.h>
41 #include <syslinux/movebits.h>
45 * Create an empty syslinux_memmap list.
47 struct syslinux_memmap *syslinux_init_memmap(void)
49 struct syslinux_memmap *sp, *ep;
51 sp = malloc(sizeof(*sp));
55 ep = malloc(sizeof(*ep));
62 sp->type = SMT_UNDEFINED;
65 ep->start = 0; /* Wrap around... */
66 ep->type = SMT_END; /* End of chain */
73 * Add an item to a syslinux_memmap list, potentially overwriting
74 * what is already there.
76 int syslinux_add_memmap(struct syslinux_memmap **list,
77 addr_t start, addr_t len,
78 enum syslinux_memmap_types type)
81 struct syslinux_memmap *mp, **mpp;
82 struct syslinux_memmap *range;
83 enum syslinux_memmap_types oldtype;
86 dprintf("Input memmap:\n");
87 syslinux_dump_memmap(stdout, *list);
90 /* Remove this to make len == 0 mean all of memory */
94 /* Last byte -- to avoid rollover */
95 last = start + len - 1;
98 oldtype = SMT_END; /* Impossible value */
99 while (mp = *mpp, start > mp->start && mp->type != SMT_END) {
104 if (start < mp->start || mp->type == SMT_END) {
105 if (type != oldtype) {
106 /* Splice in a new start token */
107 range = malloc(sizeof(*range));
111 range->start = start;
118 /* mp is exactly aligned with the start of our region */
119 if (type != oldtype) {
120 /* Reclaim this entry as our own boundary marker */
127 while (mp = *mpp, last > mp->start - 1) {
133 if (last < mp->start - 1) {
134 if (oldtype != type) {
135 /* Need a new end token */
136 range = malloc(sizeof(*range));
140 range->start = last + 1;
141 range->type = oldtype;
146 if (mp->type == type) {
147 /* Merge this region with the following one */
154 dprintf("After adding (%#x,%#x,%d):\n", start, len, type);
155 syslinux_dump_memmap(stdout, *list);
162 * Verify what type a certain memory region is. This function returns
163 * SMT_ERROR if the memory region has multiple types.
165 enum syslinux_memmap_types syslinux_memmap_type(struct syslinux_memmap *list,
166 addr_t start, addr_t len)
170 last = start + len - 1;
172 while (list->type != SMT_END) {
173 llast = list->next->start - 1;
174 if (list->start <= start) {
176 return list->type; /* Region has a well-defined type */
177 else if (llast >= start)
178 return SMT_ERROR; /* Crosses region boundary */
183 return SMT_ERROR; /* Internal error? */
187 * Find the largest zone of a specific type. Returns -1 on failure.
189 int syslinux_memmap_largest(struct syslinux_memmap *list,
190 enum syslinux_memmap_types type,
191 addr_t * start, addr_t * len)
193 addr_t size, best_size = 0;
194 struct syslinux_memmap *best = NULL;
196 while (list->type != SMT_END) {
197 size = list->next->start - list->start;
199 if (list->type == type && size > best_size) {
210 *start = best->start;
217 * Find the first (lowest address) zone of a specific type and of
218 * a certain minimum size, with an optional starting address.
219 * The input values of start and len are used as minima.
221 int syslinux_memmap_find(struct syslinux_memmap *list,
222 enum syslinux_memmap_types type,
223 addr_t * start, addr_t * len, addr_t align)
225 addr_t min_start = *start;
226 addr_t min_len = *len;
228 while (list->type != SMT_END) {
229 if (list->type == type) {
231 xstart = min_start > list->start ? min_start : list->start;
232 xstart = ALIGN_UP(xstart, align);
234 if (xstart < list->next->start) {
235 xlen = list->next->start - xstart;
236 if (xlen >= min_len) {
246 return -1; /* Not found */
252 void syslinux_free_memmap(struct syslinux_memmap *list)
254 struct syslinux_memmap *ml;
264 * Duplicate a zonelist. Returns NULL on failure.
266 struct syslinux_memmap *syslinux_dup_memmap(struct syslinux_memmap *list)
268 struct syslinux_memmap *newlist = NULL, **nlp = &newlist;
269 struct syslinux_memmap *ml;
272 ml = malloc(sizeof(*ml));
274 syslinux_free_memmap(newlist);
277 ml->start = list->start;
278 ml->type = list->type;