You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memblock.c 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Largest memory block
  27. *
  28. */
  29. #include <stdint.h>
  30. #include <ipxe/uaccess.h>
  31. #include <ipxe/io.h>
  32. #include <ipxe/memblock.h>
  33. /**
  34. * Find largest usable memory region
  35. *
  36. * @ret start Start of region
  37. * @ret len Length of region
  38. */
  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;
  45. size_t region_len;
  46. unsigned int i;
  47. size_t len = 0;
  48. /* Avoid returning uninitialised data on error */
  49. *start = UNULL;
  50. /* Scan through all memory regions */
  51. get_memmap ( &memmap );
  52. for ( i = 0 ; i < memmap.count ; i++ ) {
  53. region = &memmap.regions[i];
  54. DBG ( "Considering [%llx,%llx)\n", region->start, region->end );
  55. /* Truncate block to maximum physical address */
  56. if ( region->start > max ) {
  57. DBG ( "...starts after maximum address %lx\n", max );
  58. continue;
  59. }
  60. region_start = region->start;
  61. if ( region->end > max ) {
  62. DBG ( "...end truncated to maximum address %lx\n", max);
  63. region_end = 0; /* =max, given the wraparound */
  64. } else {
  65. region_end = region->end;
  66. }
  67. region_len = ( region_end - region_start );
  68. /* Use largest block */
  69. if ( region_len > len ) {
  70. DBG ( "...new best block found\n" );
  71. *start = phys_to_user ( region_start );
  72. len = region_len;
  73. }
  74. }
  75. return len;
  76. }