Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

script.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * iPXE scripts
  23. *
  24. */
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #include <ipxe/image.h>
  30. struct image_type script_image_type __image_type ( PROBE_NORMAL );
  31. /**
  32. * Execute script
  33. *
  34. * @v image Script
  35. * @ret rc Return status code
  36. */
  37. static int script_exec ( struct image *image ) {
  38. size_t offset = 0;
  39. off_t eol;
  40. size_t len;
  41. int rc;
  42. /* Temporarily de-register image, so that a "boot" command
  43. * doesn't throw us into an execution loop.
  44. */
  45. unregister_image ( image );
  46. while ( offset < image->len ) {
  47. /* Find length of next line, excluding any terminating '\n' */
  48. eol = memchr_user ( image->data, offset, '\n',
  49. ( image->len - offset ) );
  50. if ( eol < 0 )
  51. eol = image->len;
  52. len = ( eol - offset );
  53. /* Copy line, terminate with NUL, and execute command */
  54. {
  55. char cmdbuf[ len + 1 ];
  56. copy_from_user ( cmdbuf, image->data, offset, len );
  57. cmdbuf[len] = '\0';
  58. DBG ( "$ %s\n", cmdbuf );
  59. if ( ( rc = system ( cmdbuf ) ) != 0 ) {
  60. DBG ( "Command \"%s\" failed: %s\n",
  61. cmdbuf, strerror ( rc ) );
  62. goto done;
  63. }
  64. }
  65. /* Move to next line */
  66. offset += ( len + 1 );
  67. }
  68. rc = 0;
  69. done:
  70. /* Re-register image and return */
  71. register_image ( image );
  72. return rc;
  73. }
  74. /**
  75. * Load script into memory
  76. *
  77. * @v image Script
  78. * @ret rc Return status code
  79. */
  80. static int script_load ( struct image *image ) {
  81. static const char ipxe_magic[] = "#!ipxe";
  82. static const char gpxe_magic[] = "#!gpxe";
  83. linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
  84. magic_size_mismatch );
  85. char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
  86. + 1 /* terminating space */];
  87. /* Sanity check */
  88. if ( image->len < sizeof ( test ) ) {
  89. DBG ( "Too short to be a script\n" );
  90. return -ENOEXEC;
  91. }
  92. /* Check for magic signature */
  93. copy_from_user ( test, image->data, 0, sizeof ( test ) );
  94. if ( ! ( ( ( memcmp ( test, ipxe_magic, sizeof ( test ) - 1 ) == 0 ) ||
  95. ( memcmp ( test, gpxe_magic, sizeof ( test ) - 1 ) == 0 )) &&
  96. isspace ( test[ sizeof ( test ) - 1 ] ) ) ) {
  97. DBG ( "Invalid magic signature\n" );
  98. return -ENOEXEC;
  99. }
  100. /* This is a script */
  101. image->type = &script_image_type;
  102. /* We don't actually load it anywhere; we will pick the lines
  103. * out of the image as we need them.
  104. */
  105. return 0;
  106. }
  107. /** Script image type */
  108. struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
  109. .name = "script",
  110. .load = script_load,
  111. .exec = script_exec,
  112. };