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.

script.c 3.1KB

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