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

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