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

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