Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. while ( offset < image->len ) {
  43. /* Read up to cmdbuf bytes from script into buffer */
  44. remaining = ( image->len - offset );
  45. len = sizeof ( cmdbuf );
  46. if ( len > remaining )
  47. len = remaining;
  48. copy_from_user ( cmdbuf, image->data, offset, len );
  49. /* Find end of line */
  50. eol = memchr ( cmdbuf, '\n', sizeof ( cmdbuf ) );
  51. if ( ! eol )
  52. eol = memchr ( cmdbuf, '\0', sizeof ( cmdbuf ) );
  53. if ( ! eol ) {
  54. DBG ( "Script line too long (max %d bytes)\n",
  55. sizeof ( cmdbuf ) );
  56. return -ENOEXEC;
  57. }
  58. /* Mark end of line and execute command */
  59. *eol = '\0';
  60. DBG ( "$ %s\n", cmdbuf );
  61. if ( ( rc = system ( cmdbuf ) ) != 0 ) {
  62. DBG ( "Command \"%s\" exited with status %d\n",
  63. cmdbuf, rc );
  64. return rc;
  65. }
  66. /* Move to next line */
  67. offset += ( ( eol - cmdbuf ) + 1 );
  68. }
  69. return 0;
  70. }
  71. /**
  72. * Load script into memory
  73. *
  74. * @v image Script
  75. * @ret rc Return status code
  76. */
  77. static int script_load ( struct image *image ) {
  78. static const char magic[] = "#!gpxe\n";
  79. char test[ sizeof ( magic ) - 1 ];
  80. /* Check for magic signature */
  81. copy_from_user ( test, image->data, 0, sizeof ( test ) );
  82. if ( memcmp ( test, magic, sizeof ( test ) ) != 0 ) {
  83. DBG ( "Invalid magic signature\n" );
  84. return -ENOEXEC;
  85. }
  86. /* This is a script */
  87. image->type = &script_image_type;
  88. /* We don't actually load it anywhere; we will pick the lines
  89. * out of the image as we need them.
  90. */
  91. return 0;
  92. }
  93. /** Script image type */
  94. struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
  95. .name = "script",
  96. .load = script_load,
  97. .exec = script_exec,
  98. };