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.6KB

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