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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. off_t eol;
  56. size_t len;
  57. char *line;
  58. int rc;
  59. script_offset = 0;
  60. do {
  61. /* Find length of next line, excluding any terminating '\n' */
  62. eol = memchr_user ( image->data, script_offset, '\n',
  63. ( image->len - script_offset ) );
  64. if ( eol < 0 )
  65. eol = image->len;
  66. len = ( eol - script_offset );
  67. /* Allocate buffer for line */
  68. line = zalloc ( len + 1 /* NUL */ );
  69. if ( ! line )
  70. return -ENOMEM;
  71. /* Copy line */
  72. copy_from_user ( line, image->data, script_offset, len );
  73. DBG ( "$ %s\n", line );
  74. /* Move to next line */
  75. script_offset += ( len + 1 );
  76. /* Process and free line */
  77. rc = process_line ( line );
  78. free ( line );
  79. if ( terminate ( rc ) )
  80. return rc;
  81. } while ( script_offset < image->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. return ( shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) ||
  92. ( rc != 0 ) );
  93. }
  94. /**
  95. * Execute script line
  96. *
  97. * @v line Line of script
  98. * @ret rc Return status code
  99. */
  100. static int script_exec_line ( const char *line ) {
  101. int rc;
  102. /* Skip label lines */
  103. if ( line[0] == ':' )
  104. return 0;
  105. /* Execute command */
  106. if ( ( rc = system ( line ) ) != 0 )
  107. return rc;
  108. return 0;
  109. }
  110. /**
  111. * Execute script
  112. *
  113. * @v image Script
  114. * @ret rc Return status code
  115. */
  116. static int script_exec ( struct image *image ) {
  117. size_t saved_offset;
  118. int rc;
  119. /* Temporarily de-register image, so that a "boot" command
  120. * doesn't throw us into an execution loop.
  121. */
  122. unregister_image ( image );
  123. /* Preserve state of any currently-running script */
  124. saved_offset = script_offset;
  125. /* Process script */
  126. rc = process_script ( image, script_exec_line,
  127. terminate_on_exit_or_failure );
  128. /* Restore saved state */
  129. script_offset = saved_offset;
  130. /* Re-register image (unless we have been replaced) */
  131. if ( ! image->replacement )
  132. register_image ( image );
  133. return rc;
  134. }
  135. /**
  136. * Probe script image
  137. *
  138. * @v image Script
  139. * @ret rc Return status code
  140. */
  141. static int script_probe ( struct image *image ) {
  142. static const char ipxe_magic[] = "#!ipxe";
  143. static const char gpxe_magic[] = "#!gpxe";
  144. linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
  145. magic_size_mismatch );
  146. char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
  147. + 1 /* terminating space */];
  148. /* Sanity check */
  149. if ( image->len < sizeof ( test ) ) {
  150. DBG ( "Too short to be a script\n" );
  151. return -ENOEXEC;
  152. }
  153. /* Check for magic signature */
  154. copy_from_user ( test, image->data, 0, sizeof ( test ) );
  155. if ( ! ( ( ( memcmp ( test, ipxe_magic, sizeof ( test ) - 1 ) == 0 ) ||
  156. ( memcmp ( test, gpxe_magic, sizeof ( test ) - 1 ) == 0 )) &&
  157. isspace ( test[ sizeof ( test ) - 1 ] ) ) ) {
  158. DBG ( "Invalid magic signature\n" );
  159. return -ENOEXEC;
  160. }
  161. return 0;
  162. }
  163. /** Script image type */
  164. struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
  165. .name = "script",
  166. .probe = script_probe,
  167. .exec = script_exec,
  168. };
  169. /** "goto" options */
  170. struct goto_options {};
  171. /** "goto" option list */
  172. static struct option_descriptor goto_opts[] = {};
  173. /** "goto" command descriptor */
  174. static struct command_descriptor goto_cmd =
  175. COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, "<label>" );
  176. /**
  177. * Current "goto" label
  178. *
  179. * Valid only during goto_exec(). Consider this part of a closure.
  180. */
  181. static const char *goto_label;
  182. /**
  183. * Check for presence of label
  184. *
  185. * @v line Script line
  186. * @ret rc Return status code
  187. */
  188. static int goto_find_label ( const char *line ) {
  189. size_t len = strlen ( goto_label );
  190. if ( line[0] != ':' )
  191. return -ENOENT;
  192. if ( strncmp ( goto_label, &line[1], len ) != 0 )
  193. return -ENOENT;
  194. if ( line[ 1 + len ] && ! isspace ( line[ 1 + len ] ) )
  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 ( ! current_image ) {
  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 ( current_image, 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_key ),
  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. };