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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <realmode.h>
  24. #include <sdi.h>
  25. #include <ipxe/image.h>
  26. #include <ipxe/features.h>
  27. /** @file
  28. *
  29. * System Deployment Image (SDI)
  30. *
  31. * Based on the MSDN article "RAM boot using SDI in Windows XP
  32. * Embedded with Service Pack 1", available at the time of writing
  33. * from:
  34. *
  35. * http://msdn.microsoft.com/en-us/library/ms838543.aspx
  36. */
  37. FEATURE ( FEATURE_IMAGE, "SDI", DHCP_EB_FEATURE_SDI, 1 );
  38. /**
  39. * Parse SDI image header
  40. *
  41. * @v image SDI file
  42. * @v sdi SDI header to fill in
  43. * @ret rc Return status code
  44. */
  45. static int sdi_parse_header ( struct image *image, struct sdi_header *sdi ) {
  46. /* Sanity check */
  47. if ( image->len < sizeof ( *sdi ) ) {
  48. DBGC ( image, "SDI %p too short for SDI header\n", image );
  49. return -ENOEXEC;
  50. }
  51. /* Read in header */
  52. copy_from_user ( sdi, image->data, 0, sizeof ( *sdi ) );
  53. /* Check signature */
  54. if ( sdi->magic != SDI_MAGIC ) {
  55. DBGC ( image, "SDI %p is not an SDI image\n", image );
  56. return -ENOEXEC;
  57. }
  58. return 0;
  59. }
  60. /**
  61. * Execute SDI image
  62. *
  63. * @v image SDI file
  64. * @ret rc Return status code
  65. */
  66. static int sdi_exec ( struct image *image ) {
  67. struct sdi_header sdi;
  68. uint32_t sdiptr;
  69. int rc;
  70. /* Parse image header */
  71. if ( ( rc = sdi_parse_header ( image, &sdi ) ) != 0 )
  72. return rc;
  73. /* Check that image is bootable */
  74. if ( sdi.boot_size == 0 ) {
  75. DBGC ( image, "SDI %p is not bootable\n", image );
  76. return -ENOTTY;
  77. }
  78. DBGC ( image, "SDI %p image at %08lx+%08zx\n",
  79. image, user_to_phys ( image->data, 0 ), image->len );
  80. DBGC ( image, "SDI %p boot code at %08lx+%llx\n", image,
  81. user_to_phys ( image->data, sdi.boot_offset ), sdi.boot_size );
  82. /* Copy boot code */
  83. memcpy_user ( real_to_user ( SDI_BOOT_SEG, SDI_BOOT_OFF ), 0,
  84. image->data, sdi.boot_offset, sdi.boot_size );
  85. /* Jump to boot code */
  86. sdiptr = ( user_to_phys ( image->data, 0 ) | SDI_WTF );
  87. __asm__ __volatile__ ( REAL_CODE ( "ljmp %0, %1\n\t" )
  88. : : "i" ( SDI_BOOT_SEG ),
  89. "i" ( SDI_BOOT_OFF ),
  90. "d" ( sdiptr ) );
  91. /* There is no way for the image to return, since we provide
  92. * no return address.
  93. */
  94. assert ( 0 );
  95. return -ECANCELED; /* -EIMPOSSIBLE */
  96. }
  97. /**
  98. * Probe SDI image
  99. *
  100. * @v image SDI file
  101. * @ret rc Return status code
  102. */
  103. static int sdi_probe ( struct image *image ) {
  104. struct sdi_header sdi;
  105. int rc;
  106. /* Parse image */
  107. if ( ( rc = sdi_parse_header ( image, &sdi ) ) != 0 )
  108. return rc;
  109. return 0;
  110. }
  111. /** SDI image type */
  112. struct image_type sdi_image_type __image_type ( PROBE_NORMAL ) = {
  113. .name = "SDI",
  114. .probe = sdi_probe,
  115. .exec = sdi_exec,
  116. };