Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

script.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 <stdio.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <getopt.h>
  31. #include <ipxe/command.h>
  32. #include <ipxe/parseopt.h>
  33. #include <ipxe/image.h>
  34. struct image_type script_image_type __image_type ( PROBE_NORMAL );
  35. /** Currently running script
  36. *
  37. * This is a global in order to allow goto_exec() to update the
  38. * offset.
  39. */
  40. static struct image *script;
  41. /** Offset within current script
  42. *
  43. * This is a global in order to allow goto_exec() to update the
  44. * offset.
  45. */
  46. static size_t script_offset;
  47. /**
  48. * Process script lines
  49. *
  50. * @v process_line Line processor
  51. * @v terminate Termination check
  52. * @ret rc Return status code
  53. */
  54. static int process_script ( int ( * process_line ) ( const char *line ),
  55. int ( * terminate ) ( int rc ) ) {
  56. off_t eol;
  57. size_t len;
  58. int rc;
  59. script_offset = 0;
  60. do {
  61. /* Find length of next line, excluding any terminating '\n' */
  62. eol = memchr_user ( script->data, script_offset, '\n',
  63. ( script->len - script_offset ) );
  64. if ( eol < 0 )
  65. eol = script->len;
  66. len = ( eol - script_offset );
  67. /* Copy line, terminate with NUL, and execute command */
  68. {
  69. char cmdbuf[ len + 1 ];
  70. copy_from_user ( cmdbuf, script->data,
  71. script_offset, len );
  72. cmdbuf[len] = '\0';
  73. DBG ( "$ %s\n", cmdbuf );
  74. /* Move to next line */
  75. script_offset += ( len + 1 );
  76. /* Process line */
  77. rc = process_line ( cmdbuf );
  78. if ( terminate ( rc ) )
  79. return rc;
  80. }
  81. } while ( script_offset < script->len );
  82. return rc;
  83. }
  84. /**
  85. * Terminate script processing on shell exit or command failure
  86. *
  87. * @v rc Line processing status
  88. * @ret terminate Terminate script processing
  89. */
  90. static int terminate_on_exit_or_failure ( int rc ) {
  91. /* Check and consume exit flag */
  92. if ( shell_exit ) {
  93. shell_exit = 0;
  94. return 1;
  95. }
  96. return ( rc != 0 );
  97. }
  98. /**
  99. * Execute script line
  100. *
  101. * @v line Line of script
  102. * @ret rc Return status code
  103. */
  104. static int script_exec_line ( const char *line ) {
  105. int rc;
  106. /* Skip label lines */
  107. if ( line[0] == ':' )
  108. return 0;
  109. /* Execute command */
  110. if ( ( rc = system ( line ) ) != 0 ) {
  111. printf ( "Aborting on \"%s\"\n", line );
  112. return rc;
  113. }
  114. return 0;
  115. }
  116. /**
  117. * Execute script
  118. *
  119. * @v image Script
  120. * @ret rc Return status code
  121. */
  122. static int script_exec ( struct image *image ) {
  123. struct image *saved_script;
  124. size_t saved_offset;
  125. int rc;
  126. /* Temporarily de-register image, so that a "boot" command
  127. * doesn't throw us into an execution loop.
  128. */
  129. unregister_image ( image );
  130. /* Preserve state of any currently-running script */
  131. saved_script = script;
  132. saved_offset = script_offset;
  133. /* Initialise state for this script */
  134. script = image;
  135. /* Process script */
  136. rc = process_script ( script_exec_line, terminate_on_exit_or_failure );
  137. /* Restore saved state, re-register image, and return */
  138. script_offset = saved_offset;
  139. script = saved_script;
  140. register_image ( image );
  141. return rc;
  142. }
  143. /**
  144. * Load script into memory
  145. *
  146. * @v image Script
  147. * @ret rc Return status code
  148. */
  149. static int script_load ( struct image *image ) {
  150. static const char ipxe_magic[] = "#!ipxe";
  151. static const char gpxe_magic[] = "#!gpxe";
  152. linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
  153. magic_size_mismatch );
  154. char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
  155. + 1 /* terminating space */];
  156. /* Sanity check */
  157. if ( image->len < sizeof ( test ) ) {
  158. DBG ( "Too short to be a script\n" );
  159. return -ENOEXEC;
  160. }
  161. /* Check for magic signature */
  162. copy_from_user ( test, image->data, 0, sizeof ( test ) );
  163. if ( ! ( ( ( memcmp ( test, ipxe_magic, sizeof ( test ) - 1 ) == 0 ) ||
  164. ( memcmp ( test, gpxe_magic, sizeof ( test ) - 1 ) == 0 )) &&
  165. isspace ( test[ sizeof ( test ) - 1 ] ) ) ) {
  166. DBG ( "Invalid magic signature\n" );
  167. return -ENOEXEC;
  168. }
  169. /* This is a script */
  170. image->type = &script_image_type;
  171. /* We don't actually load it anywhere; we will pick the lines
  172. * out of the image as we need them.
  173. */
  174. return 0;
  175. }
  176. /** Script image type */
  177. struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
  178. .name = "script",
  179. .load = script_load,
  180. .exec = script_exec,
  181. };
  182. /** "goto" options */
  183. struct goto_options {};
  184. /** "goto" option list */
  185. static struct option_descriptor goto_opts[] = {};
  186. /** "goto" command descriptor */
  187. static struct command_descriptor goto_cmd =
  188. COMMAND_DESC ( struct goto_options, goto_opts, 1, 1,
  189. "<label>", "" );
  190. /**
  191. * Current "goto" label
  192. *
  193. * Valid only during goto_exec(). Consider this part of a closure.
  194. */
  195. static const char *goto_label;
  196. /**
  197. * Check for presence of label
  198. *
  199. * @v line Script line
  200. * @ret rc Return status code
  201. */
  202. static int goto_find_label ( const char *line ) {
  203. if ( line[0] != ':' )
  204. return -ENOENT;
  205. if ( strcmp ( goto_label, &line[1] ) != 0 )
  206. return -ENOENT;
  207. return 0;
  208. }
  209. /**
  210. * Terminate script processing when label is found
  211. *
  212. * @v rc Line processing status
  213. * @ret terminate Terminate script processing
  214. */
  215. static int terminate_on_label_found ( int rc ) {
  216. return ( rc == 0 );
  217. }
  218. /**
  219. * "goto" command
  220. *
  221. * @v argc Argument count
  222. * @v argv Argument list
  223. * @ret rc Return status code
  224. */
  225. static int goto_exec ( int argc, char **argv ) {
  226. struct goto_options opts;
  227. size_t saved_offset;
  228. int rc;
  229. /* Parse options */
  230. if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
  231. return rc;
  232. /* Sanity check */
  233. if ( ! script ) {
  234. printf ( "Not in a script\n" );
  235. return -ENOTTY;
  236. }
  237. /* Parse label */
  238. goto_label = argv[optind];
  239. /* Find label */
  240. saved_offset = script_offset;
  241. if ( ( rc = process_script ( goto_find_label,
  242. terminate_on_label_found ) ) != 0 ) {
  243. script_offset = saved_offset;
  244. return rc;
  245. }
  246. return 0;
  247. }
  248. /** "goto" command */
  249. struct command goto_command __command = {
  250. .name = "goto",
  251. .exec = goto_exec,
  252. };