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.6KB

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