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

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