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.

embedded.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /** @file
  2. *
  3. * Embedded image support
  4. *
  5. * Embedded images are images built into the iPXE binary and do not require
  6. * fetching over the network.
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER );
  9. #include <string.h>
  10. #include <ipxe/image.h>
  11. #include <ipxe/uaccess.h>
  12. #include <ipxe/init.h>
  13. /**
  14. * Free embedded image
  15. *
  16. * @v refcnt Reference counter
  17. */
  18. static void __attribute__ (( unused ))
  19. embedded_image_free ( struct refcnt *refcnt __unused ) {
  20. /* Do nothing */
  21. }
  22. /* Raw image data for all embedded images */
  23. #undef EMBED
  24. #define EMBED( _index, _path, _name ) \
  25. extern char embedded_image_ ## _index ## _data[]; \
  26. extern char embedded_image_ ## _index ## _len[]; \
  27. __asm__ ( ".section \".rodata\", \"a\", @progbits\n\t" \
  28. "\nembedded_image_" #_index "_data:\n\t" \
  29. ".incbin \"" _path "\"\n\t" \
  30. "\nembedded_image_" #_index "_end:\n\t" \
  31. ".equ embedded_image_" #_index "_len, " \
  32. "( embedded_image_" #_index "_end - " \
  33. " embedded_image_" #_index "_data )\n\t" \
  34. ".previous\n\t" );
  35. EMBED_ALL
  36. /* Image structures for all embedded images */
  37. #undef EMBED
  38. #define EMBED( _index, _path, _name ) { \
  39. .refcnt = { .free = embedded_image_free, }, \
  40. .name = _name, \
  41. .data = ( userptr_t ) ( embedded_image_ ## _index ## _data ), \
  42. .len = ( size_t ) embedded_image_ ## _index ## _len, \
  43. },
  44. static struct image embedded_images[] = {
  45. EMBED_ALL
  46. };
  47. /**
  48. * Register all embedded images
  49. */
  50. static void embedded_init ( void ) {
  51. int i;
  52. struct image *image;
  53. void *data;
  54. int rc;
  55. /* Skip if we have no embedded images */
  56. if ( ! sizeof ( embedded_images ) )
  57. return;
  58. /* Fix up data pointers and register images */
  59. for ( i = 0 ; i < ( int ) ( sizeof ( embedded_images ) /
  60. sizeof ( embedded_images[0] ) ) ; i++ ) {
  61. image = &embedded_images[i];
  62. /* virt_to_user() cannot be used in a static
  63. * initialiser, so we cast the pointer to a userptr_t
  64. * in the initialiser and fix it up here. (This will
  65. * actually be a no-op on most platforms.)
  66. */
  67. data = ( ( void * ) image->data );
  68. image->data = virt_to_user ( data );
  69. DBG ( "Embedded image \"%s\": %zd bytes at %p\n",
  70. image->name, image->len, data );
  71. if ( ( rc = register_image ( image ) ) != 0 ) {
  72. DBG ( "Could not register embedded image \"%s\": "
  73. "%s\n", image->name, strerror ( rc ) );
  74. return;
  75. }
  76. }
  77. /* Load the first image */
  78. image = &embedded_images[0];
  79. if ( ( rc = image_autoload ( image ) ) != 0 ) {
  80. DBG ( "Could not load embedded image \"%s\": %s\n",
  81. image->name, strerror ( rc ) );
  82. return;
  83. }
  84. }
  85. /** Embedded image initialisation function */
  86. struct init_fn embedded_init_fn __init_fn ( INIT_NORMAL ) = {
  87. .initialise = embedded_init,
  88. };