Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

script.c 7.8KB

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