|
|
1.1 ! root 1: /* ! 2: * Copyright (C) 2007 Michael Brown <[email protected]>. ! 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 <ipxe/image.h> ! 26: #include <ipxe/downloader.h> ! 27: #include <ipxe/monojob.h> ! 28: #include <ipxe/open.h> ! 29: #include <ipxe/uri.h> ! 30: #include <usr/imgmgmt.h> ! 31: ! 32: /** @file ! 33: * ! 34: * Image management ! 35: * ! 36: */ ! 37: ! 38: /** ! 39: * Register an image and leave it registered ! 40: * ! 41: * @v image Executable image ! 42: * @ret rc Return status code ! 43: * ! 44: * This function assumes an ownership of the passed image. ! 45: */ ! 46: int register_and_put_image ( struct image *image ) { ! 47: int rc; ! 48: ! 49: rc = register_image ( image ); ! 50: image_put ( image ); ! 51: return rc; ! 52: } ! 53: ! 54: /** ! 55: * Register and probe an image ! 56: * ! 57: * @v image Executable image ! 58: * @ret rc Return status code ! 59: * ! 60: * This function assumes an ownership of the passed image. ! 61: */ ! 62: int register_and_probe_image ( struct image *image ) { ! 63: int rc; ! 64: ! 65: if ( ( rc = register_and_put_image ( image ) ) != 0 ) ! 66: return rc; ! 67: ! 68: if ( ( rc = image_probe ( image ) ) != 0 ) ! 69: return rc; ! 70: ! 71: return 0; ! 72: } ! 73: ! 74: /** ! 75: * Register and select an image ! 76: * ! 77: * @v image Executable image ! 78: * @ret rc Return status code ! 79: * ! 80: * This function assumes an ownership of the passed image. ! 81: */ ! 82: int register_and_select_image ( struct image *image ) { ! 83: int rc; ! 84: ! 85: if ( ( rc = register_and_probe_image ( image ) ) != 0 ) ! 86: return rc; ! 87: ! 88: if ( ( rc = image_select ( image ) ) != 0 ) ! 89: return rc; ! 90: ! 91: return 0; ! 92: } ! 93: ! 94: /** ! 95: * Register and boot an image ! 96: * ! 97: * @v image Image ! 98: * @ret rc Return status code ! 99: * ! 100: * This function assumes an ownership of the passed image. ! 101: */ ! 102: int register_and_boot_image ( struct image *image ) { ! 103: int rc; ! 104: ! 105: if ( ( rc = register_and_select_image ( image ) ) != 0 ) ! 106: return rc; ! 107: ! 108: if ( ( rc = image_exec ( image ) ) != 0 ) ! 109: return rc; ! 110: ! 111: return 0; ! 112: } ! 113: ! 114: /** ! 115: * Register and replace image ! 116: * ! 117: * @v image Image ! 118: * @ret rc Return status code ! 119: * ! 120: * This function assumes an ownership of the passed image. ! 121: */ ! 122: int register_and_replace_image ( struct image *image ) { ! 123: int rc; ! 124: ! 125: if ( ( rc = register_and_probe_image ( image ) ) != 0 ) ! 126: return rc; ! 127: ! 128: if ( ( rc = image_replace ( image ) ) != 0 ) ! 129: return rc; ! 130: ! 131: return 0; ! 132: } ! 133: ! 134: /** ! 135: * Download an image ! 136: * ! 137: * @v uri URI ! 138: * @v name Image name, or NULL to use default ! 139: * @v cmdline Command line, or NULL for no command line ! 140: * @v action Action to take upon a successful download ! 141: * @ret rc Return status code ! 142: */ ! 143: int imgdownload ( struct uri *uri, const char *name, const char *cmdline, ! 144: int ( * action ) ( struct image *image ) ) { ! 145: struct image *image; ! 146: size_t len = ( unparse_uri ( NULL, 0, uri, URI_ALL ) + 1 ); ! 147: char uri_string_redacted[len]; ! 148: const char *password; ! 149: int rc; ! 150: ! 151: /* Allocate image */ ! 152: image = alloc_image(); ! 153: if ( ! image ) ! 154: return -ENOMEM; ! 155: ! 156: /* Set image name */ ! 157: if ( name ) ! 158: image_set_name ( image, name ); ! 159: ! 160: /* Set image URI */ ! 161: image_set_uri ( image, uri ); ! 162: ! 163: /* Set image command line */ ! 164: image_set_cmdline ( image, cmdline ); ! 165: ! 166: /* Redact password portion of URI, if necessary */ ! 167: password = uri->password; ! 168: if ( password ) ! 169: uri->password = "***"; ! 170: unparse_uri ( uri_string_redacted, sizeof ( uri_string_redacted ), ! 171: uri, URI_ALL ); ! 172: uri->password = password; ! 173: ! 174: /* Create downloader */ ! 175: if ( ( rc = create_downloader ( &monojob, image, LOCATION_URI, ! 176: uri ) ) != 0 ) { ! 177: image_put ( image ); ! 178: return rc; ! 179: } ! 180: ! 181: /* Wait for download to complete */ ! 182: if ( ( rc = monojob_wait ( uri_string_redacted ) ) != 0 ) { ! 183: image_put ( image ); ! 184: return rc; ! 185: } ! 186: ! 187: /* Act upon downloaded image. This action assumes our ! 188: * ownership of the image. ! 189: */ ! 190: if ( ( rc = action ( image ) ) != 0 ) ! 191: return rc; ! 192: ! 193: return 0; ! 194: } ! 195: ! 196: /** ! 197: * Download an image ! 198: * ! 199: * @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz") ! 200: * @v name Image name, or NULL to use default ! 201: * @v cmdline Command line, or NULL for no command line ! 202: * @v action Action to take upon a successful download ! 203: * @ret rc Return status code ! 204: */ ! 205: int imgdownload_string ( const char *uri_string, const char *name, ! 206: const char *cmdline, ! 207: int ( * action ) ( struct image *image ) ) { ! 208: struct uri *uri; ! 209: int rc; ! 210: ! 211: if ( ! ( uri = parse_uri ( uri_string ) ) ) ! 212: return -ENOMEM; ! 213: ! 214: rc = imgdownload ( uri, name, cmdline, action ); ! 215: ! 216: uri_put ( uri ); ! 217: return rc; ! 218: } ! 219: ! 220: /** ! 221: * Display status of an image ! 222: * ! 223: * @v image Executable/loadable image ! 224: */ ! 225: void imgstat ( struct image *image ) { ! 226: printf ( "%s : %zd bytes", image->name, image->len ); ! 227: if ( image->type ) ! 228: printf ( " [%s]", image->type->name ); ! 229: if ( image->flags & IMAGE_SELECTED ) ! 230: printf ( " [SELECTED]" ); ! 231: if ( image->cmdline ) ! 232: printf ( " \"%s\"", image->cmdline ); ! 233: printf ( "\n" ); ! 234: } ! 235: ! 236: /** ! 237: * Free an image ! 238: * ! 239: * @v image Executable/loadable image ! 240: */ ! 241: void imgfree ( struct image *image ) { ! 242: unregister_image ( image ); ! 243: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.