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.

image_cmd.c 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <libgen.h>
  21. #include <getopt.h>
  22. #include <vsprintf.h>
  23. #include <gpxe/image.h>
  24. #include <gpxe/command.h>
  25. #include <usr/imgmgmt.h>
  26. /** @file
  27. *
  28. * Image management commands
  29. *
  30. */
  31. /**
  32. * Print image description
  33. *
  34. */
  35. /**
  36. * Fill in image command line
  37. *
  38. * @v image Image
  39. * @v nargs Argument count
  40. * @v args Argument list
  41. */
  42. void imgfill_cmdline ( struct image *image, unsigned int nargs, char **args ) {
  43. size_t used = 0;
  44. image->cmdline[0] = '\0';
  45. while ( ( used < sizeof ( image->cmdline ) ) && nargs-- ) {
  46. used += snprintf ( &image->cmdline[used],
  47. ( sizeof ( image->cmdline ) - used ),
  48. "%s%s", ( used ? " " : "" ), *(args++) );
  49. }
  50. }
  51. /**
  52. * "imgfetch"/"module"/"kernel" command syntax message
  53. *
  54. * @v argv Argument list
  55. */
  56. static void imgfetch_core_syntax ( char **argv, int load ) {
  57. printf ( "Usage:\n"
  58. " %s [-n|--name <name>] filename [arguments...]\n"
  59. "\n"
  60. "%s executable/loadable image\n",
  61. argv[0], ( load ? "Fetch and load" : "Fetch" ) );
  62. }
  63. /**
  64. * The "imgfetch"/"module"/"kernel" command body
  65. *
  66. * @v argc Argument count
  67. * @v argv Argument list
  68. * @v load Load image after fetching
  69. * @ret rc Exit code
  70. */
  71. static int imgfetch_core_exec ( int argc, char **argv, int load ) {
  72. static struct option longopts[] = {
  73. { "help", 0, NULL, 'h' },
  74. { "name", required_argument, NULL, 'n' },
  75. { NULL, 0, NULL, 0 },
  76. };
  77. struct image *image;
  78. const char *name = NULL;
  79. char *filename;
  80. int c;
  81. int rc;
  82. /* Parse options */
  83. while ( ( c = getopt_long ( argc, argv, "hn:",
  84. longopts, NULL ) ) >= 0 ) {
  85. switch ( c ) {
  86. case 'n':
  87. /* Set image name */
  88. name = optarg;
  89. break;
  90. case 'h':
  91. /* Display help text */
  92. default:
  93. /* Unrecognised/invalid option */
  94. imgfetch_core_syntax ( argv, load );
  95. return 1;
  96. }
  97. }
  98. /* Need at least a filename remaining after the options */
  99. if ( optind == argc ) {
  100. imgfetch_core_syntax ( argv, load );
  101. return 1;
  102. }
  103. filename = argv[optind++];
  104. if ( ! name )
  105. name = basename ( filename );
  106. /* Fetch the image */
  107. if ( ( rc = imgfetch ( filename, name, &image ) ) != 0 ) {
  108. printf ( "Could not fetch %s: %s\n", name, strerror ( rc ) );
  109. return 1;
  110. }
  111. /* Fill in command line */
  112. imgfill_cmdline ( image, ( argc - optind ), &argv[optind] );
  113. /* Load image if required */
  114. if ( load ) {
  115. if ( ( rc = imgload ( image ) ) != 0 ) {
  116. printf ( "Could not load %s: %s\n", name,
  117. strerror ( rc ) );
  118. return 1;
  119. }
  120. }
  121. return 0;
  122. }
  123. /**
  124. * The "imgfetch"/"module" command
  125. *
  126. * @v argc Argument count
  127. * @v argv Argument list
  128. * @ret rc Exit code
  129. */
  130. static int imgfetch_exec ( int argc, char **argv ) {
  131. return imgfetch_core_exec ( argc, argv, 0 );
  132. }
  133. /**
  134. * The "kernel" command
  135. *
  136. * @v argc Argument count
  137. * @v argv Argument list
  138. * @ret rc Exit code
  139. */
  140. static int kernel_exec ( int argc, char **argv ) {
  141. return imgfetch_core_exec ( argc, argv, 1 );
  142. }
  143. /**
  144. * "imgload" command syntax message
  145. *
  146. * @v argv Argument list
  147. */
  148. static void imgload_syntax ( char **argv ) {
  149. printf ( "Usage:\n"
  150. " %s <image name>\n"
  151. "\n"
  152. "Load executable/loadable image\n",
  153. argv[0] );
  154. }
  155. /**
  156. * The "imgload" command
  157. *
  158. * @v argc Argument count
  159. * @v argv Argument list
  160. * @ret rc Exit code
  161. */
  162. static int imgload_exec ( int argc, char **argv ) {
  163. static struct option longopts[] = {
  164. { "help", 0, NULL, 'h' },
  165. { NULL, 0, NULL, 0 },
  166. };
  167. struct image *image;
  168. const char *name;
  169. int c;
  170. int rc;
  171. /* Parse options */
  172. while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
  173. switch ( c ) {
  174. case 'h':
  175. /* Display help text */
  176. default:
  177. /* Unrecognised/invalid option */
  178. imgload_syntax ( argv );
  179. return 1;
  180. }
  181. }
  182. /* Need exactly one image name remaining after the options */
  183. if ( optind != ( argc - 1 ) ) {
  184. imgload_syntax ( argv );
  185. return 1;
  186. }
  187. name = argv[optind];
  188. /* Load all specified images */
  189. image = find_image ( name );
  190. if ( ! image ) {
  191. printf ( "No such image: %s\n", name );
  192. return 1;
  193. }
  194. if ( ( rc = imgload ( image ) ) != 0 ) {
  195. printf ( "Could not load %s: %s\n", name, strerror ( rc ) );
  196. return 1;
  197. }
  198. return 0;
  199. }
  200. /**
  201. * "imgargs" command syntax message
  202. *
  203. * @v argv Argument list
  204. */
  205. static void imgargs_syntax ( char **argv ) {
  206. printf ( "Usage:\n"
  207. " %s <image name> [<arguments>...]\n"
  208. "\n"
  209. "Set arguments for executable/loadable image\n",
  210. argv[0] );
  211. }
  212. /**
  213. * The "imgargs" command body
  214. *
  215. * @v argc Argument count
  216. * @v argv Argument list
  217. * @ret rc Exit code
  218. */
  219. static int imgargs_exec ( int argc, char **argv ) {
  220. static struct option longopts[] = {
  221. { "help", 0, NULL, 'h' },
  222. { NULL, 0, NULL, 0 },
  223. };
  224. struct image *image;
  225. const char *name;
  226. int c;
  227. /* Parse options */
  228. while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
  229. switch ( c ) {
  230. case 'h':
  231. /* Display help text */
  232. default:
  233. /* Unrecognised/invalid option */
  234. imgargs_syntax ( argv );
  235. return 1;
  236. }
  237. }
  238. /* Need at least an image name remaining after the options */
  239. if ( optind == argc ) {
  240. imgargs_syntax ( argv );
  241. return 1;
  242. }
  243. name = argv[optind++];
  244. /* Fill in command line */
  245. image = find_image ( name );
  246. if ( ! image ) {
  247. printf ( "No such image: %s\n", name );
  248. return 1;
  249. }
  250. imgfill_cmdline ( image, ( argc - optind ), &argv[optind] );
  251. return 0;
  252. }
  253. /**
  254. * "imgexec" command syntax message
  255. *
  256. * @v argv Argument list
  257. */
  258. static void imgexec_syntax ( char **argv ) {
  259. printf ( "Usage:\n"
  260. " %s <image name>\n"
  261. "\n"
  262. "Execute executable/loadable image\n",
  263. argv[0] );
  264. }
  265. /**
  266. * The "imgexec" command
  267. *
  268. * @v argc Argument count
  269. * @v argv Argument list
  270. * @ret rc Exit code
  271. */
  272. static int imgexec_exec ( int argc, char **argv ) {
  273. static struct option longopts[] = {
  274. { "help", 0, NULL, 'h' },
  275. { NULL, 0, NULL, 0 },
  276. };
  277. struct image *image;
  278. const char *name = NULL;
  279. int c;
  280. int rc;
  281. /* Parse options */
  282. while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
  283. switch ( c ) {
  284. case 'h':
  285. /* Display help text */
  286. default:
  287. /* Unrecognised/invalid option */
  288. imgexec_syntax ( argv );
  289. return 1;
  290. }
  291. }
  292. /* Need no more than one image name */
  293. if ( optind != argc )
  294. name = argv[optind++];
  295. if ( optind != argc ) {
  296. imgexec_syntax ( argv );
  297. return 1;
  298. }
  299. /* Execute specified image */
  300. if ( name ) {
  301. image = find_image ( name );
  302. if ( ! image ) {
  303. printf ( "No such image: %s\n", name );
  304. return 1;
  305. }
  306. } else {
  307. image = imgautoselect();
  308. if ( ! image ) {
  309. printf ( "No loaded images\n" );
  310. return 1;
  311. }
  312. }
  313. if ( ( rc = imgexec ( image ) ) != 0 ) {
  314. printf ( "Could not execute %s: %s\n",
  315. image->name, strerror ( rc ) );
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. /**
  321. * "imgstat" command syntax message
  322. *
  323. * @v argv Argument list
  324. */
  325. static void imgstat_syntax ( char **argv ) {
  326. printf ( "Usage:\n"
  327. " %s\n"
  328. "\n"
  329. "List executable/loadable images\n",
  330. argv[0] );
  331. }
  332. /**
  333. * The "imgstat" command
  334. *
  335. * @v argc Argument count
  336. * @v argv Argument list
  337. * @ret rc Exit code
  338. */
  339. static int imgstat_exec ( int argc, char **argv ) {
  340. static struct option longopts[] = {
  341. { "help", 0, NULL, 'h' },
  342. { NULL, 0, NULL, 0 },
  343. };
  344. struct image *image;
  345. int c;
  346. /* Parse options */
  347. while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
  348. switch ( c ) {
  349. case 'h':
  350. /* Display help text */
  351. default:
  352. /* Unrecognised/invalid option */
  353. imgstat_syntax ( argv );
  354. return 1;
  355. }
  356. }
  357. /* No arguments */
  358. if ( optind != argc ) {
  359. imgstat_syntax ( argv );
  360. return 1;
  361. }
  362. /* Show status of all images */
  363. for_each_image ( image ) {
  364. imgstat ( image );
  365. }
  366. return 0;
  367. }
  368. /**
  369. * "imgstat" command syntax message
  370. *
  371. * @v argv Argument list
  372. */
  373. static void imgfree_syntax ( char **argv ) {
  374. printf ( "Usage:\n"
  375. " %s\n"
  376. "\n"
  377. "Free all executable/loadable images\n",
  378. argv[0] );
  379. }
  380. /**
  381. * The "imgfree" command
  382. *
  383. * @v argc Argument count
  384. * @v argv Argument list
  385. * @ret rc Exit code
  386. */
  387. static int imgfree_exec ( int argc, char **argv ) {
  388. static struct option longopts[] = {
  389. { "help", 0, NULL, 'h' },
  390. { NULL, 0, NULL, 0 },
  391. };
  392. struct image *image;
  393. struct image *tmp;
  394. int c;
  395. /* Parse options */
  396. while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
  397. switch ( c ) {
  398. case 'h':
  399. /* Display help text */
  400. default:
  401. /* Unrecognised/invalid option */
  402. imgfree_syntax ( argv );
  403. return 1;
  404. }
  405. }
  406. /* No arguments */
  407. if ( optind != argc ) {
  408. imgfree_syntax ( argv );
  409. return 1;
  410. }
  411. /* Free all images */
  412. list_for_each_entry_safe ( image, tmp, &images, list ) {
  413. imgfree ( image );
  414. }
  415. return 0;
  416. }
  417. /** Image management commands */
  418. struct command image_commands[] __command = {
  419. {
  420. .name = "imgfetch",
  421. .exec = imgfetch_exec,
  422. },
  423. {
  424. .name = "module",
  425. .exec = imgfetch_exec, /* synonym for "imgfetch" */
  426. },
  427. {
  428. .name = "kernel",
  429. .exec = kernel_exec,
  430. },
  431. {
  432. .name = "imgload",
  433. .exec = imgload_exec,
  434. },
  435. {
  436. .name = "imgargs",
  437. .exec = imgargs_exec,
  438. },
  439. {
  440. .name = "imgexec",
  441. .exec = imgexec_exec,
  442. },
  443. {
  444. .name = "boot", /* synonym for "imgexec" */
  445. .exec = imgexec_exec,
  446. },
  447. {
  448. .name = "imgstat",
  449. .exec = imgstat_exec,
  450. },
  451. {
  452. .name = "imgfree",
  453. .exec = imgfree_exec,
  454. },
  455. };