Start a new LOG file in preparation for release
[gpxe.git] / src / usr / imgmgmt.c
blob28801fe85aee55716d27d115178e4316a1e3672b
1 /*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <gpxe/image.h>
24 #include <gpxe/umalloc.h>
25 #include <gpxe/download.h>
26 #include <usr/imgmgmt.h>
28 /** @file
30 * Image management
34 /**
35 * Fetch an image
37 * @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
38 * @v name Name for image, or NULL
39 * @ret new_image Newly created image
40 * @ret rc Return status code
42 int imgfetch ( const char *uri_string, const char *name,
43 struct image **new_image ) {
44 struct image *image;
45 struct async async;
46 int rc;
48 /* Allocate new image */
49 image = malloc ( sizeof ( *image ) );
50 if ( ! image )
51 return -ENOMEM;
52 memset ( image, 0, sizeof ( *image ) );
54 /* Fill in image name */
55 if ( name )
56 strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
58 /* Download the file */
59 if ( ( rc = async_block_progress ( &async,
60 start_download ( uri_string, &async,
61 &image->data,
62 &image->len )))!=0)
63 goto err;
65 /* Register the image */
66 if ( ( rc = register_image ( image ) ) != 0 )
67 goto err;
69 *new_image = image;
70 return 0;
72 err:
73 ufree ( image->data );
74 free ( image );
75 return rc;
78 /**
79 * Load an image
81 * @v image Image
82 * @ret rc Return status code
84 int imgload ( struct image *image ) {
85 int rc;
87 /* Try to load image */
88 if ( ( rc = image_autoload ( image ) ) != 0 )
89 return rc;
91 /* If we succeed, move the image to the start of the list */
92 promote_image ( image );
94 return 0;
97 /**
98 * Execute an image
100 * @v image Image
101 * @ret rc Return status code
103 int imgexec ( struct image *image ) {
104 return image_exec ( image );
108 * Identify the first loaded image
110 * @ret image Image, or NULL
112 struct image * imgautoselect ( void ) {
113 struct image *image;
115 for_each_image ( image ) {
116 if ( image->flags & IMAGE_LOADED )
117 return image;
120 return NULL;
124 * Display status of an image
126 * @v image Executable/loadable image
128 void imgstat ( struct image *image ) {
129 printf ( "%s: %zd bytes", image->name, image->len );
130 if ( image->type )
131 printf ( " [%s]", image->type->name );
132 if ( image->flags & IMAGE_LOADED )
133 printf ( " [LOADED]" );
134 if ( image->cmdline[0] )
135 printf ( " \"%s\"", image->cmdline );
136 printf ( "\n" );
140 * Free an image
142 * @v image Executable/loadable image
144 void imgfree ( struct image *image ) {
145 unregister_image ( image );
146 ufree ( image->data );
147 free ( image );