Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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. /** 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 */
  127. script_offset = saved_offset;
  128. /* Re-register image (unless we have been replaced) */
  129. if ( ! image->replacement )
  130. register_image ( image );
  131. return rc;
  132. }
  133. /**
  134. * Probe script image
  135. *
  136. * @v image Script
  137. * @ret rc Return status code
  138. */
  139. static int script_probe ( struct image *image ) {
  140. static const char ipxe_magic[] = "#!ipxe";
  141. static const char gpxe_magic[] = "#!gpxe";
  142. linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
  143. magic_size_mismatch );
  144. char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
  145. + 1 /* terminating space */];
  146. /* Sanity check */
  147. if ( image->len < sizeof ( test ) ) {
  148. DBG ( "Too short to be a script\n" );
  149. return -ENOEXEC;
  150. }
  151. /* Check for magic signature */
  152. copy_from_user ( test, image->data, 0, sizeof ( test ) );
  153. if ( ! ( ( ( memcmp ( test, ipxe_magic, sizeof ( test ) - 1 ) == 0 ) ||
  154. ( memcmp ( test, gpxe_magic, sizeof ( test ) - 1 ) == 0 )) &&
  155. isspace ( test[ sizeof ( test ) - 1 ] ) ) ) {
  156. DBG ( "Invalid magic signature\n" );
  157. return -ENOEXEC;
  158. }
  159. return 0;
  160. }
  161. /** Script image type */
  162. struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
  163. .name = "script",
  164. .probe = script_probe,
  165. .exec = script_exec,
  166. };
  167. /** "goto" options */
  168. struct goto_options {};
  169. /** "goto" option list */
  170. static struct option_descriptor goto_opts[] = {};
  171. /** "goto" command descriptor */
  172. static struct command_descriptor goto_cmd =
  173. COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, "<label>" );
  174. /**
  175. * Current "goto" label
  176. *
  177. * Valid only during goto_exec(). Consider this part of a closure.
  178. */
  179. static const char *goto_label;
  180. /**
  181. * Check for presence of label
  182. *
  183. * @v line Script line
  184. * @ret rc Return status code
  185. */
  186. static int goto_find_label ( const char *line ) {
  187. size_t len = strlen ( goto_label );
  188. if ( line[0] != ':' )
  189. return -ENOENT;
  190. if ( strncmp ( goto_label, &line[1], len ) != 0 )
  191. return -ENOENT;
  192. if ( line[ 1 + len ] && ! isspace ( line[ 1 + len ] ) )
  193. return -ENOENT;
  194. return 0;
  195. }
  196. /**
  197. * Terminate script processing when label is found
  198. *
  199. * @v rc Line processing status
  200. * @ret terminate Terminate script processing
  201. */
  202. static int terminate_on_label_found ( int rc ) {
  203. return ( rc == 0 );
  204. }
  205. /**
  206. * "goto" command
  207. *
  208. * @v argc Argument count
  209. * @v argv Argument list
  210. * @ret rc Return status code
  211. */
  212. static int goto_exec ( int argc, char **argv ) {
  213. struct goto_options opts;
  214. size_t saved_offset;
  215. int rc;
  216. /* Parse options */
  217. if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
  218. return rc;
  219. /* Sanity check */
  220. if ( ! current_image ) {
  221. rc = -ENOTTY;
  222. printf ( "Not in a script: %s\n", strerror ( rc ) );
  223. return rc;
  224. }
  225. /* Parse label */
  226. goto_label = argv[optind];
  227. /* Find label */
  228. saved_offset = script_offset;
  229. if ( ( rc = process_script ( current_image, goto_find_label,
  230. terminate_on_label_found ) ) != 0 ) {
  231. script_offset = saved_offset;
  232. return rc;
  233. }
  234. /* Terminate processing of current command */
  235. shell_stop ( SHELL_STOP_COMMAND );
  236. return 0;
  237. }
  238. /** "goto" command */
  239. struct command goto_command __command = {
  240. .name = "goto",
  241. .exec = goto_exec,
  242. };
  243. /** "prompt" options */
  244. struct prompt_options {
  245. /** Key to wait for */
  246. unsigned int key;
  247. /** Timeout */
  248. unsigned int timeout;
  249. };
  250. /** "prompt" option list */
  251. static struct option_descriptor prompt_opts[] = {
  252. OPTION_DESC ( "key", 'k', required_argument,
  253. struct prompt_options, key, parse_integer ),
  254. OPTION_DESC ( "timeout", 't', required_argument,
  255. struct prompt_options, timeout, parse_integer ),
  256. };
  257. /** "prompt" command descriptor */
  258. static struct command_descriptor prompt_cmd =
  259. COMMAND_DESC ( struct prompt_options, prompt_opts, 0, MAX_ARGUMENTS,
  260. "[--key <key>] [--timeout <timeout>] [<text>]" );
  261. /**
  262. * "prompt" command
  263. *
  264. * @v argc Argument count
  265. * @v argv Argument list
  266. * @ret rc Return status code
  267. */
  268. static int prompt_exec ( int argc, char **argv ) {
  269. struct prompt_options opts;
  270. char *text;
  271. int rc;
  272. /* Parse options */
  273. if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
  274. goto err_parse;
  275. /* Parse prompt text */
  276. text = concat_args ( &argv[optind] );
  277. if ( ! text ) {
  278. rc = -ENOMEM;
  279. goto err_concat;
  280. }
  281. /* Display prompt and wait for key */
  282. if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
  283. goto err_prompt;
  284. /* Free prompt text */
  285. free ( text );
  286. return 0;
  287. err_prompt:
  288. free ( text );
  289. err_concat:
  290. err_parse:
  291. return rc;
  292. }
  293. /** "prompt" command */
  294. struct command prompt_command __command = {
  295. .name = "prompt",
  296. .exec = prompt_exec,
  297. };