Annotation of qemu/roms/ipxe/src/hci/commands/image_cmd.c, revision 1.1.1.1

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 <getopt.h>
                     26: #include <ipxe/image.h>
                     27: #include <ipxe/command.h>
                     28: #include <ipxe/parseopt.h>
                     29: #include <usr/imgmgmt.h>
                     30: 
                     31: /** @file
                     32:  *
                     33:  * Image management commands
                     34:  *
                     35:  */
                     36: 
                     37: /** "imgfetch" options */
                     38: struct imgfetch_options {
                     39:        /** Image name */
                     40:        const char *name;
                     41: };
                     42: 
                     43: /** "imgfetch" option list */
                     44: static struct option_descriptor imgfetch_opts[] = {
                     45:        OPTION_DESC ( "name", 'n', required_argument,
                     46:                      struct imgfetch_options, name, parse_string ),
                     47: };
                     48: 
                     49: /** "imgfetch" command descriptor */
                     50: static struct command_descriptor imgfetch_cmd =
                     51:        COMMAND_DESC ( struct imgfetch_options, imgfetch_opts, 1, MAX_ARGUMENTS,
                     52:                       "[--name <name>] <uri> [<arguments>...]" );
                     53: 
                     54: /**
                     55:  * The "imgfetch" and friends command body
                     56:  *
                     57:  * @v argc             Argument count
                     58:  * @v argv             Argument list
                     59:  * @v cmd              Command descriptor
                     60:  * @v action_name      Action name (for error messages)
                     61:  * @v action           Action to take upon a successful download
                     62:  * @ret rc             Return status code
                     63:  */
                     64: static int imgfetch_core_exec ( int argc, char **argv,
                     65:                                const char *action_name,
                     66:                                int ( * action ) ( struct image *image ) ) {
                     67:        struct imgfetch_options opts;
                     68:        char *uri_string;
                     69:        char *cmdline = NULL;
                     70:        int rc;
                     71: 
                     72:        /* Parse options */
                     73:        if ( ( rc = parse_options ( argc, argv, &imgfetch_cmd, &opts ) ) != 0 )
                     74:                goto err_parse_options;
                     75: 
                     76:        /* Parse URI string */
                     77:        uri_string = argv[optind];
                     78: 
                     79:        /* Parse command line */
                     80:        if ( argv[ optind + 1 ] != NULL ) {
                     81:                cmdline = concat_args ( &argv[ optind + 1 ] );
                     82:                if ( ! cmdline ) {
                     83:                        rc = -ENOMEM;
                     84:                        goto err_cmdline;
                     85:                }
                     86:        }
                     87: 
                     88:        /* Fetch the image */
                     89:        if ( ( rc = imgdownload_string ( uri_string, opts.name, cmdline,
                     90:                                         action ) ) != 0 ) {
                     91:                printf ( "Could not %s %s: %s\n",
                     92:                         action_name, uri_string, strerror ( rc ) );
                     93:                goto err_imgdownload;
                     94:        }
                     95: 
                     96:        /* Free command line */
                     97:        free ( cmdline );
                     98: 
                     99:        return 0;
                    100: 
                    101:  err_imgdownload:
                    102:        free ( cmdline );
                    103:  err_cmdline:
                    104:  err_parse_options:
                    105:        return rc;
                    106: }
                    107: 
                    108: /**
                    109:  * The "imgfetch"/"module" command
                    110:  *
                    111:  * @v argc             Argument count
                    112:  * @v argv             Argument list
                    113:  * @ret rc             Return status code
                    114:  */
                    115: static int imgfetch_exec ( int argc, char **argv ) {
                    116: 
                    117:        return imgfetch_core_exec ( argc, argv, "fetch",
                    118:                                    register_and_put_image );
                    119: }
                    120: 
                    121: /**
                    122:  * The "kernel" command
                    123:  *
                    124:  * @v argc             Argument count
                    125:  * @v argv             Argument list
                    126:  * @ret rc             Return status code
                    127:  */
                    128: static int kernel_exec ( int argc, char **argv ) {
                    129: 
                    130:        return imgfetch_core_exec ( argc, argv, "select",
                    131:                                    register_and_select_image );
                    132: }
                    133: 
                    134: /**
                    135:  * The "chain" command
                    136:  *
                    137:  * @v argc             Argument count
                    138:  * @v argv             Argument list
                    139:  * @ret rc             Return status code
                    140:  */
                    141: static int chain_exec ( int argc, char **argv) {
                    142: 
                    143:        return imgfetch_core_exec ( argc, argv, "boot",
                    144:                                    register_and_boot_image );
                    145: }
                    146: 
                    147: /** "imgselect" options */
                    148: struct imgselect_options {};
                    149: 
                    150: /** "imgselect" option list */
                    151: static struct option_descriptor imgselect_opts[] = {};
                    152: 
                    153: /** "imgselect" command descriptor */
                    154: static struct command_descriptor imgselect_cmd =
                    155:        COMMAND_DESC ( struct imgselect_options, imgselect_opts, 1, 1,
                    156:                       "<image>" );
                    157: 
                    158: /**
                    159:  * The "imgselect" command
                    160:  *
                    161:  * @v argc             Argument count
                    162:  * @v argv             Argument list
                    163:  * @ret rc             Return status code
                    164:  */
                    165: static int imgselect_exec ( int argc, char **argv ) {
                    166:        struct imgselect_options opts;
                    167:        struct image *image;
                    168:        int rc;
                    169: 
                    170:        /* Parse options */
                    171:        if ( ( rc = parse_options ( argc, argv, &imgselect_cmd, &opts ) ) != 0 )
                    172:                return rc;
                    173: 
                    174:        /* Parse image name */
                    175:        if ( ( rc = parse_image ( argv[optind], &image ) ) != 0 )
                    176:                return rc;
                    177: 
                    178:        /* Load image */
                    179:        if ( ( rc = imgselect ( image ) ) != 0 ) {
                    180:                printf ( "Could not select %s: %s\n",
                    181:                         image->name, strerror ( rc ) );
                    182:                return rc;
                    183:        }
                    184: 
                    185:        return 0;
                    186: }
                    187: 
                    188: /** "imgargs" options */
                    189: struct imgargs_options {};
                    190: 
                    191: /** "imgargs" option list */
                    192: static struct option_descriptor imgargs_opts[] = {};
                    193: 
                    194: /** "imgargs" command descriptor */
                    195: static struct command_descriptor imgargs_cmd =
                    196:        COMMAND_DESC ( struct imgargs_options, imgargs_opts, 1, MAX_ARGUMENTS,
                    197:                       "<image> [<arguments>...]" );
                    198: 
                    199: /**
                    200:  * The "imgargs" command body
                    201:  *
                    202:  * @v argc             Argument count
                    203:  * @v argv             Argument list
                    204:  * @ret rc             Return status code
                    205:  */
                    206: static int imgargs_exec ( int argc, char **argv ) {
                    207:        struct imgargs_options opts;
                    208:        struct image *image;
                    209:        char *cmdline = NULL;
                    210:        int rc;
                    211: 
                    212:        /* Parse options */
                    213:        if ( ( rc = parse_options ( argc, argv, &imgargs_cmd, &opts ) ) != 0 )
                    214:                goto err_parse_options;
                    215: 
                    216:        /* Parse image name */
                    217:        if ( ( rc = parse_image ( argv[optind], &image ) ) != 0 )
                    218:                goto err_parse_image;
                    219: 
                    220:        /* Parse command line */
                    221:        if ( argv[ optind + 1 ] != NULL ) {
                    222:                cmdline = concat_args ( &argv[ optind + 1 ] );
                    223:                if ( ! cmdline ) {
                    224:                        rc = -ENOMEM;
                    225:                        goto err_cmdline;
                    226:                }
                    227:        }
                    228: 
                    229:        /* Set command line */
                    230:        if ( ( rc = image_set_cmdline ( image, cmdline ) ) != 0 )
                    231:                goto err_set_cmdline;
                    232: 
                    233:        /* Free command line */
                    234:        free ( cmdline );
                    235: 
                    236:        return 0;
                    237: 
                    238:  err_set_cmdline:
                    239:        free ( cmdline );
                    240:  err_cmdline:
                    241:  err_parse_image:
                    242:  err_parse_options:
                    243:        return rc;
                    244: }
                    245: 
                    246: /** "imgexec" options */
                    247: struct imgexec_options {};
                    248: 
                    249: /** "imgexec" option list */
                    250: static struct option_descriptor imgexec_opts[] = {};
                    251: 
                    252: /** "imgexec" command descriptor */
                    253: static struct command_descriptor imgexec_cmd =
                    254:        COMMAND_DESC ( struct imgexec_options, imgexec_opts, 0, 1,
                    255:                       "[<image>]" );
                    256: 
                    257: /**
                    258:  * The "imgexec" command
                    259:  *
                    260:  * @v argc             Argument count
                    261:  * @v argv             Argument list
                    262:  * @ret rc             Return status code
                    263:  */
                    264: static int imgexec_exec ( int argc, char **argv ) {
                    265:        struct imgexec_options opts;
                    266:        struct image *image;
                    267:        int rc;
                    268: 
                    269:        /* Parse options */
                    270:        if ( ( rc = parse_options ( argc, argv, &imgexec_cmd, &opts ) ) != 0 )
                    271:                return rc;
                    272: 
                    273:        /* Parse image name */
                    274:        if ( optind < argc ) {
                    275:                if ( ( rc = parse_image ( argv[optind], &image ) ) != 0 )
                    276:                        return rc;
                    277:        } else {
                    278:                image = imgautoselect();
                    279:                if ( ! image ) {
                    280:                        rc = -ENOTTY;
                    281:                        printf ( "No image selected: %s\n", strerror ( rc ) );
                    282:                        return rc;
                    283:                }
                    284:        }
                    285: 
                    286:        /* Execute image */
                    287:        if ( ( rc = imgexec ( image ) ) != 0 ) {
                    288:                printf ( "Could not execute %s: %s\n",
                    289:                         image->name, strerror ( rc ) );
                    290:                return rc;
                    291:        }
                    292: 
                    293:        return 0;
                    294: }
                    295: 
                    296: /** "imgstat" options */
                    297: struct imgstat_options {};
                    298: 
                    299: /** "imgstat" option list */
                    300: static struct option_descriptor imgstat_opts[] = {};
                    301: 
                    302: /** "imgstat" command descriptor */
                    303: static struct command_descriptor imgstat_cmd =
                    304:        COMMAND_DESC ( struct imgstat_options, imgstat_opts, 0, 0, "" );
                    305: 
                    306: /**
                    307:  * The "imgstat" command
                    308:  *
                    309:  * @v argc             Argument count
                    310:  * @v argv             Argument list
                    311:  * @ret rc             Return status code
                    312:  */
                    313: static int imgstat_exec ( int argc, char **argv ) {
                    314:        struct imgstat_options opts;
                    315:        struct image *image;
                    316:        int rc;
                    317: 
                    318:        /* Parse options */
                    319:        if ( ( rc = parse_options ( argc, argv, &imgstat_cmd, &opts ) ) != 0 )
                    320:                return rc;
                    321: 
                    322:        /* Show status of all images */
                    323:        for_each_image ( image ) {
                    324:                imgstat ( image );
                    325:        }
                    326: 
                    327:        return 0;
                    328: }
                    329: 
                    330: /** "imgfree" options */
                    331: struct imgfree_options {};
                    332: 
                    333: /** "imgfree" option list */
                    334: static struct option_descriptor imgfree_opts[] = {};
                    335: 
                    336: /** "imgfree" command descriptor */
                    337: static struct command_descriptor imgfree_cmd =
                    338:        COMMAND_DESC ( struct imgfree_options, imgfree_opts, 0, 1,
                    339:                       "[<image>]" );
                    340: 
                    341: /**
                    342:  * The "imgfree" command
                    343:  *
                    344:  * @v argc             Argument count
                    345:  * @v argv             Argument list
                    346:  * @ret rc             Return status code
                    347:  */
                    348: static int imgfree_exec ( int argc, char **argv ) {
                    349:        struct imgfree_options opts;
                    350:        struct image *image;
                    351:        struct image *tmp;
                    352:        int rc;
                    353: 
                    354:        /* Parse options */
                    355:        if ( ( rc = parse_options ( argc, argv, &imgfree_cmd, &opts ) ) != 0 )
                    356:                return rc;
                    357: 
                    358:        if ( optind < argc ) {
                    359:                /* Free specified image */
                    360:                if ( ( rc = parse_image ( argv[optind], &image ) ) != 0 )
                    361:                        return rc;
                    362:                imgfree ( image );
                    363:        } else {
                    364:                /* Free all images */
                    365:                list_for_each_entry_safe ( image, tmp, &images, list ) {
                    366:                        imgfree ( image );
                    367:                }
                    368:        }
                    369: 
                    370:        return 0;
                    371: }
                    372: 
                    373: /** Image management commands */
                    374: struct command image_commands[] __command = {
                    375:        {
                    376:                .name = "imgfetch",
                    377:                .exec = imgfetch_exec,
                    378:        },
                    379:        {
                    380:                .name = "module",
                    381:                .exec = imgfetch_exec, /* synonym for "imgfetch" */
                    382:        },
                    383:        {
                    384:                .name = "initrd",
                    385:                .exec = imgfetch_exec, /* synonym for "imgfetch" */
                    386:        },
                    387:        {
                    388:                .name = "kernel",
                    389:                .exec = kernel_exec,
                    390:        },
                    391:        {
                    392:                .name = "chain",
                    393:                .exec = chain_exec,
                    394:        },
                    395:        {
                    396:                .name = "imgselect",
                    397:                .exec = imgselect_exec,
                    398:        },
                    399:        {
                    400:                .name = "imgload", /* synonym for "imgselect" */
                    401:                .exec = imgselect_exec,
                    402:        },
                    403:        {
                    404:                .name = "imgargs",
                    405:                .exec = imgargs_exec,
                    406:        },
                    407:        {
                    408:                .name = "imgexec",
                    409:                .exec = imgexec_exec,
                    410:        },
                    411:        {
                    412:                .name = "boot", /* synonym for "imgexec" */
                    413:                .exec = imgexec_exec,
                    414:        },
                    415:        {
                    416:                .name = "imgstat",
                    417:                .exec = imgstat_exec,
                    418:        },
                    419:        {
                    420:                .name = "imgfree",
                    421:                .exec = imgfree_exec,
                    422:        },
                    423: };

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.