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.

runtime.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (C) 2011 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. /** @file
  25. *
  26. * Command line and initrd passed to iPXE at runtime
  27. *
  28. */
  29. #include <stddef.h>
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <ctype.h>
  33. #include <errno.h>
  34. #include <assert.h>
  35. #include <ipxe/init.h>
  36. #include <ipxe/image.h>
  37. #include <ipxe/script.h>
  38. #include <ipxe/umalloc.h>
  39. #include <realmode.h>
  40. /** Command line physical address
  41. *
  42. * This can be set by the prefix.
  43. */
  44. uint32_t __bss16 ( cmdline_phys );
  45. #define cmdline_phys __use_data16 ( cmdline_phys )
  46. /** initrd physical address
  47. *
  48. * This can be set by the prefix.
  49. */
  50. uint32_t __bss16 ( initrd_phys );
  51. #define initrd_phys __use_data16 ( initrd_phys )
  52. /** initrd length
  53. *
  54. * This can be set by the prefix.
  55. */
  56. uint32_t __bss16 ( initrd_len );
  57. #define initrd_len __use_data16 ( initrd_len )
  58. /** Internal copy of the command line */
  59. static char *cmdline_copy;
  60. /** Free command line image */
  61. static void cmdline_image_free ( struct refcnt *refcnt ) {
  62. struct image *image = container_of ( refcnt, struct image, refcnt );
  63. DBGC ( image, "RUNTIME freeing command line\n" );
  64. free ( cmdline_copy );
  65. }
  66. /** Embedded script representing the command line */
  67. static struct image cmdline_image = {
  68. .refcnt = REF_INIT ( cmdline_image_free ),
  69. .name = "<CMDLINE>",
  70. .type = &script_image_type,
  71. };
  72. /** Colour for debug messages */
  73. #define colour &cmdline_image
  74. /**
  75. * Strip unwanted cruft from command line
  76. *
  77. * @v cmdline Command line
  78. * @v cruft Initial substring of cruft to strip
  79. */
  80. static void cmdline_strip ( char *cmdline, const char *cruft ) {
  81. char *strip;
  82. char *strip_end;
  83. /* Find unwanted cruft, if present */
  84. if ( ! ( strip = strstr ( cmdline, cruft ) ) )
  85. return;
  86. /* Strip unwanted cruft */
  87. strip_end = strchr ( strip, ' ' );
  88. if ( strip_end ) {
  89. *strip_end = '\0';
  90. DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
  91. strcpy ( strip, ( strip_end + 1 ) );
  92. } else {
  93. DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
  94. *strip = '\0';
  95. }
  96. }
  97. /**
  98. * Initialise command line
  99. *
  100. * @ret rc Return status code
  101. */
  102. static int cmdline_init ( void ) {
  103. userptr_t cmdline_user;
  104. char *cmdline;
  105. size_t len;
  106. int rc;
  107. /* Do nothing if no command line was specified */
  108. if ( ! cmdline_phys ) {
  109. DBGC ( colour, "RUNTIME found no command line\n" );
  110. return 0;
  111. }
  112. cmdline_user = phys_to_user ( cmdline_phys );
  113. len = ( strlen_user ( cmdline_user, 0 ) + 1 /* NUL */ );
  114. /* Allocate and copy command line */
  115. cmdline_copy = malloc ( len );
  116. if ( ! cmdline_copy ) {
  117. DBGC ( colour, "RUNTIME could not allocate %zd bytes for "
  118. "command line\n", len );
  119. rc = -ENOMEM;
  120. goto err_alloc_cmdline_copy;
  121. }
  122. cmdline = cmdline_copy;
  123. copy_from_user ( cmdline, cmdline_user, 0, len );
  124. DBGC ( colour, "RUNTIME found command line \"%s\" at %08x\n",
  125. cmdline, cmdline_phys );
  126. /* Mark command line as consumed */
  127. cmdline_phys = 0;
  128. /* Strip unwanted cruft from the command line */
  129. cmdline_strip ( cmdline, "BOOT_IMAGE=" );
  130. cmdline_strip ( cmdline, "initrd=" );
  131. while ( isspace ( *cmdline ) )
  132. cmdline++;
  133. DBGC ( colour, "RUNTIME using command line \"%s\"\n", cmdline );
  134. /* Prepare and register image */
  135. cmdline_image.data = virt_to_user ( cmdline );
  136. cmdline_image.len = strlen ( cmdline );
  137. if ( cmdline_image.len ) {
  138. if ( ( rc = register_image ( &cmdline_image ) ) != 0 ) {
  139. DBGC ( colour, "RUNTIME could not register command "
  140. "line: %s\n", strerror ( rc ) );
  141. goto err_register_image;
  142. }
  143. }
  144. /* Drop our reference to the image */
  145. image_put ( &cmdline_image );
  146. return 0;
  147. err_register_image:
  148. image_put ( &cmdline_image );
  149. err_alloc_cmdline_copy:
  150. return rc;
  151. }
  152. /**
  153. * Initialise initrd
  154. *
  155. * @ret rc Return status code
  156. */
  157. static int initrd_init ( void ) {
  158. struct image *image;
  159. int rc;
  160. /* Do nothing if no initrd was specified */
  161. if ( ! initrd_phys ) {
  162. DBGC ( colour, "RUNTIME found no initrd\n" );
  163. return 0;
  164. }
  165. if ( ! initrd_len ) {
  166. DBGC ( colour, "RUNTIME found empty initrd\n" );
  167. return 0;
  168. }
  169. DBGC ( colour, "RUNTIME found initrd at [%x,%x)\n",
  170. initrd_phys, ( initrd_phys + initrd_len ) );
  171. /* Allocate image */
  172. image = alloc_image ( NULL );
  173. if ( ! image ) {
  174. DBGC ( colour, "RUNTIME could not allocate image for "
  175. "initrd\n" );
  176. rc = -ENOMEM;
  177. goto err_alloc_image;
  178. }
  179. if ( ( rc = image_set_name ( image, "<INITRD>" ) ) != 0 ) {
  180. DBGC ( colour, "RUNTIME could not set image name: %s\n",
  181. strerror ( rc ) );
  182. goto err_set_name;
  183. }
  184. /* Allocate and copy initrd content */
  185. image->data = umalloc ( initrd_len );
  186. if ( ! image->data ) {
  187. DBGC ( colour, "RUNTIME could not allocate %d bytes for "
  188. "initrd\n", initrd_len );
  189. rc = -ENOMEM;
  190. goto err_umalloc;
  191. }
  192. image->len = initrd_len;
  193. memcpy_user ( image->data, 0, phys_to_user ( initrd_phys ), 0,
  194. initrd_len );
  195. /* Mark initrd as consumed */
  196. initrd_phys = 0;
  197. /* Register image */
  198. if ( ( rc = register_image ( image ) ) != 0 ) {
  199. DBGC ( colour, "RUNTIME could not register initrd: %s\n",
  200. strerror ( rc ) );
  201. goto err_register_image;
  202. }
  203. /* Drop our reference to the image */
  204. image_put ( image );
  205. return 0;
  206. err_register_image:
  207. err_umalloc:
  208. err_set_name:
  209. image_put ( image );
  210. err_alloc_image:
  211. return rc;
  212. }
  213. /**
  214. * Initialise command line and initrd
  215. *
  216. */
  217. static void runtime_init ( void ) {
  218. int rc;
  219. /* Initialise command line */
  220. if ( ( rc = cmdline_init() ) != 0 ) {
  221. /* No way to report failure */
  222. return;
  223. }
  224. /* Initialise initrd */
  225. if ( ( rc = initrd_init() ) != 0 ) {
  226. /* No way to report failure */
  227. return;
  228. }
  229. }
  230. /** Command line and initrd initialisation function */
  231. struct startup_fn runtime_startup_fn __startup_fn ( STARTUP_NORMAL ) = {
  232. .name = "runtime",
  233. .startup = runtime_init,
  234. };