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

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