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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #include <ipxe/shell.h>
  35. #include <usr/prompt.h>
  36. #include <ipxe/script.h>
  37. /** Offset within current script
  38. *
  39. * This is a global in order to allow goto_exec() to update the
  40. * offset.
  41. */
  42. static size_t script_offset;
  43. /**
  44. * Process script lines
  45. *
  46. * @v image Script
  47. * @v process_line Line processor
  48. * @v terminate Termination check
  49. * @ret rc Return status code
  50. */
  51. static int process_script ( struct image *image,
  52. int ( * process_line ) ( const char *line ),
  53. int ( * terminate ) ( int rc ) ) {
  54. off_t eol;
  55. size_t len;
  56. char *line;
  57. int rc;
  58. script_offset = 0;
  59. do {
  60. /* Find length of next line, excluding any terminating '\n' */
  61. eol = memchr_user ( image->data, script_offset, '\n',
  62. ( image->len - script_offset ) );
  63. if ( eol < 0 )
  64. eol = image->len;
  65. len = ( eol - script_offset );
  66. /* Allocate buffer for line */
  67. line = zalloc ( len + 1 /* NUL */ );
  68. if ( ! line )
  69. return -ENOMEM;
  70. /* Copy line */
  71. copy_from_user ( line, image->data, script_offset, len );
  72. DBG ( "$ %s\n", line );
  73. /* Move to next line */
  74. script_offset += ( len + 1 );
  75. /* Process and free line */
  76. rc = process_line ( line );
  77. free ( line );
  78. if ( terminate ( rc ) )
  79. return rc;
  80. } while ( script_offset < image->len );
  81. return rc;
  82. }
  83. /**
  84. * Terminate script processing on shell exit or command failure
  85. *
  86. * @v rc Line processing status
  87. * @ret terminate Terminate script processing
  88. */
  89. static int terminate_on_exit_or_failure ( int rc ) {
  90. return ( shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) ||
  91. ( rc != 0 ) );
  92. }
  93. /**
  94. * Execute script line
  95. *
  96. * @v line Line of script
  97. * @ret rc Return status code
  98. */
  99. static int script_exec_line ( const char *line ) {
  100. int rc;
  101. /* Skip label lines */
  102. if ( line[0] == ':' )
  103. return 0;
  104. /* Execute command */
  105. if ( ( rc = system ( line ) ) != 0 )
  106. return rc;
  107. return 0;
  108. }
  109. /**
  110. * Execute script
  111. *
  112. * @v image Script
  113. * @ret rc Return status code
  114. */
  115. static int script_exec ( struct image *image ) {
  116. size_t saved_offset;
  117. int rc;
  118. /* Temporarily de-register image, so that a "boot" command
  119. * doesn't throw us into an execution loop.
  120. */
  121. unregister_image ( image );
  122. /* Preserve state of any currently-running script */
  123. saved_offset = script_offset;
  124. /* Process script */
  125. rc = process_script ( image, script_exec_line,
  126. terminate_on_exit_or_failure );
  127. /* Restore saved state */
  128. script_offset = saved_offset;
  129. /* Re-register image (unless we have been replaced) */
  130. if ( ! image->replacement )
  131. register_image ( image );
  132. return rc;
  133. }
  134. /**
  135. * Probe script image
  136. *
  137. * @v image Script
  138. * @ret rc Return status code
  139. */
  140. static int script_probe ( struct image *image ) {
  141. static const char ipxe_magic[] = "#!ipxe";
  142. static const char gpxe_magic[] = "#!gpxe";
  143. linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
  144. magic_size_mismatch );
  145. char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
  146. + 1 /* terminating space */];
  147. /* Sanity check */
  148. if ( image->len < sizeof ( test ) ) {
  149. DBG ( "Too short to be a script\n" );
  150. return -ENOEXEC;
  151. }
  152. /* Check for magic signature */
  153. copy_from_user ( test, image->data, 0, sizeof ( test ) );
  154. if ( ! ( ( ( memcmp ( test, ipxe_magic, sizeof ( test ) - 1 ) == 0 ) ||
  155. ( memcmp ( test, gpxe_magic, sizeof ( test ) - 1 ) == 0 )) &&
  156. isspace ( test[ sizeof ( test ) - 1 ] ) ) ) {
  157. DBG ( "Invalid magic signature\n" );
  158. return -ENOEXEC;
  159. }
  160. return 0;
  161. }
  162. /** Script image type */
  163. struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
  164. .name = "script",
  165. .probe = script_probe,
  166. .exec = script_exec,
  167. };
  168. /** "goto" options */
  169. struct goto_options {};
  170. /** "goto" option list */
  171. static struct option_descriptor goto_opts[] = {};
  172. /** "goto" command descriptor */
  173. static struct command_descriptor goto_cmd =
  174. COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, "<label>" );
  175. /**
  176. * Current "goto" label
  177. *
  178. * Valid only during goto_exec(). Consider this part of a closure.
  179. */
  180. static const char *goto_label;
  181. /**
  182. * Check for presence of label
  183. *
  184. * @v line Script line
  185. * @ret rc Return status code
  186. */
  187. static int goto_find_label ( const char *line ) {
  188. size_t len = strlen ( goto_label );
  189. if ( line[0] != ':' )
  190. return -ENOENT;
  191. if ( strncmp ( goto_label, &line[1], len ) != 0 )
  192. return -ENOENT;
  193. if ( line[ 1 + len ] && ! isspace ( line[ 1 + len ] ) )
  194. return -ENOENT;
  195. return 0;
  196. }
  197. /**
  198. * Terminate script processing when label is found
  199. *
  200. * @v rc Line processing status
  201. * @ret terminate Terminate script processing
  202. */
  203. static int terminate_on_label_found ( int rc ) {
  204. return ( rc == 0 );
  205. }
  206. /**
  207. * "goto" command
  208. *
  209. * @v argc Argument count
  210. * @v argv Argument list
  211. * @ret rc Return status code
  212. */
  213. static int goto_exec ( int argc, char **argv ) {
  214. struct goto_options opts;
  215. size_t saved_offset;
  216. int rc;
  217. /* Parse options */
  218. if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
  219. return rc;
  220. /* Sanity check */
  221. if ( ! current_image ) {
  222. rc = -ENOTTY;
  223. printf ( "Not in a script: %s\n", strerror ( rc ) );
  224. return rc;
  225. }
  226. /* Parse label */
  227. goto_label = argv[optind];
  228. /* Find label */
  229. saved_offset = script_offset;
  230. if ( ( rc = process_script ( current_image, goto_find_label,
  231. terminate_on_label_found ) ) != 0 ) {
  232. script_offset = saved_offset;
  233. return rc;
  234. }
  235. /* Terminate processing of current command */
  236. shell_stop ( SHELL_STOP_COMMAND );
  237. return 0;
  238. }
  239. /** "goto" command */
  240. struct command goto_command __command = {
  241. .name = "goto",
  242. .exec = goto_exec,
  243. };
  244. /** "prompt" options */
  245. struct prompt_options {
  246. /** Key to wait for */
  247. unsigned int key;
  248. /** Timeout */
  249. unsigned int timeout;
  250. };
  251. /** "prompt" option list */
  252. static struct option_descriptor prompt_opts[] = {
  253. OPTION_DESC ( "key", 'k', required_argument,
  254. struct prompt_options, key, parse_key ),
  255. OPTION_DESC ( "timeout", 't', required_argument,
  256. struct prompt_options, timeout, parse_integer ),
  257. };
  258. /** "prompt" command descriptor */
  259. static struct command_descriptor prompt_cmd =
  260. COMMAND_DESC ( struct prompt_options, prompt_opts, 0, MAX_ARGUMENTS,
  261. "[--key <key>] [--timeout <timeout>] [<text>]" );
  262. /**
  263. * "prompt" command
  264. *
  265. * @v argc Argument count
  266. * @v argv Argument list
  267. * @ret rc Return status code
  268. */
  269. static int prompt_exec ( int argc, char **argv ) {
  270. struct prompt_options opts;
  271. char *text;
  272. int rc;
  273. /* Parse options */
  274. if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
  275. goto err_parse;
  276. /* Parse prompt text */
  277. text = concat_args ( &argv[optind] );
  278. if ( ! text ) {
  279. rc = -ENOMEM;
  280. goto err_concat;
  281. }
  282. /* Display prompt and wait for key */
  283. if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
  284. goto err_prompt;
  285. /* Free prompt text */
  286. free ( text );
  287. return 0;
  288. err_prompt:
  289. free ( text );
  290. err_concat:
  291. err_parse:
  292. return rc;
  293. }
  294. /** "prompt" command */
  295. struct command prompt_command __command = {
  296. .name = "prompt",
  297. .exec = prompt_exec,
  298. };