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.5KB

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