• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 FILE_LICENCE ( GPL2_OR_LATER );
20 
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <gpxe/image.h>
26 #include <gpxe/downloader.h>
27 #include <gpxe/monojob.h>
28 #include <gpxe/open.h>
29 #include <gpxe/uri.h>
30 #include <usr/imgmgmt.h>
31 
32 /** @file
33  *
34  * Image management
35  *
36  */
37 
38 /**
39  * Fetch an image
40  *
41  * @v uri_string	URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
42  * @v name		Name for image, or NULL
43  * @v register_image	Image registration routine
44  * @ret rc		Return status code
45  */
imgfetch(struct image * image,const char * uri_string,int (* image_register)(struct image * image))46 int imgfetch ( struct image *image, const char *uri_string,
47 	       int ( * image_register ) ( struct image *image ) ) {
48 	char uri_string_redacted[ strlen ( uri_string ) + 3 /* "***" */
49 				  + 1 /* NUL */ ];
50 	struct uri *uri;
51 	const char *password;
52 	int rc;
53 
54 	if ( ! ( uri = parse_uri ( uri_string ) ) )
55 		return -ENOMEM;
56 
57 	image_set_uri ( image, uri );
58 
59 	/* Redact password portion of URI, if necessary */
60 	password = uri->password;
61 	if ( password )
62 		uri->password = "***";
63 	unparse_uri ( uri_string_redacted, sizeof ( uri_string_redacted ),
64 		      uri, URI_ALL );
65 	uri->password = password;
66 
67 	if ( ( rc = create_downloader ( &monojob, image, image_register,
68 					LOCATION_URI, uri ) ) == 0 )
69 		rc = monojob_wait ( uri_string_redacted );
70 
71 	uri_put ( uri );
72 	return rc;
73 }
74 
75 /**
76  * Load an image
77  *
78  * @v image		Image
79  * @ret rc		Return status code
80  */
imgload(struct image * image)81 int imgload ( struct image *image ) {
82 	int rc;
83 
84 	/* Try to load image */
85 	if ( ( rc = image_autoload ( image ) ) != 0 )
86 		return rc;
87 
88 	return 0;
89 }
90 
91 /**
92  * Execute an image
93  *
94  * @v image		Image
95  * @ret rc		Return status code
96  */
imgexec(struct image * image)97 int imgexec ( struct image *image ) {
98 	return image_exec ( image );
99 }
100 
101 /**
102  * Identify the only loaded image
103  *
104  * @ret image		Image, or NULL if 0 or >1 images are loaded
105  */
imgautoselect(void)106 struct image * imgautoselect ( void ) {
107 	struct image *image;
108 	struct image *selected_image = NULL;
109 	int flagged_images = 0;
110 
111 	for_each_image ( image ) {
112 		if ( image->flags & IMAGE_LOADED ) {
113 			selected_image = image;
114 			flagged_images++;
115 		}
116 	}
117 
118 	return ( ( flagged_images == 1 ) ? selected_image : NULL );
119 }
120 
121 /**
122  * Display status of an image
123  *
124  * @v image		Executable/loadable image
125  */
imgstat(struct image * image)126 void imgstat ( struct image *image ) {
127 	printf ( "%s: %zd bytes", image->name, image->len );
128 	if ( image->type )
129 		printf ( " [%s]", image->type->name );
130 	if ( image->flags & IMAGE_LOADED )
131 		printf ( " [LOADED]" );
132 	if ( image->cmdline )
133 		printf ( " \"%s\"", image->cmdline );
134 	printf ( "\n" );
135 }
136 
137 /**
138  * Free an image
139  *
140  * @v image		Executable/loadable image
141  */
imgfree(struct image * image)142 void imgfree ( struct image *image ) {
143 	unregister_image ( image );
144 }
145