2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
24 * Largest memory block
29 #include <ipxe/uaccess.h>
31 #include <ipxe/memblock.h>
34 * Find largest usable memory region
36 * @ret start Start of region
37 * @ret len Length of region
39 size_t largest_memblock ( userptr_t *start ) {
40 struct memory_map memmap;
41 struct memory_region *region;
42 physaddr_t max = ~( ( physaddr_t ) 0 );
43 physaddr_t region_start;
44 physaddr_t region_end;
49 /* Avoid returning uninitialised data on error */
52 /* Scan through all memory regions */
53 get_memmap ( &memmap );
54 for ( i = 0 ; i < memmap.count ; i++ ) {
55 region = &memmap.regions[i];
56 DBG ( "Considering [%llx,%llx)\n", region->start, region->end );
58 /* Truncate block to maximum physical address */
59 if ( region->start > max ) {
60 DBG ( "...starts after maximum address %lx\n", max );
63 region_start = region->start;
64 if ( region->end > max ) {
65 DBG ( "...end truncated to maximum address %lx\n", max);
66 region_end = 0; /* =max, given the wraparound */
68 region_end = region->end;
70 region_len = ( region_end - region_start );
72 /* Use largest block */
73 if ( region_len > len ) {
74 DBG ( "...new best block found\n" );
75 *start = phys_to_user ( region_start );