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.

segment.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) 2007 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /**
  20. * @file
  21. *
  22. * Executable image segments
  23. *
  24. */
  25. #include <errno.h>
  26. #include <gpxe/uaccess.h>
  27. #include <gpxe/memmap.h>
  28. #include <gpxe/errortab.h>
  29. #include <gpxe/segment.h>
  30. /**
  31. * Prepare segment for loading
  32. *
  33. * @v segment Segment start
  34. * @v filesz Size of the "allocated bytes" portion of the segment
  35. * @v memsz Size of the segment
  36. * @ret rc Return status code
  37. */
  38. int prep_segment ( userptr_t segment, size_t filesz, size_t memsz ) {
  39. struct memory_map memmap;
  40. physaddr_t start = user_to_phys ( segment, 0 );
  41. physaddr_t mid = user_to_phys ( segment, filesz );
  42. physaddr_t end = user_to_phys ( segment, memsz );
  43. unsigned int i;
  44. DBG ( "Preparing segment [%lx,%lx,%lx)\n", start, mid, end );
  45. /* Sanity check */
  46. if ( filesz > memsz ) {
  47. DBG ( "Insane segment [%lx,%lx,%lx)\n", start, mid, end );
  48. return -EINVAL;
  49. }
  50. /* Get a fresh memory map. This allows us to automatically
  51. * avoid treading on any regions that Etherboot is currently
  52. * editing out of the memory map.
  53. */
  54. get_memmap ( &memmap );
  55. /* Look for a suitable memory region */
  56. for ( i = 0 ; i < memmap.count ; i++ ) {
  57. if ( ( start >= memmap.regions[i].start ) &&
  58. ( end <= memmap.regions[i].end ) ) {
  59. /* Found valid region: zero bss and return */
  60. memset_user ( segment, filesz, 0, ( memsz - filesz ) );
  61. return 0;
  62. }
  63. }
  64. /* No suitable memory region found */
  65. DBG ( "Segment [%lx,%lx,%lx) does not fit into available memory\n",
  66. start, mid, end );
  67. return -ERANGE;
  68. }
  69. /**
  70. * Segment-specific error messages
  71. *
  72. * This error happens sufficiently often to merit a user-friendly
  73. * description.
  74. */
  75. struct errortab segment_errors[] __errortab = {
  76. { ERANGE, "Requested memory not available" },
  77. };