Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

segment.c 2.6KB

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