Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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