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

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