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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 ) ( struct image *image,
  54. size_t offset,
  55. const char *label,
  56. const char *command ),
  57. int ( * terminate ) ( int rc ) ) {
  58. size_t len = 0;
  59. char *line = NULL;
  60. size_t line_offset;
  61. char *label;
  62. char *command;
  63. off_t eol;
  64. size_t frag_len;
  65. char *tmp;
  66. int rc;
  67. /* Initialise script and line offsets */
  68. script_offset = 0;
  69. line_offset = 0;
  70. do {
  71. /* Find length of next line, excluding any terminating '\n' */
  72. eol = memchr_user ( image->data, script_offset, '\n',
  73. ( image->len - script_offset ) );
  74. if ( eol < 0 )
  75. eol = image->len;
  76. frag_len = ( eol - script_offset );
  77. /* Allocate buffer for line */
  78. tmp = realloc ( line, ( len + frag_len + 1 /* NUL */ ) );
  79. if ( ! tmp ) {
  80. rc = -ENOMEM;
  81. goto err_alloc;
  82. }
  83. line = tmp;
  84. /* Copy line */
  85. copy_from_user ( ( line + len ), image->data, script_offset,
  86. frag_len );
  87. len += frag_len;
  88. /* Move to next line in script */
  89. script_offset += ( frag_len + 1 );
  90. /* Strip trailing CR, if present */
  91. if ( len && ( line[ len - 1 ] == '\r' ) )
  92. len--;
  93. /* Handle backslash continuations */
  94. if ( len && ( line[ len - 1 ] == '\\' ) ) {
  95. len--;
  96. rc = -EINVAL;
  97. continue;
  98. }
  99. /* Terminate line */
  100. line[len] = '\0';
  101. /* Split line into (optional) label and command */
  102. command = line;
  103. while ( isspace ( *command ) )
  104. command++;
  105. if ( *command == ':' ) {
  106. label = ++command;
  107. while ( *command && ! isspace ( *command ) )
  108. command++;
  109. if ( *command )
  110. *(command++) = '\0';
  111. } else {
  112. label = NULL;
  113. }
  114. /* Process line */
  115. rc = process_line ( image, line_offset, label, command );
  116. if ( terminate ( rc ) )
  117. goto err_process;
  118. /* Free line */
  119. free ( line );
  120. line = NULL;
  121. len = 0;
  122. /* Update line offset */
  123. line_offset = script_offset;
  124. } while ( script_offset < image->len );
  125. err_process:
  126. err_alloc:
  127. free ( line );
  128. return rc;
  129. }
  130. /**
  131. * Terminate script processing on shell exit or command failure
  132. *
  133. * @v rc Line processing status
  134. * @ret terminate Terminate script processing
  135. */
  136. static int terminate_on_exit_or_failure ( int rc ) {
  137. return ( shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) ||
  138. ( rc != 0 ) );
  139. }
  140. /**
  141. * Execute script line
  142. *
  143. * @v image Script
  144. * @v offset Offset within script
  145. * @v label Label, or NULL
  146. * @v command Command
  147. * @ret rc Return status code
  148. */
  149. static int script_exec_line ( struct image *image, size_t offset,
  150. const char *label __unused,
  151. const char *command ) {
  152. int rc;
  153. DBGC ( image, "[%04zx] $ %s\n", offset, command );
  154. /* Execute command */
  155. if ( ( rc = system ( command ) ) != 0 )
  156. return rc;
  157. return 0;
  158. }
  159. /**
  160. * Execute script
  161. *
  162. * @v image Script
  163. * @ret rc Return status code
  164. */
  165. static int script_exec ( struct image *image ) {
  166. size_t saved_offset;
  167. int rc;
  168. /* Temporarily de-register image, so that a "boot" command
  169. * doesn't throw us into an execution loop.
  170. */
  171. unregister_image ( image );
  172. /* Preserve state of any currently-running script */
  173. saved_offset = script_offset;
  174. /* Process script */
  175. rc = process_script ( image, script_exec_line,
  176. terminate_on_exit_or_failure );
  177. /* Restore saved state */
  178. script_offset = saved_offset;
  179. /* Re-register image (unless we have been replaced) */
  180. if ( ! image->replacement )
  181. register_image ( image );
  182. return rc;
  183. }
  184. /**
  185. * Probe script image
  186. *
  187. * @v image Script
  188. * @ret rc Return status code
  189. */
  190. static int script_probe ( struct image *image ) {
  191. static const char ipxe_magic[] = "#!ipxe";
  192. static const char gpxe_magic[] = "#!gpxe";
  193. linker_assert ( sizeof ( ipxe_magic ) == sizeof ( gpxe_magic ),
  194. magic_size_mismatch );
  195. char test[ sizeof ( ipxe_magic ) - 1 /* NUL */
  196. + 1 /* terminating space */];
  197. /* Sanity check */
  198. if ( image->len < sizeof ( test ) ) {
  199. DBGC ( image, "Too short to be a script\n" );
  200. return -ENOEXEC;
  201. }
  202. /* Check for magic signature */
  203. copy_from_user ( test, image->data, 0, sizeof ( test ) );
  204. if ( ! ( ( ( memcmp ( test, ipxe_magic, sizeof ( test ) - 1 ) == 0 ) ||
  205. ( memcmp ( test, gpxe_magic, sizeof ( test ) - 1 ) == 0 )) &&
  206. isspace ( test[ sizeof ( test ) - 1 ] ) ) ) {
  207. DBGC ( image, "Invalid magic signature\n" );
  208. return -ENOEXEC;
  209. }
  210. return 0;
  211. }
  212. /** Script image type */
  213. struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
  214. .name = "script",
  215. .probe = script_probe,
  216. .exec = script_exec,
  217. };
  218. /** "goto" options */
  219. struct goto_options {};
  220. /** "goto" option list */
  221. static struct option_descriptor goto_opts[] = {};
  222. /** "goto" command descriptor */
  223. static struct command_descriptor goto_cmd =
  224. COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, "<label>" );
  225. /**
  226. * Current "goto" label
  227. *
  228. * Valid only during goto_exec(). Consider this part of a closure.
  229. */
  230. static const char *goto_label;
  231. /**
  232. * Check for presence of label
  233. *
  234. * @v image Script
  235. * @v offset Offset within script
  236. * @v label Label
  237. * @v command Command
  238. * @ret rc Return status code
  239. */
  240. static int goto_find_label ( struct image *image, size_t offset,
  241. const char *label, const char *command __unused ) {
  242. /* Check label exists */
  243. if ( ! label )
  244. return -ENOENT;
  245. /* Check label matches */
  246. if ( strcmp ( goto_label, label ) != 0 )
  247. return -ENOENT;
  248. /* Update script offset */
  249. script_offset = offset;
  250. DBGC ( image, "[%04zx] Gone to :%s\n", offset, label );
  251. return 0;
  252. }
  253. /**
  254. * Terminate script processing when label is found
  255. *
  256. * @v rc Line processing status
  257. * @ret terminate Terminate script processing
  258. */
  259. static int terminate_on_label_found ( int rc ) {
  260. return ( rc == 0 );
  261. }
  262. /**
  263. * "goto" command
  264. *
  265. * @v argc Argument count
  266. * @v argv Argument list
  267. * @ret rc Return status code
  268. */
  269. static int goto_exec ( int argc, char **argv ) {
  270. struct goto_options opts;
  271. size_t saved_offset;
  272. int rc;
  273. /* Parse options */
  274. if ( ( rc = parse_options ( argc, argv, &goto_cmd, &opts ) ) != 0 )
  275. return rc;
  276. /* Sanity check */
  277. if ( ! current_image ) {
  278. rc = -ENOTTY;
  279. printf ( "Not in a script: %s\n", strerror ( rc ) );
  280. return rc;
  281. }
  282. /* Parse label */
  283. goto_label = argv[optind];
  284. /* Find label */
  285. saved_offset = script_offset;
  286. if ( ( rc = process_script ( current_image, goto_find_label,
  287. terminate_on_label_found ) ) != 0 ) {
  288. script_offset = saved_offset;
  289. DBGC ( current_image, "[%04zx] No such label :%s\n",
  290. script_offset, goto_label );
  291. return rc;
  292. }
  293. /* Terminate processing of current command */
  294. shell_stop ( SHELL_STOP_COMMAND );
  295. return 0;
  296. }
  297. /** "goto" command */
  298. struct command goto_command __command = {
  299. .name = "goto",
  300. .exec = goto_exec,
  301. };
  302. /** "prompt" options */
  303. struct prompt_options {
  304. /** Key to wait for */
  305. unsigned int key;
  306. /** Timeout */
  307. unsigned long timeout;
  308. };
  309. /** "prompt" option list */
  310. static struct option_descriptor prompt_opts[] = {
  311. OPTION_DESC ( "key", 'k', required_argument,
  312. struct prompt_options, key, parse_key ),
  313. OPTION_DESC ( "timeout", 't', required_argument,
  314. struct prompt_options, timeout, parse_timeout ),
  315. };
  316. /** "prompt" command descriptor */
  317. static struct command_descriptor prompt_cmd =
  318. COMMAND_DESC ( struct prompt_options, prompt_opts, 0, MAX_ARGUMENTS,
  319. "[<text>]" );
  320. /**
  321. * "prompt" command
  322. *
  323. * @v argc Argument count
  324. * @v argv Argument list
  325. * @ret rc Return status code
  326. */
  327. static int prompt_exec ( int argc, char **argv ) {
  328. struct prompt_options opts;
  329. char *text;
  330. int rc;
  331. /* Parse options */
  332. if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
  333. goto err_parse;
  334. /* Parse prompt text */
  335. text = concat_args ( &argv[optind] );
  336. if ( ! text ) {
  337. rc = -ENOMEM;
  338. goto err_concat;
  339. }
  340. /* Display prompt and wait for key */
  341. if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
  342. goto err_prompt;
  343. /* Free prompt text */
  344. free ( text );
  345. return 0;
  346. err_prompt:
  347. free ( text );
  348. err_concat:
  349. err_parse:
  350. return rc;
  351. }
  352. /** "prompt" command */
  353. struct command prompt_command __command = {
  354. .name = "prompt",
  355. .exec = prompt_exec,
  356. };