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.

sdi.c 3.6KB

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